MCPcopy Create free account
hub / github.com/davisking/dlib / main

Function main

examples/using_custom_kernels_ex.cpp:133–207  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

131// ----------------------------------------------------------------------------------------
132
133int main()
134{
135 // We are going to be working with 2 dimensional samples and trying to perform
136 // binary classification on them using our new ukf_kernel.
137 typedef matrix<double, 2, 1> sample_type;
138
139 typedef ukf_kernel<sample_type> kernel_type;
140
141
142 // Now let's generate some training data
143 std::vector<sample_type> samples;
144 std::vector<double> labels;
145 for (double r = -20; r <= 20; r += 0.9)
146 {
147 for (double c = -20; c <= 20; c += 0.9)
148 {
149 sample_type samp;
150 samp(0) = r;
151 samp(1) = c;
152 samples.push_back(samp);
153
154 // if this point is less than 13 from the origin
155 if (sqrt(r*r + c*c) <= 13)
156 labels.push_back(+1);
157 else
158 labels.push_back(-1);
159
160 }
161 }
162 cout << "samples generated: " << samples.size() << endl;
163 cout << " number of +1 samples: " << sum(mat(labels) > 0) << endl;
164 cout << " number of -1 samples: " << sum(mat(labels) < 0) << endl;
165
166
167 // A valid kernel must always give rise to kernel matrices which are symmetric
168 // and positive semidefinite (i.e. have nonnegative eigenvalues). This next
169 // bit of code makes a kernel matrix and checks if it has these properties.
170 const matrix<double> K = kernel_matrix(kernel_type(0.1), randomly_subsample(samples, 500));
171 cout << "\nIs it symmetric? (this value should be 0): "<< min(abs(K - trans(K))) << endl;
172 cout << "Smallest eigenvalue (should be >= 0): " << min(real_eigenvalues(K)) << endl;
173
174
175 // here we make an instance of the krr_trainer object that uses our new kernel.
176 krr_trainer<kernel_type> trainer;
177 trainer.use_classification_loss_for_loo_cv();
178
179
180 // Finally, let's test how good our new kernel is by doing some leave-one-out cross-validation.
181 cout << "\ndoing leave-one-out cross-validation" << endl;
182 for (double sigma = 0.01; sigma <= 100; sigma *= 3)
183 {
184 // tell the trainer the parameters we want to use
185 trainer.set_kernel(kernel_type(sigma));
186
187 std::vector<double> loo_values;
188 trainer.train(samples, labels, loo_values);
189
190 // Print sigma and the fraction of samples correctly classified during LOO cross-validation.

Callers

nothing calls this directly

Calls 15

kernel_matrixFunction · 0.85
randomly_subsampleFunction · 0.85
absFunction · 0.85
real_eigenvaluesFunction · 0.85
mean_sign_agreementFunction · 0.85
sqrtFunction · 0.50
sumFunction · 0.50
matFunction · 0.50
minFunction · 0.50
transFunction · 0.50
derivativeFunction · 0.50
push_backMethod · 0.45

Tested by

no test coverage detected