Commit b0fb6d54 authored by Davis King's avatar Davis King

Fixed the svm_c_linear_trainer so that it compiles with sparse vectors.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403510
parent d719e96a
......@@ -147,6 +147,44 @@ namespace dlib
return std::sqrt(distance_squared(a_scale,a,b_scale,b));
}
// ------------------------------------------------------------------------------------
template <typename T, typename EXP>
typename enable_if<is_matrix<T> >::type assign_dense_to_sparse (
T& dest,
const matrix_exp<EXP>& src
)
{
// make sure requires clause is not broken
DLIB_ASSERT(is_vector(src),
"\t void assign_dense_to_sparse(dest,src)"
<< "\n\t the src matrix must be a row or column vector"
);
dest = src;
}
template <typename T, typename EXP>
typename disable_if<is_matrix<T> >::type assign_dense_to_sparse (
T& dest,
const matrix_exp<EXP>& src
)
{
// make sure requires clause is not broken
DLIB_ASSERT(is_vector(src),
"\t void assign_dense_to_sparse(dest,src)"
<< "\n\t the src matrix must be a row or column vector"
);
dest.clear();
typedef typename T::value_type item_type;
for (long i = 0; i < src.size(); ++i)
{
if (src(i) != 0)
dest.insert(dest.end(),item_type(i, src(i)));
}
}
// ------------------------------------------------------------------------------------
template <typename T, typename U>
......
......@@ -99,6 +99,29 @@ namespace dlib
a_scale*a and b_scale*b. (i.e. std::sqrt(distance_squared(a_scale,a,b_scale,b)))
!*/
// ----------------------------------------------------------------------------------------
template <typename T, typename EXP>
void assign_dense_to_sparse (
T& dest,
const matrix_exp<EXP>& src
);
/*!
requires
- is_vector(src) == true
- the key type in T is capable of holding integers or T is a dlib::matrix
capable of storing src
ensures
- if (T is a dlib::matrix) then
- #dest == src
(if dest is just a normal matrix then this function just does a normal copy)
- else
- dest is a sparse vector and this function copies src into it. The
assignment is performed such that the following is true:
for all i: if (src(i) != 0) then make_pair(i, src(i)) is an element of #dest
- #dest will be properly sorted
!*/
// ----------------------------------------------------------------------------------------
template <typename T, typename U>
......
......@@ -11,6 +11,7 @@
#include "kernel.h"
#include <iostream>
#include <vector>
#include "sparse_vector.h"
namespace dlib
{
......@@ -170,8 +171,8 @@ namespace dlib
const scalar_type& C
) const
{
for (unsigned long i = 0; i < sample.size(); ++i)
subgradient(sample[i].first) += C*sample[i].second;
for (typename T::const_iterator i = sample.begin(); i != sample.end(); ++i)
subgradient(i->first) += C*i->second;
}
template <typename EXP>
......@@ -192,8 +193,8 @@ namespace dlib
const scalar_type& C
) const
{
for (unsigned long i = 0; i < sample.size(); ++i)
subgradient(sample[i].first) -= C*sample[i].second;
for (typename T::const_iterator i = sample.begin(); i != sample.end(); ++i)
subgradient(i->first) -= C*i->second;
}
template <typename EXP>
......@@ -213,8 +214,9 @@ namespace dlib
{
// compute a dot product between a dense column vector and a sparse vector
scalar_type temp = 0;
for (unsigned long i = 0; i < sample.size(); ++i)
temp += w(sample[i].first) * sample[i].second;
for (typename T::const_iterator i = sample.begin(); i != sample.end(); ++i)
temp += w(i->first) * i->second;
return temp;
}
template <typename T>
......@@ -241,7 +243,7 @@ namespace dlib
for (long i = 0; i < samples.size(); ++i)
{
if (samples(i).size() > 0)
max_dim = std::max<unsigned long>(max_dim, samples(i).back().first + 1);
max_dim = std::max<unsigned long>(max_dim, (--samples(i).end())->first + 1);
}
return max_dim;
......@@ -601,7 +603,9 @@ namespace dlib
decision_function<kernel_type> df;
df.b = static_cast<scalar_type>(w(w.size()-1));
df.basis_vectors.set_size(1);
df.basis_vectors(0) = matrix_cast<scalar_type>(colm(w, 0, w.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<scalar_type>(colm(w, 0, w.size()-1)));
df.alpha.set_size(1);
df.alpha(0) = 1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment