63 using namespace shogun;
84 SG_DEBUG(
"created OligoStringKernel (%p) with size %d, k %d, width %f.\n", kern, size, k, width);
93 SG_ERROR(
"Couldn't create DiagKernel with size %d, diag %f.\n", size, diag);
95 SG_DEBUG(
"created DiagKernel (%p) with size %d, diag %f.\n", kern, size, diag);
104 SG_ERROR(
"Couldn't create ConstKernel with c %f.\n", c);
106 SG_DEBUG(
"created ConstKernel (%p) with c %f.\n", kern, c);
116 SG_DEBUG(
"created CustomKernel (%p).\n", kern);
120 if (source_is_diag && dest_is_diag && num_feat==1)
125 else if (!source_is_diag && dest_is_diag && num_vec==num_feat)
136 int32_t size,
float64_t width, int32_t max_shift, int32_t shift_step)
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);
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);
151 SG_ERROR(
"Couldn't create GaussianKernel with size %d, width %f.\n", size, width);
153 SG_DEBUG(
"created GaussianKernel (%p) with size %d, width %f.\n", kern, size, width);
162 SG_ERROR(
"Couldn't create GaussianKernel with size %d, width %f.\n", size, width);
164 SG_DEBUG(
"created GaussianKernel (%p) with size %d, width %f.\n", kern, size, width);
174 SG_ERROR(
"Couldn't create SigmoidKernel with size %d, gamma %f, coef0 %f.\n", size, gamma, coef0);
176 SG_DEBUG(
"created SigmoidKernel (%p) with size %d, gamma %f, coef0 %f.\n", kern, size, gamma, coef0);
185 SG_ERROR(
"Couldn't create WaveletKernel with size %d, Wdilation %f, Wtranslation %f.\n", size, Wdilation, Wtranslation);
187 SG_DEBUG(
"created WaveletKernel (%p) with size %d, Wdilation %f, Wtranslation %f.\n", kern, size, Wdilation, Wtranslation);
192 int32_t size, int32_t degree,
bool inhomogene,
bool normalize)
197 SG_DEBUG(
"created PolyKernel with size %d, degree %d, inhomogene %d normalize %d.\n", kern, size, degree, inhomogene, normalize);
203 int32_t size, int32_t degree,
bool inhomogene,
bool normalize)
208 SG_DEBUG(
"created PolyKernel (%p) with size %d, degree %d, inhomogene %d, normalize %d.\n", kern, size, degree, inhomogene, normalize);
214 int32_t size, int32_t length, int32_t inner_degree, int32_t outer_degree,
222 size, length, inner_degree, outer_degree);
227 size, length, inner_degree, outer_degree);
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);
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);
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)
242 float64_t* weights=get_weights(order, max_mismatch);
245 if (single_degree>=0)
247 ASSERT(single_degree<order);
248 for (i=0; i<order; i++)
250 if (i!=single_degree)
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);
265 if (!use_normalization)
269 set_use_block_computation(block_computation);
279 int32_t size, int32_t order, int32_t max_mismatch, int32_t length,
283 int32_t* shifts=
SG_MALLOC(int32_t, length);
285 for (i=center; i<length; i++)
286 shifts[i]=(int32_t) floor(((
float64_t) (i-center))/step);
288 for (i=center-1; i>=0; i--)
289 shifts[i]=(int32_t) floor(((
float64_t) (center-i))/step);
291 for (i=0; i<length; i++)
293 if (shifts[i]>length)
297 for (i=0; i<length; i++)
298 SG_INFO(
"shift[%i]=%i\n", i, shifts[i]);
300 float64_t* weights=get_weights(order, max_mismatch);
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);
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);
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)
317 float64_t* weights=get_weights(order, max_mismatch);
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);
324 if (!position_weights)
327 for (int32_t i=0; i<length; i++)
328 position_weights[i]=1.0/length;
338 int32_t size, int32_t order, int32_t max_mismatch, int32_t* shifts,
339 int32_t length,
bool use_normalization)
341 float64_t* weights=get_weights(order, max_mismatch);
344 if (!use_normalization)
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);
354 float64_t* CGUIKernel::get_weights(int32_t order, int32_t max_mismatch)
360 for (i=0; i<order; i++)
365 for (i=0; i<order; i++)
368 for (i=0; i<order; i++)
370 for (int32_t j=1; j<=max_mismatch; j++)
375 weights[i+j*order]=weights[i]/(nk*
CMath::pow(3, j));
378 weights[i+j*order]=0;
389 SG_ERROR(
"Couldn't create WeightedDegreeRBFKernel with size %d, width %f, degree %d, nof_properties %d.\n", size, width, degree, nof_properties);
391 SG_DEBUG(
"created WeightedDegreeRBFKernel (%p) with size %d, width %f, degree %d, nof_properties %d.\n", kern, size, width, degree, nof_properties);
401 SG_ERROR(
"Couldn't create SpectrumMismatchRBFKernel with size %d, width %f, degree %d, max_mismatch %d.\n", size, width, degree, max_mismatch);
403 SG_DEBUG(
"created SpectrumMismatchRBFKernel (%p) with size %d, width %f, degree %d, max_mismatch %d.\n", kern, size, width, degree, max_mismatch);
414 SG_ERROR(
"Couldn't create LocalAlignmentStringKernel with size %d.\n", size);
416 SG_DEBUG(
"created LocalAlignmentStringKernel (%p) with size %d.\n", kern, size);
425 SG_ERROR(
"Couldn't create FixedDegreeStringKernel with size %d and d %d.\n", size, d);
427 SG_DEBUG(
"created FixedDegreeStringKernel (%p) with size %d and d %d.\n", kern, size, d);
436 SG_ERROR(
"Couldn't create Chi2Kernel with size %d and width %f.\n", size, width);
438 SG_DEBUG(
"created Chi2Kernel (%p) with size %d and width %f.\n", kern, size, width);
444 int32_t size,
bool use_sign,
char* norm_str,
EKernelType ktype)
449 norm_str= (
char*)
"FULL";
458 SG_DEBUG(
"created WeightedCommWord/CommWord/CommUlongStringKernel (%p) with size %d, use_sign %d norm_str %s.\n", kern, size, use_sign, norm_str);
461 if (strncmp(norm_str,
"NO", 2)==0)
465 else if (strncmp(norm_str,
"FULL", 4)==0)
470 SG_ERROR(
"Unsupported Normalizer requested, supports only FULL and NO\n");
476 int32_t size, int32_t d,
bool normalize)
479 SG_DEBUG(
"created MatchWordStringKernel (%p) with size %d and d %d.\n", kern, size, d);
487 int32_t size, int32_t degree,
bool inhomogene,
bool normalize)
490 SG_DEBUG(
"created PolyMatchStringKernel (%p) with size %d, degree %d, inhomogene %d normalize %d.\n", kern, size, degree, inhomogene, normalize);
498 int32_t size, int32_t degree,
bool inhomogene,
bool normalize)
501 SG_DEBUG(
"created PolyMatchWordStringKernel (%p) with size %d, degree %d, inhomogene %d, normalize %d.\n", kern, size, degree, inhomogene, normalize);
510 SG_INFO(
"Getting estimator.\n");
517 SG_ERROR(
"Couldn't create SalzbergWordString with size %d.\n", size);
519 SG_DEBUG(
"created SalzbergWordString (%p) with size %d.\n", kern, size);
538 SG_INFO(
"Getting estimator.\n");
545 SG_ERROR(
"Couldn't create HistogramWordString with size %d.\n", size);
547 SG_DEBUG(
"created HistogramWordString (%p) with size %d.\n", kern, size);
557 SG_DEBUG(
"created LinearByteKernel (%p) with size %d and scale %f.\n", kern, size, scale);
567 SG_DEBUG(
"created LinearWordKernel (%p) with size %d and scale %f.\n", kern, size, scale);
579 SG_DEBUG(
"created LinearStringKernel (%p) with size %d and scale %f.\n", kern, size, scale);
590 SG_DEBUG(
"created LinearKernel (%p) with size %d and scale %f.\n", kern, size, scale);
601 SG_DEBUG(
"created LinearKernel (%p) with size %d and scale %f.\n", kern, size, scale);
613 SG_DEBUG(
"created TPPK (%p) with size %d and km %p, rows %d, cols %d.\n", kern, size, km, rows, cols);
622 SG_ERROR(
"No distance set for DistanceKernel.\n");
626 SG_ERROR(
"Couldn't create DistanceKernel with size %d and width %f.\n", size, width);
628 SG_DEBUG(
"created DistanceKernel (%p) with size %d and width %f.\n", kern, size, width);
634 int32_t size,
bool append_subkernel_weights)
638 SG_ERROR(
"Couldn't create CombinedKernel with size %d and append_subkernel_weights %d.\n", size, append_subkernel_weights);
640 SG_DEBUG(
"created CombinedKernel (%p) with size %d and append_subkernel_weights %d.\n", kern, size, append_subkernel_weights);
655 if (strncmp(normalization,
"IDENTITY", 8)==0)
657 SG_INFO(
"Identity Normalization (==NO NORMALIZATION) selected\n");
660 else if (strncmp(normalization,
"AVGDIAG", 7)==0)
662 SG_INFO(
"Average Kernel Diagonal Normalization selected\n");
665 else if (strncmp(normalization,
"RIDGE", 5)==0)
667 SG_INFO(
"Ridge Kernel Normalization selected\n");
670 else if (strncmp(normalization,
"SQRTDIAG", 8)==0)
672 SG_INFO(
"Sqrt Diagonal Normalization selected\n");
675 else if (strncmp(normalization,
"FIRSTELEMENT", 12)==0)
677 SG_INFO(
"First Element Normalization selected\n");
680 else if (strncmp(normalization,
"VARIANCE", 8)==0)
682 SG_INFO(
"Variance Normalization selected\n");
685 else if (strncmp(normalization,
"SCATTER", 7)==0)
687 SG_INFO(
"Scatter Normalization selected\n");
688 CLabels* train_labels=
ui->ui_labels->get_train_labels();
692 else if (strncmp(normalization,
"ZEROMEANCENTER", 13)==0)
694 SG_INFO(
"Zero Mean Center Normalization selected\n");
698 SG_ERROR(
"Wrong kernel normalizer name.\n");
713 SG_DEBUG(
"set new kernel (%p).\n", kern);
723 CSVM* svm=(
CSVM*)
ui->ui_classifier->get_classifier();
729 int32_t* sv_idx=
SG_MALLOC(int32_t, num_sv);
732 for (int32_t i=0; i<num_sv; i++)
744 SG_ERROR(
"Initialization of kernel optimization failed\n");
778 if (!strncmp(target,
"TRAIN", 5))
780 CFeatures* train=
ui->ui_features->get_train_features();
786 if ((k_fclass==fclass || k_fclass==
C_ANY || fclass==
C_ANY) &&
787 (k_ftype==ftype || k_ftype==
F_ANY || ftype==
F_ANY))
790 SG_INFO(
"Initialising kernel with TRAIN DATA, train: %p\n", train);
795 SG_ERROR(
"Kernel can not process this train feature type: %d %d.\n", fclass, ftype);
798 SG_DEBUG(
"Not initing kernel - no train features assigned.\n");
800 else if (!strncmp(target,
"TEST", 4))
802 CFeatures* train=
ui->ui_features->get_train_features();
803 CFeatures* test=
ui->ui_features->get_test_features();
808 if ((k_fclass==fclass || k_fclass==
C_ANY || fclass==
C_ANY) &&
809 (k_ftype==ftype || k_ftype==
F_ANY || ftype==
F_ANY))
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))
819 SG_INFO(
"Initialising kernel with TRAIN DATA, train: %p\n", train);
824 SG_ERROR(
"Kernel can not process this train feature type: %d %d.\n", fclass, ftype);
827 SG_INFO(
"Initialising kernel with TEST DATA, train: %p test %p\n", train, test);
832 SG_ERROR(
"Kernel can not process this test feature type: %d %d.\n", fclass, ftype);
835 SG_DEBUG(
"Not initing kernel - no train and test features assigned.\n");
838 SG_ERROR(
"Unknown target %s.\n", target);
854 SG_ERROR(
"Writing to file %s failed!\n", filename);
858 SG_INFO(
"Successfully written kernel to \"%s\" !\n", filename);
862 SG_ERROR(
"No kernel set / kernel not initialized!\n");
870 SG_ERROR(
"Given kernel to add is invalid.\n");
887 SG_ERROR(
"Combined kernel object could not be created.\n");
897 SG_ERROR(
"Adding of kernel failed.\n");
909 SG_ERROR(
"Need a combined kernel for deleting the last kernel in it.\n");
915 SG_ERROR(
"No kernel available to delete.\n");
935 if (strncmp(opt_type,
"FASTBUTMEMHUNGRY", 16)==0)
937 SG_INFO(
"FAST METHOD selected\n");
943 else if (strncmp(opt_type,
"SLOWBUTMEMEFFICIENT", 19)==0)
945 SG_INFO(
"MEMORY EFFICIENT METHOD selected\n");
952 SG_ERROR(
"Wrong kernel optimization type.\n");
963 SG_ERROR(
"Not a combined kernel.\n");