| 325 | |
| 326 | template <typename T> |
| 327 | void load_impl( |
| 328 | const kernel_type& kernel_, |
| 329 | const T& basis_samples |
| 330 | ) |
| 331 | { |
| 332 | // make sure requires clause is not broken |
| 333 | DLIB_ASSERT(basis_samples.size() > 0 && is_vector(basis_samples), |
| 334 | "\tvoid empirical_kernel_map::load(kernel,basis_samples)" |
| 335 | << "\n\t You have to give a non-empty set of basis_samples and it must be a vector" |
| 336 | << "\n\t basis_samples.size(): " << basis_samples.size() |
| 337 | << "\n\t is_vector(basis_samples): " << is_vector(basis_samples) |
| 338 | << "\n\t this: " << this |
| 339 | ); |
| 340 | |
| 341 | // clear out the weights before we begin. This way if an exception throws |
| 342 | // this object will already be in the right state. |
| 343 | weights.set_size(0,0); |
| 344 | kernel = kernel_; |
| 345 | basis.clear(); |
| 346 | basis.reserve(basis_samples.size()); |
| 347 | |
| 348 | // find out the value of the largest norm of the elements in basis_samples. |
| 349 | const scalar_type max_norm = max(diag(kernel_matrix(kernel, basis_samples))); |
| 350 | // we will consider anything less than or equal to this number to be 0 |
| 351 | const scalar_type eps = max_norm*100*std::numeric_limits<scalar_type>::epsilon(); |
| 352 | |
| 353 | // Copy all the basis_samples into basis but make sure we don't copy any samples |
| 354 | // that have length 0 |
| 355 | for (long i = 0; i < basis_samples.size(); ++i) |
| 356 | { |
| 357 | const scalar_type norm = kernel(basis_samples(i), basis_samples(i)); |
| 358 | if (norm > eps) |
| 359 | { |
| 360 | basis.push_back(basis_samples(i)); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | if (basis.size() == 0) |
| 365 | { |
| 366 | clear(); |
| 367 | throw empirical_kernel_map_error("All basis_samples given to empirical_kernel_map::load() were zero vectors"); |
| 368 | } |
| 369 | |
| 370 | matrix<scalar_type,0,0,mem_manager_type> K(kernel_matrix(kernel, basis)), U,W,V; |
| 371 | |
| 372 | if (svd2(false,true,K,U,W,V)) |
| 373 | { |
| 374 | clear(); |
| 375 | throw empirical_kernel_map_error("While loading empirical_kernel_map with data, SVD failed to converge."); |
| 376 | } |
| 377 | |
| 378 | |
| 379 | // now count how many elements of W are non-zero |
| 380 | const long num_not_zero = static_cast<long>(sum(W>eps)); |
| 381 | |
| 382 | // Really, this should never happen. But I'm checking for good measure. |
| 383 | if (num_not_zero == 0) |
| 384 | { |
nothing calls this directly
no test coverage detected