14 using namespace shark;
29 unsigned int train_size = 500;
30 unsigned int test_size = 5000;
37 normalizationTrainer.
train( normalizer, train.
inputs() );
45 unsigned int num_folds = 5;
49 bool log_enc_c =
true;
57 for (
unsigned int k=0; k<
total_dim; k++ )
61 double start_value = mlms.
eval( start );
64 std::cout <<
"Value of model selection criterion at starting point: " << start_value << std::endl << std::endl;
65 std::cout <<
" -------------------------------------------------------------------------------- " << std::endl;
66 std::cout <<
" ----------- Beginning gradient-based optimization of MLMS criterion ------------ " << std::endl;
67 std::cout <<
" -------------------------------------------------------------------------------- " << std::endl << std::endl;
72 double stepsize = 0.1;
73 double stop_delta = 1e-3;
74 rprop.
init( mlms, start, stepsize );
75 unsigned int its = 50;
78 for (
unsigned int i=0; i<its; i++) {
81 std::cout <<
"iteration " << i <<
": current NCLL = " << rprop.
solution().
value <<
" at parameter: " << rprop.
solution().
point << std::endl;
82 if ( rprop.
maxDelta() < stop_delta ) {
83 if ( verbose ) std::cout <<
" Rprop quit pecause of small progress " << std::endl;
89 std::cout << std::endl;
90 std::cout <<
" -------------------------------------------------------------------------------- " << std::endl;
91 std::cout <<
" ----------- Done with gradient-based optimization of MLMS criterion ------------ " << std::endl;
92 std::cout <<
" -------------------------------------------------------------------------------- " << std::endl << std::endl;
94 if ( verbose ) std::cout << std::endl << std::endl <<
" EVALUATION of hyperparameters found:" << std::endl << std::endl << std::endl;
97 double test_error_v1, train_error_v1;
98 double test_error_v2, train_error_v2;
101 if ( verbose ) std::cout << std::endl <<
" Possibility 1: copy kernel parameters via eval() and C by hand..." << std::endl << std::endl;
108 std::cout <<
" Value of model selection criterion at final point: " << end_value << std::endl;
109 std::cout <<
" Done optimizing the SVM hyperparameters. The final parameters (true/unencoded) are:" << std::endl << std::endl;
110 std::cout <<
" C = " << C_reg << std::endl;
111 for (
unsigned int i=0; i<
total_dim; i++ )
113 std::cout << std::endl <<
" (as also given by kernel.gammaVector() : " << kernel.
gammaVector() <<
" ) " << std::endl;
120 std::cout << std::endl << std::endl <<
" Used mlms.eval(...) to copy kernel.parameterVector() " << kernel.
parameterVector() << std::endl;
121 std::cout <<
" into trainer_v1.parameterVector() " << trainer_v1.
parameterVector() << std::endl;
122 std::cout <<
" , where C (the last parameter) was set manually to " << trainer_v1.
C() << std::endl << std::endl << std::endl;
124 trainer_v1.
train( svm_v1, train );
129 output_v1 = svm_v1( train.
inputs() );
130 train_error_v1 = loss_v1.
eval( train.
labels(), output_v1 );
131 output_v1 = svm_v1( test.
inputs() );
132 test_error_v1 = loss_v1.
eval( test.
labels(), output_v1 );
134 std::cout <<
" training error via possibility 1: " << train_error_v1 << std::endl;
135 std::cout <<
" test error via possibility 1: " << test_error_v1 << std::endl << std::endl << std::endl;
140 if ( verbose ) std::cout << std::endl <<
" Possibility 2: copy best parameters via solution().point()..." << std::endl << std::endl;
147 std::cout <<
" Copied rprop.solution().point = " << rprop.
solution().
point << std::endl;
148 std::cout <<
" into trainer_v2.parameterVector(), now = " << trainer_v2.
parameterVector() << std::endl << std::endl << std::endl;
151 trainer_v2.
train( svm_v2, train );
156 output_v2 = svm_v2( train.
inputs() );
157 train_error_v2 = loss_v2.
eval( train.
labels(), output_v2 );
158 output_v2 = svm_v2( test.
inputs() );
159 test_error_v2 = loss_v2.
eval( test.
labels(), output_v2 );
161 std::cout <<
" training error via possibility 2: " << train_error_v2 << std::endl;
162 std::cout <<
" test error via possibility 2: " << test_error_v2 << std::endl << std::endl << std::endl;
163 std::cout << std::endl <<
"That's all folks - we are done!" << std::endl;
168 RealVector final_params(total_dim+3);
169 final_params(total_dim) = C_reg;
170 for (
unsigned int i=0; i<
total_dim; i++ )
172 final_params(total_dim+1) = train_error_v1;
173 final_params(total_dim+2) = test_error_v1;
183 std::cout <<
"\nNOW REPEAT WITH 100 TRIALS: now we do the exact same thing multiple times in a row, and note the average kernel weights. Please wait." << std::endl << std::endl;
186 unsigned int num_trials = 100;
188 for (
unsigned int i=0; i<num_trials; i++ ) {
190 std::cout <<
"." << std::flush;
192 std::cout <<
"\n" << std::endl;
194 RealVector overall_mean, overall_variance;
195 meanvar( many_results, overall_mean, overall_variance );
196 for (
unsigned int i=0; i<
total_dim+1; i++ ) {
197 std::cout <<
"avg-param(" << i <<
") = " << overall_mean(i) <<
" +- "<< overall_variance(i) << std::endl;
199 std::cout << std::endl <<
"avg-error-train = " << overall_mean(total_dim+1) <<
" +- "<< overall_variance(total_dim+1) << std::endl;
200 std::cout <<
"avg-error-test = " << overall_mean(total_dim+2) <<
" +- "<< overall_variance(total_dim+2) << std::endl;