diff --git a/dlib/svm/sparse_vector.h b/dlib/svm/sparse_vector.h index 4a1b9a209..54a178a60 100644 --- a/dlib/svm/sparse_vector.h +++ b/dlib/svm/sparse_vector.h @@ -185,6 +185,14 @@ namespace dlib } } + // ------------------------------------------------------------------------------------ + + template + struct has_unsigned_keys + { + static const bool value = is_unsigned_type::value; + }; + // ------------------------------------------------------------------------------------ template diff --git a/dlib/svm/sparse_vector_abstract.h b/dlib/svm/sparse_vector_abstract.h index b144c7c41..5ec0798ee 100644 --- a/dlib/svm/sparse_vector_abstract.h +++ b/dlib/svm/sparse_vector_abstract.h @@ -182,6 +182,20 @@ namespace dlib } +// ---------------------------------------------------------------------------------------- + + /*!A has_unsigned_keys + + This is a template where has_unsigned_keys::value == true when T is a + sparse vector that contains unsigned integral keys and false otherwise. + !*/ + + template + struct has_unsigned_keys + { + static const bool value = is_unsigned_type::value; + }; + // ---------------------------------------------------------------------------------------- } diff --git a/dlib/svm/svm_c_linear_trainer.h b/dlib/svm/svm_c_linear_trainer.h index 55a66e019..d6495d512 100644 --- a/dlib/svm/svm_c_linear_trainer.h +++ b/dlib/svm/svm_c_linear_trainer.h @@ -238,6 +238,13 @@ namespace dlib 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) diff --git a/dlib/svm/svm_c_linear_trainer_abstract.h b/dlib/svm/svm_c_linear_trainer_abstract.h index d6b2f7a2e..9469aff54 100644 --- a/dlib/svm/svm_c_linear_trainer_abstract.h +++ b/dlib/svm/svm_c_linear_trainer_abstract.h @@ -17,7 +17,8 @@ namespace dlib { /*! REQUIREMENTS ON K - is either linear_kernel or sparse_linear_kernel + Is either linear_kernel or sparse_linear_kernel. If you use a sparse_linear_kernel + then it must operate on sparse vectors that use unsigned integral keys. WHAT THIS OBJECT REPRESENTS This object represents a tool for training the C formulation of