SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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  return kern;
131 }
132 
133 
135  int32_t size, float64_t width, int32_t max_shift, int32_t shift_step)
136 {
137  CKernel* kern=new CGaussianShiftKernel(size, width, max_shift, shift_step);
138  if (!kern)
139  SG_ERROR("Couldn't create GaussianShiftKernel with size %d, width %f, max_shift %d, shift_step %d.\n", size, width, max_shift, shift_step);
140  else
141  SG_DEBUG("created GaussianShiftKernel (%p) with size %d, width %f, max_shift %d, shift_step %d.\n", kern, size, width, max_shift, shift_step);
142 
143  return kern;
144 }
145 
147 {
148  CKernel* kern=new CGaussianKernel(size, width);
149  if (!kern)
150  SG_ERROR("Couldn't create GaussianKernel with size %d, width %f.\n", size, width);
151  else
152  SG_DEBUG("created GaussianKernel (%p) with size %d, width %f.\n", kern, size, width);
153 
154  return kern;
155 }
156 
158 {
159  CKernel* kern=new CGaussianKernel(size, width);
160  if (!kern)
161  SG_ERROR("Couldn't create GaussianKernel with size %d, width %f.\n", size, width);
162  else
163  SG_DEBUG("created GaussianKernel (%p) with size %d, width %f.\n", kern, size, width);
164 
165  return kern;
166 }
167 
169  int32_t size, float64_t gamma, float64_t coef0)
170 {
171  CKernel* kern=new CSigmoidKernel(size, gamma, coef0);
172  if (!kern)
173  SG_ERROR("Couldn't create SigmoidKernel with size %d, gamma %f, coef0 %f.\n", size, gamma, coef0);
174  else
175  SG_DEBUG("created SigmoidKernel (%p) with size %d, gamma %f, coef0 %f.\n", kern, size, gamma, coef0);
176 
177  return kern;
178 }
180  int32_t size, float64_t Wdilation, float64_t Wtranslation)
181 {
182  CKernel* kern=new CWaveletKernel(size, Wdilation, Wtranslation);
183  if (!kern)
184  SG_ERROR("Couldn't create WaveletKernel with size %d, Wdilation %f, Wtranslation %f.\n", size, Wdilation, Wtranslation);
185  else
186  SG_DEBUG("created WaveletKernel (%p) with size %d, Wdilation %f, Wtranslation %f.\n", kern, size, Wdilation, Wtranslation);
187 
188  return kern;
189 }
191  int32_t size, int32_t degree, bool inhomogene, bool normalize)
192 {
193  CKernel* kern=new CPolyKernel(size, degree, inhomogene);
194  if (!normalize)
196  SG_DEBUG("created PolyKernel with size %d, degree %d, inhomogene %d normalize %d.\n", kern, size, degree, inhomogene, normalize);
197 
198  return kern;
199 }
200 
202  int32_t size, int32_t degree, bool inhomogene, bool normalize)
203 {
204  CKernel* kern=new CPolyKernel(size, degree, inhomogene);
205  if (!normalize)
207  SG_DEBUG("created PolyKernel (%p) with size %d, degree %d, inhomogene %d, normalize %d.\n", kern, size, degree, inhomogene, normalize);
208 
209  return kern;
210 }
211 
213  int32_t size, int32_t length, int32_t inner_degree, int32_t outer_degree,
214  EKernelType ktype)
215 {
216  CKernel* kern=NULL;
217 
218  if (ktype==K_SIMPLELOCALITYIMPROVED)
219  {
221  size, length, inner_degree, outer_degree);
222  }
223  else if (ktype==K_LOCALITYIMPROVED)
224  {
226  size, length, inner_degree, outer_degree);
227  }
228 
229  if (!kern)
230  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);
231  else
232  SG_DEBUG("created (Simple)LocalityImprovedStringKernel with size %d, length %d, inner_degree %d, outer_degree %d.\n", kern, size, length, inner_degree, outer_degree);
233 
234  return kern;
235 }
236 
238  int32_t size, int32_t order, int32_t max_mismatch, bool use_normalization,
239  int32_t mkl_stepsize, bool block_computation, int32_t single_degree)
240 {
241  float64_t* weights=get_weights(order, max_mismatch);
242 
243  int32_t i=0;
244  if (single_degree>=0)
245  {
246  ASSERT(single_degree<order);
247  for (i=0; i<order; i++)
248  {
249  if (i!=single_degree)
250  weights[i]=0;
251  else
252  weights[i]=1;
253  }
254  }
255 
256  CKernel* kern=new CWeightedDegreeStringKernel(SGVector<float64_t>(weights, order));
257 
258  SG_DEBUG("created WeightedDegreeStringKernel (%p) with size %d, order %d, "
259  "max_mismatch %d, use_normalization %d, mkl_stepsize %d, "
260  "block_computation %d, single_degree %d.\n",
261  kern, size, order, max_mismatch, (int) use_normalization, mkl_stepsize,
262  block_computation, single_degree);
263 
264  if (!use_normalization)
266 
267  ((CWeightedDegreeStringKernel*) kern)->
268  set_use_block_computation(block_computation);
269  ((CWeightedDegreeStringKernel*) kern)->set_max_mismatch(max_mismatch);
270  ((CWeightedDegreeStringKernel*) kern)->set_mkl_stepsize(mkl_stepsize);
271  ((CWeightedDegreeStringKernel*) kern)->set_which_degree(single_degree);
272 
273  return kern;
274 }
275 
277  int32_t size, int32_t order, int32_t max_mismatch, int32_t length,
278  int32_t center, float64_t step)
279 {
280  int32_t i=0;
281  int32_t* shifts=SG_MALLOC(int32_t, length);
282 
283  for (i=center; i<length; i++)
284  shifts[i]=(int32_t) floor(((float64_t) (i-center))/step);
285 
286  for (i=center-1; i>=0; i--)
287  shifts[i]=(int32_t) floor(((float64_t) (center-i))/step);
288 
289  for (i=0; i<length; i++)
290  {
291  if (shifts[i]>length)
292  shifts[i]=length;
293  }
294 
295  for (i=0; i<length; i++)
296  SG_INFO( "shift[%i]=%i\n", i, shifts[i]);
297 
298  float64_t* weights=get_weights(order, max_mismatch);
299 
300  CKernel* kern=new CWeightedDegreePositionStringKernel(size, weights, order, max_mismatch, shifts, length);
301  if (!kern)
302  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);
303  else
304  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);
305 
306  SG_FREE(weights);
307  SG_FREE(shifts);
308  return kern;
309 }
310 
312  int32_t size, int32_t order, int32_t max_mismatch, int32_t* shifts,
313  int32_t length, int32_t mkl_stepsize, float64_t* position_weights)
314 {
315  float64_t* weights=get_weights(order, max_mismatch);
316 
317  CKernel* kern=new CWeightedDegreePositionStringKernel(size, weights, order, max_mismatch, shifts, length, mkl_stepsize);
319 
320  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);
321 
322  if (!position_weights)
323  {
324  position_weights=SG_MALLOC(float64_t, length);
325  for (int32_t i=0; i<length; i++)
326  position_weights[i]=1.0/length;
327  }
329  set_position_weights(SGVector<float64_t>(position_weights, length));
330 
331  SG_FREE(weights);
332  return kern;
333 }
334 
336  int32_t size, int32_t order, int32_t max_mismatch, int32_t* shifts,
337  int32_t length, bool use_normalization)
338 {
339  float64_t* weights=get_weights(order, max_mismatch);
340 
341  CKernel* kern=new CWeightedDegreePositionStringKernel(size, weights, order, max_mismatch, shifts, length);
342  if (!use_normalization)
344 
345 
346  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);
347 
348  SG_FREE(weights);
349  return kern;
350 }
351 
352 float64_t* CGUIKernel::get_weights(int32_t order, int32_t max_mismatch)
353 {
354  float64_t *weights=SG_MALLOC(float64_t, order*(1+max_mismatch));
355  float64_t sum=0;
356  int32_t i=0;
357 
358  for (i=0; i<order; i++)
359  {
360  weights[i]=order-i;
361  sum+=weights[i];
362  }
363  for (i=0; i<order; i++)
364  weights[i]/=sum;
365 
366  for (i=0; i<order; i++)
367  {
368  for (int32_t j=1; j<=max_mismatch; j++)
369  {
370  if (j<i+1)
371  {
372  int32_t nk=CMath::nchoosek(i+1, j);
373  weights[i+j*order]=weights[i]/(nk*CMath::pow(3, j));
374  }
375  else
376  weights[i+j*order]=0;
377  }
378  }
379 
380  return weights;
381 }
382 
383 CKernel* CGUIKernel::create_weighteddegreerbf(int32_t size, int32_t degree, int32_t nof_properties, float64_t width)
384 {
385  CKernel* kern=new CWeightedDegreeRBFKernel(size, width, degree, nof_properties);
386  if (!kern)
387  SG_ERROR("Couldn't create WeightedDegreeRBFKernel with size %d, width %f, degree %d, nof_properties %d.\n", size, width, degree, nof_properties);
388  else
389  SG_DEBUG("created WeightedDegreeRBFKernel (%p) with size %d, width %f, degree %d, nof_properties %d.\n", kern, size, width, degree, nof_properties);
390 
391  return kern;
392 }
393 
394 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)
395 {
396 
397  CKernel* kern = new CSpectrumMismatchRBFKernel(size, AA_matrix, nr, nc, degree, max_mismatch, width);
398  if (!kern)
399  SG_ERROR("Couldn't create SpectrumMismatchRBFKernel with size %d, width %f, degree %d, max_mismatch %d.\n", size, width, degree, max_mismatch);
400  else
401  SG_DEBUG("created SpectrumMismatchRBFKernel (%p) with size %d, width %f, degree %d, max_mismatch %d.\n", kern, size, width, degree, max_mismatch);
402 
403  return kern;
404 
405 }
406 
407 
409 {
410  CKernel* kern=new CLocalAlignmentStringKernel(size);
411  if (!kern)
412  SG_ERROR("Couldn't create LocalAlignmentStringKernel with size %d.\n", size);
413  else
414  SG_DEBUG("created LocalAlignmentStringKernel (%p) with size %d.\n", kern, size);
415 
416  return kern;
417 }
418 
420 {
421  CKernel* kern=new CFixedDegreeStringKernel(size, d);
422  if (!kern)
423  SG_ERROR("Couldn't create FixedDegreeStringKernel with size %d and d %d.\n", size, d);
424  else
425  SG_DEBUG("created FixedDegreeStringKernel (%p) with size %d and d %d.\n", kern, size, d);
426 
427  return kern;
428 }
429 
431 {
432  CKernel* kern=new CChi2Kernel(size, width);
433  if (!kern)
434  SG_ERROR("Couldn't create Chi2Kernel with size %d and width %f.\n", size, width);
435  else
436  SG_DEBUG("created Chi2Kernel (%p) with size %d and width %f.\n", kern, size, width);
437 
438  return kern;
439 }
440 
442  int32_t size, bool use_sign, char* norm_str, EKernelType ktype)
443 {
444  CKernel* kern=NULL;
445 
446  if (!norm_str)
447  norm_str= (char*) "FULL";
448 
449  if (ktype==K_COMMULONGSTRING)
450  kern=new CCommUlongStringKernel(size, use_sign);
451  else if (ktype==K_COMMWORDSTRING)
452  kern=new CCommWordStringKernel(size, use_sign);
453  else if (ktype==K_WEIGHTEDCOMMWORDSTRING)
454  kern=new CWeightedCommWordStringKernel(size, use_sign);
455 
456  SG_DEBUG("created WeightedCommWord/CommWord/CommUlongStringKernel (%p) with size %d, use_sign %d norm_str %s.\n", kern, size, use_sign, norm_str);
457 
458 
459  if (strncmp(norm_str, "NO", 2)==0)
460  {
462  }
463  else if (strncmp(norm_str, "FULL", 4)==0)
464  {
465  //nop, as this one is default
466  }
467  else
468  SG_ERROR("Unsupported Normalizer requested, supports only FULL and NO\n");
469 
470  return kern;
471 }
472 
474  int32_t size, int32_t d, bool normalize)
475 {
476  CKernel* kern=new CMatchWordStringKernel(size, d);
477  SG_DEBUG("created MatchWordStringKernel (%p) with size %d and d %d.\n", kern, size, d);
478  if (!normalize)
480 
481  return kern;
482 }
483 
485  int32_t size, int32_t degree, bool inhomogene, bool normalize)
486 {
487  CKernel* kern=new CPolyMatchStringKernel(size, degree, inhomogene);
488  SG_DEBUG("created PolyMatchStringKernel (%p) with size %d, degree %d, inhomogene %d normalize %d.\n", kern, size, degree, inhomogene, normalize);
489  if (!normalize)
491 
492  return kern;
493 }
494 
496  int32_t size, int32_t degree, bool inhomogene, bool normalize)
497 {
498  CKernel* kern=new CPolyMatchWordStringKernel(size, degree, inhomogene);
499  SG_DEBUG("created PolyMatchWordStringKernel (%p) with size %d, degree %d, inhomogene %d, normalize %d.\n", kern, size, degree, inhomogene, normalize);
500  if (!normalize)
502 
503  return kern;
504 }
505 
507 {
508  SG_INFO("Getting estimator.\n");
509  CPluginEstimate* estimator=ui->ui_pluginestimate->get_estimator();
510  if (!estimator)
511  SG_ERROR("No estimator set.\n");
512 
513  CKernel* kern=new CSalzbergWordStringKernel(size, estimator);
514  if (!kern)
515  SG_ERROR("Couldn't create SalzbergWordString with size %d.\n", size);
516  else
517  SG_DEBUG("created SalzbergWordString (%p) with size %d.\n", kern, size);
518 
519 /*
520  // prior stuff
521  SG_INFO("Getting labels.\n");
522  CLabels* train_labels=ui->ui_labels->get_train_labels();
523  if (!train_labels)
524  {
525  SG_INFO("Assign train labels first!\n");
526  return NULL;
527  }
528  ((CSalzbergWordStringKernel *) kern)->set_prior_probs_from_labels(train_labels);
529 */
530 
531  return kern;
532 }
533 
535 {
536  SG_INFO("Getting estimator.\n");
537  CPluginEstimate* estimator=ui->ui_pluginestimate->get_estimator();
538  if (!estimator)
539  SG_ERROR("No estimator set.\n");
540 
541  CKernel* kern=new CHistogramWordStringKernel(size, estimator);
542  if (!kern)
543  SG_ERROR("Couldn't create HistogramWordString with size %d.\n", size);
544  else
545  SG_DEBUG("created HistogramWordString (%p) with size %d.\n", kern, size);
546 
547  return kern;
548 }
549 
551 {
552  size=0;
553  CKernel* kern=new CLinearKernel();
554  kern->set_normalizer(new CAvgDiagKernelNormalizer(scale));
555  SG_DEBUG("created LinearByteKernel (%p) with size %d and scale %f.\n", kern, size, scale);
556 
557  return kern;
558 }
559 
561 {
562  size=0;
563  CKernel* kern=new CLinearKernel();
564  kern->set_normalizer(new CAvgDiagKernelNormalizer(scale));
565  SG_DEBUG("created LinearWordKernel (%p) with size %d and scale %f.\n", kern, size, scale);
566 
567  return kern;
568 }
569 
571 {
572  size=0;
573  CKernel* kern=NULL;
574  kern=new CLinearStringKernel();
575  kern->set_normalizer(new CAvgDiagKernelNormalizer(scale));
576 
577  SG_DEBUG("created LinearStringKernel (%p) with size %d and scale %f.\n", kern, size, scale);
578 
579  return kern;
580 }
581 
583 {
584  size=0;
585  CKernel* kern=new CLinearKernel();
586  kern->set_normalizer(new CAvgDiagKernelNormalizer(scale));
587 
588  SG_DEBUG("created LinearKernel (%p) with size %d and scale %f.\n", kern, size, scale);
589 
590  return kern;
591 }
592 
594 {
595  size=0;
596  CKernel* kern=new CLinearKernel();
597  kern->set_normalizer(new CAvgDiagKernelNormalizer(scale));
598 
599  SG_DEBUG("created LinearKernel (%p) with size %d and scale %f.\n", kern, size, scale);
600 
601  return kern;
602 }
603 
604 CKernel* CGUIKernel::create_tppk(int32_t size, float64_t* km, int32_t rows, int32_t cols)
605 {
606  CCustomKernel* k=new CCustomKernel();
608 
609  CKernel* kern=new CTensorProductPairKernel(size, k);
610 
611  SG_DEBUG("created TPPK (%p) with size %d and km %p, rows %d, cols %d.\n", kern, size, km, rows, cols);
612 
613  return kern;
614 }
615 
617 {
618  CDistance* dist=ui->ui_distance->get_distance();
619  if (!dist)
620  SG_ERROR("No distance set for DistanceKernel.\n");
621 
622  CKernel* kern=new CDistanceKernel(size, width, dist);
623  if (!kern)
624  SG_ERROR("Couldn't create DistanceKernel with size %d and width %f.\n", size, width);
625  else
626  SG_DEBUG("created DistanceKernel (%p) with size %d and width %f.\n", kern, size, width);
627 
628  return kern;
629 }
630 
632  int32_t size, bool append_subkernel_weights)
633 {
634  CKernel* kern=new CCombinedKernel(size, append_subkernel_weights);
635  if (!kern)
636  SG_ERROR("Couldn't create CombinedKernel with size %d and append_subkernel_weights %d.\n", size, append_subkernel_weights);
637  else
638  SG_DEBUG("created CombinedKernel (%p) with size %d and append_subkernel_weights %d.\n", kern, size, append_subkernel_weights);
639 
640  return kern;
641 }
642 
643 bool CGUIKernel::set_normalization(char* normalization, float64_t c, float64_t r)
644 {
645  CKernel* k=kernel;
646 
647  if (k && k->get_kernel_type()==K_COMBINED)
648  k=((CCombinedKernel*) kernel)->get_last_kernel();
649 
650  if (!k)
651  SG_ERROR("No kernel available.\n");
652 
653  if (strncmp(normalization, "IDENTITY", 8)==0)
654  {
655  SG_INFO("Identity Normalization (==NO NORMALIZATION) selected\n");
656  return k->set_normalizer(new CIdentityKernelNormalizer());
657  }
658  else if (strncmp(normalization,"AVGDIAG", 7)==0)
659  {
660  SG_INFO("Average Kernel Diagonal Normalization selected\n");
661  return k->set_normalizer(new CAvgDiagKernelNormalizer(c));
662  }
663  else if (strncmp(normalization,"RIDGE", 5)==0)
664  {
665  SG_INFO("Ridge Kernel Normalization selected\n");
666  return k->set_normalizer(new CRidgeKernelNormalizer(r, c));
667  }
668  else if (strncmp(normalization,"SQRTDIAG", 8)==0)
669  {
670  SG_INFO("Sqrt Diagonal Normalization selected\n");
671  return k->set_normalizer(new CSqrtDiagKernelNormalizer());
672  }
673  else if (strncmp(normalization,"FIRSTELEMENT", 12)==0)
674  {
675  SG_INFO("First Element Normalization selected\n");
677  }
678  else if (strncmp(normalization,"VARIANCE", 8)==0)
679  {
680  SG_INFO("Variance Normalization selected\n");
681  return k->set_normalizer(new CVarianceKernelNormalizer());
682  }
683  else if (strncmp(normalization,"SCATTER", 7)==0)
684  {
685  SG_INFO("Scatter Normalization selected\n");
686  CLabels* train_labels=ui->ui_labels->get_train_labels();
687  ASSERT(train_labels);
688  return k->set_normalizer(new CScatterKernelNormalizer(c,r, train_labels));
689  }
690  else if (strncmp(normalization,"ZEROMEANCENTER", 13)==0)
691  {
692  SG_INFO("Zero Mean Center Normalization selected\n");
694  }
695  else
696  SG_ERROR("Wrong kernel normalizer name.\n");
697 
698  SG_UNREF(k);
699 
700  return false;
701 }
702 
704 {
705  if (kern)
706  {
707  SG_DEBUG("deleting old kernel (%p).\n", kernel);
708  SG_UNREF(kernel);
709  SG_REF(kern);
710  kernel=kern;
711  SG_DEBUG("set new kernel (%p).\n", kern);
712 
713  return true;
714  }
715  else
716  return false;
717 }
718 
720 {
721  CSVM* svm=(CSVM*) ui->ui_classifier->get_classifier();
722  if (svm)
723  {
725  {
726  int32_t num_sv=svm->get_num_support_vectors();
727  int32_t* sv_idx=SG_MALLOC(int32_t, num_sv);
728  float64_t* sv_weight=SG_MALLOC(float64_t, num_sv);
729 
730  for (int32_t i=0; i<num_sv; i++)
731  {
732  sv_idx[i]=svm->get_support_vector(i);
733  sv_weight[i]=svm->get_alpha(i);
734  }
735 
736  bool ret=kernel->init_optimization(num_sv, sv_idx, sv_weight);
737 
738  SG_FREE(sv_idx);
739  SG_FREE(sv_weight);
740 
741  if (!ret)
742  SG_ERROR("Initialization of kernel optimization failed\n");
743  return ret;
744  }
745  }
746  else
747  SG_ERROR("Create SVM first!\n");
748 
749  return true;
750 }
751 
753 {
756 
757  return true;
758 }
759 
760 
761 bool CGUIKernel::init_kernel(const char* target)
762 {
763  if (!kernel)
764  SG_ERROR("No kernel available.\n");
765 
766  // no need to init custom kernel
767  if (kernel->get_kernel_type() == K_CUSTOM || !target)
768  {
769  initialized=true;
770  return true;
771  }
772 
775 
776  if (!strncmp(target, "TRAIN", 5))
777  {
778  CFeatures* train=ui->ui_features->get_train_features();
779 
780  if (train)
781  {
782  EFeatureClass fclass=train->get_feature_class();
783  EFeatureType ftype=train->get_feature_type();
784  if ((k_fclass==fclass || k_fclass==C_ANY || fclass==C_ANY) &&
785  (k_ftype==ftype || k_ftype==F_ANY || ftype==F_ANY))
786 
787  {
788  SG_INFO("Initialising kernel with TRAIN DATA, train: %p\n", train);
789  kernel->init(train, train);
790  initialized=true;
791  }
792  else
793  SG_ERROR("Kernel can not process this train feature type: %d %d.\n", fclass, ftype);
794  }
795  else
796  SG_DEBUG("Not initing kernel - no train features assigned.\n");
797  }
798  else if (!strncmp(target, "TEST", 4))
799  {
800  CFeatures* train=ui->ui_features->get_train_features();
801  CFeatures* test=ui->ui_features->get_test_features();
802  if (train && test)
803  {
804  EFeatureClass fclass=test->get_feature_class();
805  EFeatureType ftype=test->get_feature_type();
806  if ((k_fclass==fclass || k_fclass==C_ANY || fclass==C_ANY) &&
807  (k_ftype==ftype || k_ftype==F_ANY || ftype==F_ANY))
808 
809  {
810  if (!initialized)
811  {
812  EFeatureClass tr_fclass=train->get_feature_class();
813  EFeatureType tr_ftype=train->get_feature_type();
814  if ((k_fclass==tr_fclass || k_fclass==C_ANY || tr_fclass==C_ANY) &&
815  (k_ftype==tr_ftype || k_ftype==F_ANY || tr_ftype==F_ANY))
816  {
817  SG_INFO("Initialising kernel with TRAIN DATA, train: %p\n", train);
818  kernel->init(train, train);
819  initialized=true;
820  }
821  else
822  SG_ERROR("Kernel can not process this train feature type: %d %d.\n", fclass, ftype);
823  }
824 
825  SG_INFO("Initialising kernel with TEST DATA, train: %p test %p\n", train, test);
826  // lhs -> always train_features; rhs -> always test_features
827  kernel->init(train, test);
828  }
829  else
830  SG_ERROR("Kernel can not process this test feature type: %d %d.\n", fclass, ftype);
831  }
832  else
833  SG_DEBUG("Not initing kernel - no train and test features assigned.\n");
834  }
835  else
836  SG_ERROR("Unknown target %s.\n", target);
837 
838  return true;
839 }
840 
841 bool CGUIKernel::save_kernel(char* filename)
842 {
843  if (kernel && initialized)
844  {
845  CAsciiFile* file=new CAsciiFile(filename);
846  try
847  {
848  kernel->save(file);
849  }
850  catch (...)
851  {
852  SG_ERROR("Writing to file %s failed!\n", filename);
853  }
854 
855  SG_UNREF(file);
856  SG_INFO("Successfully written kernel to \"%s\" !\n", filename);
857  return true;
858  }
859  else
860  SG_ERROR("No kernel set / kernel not initialized!\n");
861 
862  return false;
863 }
864 
866 {
867  if (!kern)
868  SG_ERROR("Given kernel to add is invalid.\n");
869 
870  if (!kernel)
871  {
872  kernel= new CCombinedKernel(20, false);
873  SG_REF(kernel);
874  }
875 
877  {
878  CKernel* first_elem=kernel;
879  kernel= new CCombinedKernel(20, false);
880  SG_REF(kernel);
881  ((CCombinedKernel*) kernel)->append_kernel(first_elem);
882  }
883 
884  if (!kernel)
885  SG_ERROR("Combined kernel object could not be created.\n");
886 
887  kern->set_combined_kernel_weight(weight);
888 
889  bool success=((CCombinedKernel*) kernel)->append_kernel(kern);
890 
891  initialized=true;
892  if (success)
893  ((CCombinedKernel*) kernel)->list_kernels();
894  else
895  SG_ERROR("Adding of kernel failed.\n");
896 
897  return success;
898 }
899 
900 
902 {
903  if (!kernel)
904  SG_ERROR("No kernel available.\n");
905 
907  SG_ERROR("Need a combined kernel for deleting the last kernel in it.\n");
908 
909  CKernel* last=((CCombinedKernel*) kernel)->get_last_kernel();
910  if (last)
911  return ((CCombinedKernel*) kernel)->delete_kernel();
912  else
913  SG_ERROR("No kernel available to delete.\n");
914 
915  return false;
916 }
917 
919 {
920  SG_UNREF(kernel);
921  kernel=NULL;
922  return true;
923 }
924 
925 #ifdef USE_SVMLIGHT
927 {
928  if (!kernel)
929  SG_ERROR("No kernel available.\n");
930 
932  return true;
933 }
934 #endif //USE_SVMLIGHT
935 
937 {
939  if (!kernel)
940  SG_ERROR("No kernel available.\n");
941 
942  if (strncmp(opt_type, "FASTBUTMEMHUNGRY", 16)==0)
943  {
944  SG_INFO("FAST METHOD selected\n");
945  opt=FASTBUTMEMHUNGRY;
947 
948  return true;
949  }
950  else if (strncmp(opt_type,"SLOWBUTMEMEFFICIENT", 19)==0)
951  {
952  SG_INFO("MEMORY EFFICIENT METHOD selected\n");
955 
956  return true;
957  }
958  else
959  SG_ERROR("Wrong kernel optimization type.\n");
960 
961  return false;
962 }
963 
965 {
966  if (!kernel)
967  SG_ERROR("No kernel available.\n");
968 
970  SG_ERROR("Not a combined kernel.\n");
971 
973 }

SHOGUN Machine Learning Toolbox - Documentation