Upgraded the structural_svm_problem so that it collapses down excessively large

sparse vectors to avoid unnecessary RAM usage in this case.
This commit is contained in:
Davis King 2013-06-18 23:11:21 -04:00
parent a0797fd132
commit 623c657c28
1 changed files with 27 additions and 0 deletions

View File

@ -48,7 +48,10 @@ namespace dlib
lru_count.clear();
if (prob->get_max_cache_size() != 0)
{
prob->get_truth_joint_feature_vector(idx, true_psi);
compact_sparse_vector(true_psi);
}
}
void get_truth_joint_feature_vector_cached (
@ -106,6 +109,8 @@ namespace dlib
if (prob->get_max_cache_size() == 0)
return;
compact_sparse_vector(out_psi);
// if the cache is full
if (loss.size() >= prob->get_max_cache_size())
{
@ -132,6 +137,28 @@ namespace dlib
}
}
// Do nothing if T isn't actually a sparse vector
template <typename T> void compact_sparse_vector( T& ) const { }
template <
typename T,
typename U,
typename alloc
>
void compact_sparse_vector (
std::vector<std::pair<T,U>,alloc>& vect
) const
{
// If the sparse vector has more entires than dimensions then it must have some
// duplicate elements. So compact them using make_sparse_vector_inplace().
if (vect.size() > (unsigned long)prob->get_num_dimensions())
{
make_sparse_vector_inplace(vect);
// make sure the vector doesn't use more RAM than is necessary
std::vector<std::pair<T,U>,alloc>(vect).swap(vect);
}
}
const structural_svm_problem* prob;
long sample_idx;