From 1ef9e9189a0b9bcfc49191f2d08b59db7cbfa2f3 Mon Sep 17 00:00:00 2001 From: Davis King Date: Mon, 1 Mar 2010 03:10:24 +0000 Subject: [PATCH] Added some compile time checks to make sure that only sparse vectors that contain unsigned integral keys get used with the svm_c_linear_trainer. --HG-- extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403511 --- dlib/svm/sparse_vector.h | 8 ++++++++ dlib/svm/sparse_vector_abstract.h | 14 ++++++++++++++ dlib/svm/svm_c_linear_trainer.h | 7 +++++++ dlib/svm/svm_c_linear_trainer_abstract.h | 3 ++- 4 files changed, 31 insertions(+), 1 deletion(-) 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