SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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/CSVFile.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, SGVector<float64_t>(weights, order*(1+max_mismatch)), order, max_mismatch, SGVector<int32_t>(shifts, length).clone());
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  return kern;
307 }
308 
310  int32_t size, int32_t order, int32_t max_mismatch, int32_t* shifts,
311  int32_t length, int32_t mkl_stepsize, float64_t* position_weights)
312 {
313  float64_t* weights=get_weights(order, max_mismatch);
314 
315  CKernel* kern=new CWeightedDegreePositionStringKernel(size, SGVector<float64_t>(weights, order*(1+max_mismatch)), order, max_mismatch, SGVector<int32_t>(shifts, length, false).clone(), mkl_stepsize);
317 
318  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)
319 
320  if (!position_weights)
321  {
322  position_weights=SG_MALLOC(float64_t, length);
323  for (int32_t i=0; i<length; i++)
324  position_weights[i]=1.0/length;
325  }
327  set_position_weights(SGVector<float64_t>(position_weights, length));
328 
329  return kern;
330 }
331 
333  int32_t size, int32_t order, int32_t max_mismatch, int32_t* shifts,
334  int32_t length, bool use_normalization)
335 {
336  float64_t* weights=get_weights(order, max_mismatch);
337 
338  CKernel* kern=new CWeightedDegreePositionStringKernel(size, SGVector<float64_t>(weights, order*(1+max_mismatch)), order, max_mismatch, SGVector<int32_t>(shifts, length, false).clone());
339  if (!use_normalization)
341 
342 
343  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)
344 
345  return kern;
346 }
347 
348 float64_t* CGUIKernel::get_weights(int32_t order, int32_t max_mismatch)
349 {
350  float64_t *weights=SG_MALLOC(float64_t, order*(1+max_mismatch));
351  float64_t sum=0;
352  int32_t i=0;
353 
354  for (i=0; i<order; i++)
355  {
356  weights[i]=order-i;
357  sum+=weights[i];
358  }
359  for (i=0; i<order; i++)
360  weights[i]/=sum;
361 
362  for (i=0; i<order; i++)
363  {
364  for (int32_t j=1; j<=max_mismatch; j++)
365  {
366  if (j<i+1)
367  {
368  int32_t nk=CMath::nchoosek(i+1, j);
369  weights[i+j*order]=weights[i]/(nk*CMath::pow(3, j));
370  }
371  else
372  weights[i+j*order]=0;
373  }
374  }
375 
376  return weights;
377 }
378 
379 CKernel* CGUIKernel::create_weighteddegreerbf(int32_t size, int32_t degree, int32_t nof_properties, float64_t width)
380 {
381  CKernel* kern=new CWeightedDegreeRBFKernel(size, width, degree, nof_properties);
382  if (!kern)
383  SG_ERROR("Couldn't create WeightedDegreeRBFKernel with size %d, width %f, degree %d, nof_properties %d.\n", size, width, degree, nof_properties)
384  else
385  SG_DEBUG("created WeightedDegreeRBFKernel (%p) with size %d, width %f, degree %d, nof_properties %d.\n", kern, size, width, degree, nof_properties)
386 
387  return kern;
388 }
389 
390 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)
391 {
392 
393  CKernel* kern = new CSpectrumMismatchRBFKernel(size, AA_matrix, nr, nc, degree, max_mismatch, width);
394  if (!kern)
395  SG_ERROR("Couldn't create SpectrumMismatchRBFKernel with size %d, width %f, degree %d, max_mismatch %d.\n", size, width, degree, max_mismatch)
396  else
397  SG_DEBUG("created SpectrumMismatchRBFKernel (%p) with size %d, width %f, degree %d, max_mismatch %d.\n", kern, size, width, degree, max_mismatch)
398 
399  return kern;
400 
401 }
402 
403 
405 {
406  CKernel* kern=new CLocalAlignmentStringKernel(size);
407  if (!kern)
408  SG_ERROR("Couldn't create LocalAlignmentStringKernel with size %d.\n", size)
409  else
410  SG_DEBUG("created LocalAlignmentStringKernel (%p) with size %d.\n", kern, size)
411 
412  return kern;
413 }
414 
416 {
417  CKernel* kern=new CFixedDegreeStringKernel(size, d);
418  if (!kern)
419  SG_ERROR("Couldn't create FixedDegreeStringKernel with size %d and d %d.\n", size, d)
420  else
421  SG_DEBUG("created FixedDegreeStringKernel (%p) with size %d and d %d.\n", kern, size, d)
422 
423  return kern;
424 }
425 
427 {
428  CKernel* kern=new CChi2Kernel(size, width);
429  if (!kern)
430  SG_ERROR("Couldn't create Chi2Kernel with size %d and width %f.\n", size, width)
431  else
432  SG_DEBUG("created Chi2Kernel (%p) with size %d and width %f.\n", kern, size, width)
433 
434  return kern;
435 }
436 
438  int32_t size, bool use_sign, char* norm_str, EKernelType ktype)
439 {
440  CKernel* kern=NULL;
441 
442  if (!norm_str)
443  norm_str= (char*) "FULL";
444 
445  if (ktype==K_COMMULONGSTRING)
446  kern=new CCommUlongStringKernel(size, use_sign);
447  else if (ktype==K_COMMWORDSTRING)
448  kern=new CCommWordStringKernel(size, use_sign);
449  else if (ktype==K_WEIGHTEDCOMMWORDSTRING)
450  kern=new CWeightedCommWordStringKernel(size, use_sign);
451 
452  SG_DEBUG("created WeightedCommWord/CommWord/CommUlongStringKernel (%p) with size %d, use_sign %d norm_str %s.\n", kern, size, use_sign, norm_str)
453 
454 
455  if (strncmp(norm_str, "NO", 2)==0)
456  {
458  }
459  else if (strncmp(norm_str, "FULL", 4)==0)
460  {
461  //nop, as this one is default
462  }
463  else
464  SG_ERROR("Unsupported Normalizer requested, supports only FULL and NO\n")
465 
466  return kern;
467 }
468 
470  int32_t size, int32_t d, bool normalize)
471 {
472  CKernel* kern=new CMatchWordStringKernel(size, d);
473  SG_DEBUG("created MatchWordStringKernel (%p) with size %d and d %d.\n", kern, size, d)
474  if (!normalize)
476 
477  return kern;
478 }
479 
481  int32_t size, int32_t degree, bool inhomogene, bool normalize)
482 {
483  CKernel* kern=new CPolyMatchStringKernel(size, degree, inhomogene);
484  SG_DEBUG("created PolyMatchStringKernel (%p) with size %d, degree %d, inhomogene %d normalize %d.\n", kern, size, degree, inhomogene, normalize)
485  if (!normalize)
487 
488  return kern;
489 }
490 
492  int32_t size, int32_t degree, bool inhomogene, bool normalize)
493 {
494  CKernel* kern=new CPolyMatchWordStringKernel(size, degree, inhomogene);
495  SG_DEBUG("created PolyMatchWordStringKernel (%p) with size %d, degree %d, inhomogene %d, normalize %d.\n", kern, size, degree, inhomogene, normalize)
496  if (!normalize)
498 
499  return kern;
500 }
501 
503 {
504  SG_INFO("Getting estimator.\n")
505  CPluginEstimate* estimator=ui->ui_pluginestimate->get_estimator();
506  if (!estimator)
507  SG_ERROR("No estimator set.\n")
508 
509  CKernel* kern=new CSalzbergWordStringKernel(size, estimator);
510  if (!kern)
511  SG_ERROR("Couldn't create SalzbergWordString with size %d.\n", size)
512  else
513  SG_DEBUG("created SalzbergWordString (%p) with size %d.\n", kern, size)
514 
515 /*
516  // prior stuff
517  SG_INFO("Getting labels.\n")
518  CLabels* train_labels=ui->ui_labels->get_train_labels();
519  if (!train_labels)
520  {
521  SG_INFO("Assign train labels first!\n")
522  return NULL;
523  }
524  ((CSalzbergWordStringKernel *) kern)->set_prior_probs_from_labels(train_labels);
525 */
526 
527  return kern;
528 }
529 
531 {
532  SG_INFO("Getting estimator.\n")
533  CPluginEstimate* estimator=ui->ui_pluginestimate->get_estimator();
534  if (!estimator)
535  SG_ERROR("No estimator set.\n")
536 
537  CKernel* kern=new CHistogramWordStringKernel(size, estimator);
538  if (!kern)
539  SG_ERROR("Couldn't create HistogramWordString with size %d.\n", size)
540  else
541  SG_DEBUG("created HistogramWordString (%p) with size %d.\n", kern, size)
542 
543  return kern;
544 }
545 
547 {
548  size=0;
549  CKernel* kern=new CLinearKernel();
550  kern->set_normalizer(new CAvgDiagKernelNormalizer(scale));
551  SG_DEBUG("created LinearByteKernel (%p) with size %d and scale %f.\n", kern, size, scale)
552 
553  return kern;
554 }
555 
557 {
558  size=0;
559  CKernel* kern=new CLinearKernel();
560  kern->set_normalizer(new CAvgDiagKernelNormalizer(scale));
561  SG_DEBUG("created LinearWordKernel (%p) with size %d and scale %f.\n", kern, size, scale)
562 
563  return kern;
564 }
565 
567 {
568  size=0;
569  CKernel* kern=NULL;
570  kern=new CLinearStringKernel();
571  kern->set_normalizer(new CAvgDiagKernelNormalizer(scale));
572 
573  SG_DEBUG("created LinearStringKernel (%p) with size %d and scale %f.\n", kern, size, scale)
574 
575  return kern;
576 }
577 
579 {
580  size=0;
581  CKernel* kern=new CLinearKernel();
582  kern->set_normalizer(new CAvgDiagKernelNormalizer(scale));
583 
584  SG_DEBUG("created LinearKernel (%p) with size %d and scale %f.\n", kern, size, scale)
585 
586  return kern;
587 }
588 
590 {
591  size=0;
592  CKernel* kern=new CLinearKernel();
593  kern->set_normalizer(new CAvgDiagKernelNormalizer(scale));
594 
595  SG_DEBUG("created LinearKernel (%p) with size %d and scale %f.\n", kern, size, scale)
596 
597  return kern;
598 }
599 
600 CKernel* CGUIKernel::create_tppk(int32_t size, float64_t* km, int32_t rows, int32_t cols)
601 {
602  CCustomKernel* k=new CCustomKernel();
604 
605  CKernel* kern=new CTensorProductPairKernel(size, k);
606 
607  SG_DEBUG("created TPPK (%p) with size %d and km %p, rows %d, cols %d.\n", kern, size, km, rows, cols)
608 
609  return kern;
610 }
611 
613 {
614  CDistance* dist=ui->ui_distance->get_distance();
615  if (!dist)
616  SG_ERROR("No distance set for DistanceKernel.\n")
617 
618  CKernel* kern=new CDistanceKernel(size, width, dist);
619  if (!kern)
620  SG_ERROR("Couldn't create DistanceKernel with size %d and width %f.\n", size, width)
621  else
622  SG_DEBUG("created DistanceKernel (%p) with size %d and width %f.\n", kern, size, width)
623 
624  return kern;
625 }
626 
628  int32_t size, bool append_subkernel_weights)
629 {
630  CKernel* kern=new CCombinedKernel(size, append_subkernel_weights);
631  if (!kern)
632  SG_ERROR("Couldn't create CombinedKernel with size %d and append_subkernel_weights %d.\n", size, append_subkernel_weights)
633  else
634  SG_DEBUG("created CombinedKernel (%p) with size %d and append_subkernel_weights %d.\n", kern, size, append_subkernel_weights)
635 
636  return kern;
637 }
638 
639 bool CGUIKernel::set_normalization(char* normalization, float64_t c, float64_t r)
640 {
641  CKernel* k=kernel;
642 
643  if (k && k->get_kernel_type()==K_COMBINED)
644  k=((CCombinedKernel*) kernel)->get_last_kernel();
645 
646  if (!k)
647  SG_ERROR("No kernel available.\n")
648 
649  if (strncmp(normalization, "IDENTITY", 8)==0)
650  {
651  SG_INFO("Identity Normalization (==NO NORMALIZATION) selected\n")
652  return k->set_normalizer(new CIdentityKernelNormalizer());
653  }
654  else if (strncmp(normalization,"AVGDIAG", 7)==0)
655  {
656  SG_INFO("Average Kernel Diagonal Normalization selected\n")
657  return k->set_normalizer(new CAvgDiagKernelNormalizer(c));
658  }
659  else if (strncmp(normalization,"RIDGE", 5)==0)
660  {
661  SG_INFO("Ridge Kernel Normalization selected\n")
662  return k->set_normalizer(new CRidgeKernelNormalizer(r, c));
663  }
664  else if (strncmp(normalization,"SQRTDIAG", 8)==0)
665  {
666  SG_INFO("Sqrt Diagonal Normalization selected\n")
667  return k->set_normalizer(new CSqrtDiagKernelNormalizer());
668  }
669  else if (strncmp(normalization,"FIRSTELEMENT", 12)==0)
670  {
671  SG_INFO("First Element Normalization selected\n")
673  }
674  else if (strncmp(normalization,"VARIANCE", 8)==0)
675  {
676  SG_INFO("Variance Normalization selected\n")
677  return k->set_normalizer(new CVarianceKernelNormalizer());
678  }
679  else if (strncmp(normalization,"SCATTER", 7)==0)
680  {
681  SG_INFO("Scatter Normalization selected\n")
682  CLabels* train_labels=ui->ui_labels->get_train_labels();
683  ASSERT(train_labels)
684  return k->set_normalizer(new CScatterKernelNormalizer(c,r, train_labels));
685  }
686  else if (strncmp(normalization,"ZEROMEANCENTER", 13)==0)
687  {
688  SG_INFO("Zero Mean Center Normalization selected\n")
690  }
691  else
692  SG_ERROR("Wrong kernel normalizer name.\n")
693 
694  SG_UNREF(k);
695 
696  return false;
697 }
698 
700 {
701  if (kern)
702  {
703  SG_DEBUG("deleting old kernel (%p).\n", kernel)
704  SG_REF(kern);
705  SG_UNREF(kernel);
706  kernel=kern;
707  SG_DEBUG("set new kernel (%p).\n", kern)
708 
709  return true;
710  }
711  else
712  return false;
713 }
714 
716 {
717  CSVM* svm=(CSVM*) ui->ui_classifier->get_classifier();
718  if (svm)
719  {
721  {
722  int32_t num_sv=svm->get_num_support_vectors();
723  int32_t* sv_idx=SG_MALLOC(int32_t, num_sv);
724  float64_t* sv_weight=SG_MALLOC(float64_t, num_sv);
725 
726  for (int32_t i=0; i<num_sv; i++)
727  {
728  sv_idx[i]=svm->get_support_vector(i);
729  sv_weight[i]=svm->get_alpha(i);
730  }
731 
732  bool ret=kernel->init_optimization(num_sv, sv_idx, sv_weight);
733 
734  SG_FREE(sv_idx);
735  SG_FREE(sv_weight);
736 
737  if (!ret)
738  SG_ERROR("Initialization of kernel optimization failed\n")
739  return ret;
740  }
741  }
742  else
743  SG_ERROR("Create SVM first!\n")
744 
745  return true;
746 }
747 
749 {
752 
753  return true;
754 }
755 
756 
757 bool CGUIKernel::init_kernel(const char* target)
758 {
759  if (!kernel)
760  SG_ERROR("No kernel available.\n")
761 
762  // no need to init custom kernel
763  if (kernel->get_kernel_type() == K_CUSTOM || !target)
764  {
765  initialized=true;
766  return true;
767  }
768 
771 
772  if (!strncmp(target, "TRAIN", 5))
773  {
774  CFeatures* train=ui->ui_features->get_train_features();
775 
776  if (train)
777  {
778  EFeatureClass fclass=train->get_feature_class();
779  EFeatureType ftype=train->get_feature_type();
780  if ((k_fclass==fclass || k_fclass==C_ANY || fclass==C_ANY) &&
781  (k_ftype==ftype || k_ftype==F_ANY || ftype==F_ANY))
782 
783  {
784  SG_INFO("Initialising kernel with TRAIN DATA, train: %p\n", train)
785  kernel->init(train, train);
786  initialized=true;
787  }
788  else
789  SG_ERROR("Kernel can not process this train feature type: %d %d.\n", fclass, ftype)
790  }
791  else
792  SG_DEBUG("Not initing kernel - no train features assigned.\n")
793  }
794  else if (!strncmp(target, "TEST", 4))
795  {
796  CFeatures* train=ui->ui_features->get_train_features();
797  CFeatures* test=ui->ui_features->get_test_features();
798  if (train && test)
799  {
800  EFeatureClass fclass=test->get_feature_class();
801  EFeatureType ftype=test->get_feature_type();
802  if ((k_fclass==fclass || k_fclass==C_ANY || fclass==C_ANY) &&
803  (k_ftype==ftype || k_ftype==F_ANY || ftype==F_ANY))
804 
805  {
806  if (!initialized)
807  {
808  EFeatureClass tr_fclass=train->get_feature_class();
809  EFeatureType tr_ftype=train->get_feature_type();
810  if ((k_fclass==tr_fclass || k_fclass==C_ANY || tr_fclass==C_ANY) &&
811  (k_ftype==tr_ftype || k_ftype==F_ANY || tr_ftype==F_ANY))
812  {
813  SG_INFO("Initialising kernel with TRAIN DATA, train: %p\n", train)
814  kernel->init(train, train);
815  initialized=true;
816  }
817  else
818  SG_ERROR("Kernel can not process this train feature type: %d %d.\n", fclass, ftype)
819  }
820 
821  SG_INFO("Initialising kernel with TEST DATA, train: %p test %p\n", train, test)
822  // lhs -> always train_features; rhs -> always test_features
823  kernel->init(train, test);
824  }
825  else
826  SG_ERROR("Kernel can not process this test feature type: %d %d.\n", fclass, ftype)
827  }
828  else
829  SG_DEBUG("Not initing kernel - no train and test features assigned.\n")
830  }
831  else
832  SG_ERROR("Unknown target %s.\n", target)
833 
834  return true;
835 }
836 
837 bool CGUIKernel::save_kernel(char* filename)
838 {
839  if (kernel && initialized)
840  {
841  CCSVFile* file=new CCSVFile(filename);
842  try
843  {
844  kernel->save(file);
845  }
846  catch (...)
847  {
848  SG_ERROR("Writing to file %s failed!\n", filename)
849  }
850 
851  SG_UNREF(file);
852  SG_INFO("Successfully written kernel to \"%s\" !\n", filename)
853  return true;
854  }
855  else
856  SG_ERROR("No kernel set / kernel not initialized!\n")
857 
858  return false;
859 }
860 
862 {
863  if (!kern)
864  SG_ERROR("Given kernel to add is invalid.\n")
865 
866  if (!kernel)
867  {
868  kernel= new CCombinedKernel(20, false);
869  SG_REF(kernel);
870  }
871 
873  {
874  CKernel* first_elem=kernel;
875  kernel= new CCombinedKernel(20, false);
876  SG_REF(kernel);
877  ((CCombinedKernel*) kernel)->append_kernel(first_elem);
878  }
879 
880  if (!kernel)
881  SG_ERROR("Combined kernel object could not be created.\n")
882 
883  kern->set_combined_kernel_weight(weight);
884 
885  bool success=((CCombinedKernel*) kernel)->append_kernel(kern);
886 
887  initialized=true;
888  if (success)
889  ((CCombinedKernel*) kernel)->list_kernels();
890  else
891  SG_ERROR("Adding of kernel failed.\n")
892 
893  return success;
894 }
895 
896 
898 {
899  if (!kernel)
900  SG_ERROR("No kernel available.\n")
901 
903  SG_ERROR("Need a combined kernel for deleting the last kernel in it.\n")
904 
905  if (((CCombinedKernel*) kernel)->get_num_kernels()>0)
906  return ((CCombinedKernel*) kernel)->
907  delete_kernel(((CCombinedKernel*) kernel)->get_num_kernels()-1);
908  else
909  SG_ERROR("No kernel available to delete.\n")
910 
911  return false;
912 }
913 
915 {
916  SG_UNREF(kernel);
917  kernel=NULL;
918  return true;
919 }
920 
921 #ifdef USE_SVMLIGHT
923 {
924  if (!kernel)
925  SG_ERROR("No kernel available.\n")
926 
928  return true;
929 }
930 #endif //USE_SVMLIGHT
931 
933 {
935  if (!kernel)
936  SG_ERROR("No kernel available.\n")
937 
938  if (strncmp(opt_type, "FASTBUTMEMHUNGRY", 16)==0)
939  {
940  SG_INFO("FAST METHOD selected\n")
941  opt=FASTBUTMEMHUNGRY;
943 
944  return true;
945  }
946  else if (strncmp(opt_type,"SLOWBUTMEMEFFICIENT", 19)==0)
947  {
948  SG_INFO("MEMORY EFFICIENT METHOD selected\n")
951 
952  return true;
953  }
954  else
955  SG_ERROR("Wrong kernel optimization type.\n")
956 
957  return false;
958 }
959 
961 {
962  if (!kernel)
963  SG_ERROR("No kernel available.\n")
964 
966  SG_ERROR("Not a combined kernel.\n")
967 
969 }
CKernel * create_spectrummismatchrbf(int32_t size=10, float64_t *AA_matrix=NULL, int32_t nr=128, int32_t nc=128, int32_t max_mismatch=1, int32_t degree=1, float64_t width=1)
Definition: GUIKernel.cpp:390
CKernel * create_linear(int32_t size=10, float64_t scale=-1)
Definition: GUIKernel.cpp:578
CKernel * create_weighteddegreepositionstring(int32_t size=10, int32_t order=3, int32_t max_mismatch=1, int32_t length=0, int32_t center=0, float64_t step=1)
Definition: GUIKernel.cpp:276
virtual bool init(CFeatures *lhs, CFeatures *rhs)
Definition: Kernel.cpp:98
CKernel * create_sparsepoly(int32_t size=10, int32_t degree=2, bool inhomogene=false, bool normalize=true)
Definition: GUIKernel.cpp:190
bool resize_kernel_cache(int32_t size)
Definition: GUIKernel.cpp:922
Computes the standard linear kernel on CDotFeatures.
Definition: LinearKernel.h:35
#define SG_INFO(...)
Definition: SGIO.h:118
The standard Sigmoid kernel computed on dense real valued features.
Definition: SigmoidKernel.h:31
Computes the standard polynomial kernel on CDotFeatures.
Definition: PolyKernel.h:38
CKernel * create_histogramword(int32_t size=10)
Definition: GUIKernel.cpp:530
EKernelType
Definition: Kernel.h:57
The Diagonal Kernel returns a constant for the diagonal and zero otherwise.
Definition: DiagKernel.h:28
Class Distance, a base class for all the distances used in the Shogun toolbox.
Definition: Distance.h:81
CKernel * create_commstring(int32_t size=10, bool use_sign=false, char *norm_str=NULL, EKernelType ktype=K_WEIGHTEDCOMMWORDSTRING)
Definition: GUIKernel.cpp:437
CKernel * create_polymatchwordstring(int32_t size=10, int32_t degree=2, bool inhomogene=false, bool normalize=true)
Definition: GUIKernel.cpp:491
This class offers access to the Oligo Kernel introduced by Meinicke et al. in 2004.
CKernel * create_sparselinear(int32_t size=10, float64_t scale=-1)
Definition: GUIKernel.cpp:589
The CommUlongString kernel may be used to compute the spectrum kernel from strings that have been map...
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
CKernel * create_fixeddegreestring(int32_t size=10, int32_t d=3)
Definition: GUIKernel.cpp:415
The Custom Kernel allows for custom user provided kernel matrices.
Definition: CustomKernel.h:36
virtual CSGObject * clone()
Definition: SGObject.cpp:1361
CKernel * create_linearstring(int32_t size=10, float64_t scale=-1)
Definition: GUIKernel.cpp:566
CKernel * create_localityimprovedstring(int32_t size=10, int32_t length=3, int32_t inner_degree=3, int32_t outer_degree=1, EKernelType ktype=K_LOCALITYIMPROVED)
Definition: GUIKernel.cpp:212
virtual bool set_normalizer(CKernelNormalizer *normalizer)
Definition: Kernel.cpp:139
CKernel * create_linearword(int32_t size=10, float64_t scale=-1)
Definition: GUIKernel.cpp:556
CKernel * create_weighteddegreepositionstring2(int32_t size=10, int32_t order=3, int32_t max_mismatch=1, int32_t *shifts=NULL, int32_t length=0, bool use_normalization=true)
Definition: GUIKernel.cpp:332
CKernel * create_gaussianshift(int32_t size=10, float64_t width=1, int32_t max_shift=0, int32_t shift_step=1)
Definition: GUIKernel.cpp:134
The FixedDegree String kernel takes as input two strings of same size and counts the number of matche...
#define SG_ERROR(...)
Definition: SGIO.h:129
virtual bool delete_optimization()
Definition: Kernel.cpp:822
void set_cache_size(int32_t size)
Definition: Kernel.h:586
bool init_kernel(const char *target)
Definition: GUIKernel.cpp:757
virtual void set_optimization_type(EOptimizationType t)
Definition: Kernel.h:747
CSGInterface * ui
Definition: GUIKernel.h:178
The SalzbergWordString kernel implements the Salzberg kernel.
CKernel * create_linearbyte(int32_t size=10, float64_t scale=-1)
Definition: GUIKernel.cpp:546
CKernel * create_combined(int32_t size=10, bool append_subkernel_weights=false)
Definition: GUIKernel.cpp:627
CKernel * create_custom(float64_t *kmatrix, int32_t num_feat, int32_t num_vec, bool source_is_diag, bool dest_is_diag)
Definition: GUIKernel.cpp:113
The WeightedCommWordString kernel may be used to compute the weighted spectrum kernel (i...
#define SG_REF(x)
Definition: SGObject.h:51
Computes the Tensor Product Pair Kernel (TPPK).
EFeatureClass
shogun feature class
Definition: FeatureTypes.h:38
bool get_is_initialized()
Definition: Kernel.h:753
The Constant Kernel returns a constant for all elements.
Definition: ConstKernel.h:29
The Chi2 kernel operating on realvalued vectors computes the chi-squared distance between sets of his...
Definition: Chi2Kernel.h:34
void save(CFile *writer)
Definition: Kernel.cpp:641
bool set_triangle_kernel_matrix_from_triangle(SGVector< float64_t > tri_kernel_matrix)
Definition: CustomKernel.h:139
The class PolyMatchWordStringKernel computes a variant of the polynomial kernel on word-features...
bool has_property(EKernelProperty p)
Definition: Kernel.h:723
Class CSVFile used to read data from comma-separated values (CSV) files. See http://en.wikipedia.org/wiki/Comma-separated_values.
Definition: CSVFile.h:29
bool save_kernel(char *filename)
Definition: GUIKernel.cpp:837
The HistogramWordString computes the TOP kernel on inhomogeneous Markov Chains.
CKernel * create_localalignmentstring(int32_t size=10)
Definition: GUIKernel.cpp:404
CKernel * create_salzbergword(int32_t size=10)
Definition: GUIKernel.cpp:502
bool set_optimization_type(char *opt_type)
Definition: GUIKernel.cpp:932
CKernel * kernel
Definition: GUIKernel.h:176
CKernel * create_weighteddegreepositionstring3(int32_t size=10, int32_t order=3, int32_t max_mismatch=1, int32_t *shifts=NULL, int32_t length=0, int32_t mkl_stepsize=1, float64_t *position_weights=NULL)
Definition: GUIKernel.cpp:309
#define ASSERT(x)
Definition: SGIO.h:201
CKernel * create_weighteddegreerbf(int32_t size=10, int32_t degree=1, int32_t nof_properties=1, float64_t width=1)
Definition: GUIKernel.cpp:379
CKernel * create_const(int32_t size=10, float64_t c=1)
Definition: GUIKernel.cpp:100
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
CKernel * create_weighteddegreestring(int32_t size=10, int32_t order=3, int32_t max_mismatch=1, bool use_normalization=true, int32_t mkl_stepsize=1, bool block_computation=true, int32_t single_degree=-1)
Definition: GUIKernel.cpp:237
CKernel * create_sigmoid(int32_t size=10, float64_t gamma=0.01, float64_t coef0=0)
Definition: GUIKernel.cpp:168
Computes the standard linear kernel on dense char valued features.
An experimental kernel inspired by the WeightedDegreePositionStringKernel and the Gaussian kernel...
The CommWordString kernel may be used to compute the spectrum kernel from strings that have been mapp...
double float64_t
Definition: common.h:50
virtual EFeatureType get_feature_type()=0
CKernel * create_poly(int32_t size=10, int32_t degree=2, bool inhomogene=false, bool normalize=true)
Definition: GUIKernel.cpp:201
The class PolyMatchStringKernel computes a variant of the polynomial kernel on strings of same length...
void set_combined_kernel_weight(float64_t nw)
Definition: Kernel.h:808
float64_t get_alpha(int32_t idx)
The Weighted Degree String kernel.
bool set_full_kernel_matrix_from_full(SGMatrix< float32_t > full_kernel_matrix, bool check_symmetry=false)
Definition: CustomKernel.h:263
virtual EFeatureClass get_feature_class() const =0
The Combined kernel is used to combine a number of kernels into a single CombinedKernel object by lin...
the scatter kernel normalizer
Normalize the kernel by a constant obtained from the first element of the kernel matrix, i.e. .
Identity Kernel Normalization, i.e. no normalization is applied.
The class MatchWordStringKernel computes a variant of the polynomial kernel on strings of same length...
Normalize the kernel by adding a constant term to its diagonal. This aids kernels to become positive ...
SimpleLocalityImprovedString kernel, is a ``simplified'' and better performing version of the Localit...
CKernel * create_diag(int32_t size=10, float64_t diag=1)
Definition: GUIKernel.cpp:89
int32_t get_support_vector(int32_t idx)
ZeroMeanCenterKernelNormalizer centers the kernel in feature space.
CKernel * get_kernel()
Definition: GUIKernel.cpp:76
The well known Gaussian kernel (swiss army knife for SVMs) computed on CDotFeatures.
The Distance kernel takes a distance as input.
CKernel * create_chi2(int32_t size=10, float64_t width=1)
Definition: GUIKernel.cpp:426
EFeatureType
shogun feature type
Definition: FeatureTypes.h:19
bool delete_kernel_optimization()
Definition: GUIKernel.cpp:748
static int64_t nchoosek(int32_t n, int32_t k)
Definition: Math.h:1191
#define SG_UNREF(x)
Definition: SGObject.h:52
#define SG_DEBUG(...)
Definition: SGIO.h:107
SqrtDiagKernelNormalizer divides by the Square Root of the product of the diagonal elements...
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
EOptimizationType
Definition: Kernel.h:50
CKernel * create_matchwordstring(int32_t size=10, int32_t d=3, bool normalize=true)
Definition: GUIKernel.cpp:469
Normalize the kernel by either a constant or the average value of the diagonal elements (depending on...
virtual EKernelType get_kernel_type()=0
virtual bool init_optimization(int32_t count, int32_t *IDX, float64_t *weights)
Definition: Kernel.cpp:815
The class Features is the base class of all feature objects.
Definition: Features.h:68
void scale(Matrix A, Matrix B, typename Matrix::Scalar alpha)
Definition: Core.h:93
class PluginEstimate
CKernel * create_wavelet(int32_t size=10, float64_t Wdilation=5.0, float64_t Wtranslation=2.0)
Definition: GUIKernel.cpp:179
A generic Support Vector Machine Interface.
Definition: SVM.h:49
bool set_triangle_kernel_matrix_from_full(SGMatrix< float64_t > full_kernel_matrix)
Definition: CustomKernel.h:204
bool precompute_subkernels()
Definition: GUIKernel.cpp:960
the class WaveletKernel
Definition: WaveletKernel.h:37
The Kernel base class.
Definition: Kernel.h:158
CKernel * create_gaussian(int32_t size=10, float64_t width=1)
Definition: GUIKernel.cpp:157
CKernel * create_oligo(int32_t size, int32_t k, float64_t width)
Definition: GUIKernel.cpp:81
CKernel * create_tppk(int32_t size, float64_t *km, int32_t rows, int32_t cols)
Definition: GUIKernel.cpp:600
bool init_kernel_optimization()
Definition: GUIKernel.cpp:715
The Weighted Degree Position String kernel (Weighted Degree kernel with shifts).
bool set_kernel(CKernel *kern)
Definition: GUIKernel.cpp:699
The LocalAlignmentString kernel compares two sequences through all possible local alignments between ...
CKernel * create_distance(int32_t size=10, float64_t width=1)
Definition: GUIKernel.cpp:612
CKernel * create_polymatchstring(int32_t size=10, int32_t degree=2, bool inhomogene=false, bool normalize=true)
Definition: GUIKernel.cpp:480
static int32_t pow(bool x, int32_t n)
Definition: Math.h:535
VarianceKernelNormalizer divides by the ``variance''.
void resize_kernel_cache(KERNELCACHE_IDX size, bool regression_hack=false)
Definition: Kernel.cpp:85
bool add_kernel(CKernel *kern, float64_t weight=1)
Definition: GUIKernel.cpp:861
virtual EFeatureType get_feature_type() const =0
The LocalityImprovedString kernel is inspired by the polynomial kernel. Comparing neighboring charact...
virtual EFeatureClass get_feature_class()=0
CKernel * create_sparsegaussian(int32_t size=10, float64_t width=1)
Definition: GUIKernel.cpp:146
bool set_normalization(char *normalization, float64_t c=0.0, float64_t r=0.0)
Definition: GUIKernel.cpp:639

SHOGUN Machine Learning Toolbox - Documentation