From f9805be8566c329bf62aabdc89325ddfb7c682c1 Mon Sep 17 00:00:00 2001 From: Davis King Date: Mon, 17 Dec 2012 20:33:34 -0500 Subject: [PATCH] Fixed a bug which triggered when the last weight was forced to 1. --- dlib/svm/svm_c_linear_dcd_trainer.h | 40 ++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/dlib/svm/svm_c_linear_dcd_trainer.h b/dlib/svm/svm_c_linear_dcd_trainer.h index f519f8e39..64ca66e44 100644 --- a/dlib/svm/svm_c_linear_dcd_trainer.h +++ b/dlib/svm/svm_c_linear_dcd_trainer.h @@ -301,7 +301,7 @@ namespace dlib for (long i = new_idx; i < x.size(); ++i) { - Q.push_back(dlib::dot(x(i),x(i))); + Q.push_back(length_squared(x(i))); if (have_bias) { @@ -318,6 +318,44 @@ namespace dlib w(dims-1) = 1; } + template + typename enable_if,scalar_type>::type length_squared (const T& x) const + { + if (!last_weight_1) + { + return dlib::dot(x,x); + } + else + { + // skip the last dimension + return dlib::dot(colm(x,0,x.size()-1), + colm(x,0,x.size()-1)); + } + + } + + template + typename disable_if,scalar_type>::type length_squared (const T& x) const + { + if (!last_weight_1) + { + return dlib::dot(x,x); + } + else + { + scalar_type temp = 0; + typename T::const_iterator i; + for (i = x.begin(); i != x.end(); ++i) + { + // skip the last dimension + if (static_cast(i->first) < dims-1) + temp += i->second*i->second; + } + return temp; + } + } + + bool did_init; bool have_bias; bool last_weight_1;