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;
75 rprop.
init( mlms, start, stepsize );
76 unsigned int its = 50;
79 for (
unsigned int i=0; i<its; i++) {
82 std::cout <<
"iteration " << i <<
": current NCLL = " << rprop.
solution().
value <<
" at parameter: " << rprop.
solution().
point << std::endl;
83 if ( rprop.
maxDelta() < stop_delta ) {
84 if ( verbose ) std::cout <<
" Rprop quit pecause of small progress " << std::endl;
90 std::cout << std::endl;
91 std::cout <<
" -------------------------------------------------------------------------------- " << std::endl;
92 std::cout <<
" ----------- Done with gradient-based optimization of MLMS criterion ------------ " << std::endl;
93 std::cout <<
" -------------------------------------------------------------------------------- " << std::endl << std::endl;
95 if ( verbose ) std::cout << std::endl << std::endl <<
" EVALUATION of hyperparameters found:" << std::endl << std::endl << std::endl;
98 double test_error_v1, train_error_v1;
99 double test_error_v2, train_error_v2;
102 if ( verbose ) std::cout << std::endl <<
" Possibility 1: copy kernel parameters via eval() and C by hand..." << std::endl << std::endl;
109 std::cout <<
" Value of model selection criterion at final point: " << end_value << std::endl;
110 std::cout <<
" Done optimizing the SVM hyperparameters. The final parameters (true/unencoded) are:" << std::endl << std::endl;
111 std::cout <<
" C = " << C_reg << std::endl;
112 for (
unsigned int i=0; i<
total_dim; i++ )
114 std::cout << std::endl <<
" (as also given by kernel.gammaVector() : " << kernel.
gammaVector() <<
" ) " << std::endl;
121 std::cout << std::endl << std::endl <<
" Used mlms.eval(...) to copy kernel.parameterVector() " << kernel.
parameterVector() << std::endl;
122 std::cout <<
" into trainer_v1.parameterVector() " << trainer_v1.
parameterVector() << std::endl;
123 std::cout <<
" , where C (the last parameter) was set manually to " << trainer_v1.
C() << std::endl << std::endl << std::endl;
125 trainer_v1.
train( svm_v1, train );
130 output_v1 = svm_v1( train.
inputs() );
131 train_error_v1 = loss_v1.
eval( train.
labels(), output_v1 );
132 output_v1 = svm_v1( test.
inputs() );
133 test_error_v1 = loss_v1.
eval( test.
labels(), output_v1 );
135 std::cout <<
" training error via possibility 1: " << train_error_v1 << std::endl;
136 std::cout <<
" test error via possibility 1: " << test_error_v1 << std::endl << std::endl << std::endl;
141 if ( verbose ) std::cout << std::endl <<
" Possibility 2: copy best parameters via solution().point()..." << std::endl << std::endl;
148 std::cout <<
" Copied rprop.solution().point = " << rprop.
solution().
point << std::endl;
149 std::cout <<
" into trainer_v2.parameterVector(), now = " << trainer_v2.
parameterVector() << std::endl << std::endl << std::endl;
152 trainer_v2.
train( svm_v2, train );
157 output_v2 = svm_v2( train.
inputs() );
158 train_error_v2 = loss_v2.
eval( train.
labels(), output_v2 );
159 output_v2 = svm_v2( test.
inputs() );
160 test_error_v2 = loss_v2.
eval( test.
labels(), output_v2 );
162 std::cout <<
" training error via possibility 2: " << train_error_v2 << std::endl;
163 std::cout <<
" test error via possibility 2: " << test_error_v2 << std::endl << std::endl << std::endl;
164 std::cout << std::endl <<
"That's all folks - we are done!" << std::endl;
169 RealVector final_params(total_dim+3);
170 final_params(total_dim) = C_reg;
171 for (
unsigned int i=0; i<
total_dim; i++ )
173 final_params(total_dim+1) = train_error_v1;
174 final_params(total_dim+2) = test_error_v1;
184 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;
187 unsigned int num_trials = 100;
189 for (
unsigned int i=0; i<num_trials; i++ ) {
191 std::cout <<
"." << std::flush;
193 std::cout <<
"\n" << std::endl;
195 RealVector overall_mean, overall_variance;
196 meanvar( many_results, overall_mean, overall_variance );
197 for (
unsigned int i=0; i<
total_dim+1; i++ ) {
198 std::cout <<
"avg-param(" << i <<
") = " << overall_mean(i) <<
" +- "<< overall_variance(i) << std::endl;
200 std::cout << std::endl <<
"avg-error-train = " << overall_mean(total_dim+1) <<
" +- "<< overall_variance(total_dim+1) << std::endl;
201 std::cout <<
"avg-error-test = " << overall_mean(total_dim+2) <<
" +- "<< overall_variance(total_dim+2) << std::endl;