43 #ifndef SHARK_MODELS_CSVMDERIVATIVE_H 44 #define SHARK_MODELS_CSVMDERIVATIVE_H 72 template<
class InputType,
class CacheType =
double >
121 : mep_ke( &ke->decisionFunction() ),
123 mep_k( trainer->kernel() ),
124 m_alpha( mep_ke->alpha() ),
125 m_basis( mep_ke->basis() ),
126 m_db_dParams_from_solver( trainer->get_db_dParams() ),
127 m_C ( trainer->C() ),
128 m_unconstrained( trainer->isUnconstrained() ),
129 m_nkp( trainer->kernel()->numberOfParameters() ),
130 m_nhp( trainer->kernel()->numberOfParameters()+1 )
132 SHARK_CHECK( mep_ke->
kernel() == trainer->
kernel(),
"[CSvmDerivative::CSvmDerivative] KernelExpansion and SvmTrainer must use the same KernelFunction.");
133 SHARK_CHECK( mep_ke != NULL,
"[CSvmDerivative::CSvmDerivative] KernelExpansion cannot be NULL.");
134 SHARK_CHECK( mep_ke->
outputSize() == 1,
"[CSvmDerivative::CSvmDerivative] only defined for binary SVMs.");
135 SHARK_CHECK( mep_ke->
hasOffset() == 1,
"[CSvmDerivative::CSvmDerivative] only defined for SVMs with offset.");
136 SHARK_CHECK( m_alpha.size2() == 1,
"[CSvmDerivative::CSvmDerivative] this class is only defined for binary SVMs.");
137 prepareCSvmParameterDerivative();
142 {
return "CSvmDerivative"; }
154 RealIdentityMatrix unit_weights(1);
155 RealMatrix bof_results(1,1);
158 get(bof_input, 0) = input;
161 RealVector der( m_nhp );
162 boost::shared_ptr<State> state = mep_k->
createState();
163 derivative.resize( m_nhp );
166 noalias(derivative) =
row(m_d_alphab_d_theta,m_noofFreeSVs);
169 get(bof_xi, 0) = m_basis.
element(m_freeAlphaIndices[i]);
170 mep_k->
eval( bof_input, bof_xi, bof_results, *state );
171 double ker = bof_results(0,0);
174 noalias(derivative) += ker *
row(m_d_alphab_d_theta,i);
179 get(bof_xi, 0) = m_basis.
element(m_boundedAlphaIndices[i]);
180 mep_k->
eval( bof_input, bof_xi, bof_results, *state );
181 double ker = bof_results(0,0);
184 derivative( m_nkp ) += ker * cur_label;
187 if ( m_unconstrained )
188 derivative( m_nkp ) *=
m_C;
193 if ( m_noofFreeSVs == 0 ) {
215 throw SHARKEXCEPTION(
"[CSvmDerivative::read] Not implemented yet.");
220 throw SHARKEXCEPTION(
"[CSvmDerivative::write] Not implemented yet.");
233 void prepareCSvmParameterDerivative() {
235 std::size_t numberOfAlphas = m_alpha.size1();
238 for ( std::size_t i=0; i<numberOfAlphas; i++ ) {
239 double cur_alpha =
m_alpha(i,0);
240 if ( cur_alpha != 0.0 ) {
241 if ( cur_alpha == m_C || cur_alpha == -m_C ) {
242 m_boundedAlphaIndices.push_back(i);
244 m_freeAlphaIndices.push_back(i);
248 m_noofFreeSVs = m_freeAlphaIndices.size();
249 m_noofBoundedSVs = m_boundedAlphaIndices.size();
251 m_d_alphab_d_theta.resize(m_noofFreeSVs+1, m_nhp);
252 m_d_alphab_d_theta.clear();
253 m_freeAlphaIndices.push_back( numberOfAlphas );
256 m_freeAlphas.resize( m_noofFreeSVs+1);
257 m_boundedAlphas.resize( m_noofBoundedSVs );
258 m_boundedLabels.resize( m_noofBoundedSVs );
263 double cur_alpha =
m_alpha( m_boundedAlphaIndices[i], 0 );
269 if ( m_noofFreeSVs == 0 ) {
277 RealVector der( m_nkp );
278 boost::shared_ptr<State> state = mep_k->
createState();
281 RealIdentityMatrix unit_weights(1);
282 RealMatrix bof_results(1,1);
285 if ( m_noofFreeSVs != 0 ) {
288 }
else if ( m_noofBoundedSVs != 0 ) {
292 throw SHARKEXCEPTION(
"[CSvmDerivative::prepareCSvmParameterDerivative] Something went very wrong.");
297 RealMatrix H( m_noofFreeSVs+1, m_noofFreeSVs+1,0.0 );
298 std::vector< RealMatrix > dH( m_nkp , RealMatrix(m_noofFreeSVs+1, m_noofFreeSVs+1));
300 get(bof_xi, 0) = m_basis.
element(m_freeAlphaIndices[i]);
302 for ( std::size_t j=0; j<i; j++ ) {
303 get(bof_xj, 0) = m_basis.
element(m_freeAlphaIndices[j]);
304 mep_k->
eval( bof_xi, bof_xj, bof_results, *state );
305 H( i,j ) = H( j,i ) = bof_results(0,0);
307 for ( std::size_t k=0; k<
m_nkp; k++ ) {
308 dH[k]( i,j ) = dH[k]( j,i ) = der(k);
312 mep_k->
eval( bof_xi, bof_xi, bof_results, *state );
313 H( i,i ) = bof_results(0,0);
315 for ( std::size_t k=0; k<
m_nkp; k++ ) {
316 dH[k]( i,i ) = der(k);
319 H( m_noofFreeSVs, i ) = H( i, m_noofFreeSVs ) = 1.0;
320 for (std::size_t k=0; k<
m_nkp; k++)
325 H( m_noofFreeSVs, m_noofFreeSVs ) = 0.0;
326 for ( std::size_t k=0; k<
m_nkp; k++ ) {
331 RealMatrix R( m_noofFreeSVs+1, m_noofBoundedSVs );
332 std::vector< RealMatrix > dR( m_nkp, RealMatrix(m_noofFreeSVs+1, m_noofBoundedSVs));
334 get(bof_xi, 0) = m_basis.
element(m_boundedAlphaIndices[i]);
336 get(bof_xj, 0) = m_basis.
element(m_freeAlphaIndices[j]);
337 mep_k->
eval( bof_xi, bof_xj, bof_results, *state );
338 R( j,i ) = bof_results(0,0);
340 for ( std::size_t k=0; k<
m_nkp; k++ )
341 dR[k]( j,i ) = der(k);
343 R( m_noofFreeSVs, i ) = 1.0;
344 for ( std::size_t k=0; k<
m_nkp; k++ )
345 dR[k]( m_noofFreeSVs, i ) = 0.0;
358 if ( m_noofBoundedSVs > 0 ) {
362 for ( std::size_t k=0; k<
m_nkp; k++ ) {
363 RealVector
sum =
prod( dH[k], m_freeAlphas );
364 if(m_noofBoundedSVs > 0)
374 m_d_alphab_d_theta*=-1;