diff --git a/dlib/svm/svm_c_linear_trainer.h b/dlib/svm/svm_c_linear_trainer.h index 3e3d229c5..b8f79db85 100644 --- a/dlib/svm/svm_c_linear_trainer.h +++ b/dlib/svm/svm_c_linear_trainer.h @@ -16,6 +16,45 @@ namespace dlib { +// ---------------------------------------------------------------------------------------- + + template + typename enable_if,unsigned long>::type num_dimensions_in_samples ( + const T& samples + ) + { + if (samples.size() > 0) + return samples(0).size(); + else + return 0; + } + + template + typename disable_if,unsigned long>::type num_dimensions_in_samples ( + const T& samples + ) + /*! + T must be a sparse vector with an integral key type + !*/ + { + typedef typename T::type sample_type; + // You are getting this error because you are attempting to use sparse sample vectors with + // the svm_c_linear_trainer object but you aren't using an unsigned integer as your key type + // in the sparse vectors. + COMPILE_TIME_ASSERT(sparse_vector::has_unsigned_keys::value); + + + // these should be sparse samples so look over all them to find the max dimension. + unsigned long max_dim = 0; + for (long i = 0; i < samples.size(); ++i) + { + if (samples(i).size() > 0) + max_dim = std::max(max_dim, (--samples(i).end())->first + 1); + } + + return max_dim; + } + // ---------------------------------------------------------------------------------------- template < @@ -203,7 +242,7 @@ namespace dlib const matrix_exp& sample ) const { - return dot(colm(w,0,w.size()-1), sample); + return dot(colm(w,0,sample.size()), sample); } template @@ -219,43 +258,6 @@ namespace dlib return temp; } - template - typename enable_if,unsigned long>::type num_dimensions_in_samples ( - const T& samples - ) const - { - if (samples.size() > 0) - return samples(0).size(); - else - return 0; - } - - template - typename disable_if,unsigned long>::type num_dimensions_in_samples ( - const T& samples - ) const - /*! - T must be a sparse vector with an integral key type - !*/ - { - typedef typename T::type sample_type; - // You are getting this error because you are attempting to use sparse sample vectors with - // the svm_c_linear_trainer object but you aren't using an unsigned integer as your key type - // in the sparse vectors. - COMPILE_TIME_ASSERT(sparse_vector::has_unsigned_keys::value); - - - // these should be sparse samples so look over all them to find the max dimension. - unsigned long max_dim = 0; - for (long i = 0; i < samples.size(); ++i) - { - if (samples(i).size() > 0) - max_dim = std::max(max_dim, (--samples(i).end())->first + 1); - } - - return max_dim; - } - // ----------------------------------------------------- // ----------------------------------------------------- @@ -619,7 +621,10 @@ namespace dlib df.basis_vectors.set_size(1); // Copy the plane normal into the output basis vector. The output vector might be a // sparse vector container so we need to use this special kind of copy to handle that case. - sparse_vector::assign_dense_to_sparse(df.basis_vectors(0), matrix_cast(colm(w, 0, w.size()-1))); + // As an aside, the reason for using num_dimensions_in_samples() and not just w.size()-1 is because + // doing it this way avoids an inane warning from gcc that can occur in some cases. + const long out_size = num_dimensions_in_samples(x); + sparse_vector::assign_dense_to_sparse(df.basis_vectors(0), matrix_cast(colm(w, 0, out_size))); df.alpha.set_size(1); df.alpha(0) = 1;