21 using namespace shogun;
24 using namespace Eigen;
27 void CCustomKernel::init()
39 "Subset stack of columns", MS_NOT_AVAILABLE);
40 SG_ADD(&m_free_km, "free_km", "Whether kernel matrix should be freed in "
41 "destructor", MS_NOT_AVAILABLE);
42 SG_ADD(&m_is_symmetric, "is_symmetric", "Whether kernel matrix is symmetric",
44 SG_ADD(&kmatrix, "kmatrix", "Kernel matrix.", MS_NOT_AVAILABLE);
45 SG_ADD(&upper_diagonal, "upper_diagonal", "Upper diagonal", MS_NOT_AVAILABLE);
49 new
SGParamInfo("free_km", CT_SCALAR, ST_NONE, PT_BOOL, 1),
55 new SGParamInfo("row_subset_stack", CT_SCALAR, ST_NONE, PT_SGOBJECT, 1),
61 new SGParamInfo("col_subset_stack", CT_SCALAR, ST_NONE, PT_SGOBJECT, 1),
64 m_parameter_map->finalize_map();
68 :
CKernel(10), kmatrix(), upper_diagonal(false)
96 :
CKernel(10), upper_diagonal(false)
105 :
CKernel(10), upper_diagonal(false)
138 REQUIRE(l,
"CFeatures l should not be NULL\n")
139 REQUIRE(r,
"CFeatures r should not be NULL\n")
143 "Different FeatureClass: l is %d, r is %d\n",
146 "Different FeatureType: l is %d, r is %d\n",
184 index_t block_size,
bool no_diag)
190 SG_INFO(
"Row/col subsets initialized! Falling back to "
191 "CKernel::sum_symmetric_block (slower)!\n");
198 "Invalid block begin index (%d, %d)!\n", block_begin, block_begin)
200 "Invalid block size (%d) at starting index (%d, %d)! "
201 "Please use smaller blocks!", block_size, block_begin, block_begin)
202 REQUIRE(block_size>=1,
"Invalid block size (%d)!\n", block_size)
205 const MatrixXf& k_m_block=k_m.block(block_begin, block_begin, block_size,
210 const MatrixXf& k_m_upper=k_m_block.triangularView<StrictlyUpper>();
218 sum+=k_m_block.diagonal().sum();
227 index_t block_size_col,
bool no_diag)
233 SG_INFO(
"Row/col subsets initialized! Falling back to "
234 "CKernel::sum_block (slower)!\n");
236 block_size_row, block_size_col, no_diag);
242 "Invalid block begin index (%d, %d)!\n",
243 block_begin_row, block_begin_col)
246 "Invalid block size (%d, %d) at starting index (%d, %d)! "
247 "Please use smaller blocks!", block_size_row, block_size_col,
248 block_begin_row, block_begin_col)
249 REQUIRE(block_size_row>=1 && block_size_col>=1,
250 "Invalid block size (%d, %d)!\n", block_size_row, block_size_col)
253 if (no_diag && block_size_row!=block_size_col)
255 SG_WARNING(
"Not removing the main diagonal since block is not square!\n");
260 const MatrixXf& k_m_block=k_m.block(block_begin_row, block_begin_col,
261 block_size_row, block_size_col);
267 sum-=k_m_block.diagonal().sum();
275 block_begin,
index_t block_size,
bool no_diag)
281 SG_INFO(
"Row/col subsets initialized! Falling back to "
282 "CKernel::row_wise_sum_symmetric_block (slower)!\n");
290 "Invalid block begin index (%d, %d)!\n", block_begin, block_begin)
292 "Invalid block size (%d) at starting index (%d, %d)! "
293 "Please use smaller blocks!", block_size, block_begin, block_begin)
294 REQUIRE(block_size>=1,
"Invalid block size (%d)!\n", block_size)
301 const MatrixXf& k_m_block=k_m.block(block_begin, block_begin, block_size,
305 for (
index_t i=0; i<block_size; ++i)
306 row_sum[i]=k_m_block.col(i).
sum();
311 const VectorXf& diag=k_m_block.diagonal();
312 for (
index_t i=0; i<block_size; ++i)
328 SG_INFO(
"Row/col subsets initialized! Falling back to "
329 "CKernel::row_wise_sum_squared_sum_symmetric_block (slower)!\n");
331 block_size, no_diag);
337 "Invalid block begin index (%d, %d)!\n", block_begin, block_begin)
339 "Invalid block size (%d) at starting index (%d, %d)! "
340 "Please use smaller blocks!", block_size, block_begin, block_begin)
341 REQUIRE(block_size>=1,
"Invalid block size (%d)!\n", block_size)
350 const MatrixXf& k_m_block=k_m.block(block_begin, block_begin, block_size,
354 for (
index_t i=0; i<block_size; ++i)
355 row_sum(i, 0)=k_m_block.col(i).sum();
360 for (
index_t i=0; i<block_size; ++i)
365 for (
index_t j=i+1; j<block_size; ++j)
374 const VectorXf& diag=k_m_block.diagonal();
377 for (
index_t i=0; i<block_size; ++i)
378 row_sum(i, 0)-=diag[i];
382 for (
index_t i=0; i<block_size; ++i)
383 row_sum(i, 1)+=diag[i]*diag[i];
393 index_t block_size_col,
bool no_diag)
399 SG_INFO(
"Row/col subsets initialized! Falling back to "
400 "CKernel::row_col_wise_sum_block (slower)!\n");
402 block_size_row, block_size_col, no_diag);
408 "Invalid block begin index (%d, %d)!\n",
409 block_begin_row, block_begin_col)
412 "Invalid block size (%d, %d) at starting index (%d, %d)! "
413 "Please use smaller blocks!", block_size_row, block_size_col,
414 block_begin_row, block_begin_col)
415 REQUIRE(block_size_row>=1 && block_size_col>=1,
416 "Invalid block size (%d, %d)!\n", block_size_row, block_size_col)
419 if (no_diag && block_size_row!=block_size_col)
421 SG_WARNING(
"Not removing the main diagonal since block is not square!\n");
432 const MatrixXf& k_m_block=k_m.block(block_begin_row, block_begin_col,
433 block_size_row, block_size_col);
436 for (
index_t i=0; i<block_size_row; ++i)
437 sum[i]=k_m_block.row(i).
sum();
440 for (
index_t i=0; i<block_size_col; ++i)
441 sum[i+block_size_row]=k_m_block.col(i).
sum();
446 const VectorXf& diag=k_m_block.diagonal();
447 for (
index_t i=0; i<diag.rows(); ++i)
450 sum[i+block_size_row]-=diag[i];
458 #endif // HAVE_EIGEN3
Class that holds informations about a certain parameter of an CSGObject. Contains name...
virtual void add_row_subset(SGVector< index_t > subset)
SGMatrix< float32_t > kmatrix
int32_t num_rhs
number of feature vectors on right hand side
The Custom Kernel allows for custom user provided kernel matrices.
The class IndexFeatures implements features that contain the index of the features. This features used in the CCustomKernel::init to make the subset of the kernel matrix. Initial CIndexFeature of row_idx and col_idx, pass them to the CCustomKernel::init(row_idx, col_idx), then use CCustomKernel::get_kernel_matrix() will get the sub kernel matrix specified by the row_idx and col_idx.
virtual SGMatrix< float64_t > row_wise_sum_squared_sum_symmetric_block(index_t block_begin, index_t block_size, bool no_diag=true)
SGMatrix< float32_t > get_float32_kernel_matrix()
virtual SGVector< float64_t > row_wise_sum_symmetric_block(index_t block_begin, index_t block_size, bool no_diag=true)
static T sum(T *vec, int32_t len)
return sum(vec)
virtual float64_t sum_block(index_t block_begin_row, index_t block_begin_col, index_t block_size_row, index_t block_size_col, bool no_diag=false)
bool get_lhs_equals_rhs()
CSubsetStack * m_col_subset_stack
virtual int32_t get_num_vectors() const =0
virtual float64_t sum_symmetric_block(index_t block_begin, index_t block_size, bool no_diag=true)
virtual SGVector< float64_t > row_col_wise_sum_block(index_t block_begin_row, index_t block_begin_col, index_t block_size_row, index_t block_size_col, bool no_diag=false)
virtual void remove_all_row_subsets()
SGMatrix< float64_t > get_kernel_matrix()
class to add subset support to another class. A CSubsetStackStack instance should be added and wrappe...
virtual SGMatrix< float64_t > row_wise_sum_squared_sum_symmetric_block(index_t block_begin, index_t block_size, bool no_diag=true)
virtual void remove_col_subset()
SGVector< index_t > get_feature_index()
virtual void remove_row_subset()
virtual void add_col_subset(SGVector< index_t > subset)
virtual void add_subset(SGVector< index_t > subset)
Class SGObject is the base class of all shogun objects.
virtual void row_subset_changed_post()
virtual void remove_all_subsets()
virtual SGVector< float64_t > row_col_wise_sum_block(index_t block_begin_row, index_t block_begin_col, index_t block_size_row, index_t block_size_col, bool no_diag=false)
virtual void add_subset_in_place(SGVector< index_t > subset)
virtual float64_t sum_symmetric_block(index_t block_begin, index_t block_size, bool no_diag=true)
bool set_full_kernel_matrix_from_full(SGMatrix< float32_t > full_kernel_matrix, bool check_symmetry=false)
virtual SGVector< float64_t > row_wise_sum_symmetric_block(index_t block_begin, index_t block_size, bool no_diag=true)
virtual EFeatureClass get_feature_class() const =0
int32_t num_lhs
number of feature vectors on left hand side
virtual bool dummy_init(int32_t rows, int32_t cols)
virtual void remove_all_col_subsets()
void set_const(T const_elem)
virtual bool init_normalizer()
CFeatures * rhs
feature vectors to occur on right hand side
The class DummyFeatures implements features that only know the number of feature objects (but don't a...
virtual EKernelType get_kernel_type()=0
CFeatures * lhs
feature vectors to occur on left hand side
virtual float64_t sum_block(index_t block_begin_row, index_t block_begin_col, index_t block_size_row, index_t block_size_col, bool no_diag=false)
The class Features is the base class of all feature objects.
virtual void col_subset_changed_post()
virtual bool has_subsets() const
virtual void remove_subset()
void set_const(T const_elem)
virtual void add_col_subset_in_place(SGVector< index_t > subset)
CSubsetStack * m_row_subset_stack
virtual void add_row_subset_in_place(SGVector< index_t > subset)
virtual EFeatureType get_feature_type() const =0