mirror of
https://github.com/davisking/dlib.git
synced 2024-11-01 10:14:53 +08:00
- Added make_sparse_vector()
- Refined the sparse_to_dense() routines a little.
This commit is contained in:
parent
ad3ede2d12
commit
2e7e20f2b8
@ -641,7 +641,7 @@ namespace dlib
|
||||
template <typename sparse_vector_type>
|
||||
inline matrix<typename sparse_vector_type::value_type::second_type,0,1> sparse_to_dense (
|
||||
const sparse_vector_type& vect,
|
||||
long num_dimensions
|
||||
unsigned long num_dimensions
|
||||
)
|
||||
{
|
||||
// You must use unsigned integral key types in your sparse vectors
|
||||
@ -674,7 +674,7 @@ namespace dlib
|
||||
template <typename idx_type, typename value_type, typename alloc>
|
||||
matrix<value_type,0,1> sparse_to_dense (
|
||||
const std::vector<std::pair<idx_type,value_type>,alloc>& vect,
|
||||
long num_dimensions
|
||||
unsigned long num_dimensions
|
||||
)
|
||||
{
|
||||
return impl::sparse_to_dense(vect,num_dimensions);
|
||||
@ -695,7 +695,7 @@ namespace dlib
|
||||
template <typename T1, typename T2, typename T3, typename T4>
|
||||
matrix<T2,0,1> sparse_to_dense (
|
||||
const std::map<T1,T2,T3,T4>& vect,
|
||||
long num_dimensions
|
||||
unsigned long num_dimensions
|
||||
)
|
||||
{
|
||||
return impl::sparse_to_dense(vect,num_dimensions);
|
||||
@ -720,16 +720,41 @@ namespace dlib
|
||||
|
||||
template <typename EXP>
|
||||
matrix<typename EXP::type,0,1> sparse_to_dense(
|
||||
const matrix<EXP>& item,
|
||||
long num
|
||||
const matrix_exp<EXP>& item,
|
||||
unsigned long num
|
||||
)
|
||||
{
|
||||
if (item.size() == num)
|
||||
typedef typename EXP::type type;
|
||||
if (item.size() == (long)num)
|
||||
return item;
|
||||
else if (item.size() < num)
|
||||
return join_cols(item, zeros_matrix(num-item.size(),1));
|
||||
else if (item.size() < (long)num)
|
||||
return join_cols(item, zeros_matrix<type>((long)num-item.size(),1));
|
||||
else
|
||||
return rowm(item,0,num);
|
||||
return colm(item,0,(long)num);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
template <typename sample_type, typename alloc>
|
||||
std::vector<matrix<typename sample_type::value_type::second_type,0,1> > sparse_to_dense (
|
||||
const std::vector<sample_type, alloc>& samples,
|
||||
unsigned long num_dimensions
|
||||
)
|
||||
{
|
||||
typedef typename sample_type::value_type pair_type;
|
||||
typedef typename pair_type::second_type value_type;
|
||||
|
||||
std::vector< matrix<value_type,0,1> > result;
|
||||
|
||||
// now turn all the samples into dense samples
|
||||
result.resize(samples.size());
|
||||
|
||||
for (unsigned long i = 0; i < samples.size(); ++i)
|
||||
{
|
||||
result[i] = sparse_to_dense(samples[i],num_dimensions);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
@ -739,23 +764,29 @@ namespace dlib
|
||||
const std::vector<sample_type, alloc>& samples
|
||||
)
|
||||
{
|
||||
typedef typename sample_type::value_type pair_type;
|
||||
typedef typename pair_type::second_type value_type;
|
||||
|
||||
std::vector< matrix<value_type,0,1> > result;
|
||||
|
||||
// figure out how many elements we need in our dense vectors.
|
||||
const unsigned long max_dim = max_index_plus_one(samples);
|
||||
|
||||
// now turn all the samples into dense samples
|
||||
result.resize(samples.size());
|
||||
|
||||
for (unsigned long i = 0; i < samples.size(); ++i)
|
||||
{
|
||||
result[i] = sparse_to_dense(samples[i],max_dim);
|
||||
return sparse_to_dense(samples, max_index_plus_one(samples));
|
||||
}
|
||||
|
||||
return result;
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
template <
|
||||
typename T
|
||||
>
|
||||
T make_sparse_vector (
|
||||
const T& v
|
||||
)
|
||||
{
|
||||
// You must use unsigned integral key types in your sparse vectors
|
||||
typedef typename T::value_type::first_type idx_type;
|
||||
typedef typename T::value_type::second_type value_type;
|
||||
COMPILE_TIME_ASSERT(is_unsigned_type<idx_type>::value);
|
||||
std::map<idx_type,value_type> temp;
|
||||
for (typename T::const_iterator i = v.begin(); i != v.end(); ++i)
|
||||
{
|
||||
temp[i->first] += i->second;
|
||||
}
|
||||
|
||||
return T(temp.begin(), temp.end());
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
@ -371,7 +371,7 @@ namespace dlib
|
||||
);
|
||||
/*!
|
||||
requires
|
||||
- vect must be an unsorted sparse vector or a dense column vector.
|
||||
- vect must be a sparse vector or a dense column vector.
|
||||
ensures
|
||||
- converts the single sparse or dense vector vect to a dense (column matrix form)
|
||||
representation. That is, this function returns a vector V such that:
|
||||
@ -389,11 +389,11 @@ namespace dlib
|
||||
>
|
||||
matrix<typename sample_type::value_type::second_type,0,1> sparse_to_dense (
|
||||
const sample_type& vect,
|
||||
long num_dimensions
|
||||
unsigned long num_dimensions
|
||||
);
|
||||
/*!
|
||||
requires
|
||||
- vect must be an unsorted sparse vector or a dense column vector.
|
||||
- vect must be a sparse vector or a dense column vector.
|
||||
ensures
|
||||
- converts the single sparse or dense vector vect to a dense (column matrix form)
|
||||
representation. That is, this function returns a vector V such that:
|
||||
@ -415,7 +415,7 @@ namespace dlib
|
||||
);
|
||||
/*!
|
||||
requires
|
||||
- all elements of samples must be unsorted sparse vectors or dense column vectors.
|
||||
- all elements of samples must be sparse vectors or dense column vectors.
|
||||
ensures
|
||||
- converts from sparse sample vectors to dense (column matrix form)
|
||||
- That is, this function returns a std::vector R such that:
|
||||
@ -427,6 +427,47 @@ namespace dlib
|
||||
given by samples[i].)
|
||||
!*/
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
template <
|
||||
typename sample_type,
|
||||
typename alloc
|
||||
>
|
||||
std::vector<matrix<typename sample_type::value_type::second_type,0,1> > sparse_to_dense (
|
||||
const std::vector<sample_type, alloc>& samples,
|
||||
unsigned long num_dimensions
|
||||
);
|
||||
/*!
|
||||
requires
|
||||
- all elements of samples must be sparse vectors or dense column vectors.
|
||||
ensures
|
||||
- converts from sparse sample vectors to dense (column matrix form)
|
||||
- That is, this function returns a std::vector R such that:
|
||||
- R contains column matrices
|
||||
- R.size() == samples.size()
|
||||
- for all valid i:
|
||||
- R[i] == sparse_to_dense(samples[i], num_dimensions)
|
||||
(i.e. the dense (i.e. dlib::matrix) version of the sparse sample
|
||||
given by samples[i].)
|
||||
!*/
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
template <
|
||||
typename T
|
||||
>
|
||||
T make_sparse_vector (
|
||||
const T& v
|
||||
);
|
||||
/*!
|
||||
requires
|
||||
- v is an unsorted sparse vector
|
||||
ensures
|
||||
- returns a copy of v which is a sparse vector.
|
||||
(i.e. it will be properly sorted and not have any duplicate
|
||||
key values).
|
||||
!*/
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user