43 #ifndef SHARK_MODELS_CSVMDERIVATIVE_H 44 #define SHARK_MODELS_CSVMDERIVATIVE_H 70 template<
class InputType,
class CacheType =
double >
119 : mep_ke( &ke->decisionFunction() ),
121 mep_k( trainer->kernel() ),
122 m_alpha( mep_ke->alpha() ),
123 m_basis( mep_ke->basis() ),
124 m_db_dParams_from_solver( trainer->get_db_dParams() ),
125 m_C ( trainer->C() ),
126 m_unconstrained( trainer->isUnconstrained() ),
127 m_nkp( trainer->kernel()->numberOfParameters() ),
128 m_nhp( trainer->kernel()->numberOfParameters()+1 )
130 SHARK_RUNTIME_CHECK( mep_ke->
kernel() == trainer->
kernel(),
"[CSvmDerivative::CSvmDerivative] KernelExpansion and SvmTrainer must use the same KernelFunction.");
131 SHARK_RUNTIME_CHECK( mep_ke != NULL,
"[CSvmDerivative::CSvmDerivative] KernelExpansion cannot be NULL.");
134 SHARK_RUNTIME_CHECK( m_alpha.size2() == 1,
"[CSvmDerivative::CSvmDerivative] this class is only defined for binary SVMs.");
135 prepareCSvmParameterDerivative();
140 {
return "CSvmDerivative"; }
152 RealMatrix unit_weights(1,1,1.0);
153 RealMatrix bof_results(1,1);
159 RealVector der( m_nhp );
160 boost::shared_ptr<State> state = mep_k->
createState();
161 derivative.resize( m_nhp );
164 noalias(derivative) = row(m_d_alphab_d_theta,m_noofFreeSVs);
168 mep_k->
eval( bof_input, bof_xi, bof_results, *state );
169 double ker = bof_results(0,0);
172 noalias(derivative) += ker * row(m_d_alphab_d_theta,i);
173 noalias(subrange(derivative,0,m_nkp))+= cur_alpha*der;
178 mep_k->
eval( bof_input, bof_xi, bof_results, *state );
179 double ker = bof_results(0,0);
182 derivative( m_nkp ) += ker * cur_label;
183 noalias(subrange(derivative,0,m_nkp))+= cur_label * m_C * der;
185 if ( m_unconstrained )
186 derivative( m_nkp ) *=
m_C;
191 if ( m_noofFreeSVs == 0 ) {
227 void prepareCSvmParameterDerivative() {
229 std::size_t numberOfAlphas = m_alpha.size1();
232 for ( std::size_t i=0; i<numberOfAlphas; i++ ) {
233 double cur_alpha =
m_alpha(i,0);
234 if ( cur_alpha != 0.0 ) {
235 if ( cur_alpha == m_C || cur_alpha == -m_C ) {
236 m_boundedAlphaIndices.push_back(i);
238 m_freeAlphaIndices.push_back(i);
242 m_noofFreeSVs = m_freeAlphaIndices.size();
243 m_noofBoundedSVs = m_boundedAlphaIndices.size();
245 m_d_alphab_d_theta.resize(m_noofFreeSVs+1, m_nhp);
246 m_d_alphab_d_theta.clear();
247 m_freeAlphaIndices.push_back( numberOfAlphas );
250 m_freeAlphas.resize( m_noofFreeSVs+1);
251 m_boundedAlphas.resize( m_noofBoundedSVs );
252 m_boundedLabels.resize( m_noofBoundedSVs );
257 double cur_alpha =
m_alpha( m_boundedAlphaIndices[i], 0 );
263 if ( m_noofFreeSVs == 0 ) {
271 RealVector der( m_nkp );
272 boost::shared_ptr<State> state = mep_k->
createState();
275 RealMatrix unit_weights(1,1,1.0);
276 RealMatrix bof_results(1,1);
279 if ( m_noofFreeSVs != 0 ) {
282 }
else if ( m_noofBoundedSVs != 0 ) {
286 throw SHARKEXCEPTION(
"[CSvmDerivative::prepareCSvmParameterDerivative] Something went very wrong.");
295 RealMatrix H( m_noofFreeSVs + 1, m_noofFreeSVs + 1,0.0);
296 std::vector< RealMatrix > dH( m_nkp , RealMatrix(m_noofFreeSVs+1, m_noofFreeSVs+1));
300 for ( std::size_t j=0; j<i; j++ ) {
302 mep_k->
eval( bof_xi, bof_xj, bof_results, *state );
303 H( i,j ) = H( j,i ) = bof_results(0,0);
305 for ( std::size_t k=0; k<
m_nkp; k++ ) {
306 dH[k]( i,j ) = dH[k]( j,i ) = der(k);
310 mep_k->
eval( bof_xi, bof_xi, bof_results, *state );
311 H( i,i ) = bof_results(0,0);
312 H( i, m_noofFreeSVs) = H( m_noofFreeSVs, i) = 1.0;
314 for ( std::size_t k=0; k<
m_nkp; k++ ) {
315 dH[k]( i,i ) = der(k);
318 for (std::size_t k=0; k<
m_nkp; k++)
323 for ( std::size_t k=0; k<
m_nkp; k++ ) {
328 RealMatrix R( m_noofFreeSVs+1, m_noofBoundedSVs );
329 std::vector< RealMatrix > dR( m_nkp, RealMatrix(m_noofFreeSVs+1, m_noofBoundedSVs));
334 mep_k->
eval( bof_xi, bof_xj, bof_results, *state );
335 R( j,i ) = bof_results(0,0);
337 for ( std::size_t k=0; k<
m_nkp; k++ )
338 dR[k]( j,i ) = der(k);
340 R( m_noofFreeSVs, i ) = 1.0;
341 for ( std::size_t k=0; k<
m_nkp; k++ )
342 dR[k]( m_noofFreeSVs, i ) = 0.0;
355 if ( m_noofBoundedSVs > 0 ) {
356 noalias(column(m_d_alphab_d_theta,m_nkp)) = prod( R, m_boundedLabels);
359 for ( std::size_t k=0; k<
m_nkp; k++ ) {
360 RealVector sum = prod( dH[k], m_freeAlphas );
361 if(m_noofBoundedSVs > 0)
362 noalias(sum) += prod( dR[k], m_boundedAlphas );
364 noalias(column(m_d_alphab_d_theta,k)) = sum;
371 noalias(m_d_alphab_d_theta) = - solveH(H,m_d_alphab_d_theta);
376 RealMatrix solveH(RealMatrix
const& H, RealMatrix
const& rhs){
378 RealMatrix HTH=prod(trans(H),H);
379 RealMatrix result = solve(HTH,prod(H,rhs),blas::symm_semi_pos_def(),blas::left());