11 #include <boost/fusion/algorithm/iteration/fold.hpp> 12 #include <boost/fusion/include/as_vector.hpp> 14 struct HeterogeneousInputStruct{
15 shark::RealVector rv1;
17 shark::CompressedRealVector crv3;
20 #ifndef DOXYGEN_SHOULD_SKIP_THIS 21 BOOST_FUSION_ADAPT_STRUCT(
22 HeterogeneousInputStruct,
23 (shark::RealVector, rv1)(std::size_t, st2)(shark::CompressedRealVector, crv3)
29 struct Batch< HeterogeneousInputStruct >{
31 HeterogeneousInputStruct,
32 (shark::RealVector, rv1)(std::size_t, st2)(shark::CompressedRealVector, crv3)
37 using namespace shark;
43 int main(
int argc,
char** argv)
61 std::vector< AbstractKernelFunction<RealVector> * > kernels1;
62 kernels1.push_back( &baseKernel1 );
63 kernels1.push_back( &baseKernel2 );
69 std::cout << endl <<
" ======================= WeightedSumKernel: ======================= " << std::endl;
70 cout << endl <<
"kernel1.isAdaptive(0): " << kernel1.
isAdaptive(0) << endl;
71 cout <<
"kernel1.isAdaptive(1): " << kernel1.
isAdaptive(1) << endl;
73 cout <<
"kernel1.parameterVector(): " << kernel1.
parameterVector() << endl;
74 cout <<
"kernel1.eval(x1,x2): " << kernel1.
eval(x1,x2) << endl << endl;
78 new_params_1(0) = 1.0;
82 cout <<
"kernel1.parameterVector() with 1st parameter set to 1: " << kernel1.
parameterVector() << endl;
83 cout <<
"kernel1.eval(x1,x2): " << kernel1.
eval(x1,x2) << endl << endl;
89 cout <<
"kernel1.isAdaptive(0): " << kernel1.
isAdaptive(0) << endl;
90 cout <<
"kernel1.isAdaptive(1): " << kernel1.
isAdaptive(1) << endl;
92 cout <<
"kernel1.parameterVector(): " << kernel1.
parameterVector() << endl<< endl;
99 cout <<
"kernel1.isAdaptive(0): " << kernel1.
isAdaptive(0) << endl;
100 cout <<
"kernel1.isAdaptive(1): " << kernel1.
isAdaptive(1) << endl;
102 cout <<
"kernel1.parameterVector(): " << kernel1.
parameterVector() << endl<< endl;
108 cout <<
"kernel1.isAdaptive(0): " << kernel1.
isAdaptive(0) << endl;
109 cout <<
"kernel1.isAdaptive(1): " << kernel1.
isAdaptive(1) << endl;
111 cout <<
"kernel1.parameterVector(): " << kernel1.
parameterVector() << endl;
112 cout <<
"kernel1.eval(x1,x2): " << kernel1.
eval(x1,x2) << endl << endl;
119 std::vector<AbstractKernelFunction<RealVector>* > kernels2;
120 kernels2.push_back(&baseKernel3);
121 kernels2.push_back(&baseKernel4);
123 std::vector< std::pair< std::size_t, std::size_t > > indcs_1;
124 indcs_1.push_back( std::make_pair( 0,2 ) );
125 indcs_1.push_back( std::make_pair( 0,2 ) );
131 std::cout << endl <<
" ======================= SubrangeKernel, full index range: ======================= " << std::endl;
132 cout << endl <<
"kernel2.isAdaptive(0): " << kernel2.isAdaptive(0) << endl;
133 cout <<
"kernel2.isAdaptive(1): " << kernel2.isAdaptive(1) << endl;
134 cout <<
"kernel2.numberOfParameters(): " << kernel2.numberOfParameters() << endl;
135 cout <<
"kernel2.parameterVector(): " << kernel2.parameterVector() << endl;
136 cout <<
"kernel2.eval(x1,x2): " << kernel2.eval(x1,x2) << endl << endl;
139 RealVector new_params_2( kernel2.numberOfParameters() );
140 new_params_2(0) = 1.0;
141 kernel2.setParameterVector( new_params_2 );
144 cout <<
"kernel2.parameterVector() with 1st parameter set to 1: " << kernel2.parameterVector() << endl;
145 cout <<
"kernel2.eval(x1,x2): " << kernel2.eval(x1,x2) << endl << endl;
148 kernel2.setAdaptive(0,
true);
151 cout <<
"kernel2.isAdaptive(0): " << kernel2.isAdaptive(0) << endl;
152 cout <<
"kernel2.isAdaptive(1): " << kernel2.isAdaptive(1) << endl;
153 cout <<
"kernel2.numberOfParameters(): " << kernel2.numberOfParameters() << endl;
154 cout <<
"kernel2.parameterVector(): " << kernel2.parameterVector() << endl<< endl;
157 kernel2.setAdaptive(0,
false);
158 kernel2.setAdaptive(1,
true);
161 cout <<
"kernel2.isAdaptive(0): " << kernel2.isAdaptive(0) << endl;
162 cout <<
"kernel2.isAdaptive(1): " << kernel2.isAdaptive(1) << endl;
163 cout <<
"kernel2.numberOfParameters(): " << kernel2.numberOfParameters() << endl;
164 cout <<
"kernel2.parameterVector(): " << kernel2.parameterVector() << endl<< endl;
167 kernel2.setAdaptiveAll(
true);
170 cout <<
"kernel2.isAdaptive(0): " << kernel2.isAdaptive(0) << endl;
171 cout <<
"kernel2.isAdaptive(1): " << kernel2.isAdaptive(1) << endl;
172 cout <<
"kernel2.numberOfParameters(): " << kernel2.numberOfParameters() << endl;
173 cout <<
"kernel2.parameterVector(): " << kernel2.parameterVector() << endl;
174 cout <<
"kernel2.eval(x1,x2): " << kernel2.eval(x1,x2) << endl << endl;
183 std::vector<AbstractKernelFunction<RealVector>* > kernels3;
184 kernels3.push_back(&baseKernel5);
185 kernels3.push_back(&baseKernel6);
187 std::vector< std::pair< std::size_t, std::size_t > > indcs_2;
188 indcs_2.push_back( std::make_pair( 0,1 ) );
189 indcs_2.push_back( std::make_pair( 1,2 ) );
195 std::cout << endl <<
" ======================= SubrangeKernel partial index range: ======================= " << std::endl;
196 cout << endl <<
"kernel3.isAdaptive(0): " << kernel3.isAdaptive(0) << endl;
197 cout <<
"kernel3.isAdaptive(1): " << kernel3.isAdaptive(1) << endl;
198 cout <<
"kernel3.numberOfParameters(): " << kernel3.numberOfParameters() << endl;
199 cout <<
"kernel3.parameterVector(): " << kernel3.parameterVector() << endl;
200 cout <<
"kernel3.eval(x1,x2): " << kernel3.eval(x1,x2) << endl << endl;
203 RealVector new_params_3( kernel3.numberOfParameters() );
204 new_params_3(0) = 1.0;
205 kernel3.setParameterVector( new_params_3 );
208 cout <<
"kernel3.parameterVector() with 1st parameter set to 1: " << kernel3.parameterVector() << endl;
209 cout <<
"kernel3.eval(x1,x2): " << kernel3.eval(x1,x2) << endl << endl;
212 kernel3.setAdaptive(0,
true);
215 cout <<
"kernel3.isAdaptive(0): " << kernel3.isAdaptive(0) << endl;
216 cout <<
"kernel3.isAdaptive(1): " << kernel3.isAdaptive(1) << endl;
217 cout <<
"kernel3.numberOfParameters(): " << kernel3.numberOfParameters() << endl;
218 cout <<
"kernel3.parameterVector(): " << kernel3.parameterVector() << endl<< endl;
221 kernel3.setAdaptive(0,
false);
222 kernel3.setAdaptive(1,
true);
225 cout <<
"kernel3.isAdaptive(0): " << kernel3.isAdaptive(0) << endl;
226 cout <<
"kernel3.isAdaptive(1): " << kernel3.isAdaptive(1) << endl;
227 cout <<
"kernel3.numberOfParameters(): " << kernel3.numberOfParameters() << endl;
228 cout <<
"kernel3.parameterVector(): " << kernel3.parameterVector() << endl<< endl;
231 kernel3.setAdaptiveAll(
true);
234 cout <<
"kernel3.isAdaptive(0): " << kernel3.isAdaptive(0) << endl;
235 cout <<
"kernel3.isAdaptive(1): " << kernel3.isAdaptive(1) << endl;
236 cout <<
"kernel3.numberOfParameters(): " << kernel3.numberOfParameters() << endl;
237 cout <<
"kernel3.parameterVector(): " << kernel3.parameterVector() << endl;
238 cout <<
"kernel3.eval(x1,x2): " << kernel3.eval(x1,x2) << endl << endl;
245 std::size_t
const num_samples = 2;
246 std::size_t
const dim_nonzeros = 2;
247 std::size_t
const max_elem_discr_kernel = 3;
248 std::size_t
const dim_sparse = 5;
250 std::vector<HeterogeneousInputStruct> data( num_samples );
252 data[0].rv1.resize( dim_nonzeros ); data[0].crv3.resize( dim_sparse);
253 data[1].rv1.resize( dim_nonzeros ); data[1].crv3.resize( dim_sparse);
254 data[0].rv1(0) = 1.0; data[0].rv1(1) = -1.0; data[0].crv3(1) = -0.5; data[0].crv3(4) = 8.0;
255 data[1].rv1(0) = 1.0; data[1].rv1(1) = -2.0; data[1].crv3(1) = 1.0; data[1].crv3(3) = 0.1;
256 data[0].st2 = 1; data[1].st2 = 2;
263 RealMatrix matK( max_elem_discr_kernel, max_elem_discr_kernel );
264 matK(0,0) = 0.05; matK(1,1) = 1.0; matK(2,2) = 0.5;
265 matK(0,1) = matK(1,0) = 0.2; matK(0,2) = matK(2,0) = 0.4; matK(1,2) = matK(2,1) = 0.6;
273 std::cout << endl <<
" ======================= MklKernel: ======================= " << std::endl;
274 cout << endl <<
"mkl_kernel.isAdaptive(0): " << mkl_kernel.
isAdaptive(0) << endl;
275 cout <<
"mkl_kernel.isAdaptive(1): " << mkl_kernel.
isAdaptive(1) << endl;
276 cout <<
"mkl_kernel.isAdaptive(2): " << mkl_kernel.
isAdaptive(2) << endl;
277 cout <<
"mkl_kernel.numberOfParameters(): " << mkl_kernel.
numberOfParameters() << endl;
278 cout <<
"mkl_kernel.parameterVector(): " << mkl_kernel.
parameterVector() << endl;
279 cout <<
"mkl_kernel.eval( dataset.element(0), dataset.element(1) ): " << mkl_kernel.
eval( dataset.
element(0), dataset.
element(1) ) << endl << endl;
284 new_params_4(0) = 1.0;
285 new_params_4(2) = 0.2;
289 cout <<
"mkl_kernel.isAdaptive(0): " << mkl_kernel.
isAdaptive(0) << endl;
290 cout <<
"mkl_kernel.isAdaptive(1): " << mkl_kernel.
isAdaptive(1) << endl;
291 cout <<
"mkl_kernel.isAdaptive(2): " << mkl_kernel.
isAdaptive(2) << endl;
292 cout <<
"mkl_kernel.numberOfParameters(): " << mkl_kernel.
numberOfParameters() << endl;
293 cout <<
"mkl_kernel.parameterVector(): " << mkl_kernel.
parameterVector() << endl;
294 cout <<
"mkl_kernel.eval( dataset.element(0), dataset.element(1) ): " << mkl_kernel.
eval( dataset.
element(0), dataset.
element(1) ) << endl << endl;
300 std::size_t num_dims = 9;
301 std::size_t num_points = 200;
302 std::vector<RealVector> input(num_points);
303 RealVector v(num_dims);
304 for ( std::size_t i=0; i<num_points; i++ ) {
305 for ( std::size_t j=0; j<num_dims; j++ )
317 std::vector< std::pair< std::size_t, std::size_t > > indices;
318 indices.push_back( std::make_pair( 0,3 ) );
319 indices.push_back( std::make_pair( 3,6 ) );
320 indices.push_back( std::make_pair( 6,9 ) );
324 normalizer.
train( scale, rand_data );
327 std::cout << endl <<
" ======================= Kernel normalization: ======================= " << std::endl;
329 std::cout << endl <<
"Done training. Factor is " << scale.
factor() << std::endl;
330 std::cout <<
"Mean = " << normalizer.
mean() << std::endl;
331 std::cout <<
"Trace = " << normalizer.
trace() << std::endl << std::endl;
333 double control = 0.0;
334 for ( std::size_t i=0; i<num_points; i++ ) {
335 control += scale.
eval(input[i], input[i]);
336 for ( std::size_t j=0; j<num_points; j++ ) {
337 control -= scale.
eval(input[i],input[j]) / num_points;
340 control /= num_points;
341 std::cout <<
"Resulting variance of scaled Kernel: " << control << std::endl << std::endl;
343 std::vector<AbstractKernelFunction<RealVector>* > kernels4;
344 kernels4.push_back( &unnormalized_kernel1 );
345 kernels4.push_back( &unnormalized_kernel2 );
346 kernels4.push_back( &scale );