SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GUIKernel.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2008 Soeren Sonnenburg
8  * Written (W) 1999-2008 Gunnar Raetsch
9  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
10  */
11 
12 #include <shogun/ui/SGInterface.h>
13 #include <shogun/ui/GUIKernel.h>
15 
16 #include <shogun/lib/config.h>
17 #include <shogun/io/SGIO.h>
18 #include <shogun/io/AsciiFile.h>
19 #include <shogun/kernel/Kernel.h>
60 
61 #include <string.h>
62 
63 using namespace shogun;
64 
65 CGUIKernel::CGUIKernel(CSGInterface* ui_)
66 : CSGObject(), ui(ui_)
67 {
68  kernel=NULL;
69 }
70 
72 {
74 }
75 
77 {
78  return kernel;
79 }
80 
81 CKernel* CGUIKernel::create_oligo(int32_t size, int32_t k, float64_t width)
82 {
83  CKernel* kern=new COligoStringKernel(size, k, width);
84  SG_DEBUG("created OligoStringKernel (%p) with size %d, k %d, width %f.\n", kern, size, k, width);
85 
86  return kern;
87 }
88 
90 {
91  CKernel* kern=new CDiagKernel(size, diag);
92  if (!kern)
93  SG_ERROR("Couldn't create DiagKernel with size %d, diag %f.\n", size, diag);
94  else
95  SG_DEBUG("created DiagKernel (%p) with size %d, diag %f.\n", kern, size, diag);
96 
97  return kern;
98 }
99 
101 {
102  CKernel* kern=new CConstKernel(c);
103  if (!kern)
104  SG_ERROR("Couldn't create ConstKernel with c %f.\n", c);
105  else
106  SG_DEBUG("created ConstKernel (%p) with c %f.\n", kern, c);
107 
108  kern->set_cache_size(size);
109 
110  return kern;
111 }
112 
113 CKernel* CGUIKernel::create_custom(float64_t* kmatrix, int32_t num_feat, int32_t num_vec, bool source_is_diag, bool dest_is_diag)
114 {
115  CCustomKernel* kern=new CCustomKernel();
116  SG_DEBUG("created CustomKernel (%p).\n", kern);
117 
118  SGMatrix<float64_t> km=SGMatrix<float64_t>(kmatrix, num_feat, num_vec);
119 
120  if (source_is_diag && dest_is_diag && num_feat==1)
121  {
123  SGVector<float64_t>(kmatrix, num_vec));
124  }
125  else if (!source_is_diag && dest_is_diag && num_vec==num_feat)
127  else
129 
130  SG_FREE(kmatrix);
131  return kern;
132 }
133 
134 
136  int32_t size, float64_t width, int32_t max_shift, int32_t shift_step)
137 {
138  CKernel* kern=new CGaussianShiftKernel(size, width, max_shift, shift_step);
139  if (!kern)
140  SG_ERROR("Couldn't create GaussianShiftKernel with size %d, width %f, max_shift %d, shift_step %d.\n", size, width, max_shift, shift_step);
141  else
142  SG_DEBUG("created GaussianShiftKernel (%p) with size %d, width %f, max_shift %d, shift_step %d.\n", kern, size, width, max_shift, shift_step);
143 
144  return kern;
145 }
146 
148 {
149  CKernel* kern=new CGaussianKernel(size, width);
150  if (!kern)
151  SG_ERROR("Couldn't create GaussianKernel with size %d, width %f.\n", size, width);
152  else
153  SG_DEBUG("created GaussianKernel (%p) with size %d, width %f.\n", kern, size, width);
154 
155  return kern;
156 }
157 
159 {
160  CKernel* kern=new CGaussianKernel(size, width);
161  if (!kern)
162  SG_ERROR("Couldn't create GaussianKernel with size %d, width %f.\n", size, width);
163  else
164  SG_DEBUG("created GaussianKernel (%p) with size %d, width %f.\n", kern, size, width);
165 
166  return kern;
167 }
168 
170  int32_t size, float64_t gamma, float64_t coef0)
171 {
172  CKernel* kern=new CSigmoidKernel(size, gamma, coef0);
173  if (!kern)
174  SG_ERROR("Couldn't create SigmoidKernel with size %d, gamma %f, coef0 %f.\n", size, gamma, coef0);
175  else
176  SG_DEBUG("created SigmoidKernel (%p) with size %d, gamma %f, coef0 %f.\n", kern, size, gamma, coef0);
177 
178  return kern;
179 }
181  int32_t size, float64_t Wdilation, float64_t Wtranslation)
182 {
183  CKernel* kern=new CWaveletKernel(size, Wdilation, Wtranslation);
184  if (!kern)
185  SG_ERROR("Couldn't create WaveletKernel with size %d, Wdilation %f, Wtranslation %f.\n", size, Wdilation, Wtranslation);
186  else
187  SG_DEBUG("created WaveletKernel (%p) with size %d, Wdilation %f, Wtranslation %f.\n", kern, size, Wdilation, Wtranslation);
188 
189  return kern;
190 }
192  int32_t size, int32_t degree, bool inhomogene, bool normalize)
193 {
194  CKernel* kern=new CPolyKernel(size, degree, inhomogene);
195  if (!normalize)
197  SG_DEBUG("created PolyKernel with size %d, degree %d, inhomogene %d normalize %d.\n", kern, size, degree, inhomogene, normalize);
198 
199  return kern;
200 }
201 
203  int32_t size, int32_t degree, bool inhomogene, bool normalize)
204 {
205  CKernel* kern=new CPolyKernel(size, degree, inhomogene);
206  if (!normalize)
208  SG_DEBUG("created PolyKernel (%p) with size %d, degree %d, inhomogene %d, normalize %d.\n", kern, size, degree, inhomogene, normalize);
209 
210  return kern;
211 }
212 
214  int32_t size, int32_t length, int32_t inner_degree, int32_t outer_degree,
215  EKernelType ktype)
216 {
217  CKernel* kern=NULL;
218 
219  if (ktype==K_SIMPLELOCALITYIMPROVED)
220  {
222  size, length, inner_degree, outer_degree);
223  }
224  else if (ktype==K_LOCALITYIMPROVED)
225  {
227  size, length, inner_degree, outer_degree);
228  }
229 
230  if (!kern)
231  SG_ERROR("Couldn't create (Simple)LocalityImprovedStringKernel with size %d, length %d, inner_degree %d, outer_degree %d.\n", size, length, inner_degree, outer_degree);
232  else
233  SG_DEBUG("created (Simple)LocalityImprovedStringKernel with size %d, length %d, inner_degree %d, outer_degree %d.\n", kern, size, length, inner_degree, outer_degree);
234 
235  return kern;
236 }
237 
239  int32_t size, int32_t order, int32_t max_mismatch, bool use_normalization,
240  int32_t mkl_stepsize, bool block_computation, int32_t single_degree)
241 {
242  float64_t* weights=get_weights(order, max_mismatch);
243 
244  int32_t i=0;
245  if (single_degree>=0)
246  {
247  ASSERT(single_degree<order);
248  for (i=0; i<order; i++)
249  {
250  if (i!=single_degree)
251  weights[i]=0;
252  else
253  weights[i]=1;
254  }
255  }
256 
257  CKernel* kern=new CWeightedDegreeStringKernel(weights, order);
258 
259  SG_DEBUG("created WeightedDegreeStringKernel (%p) with size %d, order %d, "
260  "max_mismatch %d, use_normalization %d, mkl_stepsize %d, "
261  "block_computation %d, single_degree %d.\n",
262  kern, size, order, max_mismatch, (int) use_normalization, mkl_stepsize,
263  block_computation, single_degree);
264 
265  if (!use_normalization)
267 
268  ((CWeightedDegreeStringKernel*) kern)->
269  set_use_block_computation(block_computation);
270  ((CWeightedDegreeStringKernel*) kern)->set_max_mismatch(max_mismatch);
271  ((CWeightedDegreeStringKernel*) kern)->set_mkl_stepsize(mkl_stepsize);
272  ((CWeightedDegreeStringKernel*) kern)->set_which_degree(single_degree);
273 
274  SG_FREE(weights);
275  return kern;
276 }
277 
279  int32_t size, int32_t order, int32_t max_mismatch, int32_t length,
280  int32_t center, float64_t step)
281 {
282  int32_t i=0;
283  int32_t* shifts=SG_MALLOC(int32_t, length);
284 
285  for (i=center; i<length; i++)
286  shifts[i]=(int32_t) floor(((float64_t) (i-center))/step);
287 
288  for (i=center-1; i>=0; i--)
289  shifts[i]=(int32_t) floor(((float64_t) (center-i))/step);
290 
291  for (i=0; i<length; i++)
292  {
293  if (shifts[i]>length)
294  shifts[i]=length;
295  }
296 
297  for (i=0; i<length; i++)
298  SG_INFO( "shift[%i]=%i\n", i, shifts[i]);
299 
300  float64_t* weights=get_weights(order, max_mismatch);
301 
302  CKernel* kern=new CWeightedDegreePositionStringKernel(size, weights, order, max_mismatch, shifts, length);
303  if (!kern)
304  SG_ERROR("Couldn't create WeightedDegreePositionStringKernel with size %d, order %d, max_mismatch %d, length %d, center %d, step %f.\n", size, order, max_mismatch, length, center, step);
305  else
306  SG_DEBUG("created WeightedDegreePositionStringKernel with size %d, order %d, max_mismatch %d, length %d, center %d, step %f.\n", kern, size, order, max_mismatch, length, center, step);
307 
308  SG_FREE(weights);
309  SG_FREE(shifts);
310  return kern;
311 }
312 
314  int32_t size, int32_t order, int32_t max_mismatch, int32_t* shifts,
315  int32_t length, int32_t mkl_stepsize, float64_t* position_weights)
316 {
317  float64_t* weights=get_weights(order, max_mismatch);
318 
319  CKernel* kern=new CWeightedDegreePositionStringKernel(size, weights, order, max_mismatch, shifts, length, mkl_stepsize);
321 
322  SG_DEBUG("created WeightedDegreePositionStringKernel (%p) with size %d, order %d, max_mismatch %d, length %d and position_weights (MKL stepsize: %d).\n", kern, size, order, max_mismatch, length, mkl_stepsize);
323 
324  if (!position_weights)
325  {
326  position_weights=SG_MALLOC(float64_t, length);
327  for (int32_t i=0; i<length; i++)
328  position_weights[i]=1.0/length;
329  }
331  set_position_weights(SGVector<float64_t>(position_weights, length));
332 
333  SG_FREE(weights);
334  return kern;
335 }
336 
338  int32_t size, int32_t order, int32_t max_mismatch, int32_t* shifts,
339  int32_t length, bool use_normalization)
340 {
341  float64_t* weights=get_weights(order, max_mismatch);
342 
343  CKernel* kern=new CWeightedDegreePositionStringKernel(size, weights, order, max_mismatch, shifts, length);
344  if (!use_normalization)
346 
347 
348  SG_DEBUG("created WeightedDegreePositionStringKernel (%p) with size %d, order %d, max_mismatch %d, length %d, use_normalization %d.\n", kern, size, order, max_mismatch, length, use_normalization);
349 
350  SG_FREE(weights);
351  return kern;
352 }
353 
354 float64_t* CGUIKernel::get_weights(int32_t order, int32_t max_mismatch)
355 {
356  float64_t *weights=SG_MALLOC(float64_t, order*(1+max_mismatch));
357  float64_t sum=0;
358  int32_t i=0;
359 
360  for (i=0; i<order; i++)
361  {
362  weights[i]=order-i;
363  sum+=weights[i];
364  }
365  for (i=0; i<order; i++)
366  weights[i]/=sum;
367 
368  for (i=0; i<order; i++)
369  {
370  for (int32_t j=1; j<=max_mismatch; j++)
371  {
372  if (j<i+1)
373  {
374  int32_t nk=CMath::nchoosek(i+1, j);
375  weights[i+j*order]=weights[i]/(nk*CMath::pow(3, j));
376  }
377  else
378  weights[i+j*order]=0;
379  }
380  }
381 
382  return weights;
383 }
384 
385 CKernel* CGUIKernel::create_weighteddegreerbf(int32_t size, int32_t degree, int32_t nof_properties, float64_t width)
386 {
387  CKernel* kern=new CWeightedDegreeRBFKernel(size, width, degree, nof_properties);
388  if (!kern)
389  SG_ERROR("Couldn't create WeightedDegreeRBFKernel with size %d, width %f, degree %d, nof_properties %d.\n", size, width, degree, nof_properties);
390  else
391  SG_DEBUG("created WeightedDegreeRBFKernel (%p) with size %d, width %f, degree %d, nof_properties %d.\n", kern, size, width, degree, nof_properties);
392 
393  return kern;
394 }
395 
396 CKernel* CGUIKernel::create_spectrummismatchrbf(int32_t size, float64_t* AA_matrix, int32_t nr, int32_t nc, int32_t max_mismatch, int32_t degree, float64_t width)
397 {
398 
399  CKernel* kern = new CSpectrumMismatchRBFKernel(size, AA_matrix, nr, nc, degree, max_mismatch, width);
400  if (!kern)
401  SG_ERROR("Couldn't create SpectrumMismatchRBFKernel with size %d, width %f, degree %d, max_mismatch %d.\n", size, width, degree, max_mismatch);
402  else
403  SG_DEBUG("created SpectrumMismatchRBFKernel (%p) with size %d, width %f, degree %d, max_mismatch %d.\n", kern, size, width, degree, max_mismatch);
404 
405  return kern;
406 
407 }
408 
409 
411 {
412  CKernel* kern=new CLocalAlignmentStringKernel(size);
413  if (!kern)
414  SG_ERROR("Couldn't create LocalAlignmentStringKernel with size %d.\n", size);
415  else
416  SG_DEBUG("created LocalAlignmentStringKernel (%p) with size %d.\n", kern, size);
417 
418  return kern;
419 }
420 
422 {
423  CKernel* kern=new CFixedDegreeStringKernel(size, d);
424  if (!kern)
425  SG_ERROR("Couldn't create FixedDegreeStringKernel with size %d and d %d.\n", size, d);
426  else
427  SG_DEBUG("created FixedDegreeStringKernel (%p) with size %d and d %d.\n", kern, size, d);
428 
429  return kern;
430 }
431 
433 {
434  CKernel* kern=new CChi2Kernel(size, width);
435  if (!kern)
436  SG_ERROR("Couldn't create Chi2Kernel with size %d and width %f.\n", size, width);
437  else
438  SG_DEBUG("created Chi2Kernel (%p) with size %d and width %f.\n", kern, size, width);
439 
440  return kern;
441 }
442 
444  int32_t size, bool use_sign, char* norm_str, EKernelType ktype)
445 {
446  CKernel* kern=NULL;
447 
448  if (!norm_str)
449  norm_str= (char*) "FULL";
450 
451  if (ktype==K_COMMULONGSTRING)
452  kern=new CCommUlongStringKernel(size, use_sign);
453  else if (ktype==K_COMMWORDSTRING)
454  kern=new CCommWordStringKernel(size, use_sign);
455  else if (ktype==K_WEIGHTEDCOMMWORDSTRING)
456  kern=new CWeightedCommWordStringKernel(size, use_sign);
457 
458  SG_DEBUG("created WeightedCommWord/CommWord/CommUlongStringKernel (%p) with size %d, use_sign %d norm_str %s.\n", kern, size, use_sign, norm_str);
459 
460 
461  if (strncmp(norm_str, "NO", 2)==0)
462  {
464  }
465  else if (strncmp(norm_str, "FULL", 4)==0)
466  {
467  //nop, as this one is default
468  }
469  else
470  SG_ERROR("Unsupported Normalizer requested, supports only FULL and NO\n");
471 
472  return kern;
473 }
474 
476  int32_t size, int32_t d, bool normalize)
477 {
478  CKernel* kern=new CMatchWordStringKernel(size, d);
479  SG_DEBUG("created MatchWordStringKernel (%p) with size %d and d %d.\n", kern, size, d);
480  if (!normalize)
482 
483  return kern;
484 }
485 
487  int32_t size, int32_t degree, bool inhomogene, bool normalize)
488 {
489  CKernel* kern=new CPolyMatchStringKernel(size, degree, inhomogene);
490  SG_DEBUG("created PolyMatchStringKernel (%p) with size %d, degree %d, inhomogene %d normalize %d.\n", kern, size, degree, inhomogene, normalize);
491  if (!normalize)
493 
494  return kern;
495 }
496 
498  int32_t size, int32_t degree, bool inhomogene, bool normalize)
499 {
500  CKernel* kern=new CPolyMatchWordStringKernel(size, degree, inhomogene);
501  SG_DEBUG("created PolyMatchWordStringKernel (%p) with size %d, degree %d, inhomogene %d, normalize %d.\n", kern, size, degree, inhomogene, normalize);
502  if (!normalize)
504 
505  return kern;
506 }
507 
509 {
510  SG_INFO("Getting estimator.\n");
511  CPluginEstimate* estimator=ui->ui_pluginestimate->get_estimator();
512  if (!estimator)
513  SG_ERROR("No estimator set.\n");
514 
515  CKernel* kern=new CSalzbergWordStringKernel(size, estimator);
516  if (!kern)
517  SG_ERROR("Couldn't create SalzbergWordString with size %d.\n", size);
518  else
519  SG_DEBUG("created SalzbergWordString (%p) with size %d.\n", kern, size);
520 
521 /*
522  // prior stuff
523  SG_INFO("Getting labels.\n");
524  CLabels* train_labels=ui->ui_labels->get_train_labels();
525  if (!train_labels)
526  {
527  SG_INFO("Assign train labels first!\n");
528  return NULL;
529  }
530  ((CSalzbergWordStringKernel *) kern)->set_prior_probs_from_labels(train_labels);
531 */
532 
533  return kern;
534 }
535 
537 {
538  SG_INFO("Getting estimator.\n");
539  CPluginEstimate* estimator=ui->ui_pluginestimate->get_estimator();
540  if (!estimator)
541  SG_ERROR("No estimator set.\n");
542 
543  CKernel* kern=new CHistogramWordStringKernel(size, estimator);
544  if (!kern)
545  SG_ERROR("Couldn't create HistogramWordString with size %d.\n", size);
546  else
547  SG_DEBUG("created HistogramWordString (%p) with size %d.\n", kern, size);
548 
549  return kern;
550 }
551 
553 {
554  size=0;
555  CKernel* kern=new CLinearKernel();
556  kern->set_normalizer(new CAvgDiagKernelNormalizer(scale));
557  SG_DEBUG("created LinearByteKernel (%p) with size %d and scale %f.\n", kern, size, scale);
558 
559  return kern;
560 }
561 
563 {
564  size=0;
565  CKernel* kern=new CLinearKernel();
566  kern->set_normalizer(new CAvgDiagKernelNormalizer(scale));
567  SG_DEBUG("created LinearWordKernel (%p) with size %d and scale %f.\n", kern, size, scale);
568 
569  return kern;
570 }
571 
573 {
574  size=0;
575  CKernel* kern=NULL;
576  kern=new CLinearStringKernel();
577  kern->set_normalizer(new CAvgDiagKernelNormalizer(scale));
578 
579  SG_DEBUG("created LinearStringKernel (%p) with size %d and scale %f.\n", kern, size, scale);
580 
581  return kern;
582 }
583 
585 {
586  size=0;
587  CKernel* kern=new CLinearKernel();
588  kern->set_normalizer(new CAvgDiagKernelNormalizer(scale));
589 
590  SG_DEBUG("created LinearKernel (%p) with size %d and scale %f.\n", kern, size, scale);
591 
592  return kern;
593 }
594 
596 {
597  size=0;
598  CKernel* kern=new CLinearKernel();
599  kern->set_normalizer(new CAvgDiagKernelNormalizer(scale));
600 
601  SG_DEBUG("created LinearKernel (%p) with size %d and scale %f.\n", kern, size, scale);
602 
603  return kern;
604 }
605 
606 CKernel* CGUIKernel::create_tppk(int32_t size, float64_t* km, int32_t rows, int32_t cols)
607 {
608  CCustomKernel* k=new CCustomKernel();
610 
611  CKernel* kern=new CTensorProductPairKernel(size, k);
612 
613  SG_DEBUG("created TPPK (%p) with size %d and km %p, rows %d, cols %d.\n", kern, size, km, rows, cols);
614 
615  return kern;
616 }
617 
619 {
620  CDistance* dist=ui->ui_distance->get_distance();
621  if (!dist)
622  SG_ERROR("No distance set for DistanceKernel.\n");
623 
624  CKernel* kern=new CDistanceKernel(size, width, dist);
625  if (!kern)
626  SG_ERROR("Couldn't create DistanceKernel with size %d and width %f.\n", size, width);
627  else
628  SG_DEBUG("created DistanceKernel (%p) with size %d and width %f.\n", kern, size, width);
629 
630  return kern;
631 }
632 
634  int32_t size, bool append_subkernel_weights)
635 {
636  CKernel* kern=new CCombinedKernel(size, append_subkernel_weights);
637  if (!kern)
638  SG_ERROR("Couldn't create CombinedKernel with size %d and append_subkernel_weights %d.\n", size, append_subkernel_weights);
639  else
640  SG_DEBUG("created CombinedKernel (%p) with size %d and append_subkernel_weights %d.\n", kern, size, append_subkernel_weights);
641 
642  return kern;
643 }
644 
645 bool CGUIKernel::set_normalization(char* normalization, float64_t c, float64_t r)
646 {
647  CKernel* k=kernel;
648 
649  if (k && k->get_kernel_type()==K_COMBINED)
650  k=((CCombinedKernel*) kernel)->get_last_kernel();
651 
652  if (!k)
653  SG_ERROR("No kernel available.\n");
654 
655  if (strncmp(normalization, "IDENTITY", 8)==0)
656  {
657  SG_INFO("Identity Normalization (==NO NORMALIZATION) selected\n");
658  return k->set_normalizer(new CIdentityKernelNormalizer());
659  }
660  else if (strncmp(normalization,"AVGDIAG", 7)==0)
661  {
662  SG_INFO("Average Kernel Diagonal Normalization selected\n");
663  return k->set_normalizer(new CAvgDiagKernelNormalizer(c));
664  }
665  else if (strncmp(normalization,"RIDGE", 5)==0)
666  {
667  SG_INFO("Ridge Kernel Normalization selected\n");
668  return k->set_normalizer(new CRidgeKernelNormalizer(r, c));
669  }
670  else if (strncmp(normalization,"SQRTDIAG", 8)==0)
671  {
672  SG_INFO("Sqrt Diagonal Normalization selected\n");
673  return k->set_normalizer(new CSqrtDiagKernelNormalizer());
674  }
675  else if (strncmp(normalization,"FIRSTELEMENT", 12)==0)
676  {
677  SG_INFO("First Element Normalization selected\n");
679  }
680  else if (strncmp(normalization,"VARIANCE", 8)==0)
681  {
682  SG_INFO("Variance Normalization selected\n");
683  return k->set_normalizer(new CVarianceKernelNormalizer());
684  }
685  else if (strncmp(normalization,"SCATTER", 7)==0)
686  {
687  SG_INFO("Scatter Normalization selected\n");
688  CLabels* train_labels=ui->ui_labels->get_train_labels();
689  ASSERT(train_labels);
690  return k->set_normalizer(new CScatterKernelNormalizer(c,r, train_labels));
691  }
692  else if (strncmp(normalization,"ZEROMEANCENTER", 13)==0)
693  {
694  SG_INFO("Zero Mean Center Normalization selected\n");
696  }
697  else
698  SG_ERROR("Wrong kernel normalizer name.\n");
699 
700  SG_UNREF(k);
701 
702  return false;
703 }
704 
706 {
707  if (kern)
708  {
709  SG_DEBUG("deleting old kernel (%p).\n", kernel);
710  SG_UNREF(kernel);
711  SG_REF(kern);
712  kernel=kern;
713  SG_DEBUG("set new kernel (%p).\n", kern);
714 
715  return true;
716  }
717  else
718  return false;
719 }
720 
722 {
723  CSVM* svm=(CSVM*) ui->ui_classifier->get_classifier();
724  if (svm)
725  {
727  {
728  int32_t num_sv=svm->get_num_support_vectors();
729  int32_t* sv_idx=SG_MALLOC(int32_t, num_sv);
730  float64_t* sv_weight=SG_MALLOC(float64_t, num_sv);
731 
732  for (int32_t i=0; i<num_sv; i++)
733  {
734  sv_idx[i]=svm->get_support_vector(i);
735  sv_weight[i]=svm->get_alpha(i);
736  }
737 
738  bool ret=kernel->init_optimization(num_sv, sv_idx, sv_weight);
739 
740  SG_FREE(sv_idx);
741  SG_FREE(sv_weight);
742 
743  if (!ret)
744  SG_ERROR("Initialization of kernel optimization failed\n");
745  return ret;
746  }
747  }
748  else
749  SG_ERROR("Create SVM first!\n");
750 
751  return true;
752 }
753 
755 {
758 
759  return true;
760 }
761 
762 
763 bool CGUIKernel::init_kernel(const char* target)
764 {
765  if (!kernel)
766  SG_ERROR("No kernel available.\n");
767 
768  // no need to init custom kernel
769  if (kernel->get_kernel_type() == K_CUSTOM || !target)
770  {
771  initialized=true;
772  return true;
773  }
774 
777 
778  if (!strncmp(target, "TRAIN", 5))
779  {
780  CFeatures* train=ui->ui_features->get_train_features();
781 
782  if (train)
783  {
784  EFeatureClass fclass=train->get_feature_class();
785  EFeatureType ftype=train->get_feature_type();
786  if ((k_fclass==fclass || k_fclass==C_ANY || fclass==C_ANY) &&
787  (k_ftype==ftype || k_ftype==F_ANY || ftype==F_ANY))
788 
789  {
790  SG_INFO("Initialising kernel with TRAIN DATA, train: %p\n", train);
791  kernel->init(train, train);
792  initialized=true;
793  }
794  else
795  SG_ERROR("Kernel can not process this train feature type: %d %d.\n", fclass, ftype);
796  }
797  else
798  SG_DEBUG("Not initing kernel - no train features assigned.\n");
799  }
800  else if (!strncmp(target, "TEST", 4))
801  {
802  CFeatures* train=ui->ui_features->get_train_features();
803  CFeatures* test=ui->ui_features->get_test_features();
804  if (train && test)
805  {
806  EFeatureClass fclass=test->get_feature_class();
807  EFeatureType ftype=test->get_feature_type();
808  if ((k_fclass==fclass || k_fclass==C_ANY || fclass==C_ANY) &&
809  (k_ftype==ftype || k_ftype==F_ANY || ftype==F_ANY))
810 
811  {
812  if (!initialized)
813  {
814  EFeatureClass tr_fclass=train->get_feature_class();
815  EFeatureType tr_ftype=train->get_feature_type();
816  if ((k_fclass==tr_fclass || k_fclass==C_ANY || tr_fclass==C_ANY) &&
817  (k_ftype==tr_ftype || k_ftype==F_ANY || tr_ftype==F_ANY))
818  {
819  SG_INFO("Initialising kernel with TRAIN DATA, train: %p\n", train);
820  kernel->init(train, train);
821  initialized=true;
822  }
823  else
824  SG_ERROR("Kernel can not process this train feature type: %d %d.\n", fclass, ftype);
825  }
826 
827  SG_INFO("Initialising kernel with TEST DATA, train: %p test %p\n", train, test);
828  // lhs -> always train_features; rhs -> always test_features
829  kernel->init(train, test);
830  }
831  else
832  SG_ERROR("Kernel can not process this test feature type: %d %d.\n", fclass, ftype);
833  }
834  else
835  SG_DEBUG("Not initing kernel - no train and test features assigned.\n");
836  }
837  else
838  SG_ERROR("Unknown target %s.\n", target);
839 
840  return true;
841 }
842 
843 bool CGUIKernel::save_kernel(char* filename)
844 {
845  if (kernel && initialized)
846  {
847  CAsciiFile* file=new CAsciiFile(filename);
848  try
849  {
850  kernel->save(file);
851  }
852  catch (...)
853  {
854  SG_ERROR("Writing to file %s failed!\n", filename);
855  }
856 
857  SG_UNREF(file);
858  SG_INFO("Successfully written kernel to \"%s\" !\n", filename);
859  return true;
860  }
861  else
862  SG_ERROR("No kernel set / kernel not initialized!\n");
863 
864  return false;
865 }
866 
868 {
869  if (!kern)
870  SG_ERROR("Given kernel to add is invalid.\n");
871 
872  if (!kernel)
873  {
874  kernel= new CCombinedKernel(20, false);
875  SG_REF(kernel);
876  }
877 
879  {
880  CKernel* first_elem=kernel;
881  kernel= new CCombinedKernel(20, false);
882  SG_REF(kernel);
883  ((CCombinedKernel*) kernel)->append_kernel(first_elem);
884  }
885 
886  if (!kernel)
887  SG_ERROR("Combined kernel object could not be created.\n");
888 
889  kern->set_combined_kernel_weight(weight);
890 
891  bool success=((CCombinedKernel*) kernel)->append_kernel(kern);
892 
893  initialized=true;
894  if (success)
895  ((CCombinedKernel*) kernel)->list_kernels();
896  else
897  SG_ERROR("Adding of kernel failed.\n");
898 
899  return success;
900 }
901 
902 
904 {
905  if (!kernel)
906  SG_ERROR("No kernel available.\n");
907 
909  SG_ERROR("Need a combined kernel for deleting the last kernel in it.\n");
910 
911  CKernel* last=((CCombinedKernel*) kernel)->get_last_kernel();
912  if (last)
913  return ((CCombinedKernel*) kernel)->delete_kernel();
914  else
915  SG_ERROR("No kernel available to delete.\n");
916 
917  return false;
918 }
919 
921 {
922  SG_UNREF(kernel);
923  kernel=NULL;
924  return true;
925 }
926 
927 
928 
930 {
932  if (!kernel)
933  SG_ERROR("No kernel available.\n");
934 
935  if (strncmp(opt_type, "FASTBUTMEMHUNGRY", 16)==0)
936  {
937  SG_INFO("FAST METHOD selected\n");
938  opt=FASTBUTMEMHUNGRY;
940 
941  return true;
942  }
943  else if (strncmp(opt_type,"SLOWBUTMEMEFFICIENT", 19)==0)
944  {
945  SG_INFO("MEMORY EFFICIENT METHOD selected\n");
948 
949  return true;
950  }
951  else
952  SG_ERROR("Wrong kernel optimization type.\n");
953 
954  return false;
955 }
956 
958 {
959  if (!kernel)
960  SG_ERROR("No kernel available.\n");
961 
963  SG_ERROR("Not a combined kernel.\n");
964 
966 }

SHOGUN Machine Learning Toolbox - Documentation