119 :
mep_ke( &
ke->decisionFunction() ),
132 SHARK_RUNTIME_CHECK(
mep_ke->outputShape().numElements() == 1,
"[CSvmDerivative::CSvmDerivative] 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 );
161 derivative.resize(
m_nhp );
168 mep_k->
eval( bof_input, bof_xi, bof_results, *state );
169 double ker = bof_results(0,0);
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;
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 ) {
271 RealVector der(
m_nkp );
275 RealMatrix unit_weights(1,1,1.0);
276 RealMatrix bof_results(1,1);
277 typename Batch<InputType>::type bof_xi;
278 typename Batch<InputType>::type bof_xj;
286 throw SHARKEXCEPTION(
"[CSvmDerivative::prepareCSvmParameterDerivative] Something went very wrong.");
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);
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++ ) {
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);
341 for ( std::size_t k=0; k<
m_nkp; k++ )
359 for ( std::size_t k=0; k<
m_nkp; k++ ) {
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());