MCPcopy Create free account
hub / github.com/Xtra-Computing/thundersvm / KernelMatrix

Method KernelMatrix

src/thundersvm/kernelmatrix.cpp:9–71  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7
8using namespace svm_kernel;
9KernelMatrix::KernelMatrix(const DataSet::node2d &instances, SvmParam param) {
10 n_instances_ = instances.size();
11 n_features_ = 0;
12 this->param = param;
13
14 //three arrays for csr representation
15 vector<kernel_type> csr_val;
16 vector<int> csr_col_ind;//index of each value of all the instances
17 vector<int> csr_row_ptr(1, 0);//the start positions of the instances
18
19 vector<kernel_type> csr_self_dot;
20 for (int i = 0; i < n_instances_; ++i) {//convert libsvm format to csr format
21 float_type self_dot = 0;
22 for (int j = 0; j < instances[i].size(); ++j) {
23 csr_val.push_back(instances[i][j].value);
24 self_dot += instances[i][j].value * instances[i][j].value;
25 csr_col_ind.push_back(instances[i][j].index);//libSVM data format is one-based, convert to zero-based
26 if (instances[i][j].index > n_features_) n_features_ = instances[i][j].index;
27 }
28 csr_row_ptr.push_back(csr_row_ptr.back() + instances[i].size());
29 csr_self_dot.push_back(self_dot);
30 }
31 n_features_++;
32
33 //three arrays (on GPU/CPU) for csr representation
34 val_.resize(csr_val.size());
35 col_ind_.resize(csr_col_ind.size());
36 row_ptr_.resize(csr_row_ptr.size());
37 //copy data to the three arrays
38 val_.copy_from(csr_val.data(), val_.size());
39 col_ind_.copy_from(csr_col_ind.data(), col_ind_.size());
40 row_ptr_.copy_from(csr_row_ptr.data(), row_ptr_.size());
41
42 self_dot_.resize(n_instances_);
43 self_dot_.copy_from(csr_self_dot.data(), self_dot_.size());
44
45 nnz_ = csr_val.size();//number of nonzero
46
47 //pre-compute diagonal elements
48
49 diag_.resize(n_instances_);
50 switch (param.kernel_type) {
51 case SvmParam::RBF:
52 case SvmParam::PRECOMPUTED://precomputed uses rbf as default
53 for (int i = 0; i < n_instances_; ++i) {
54 diag_.host_data()[i] = 1;//rbf kernel
55 }
56 break;
57 case SvmParam::LINEAR:
58 diag_.copy_from(self_dot_);
59 break;
60 case SvmParam::POLY:
61 diag_.copy_from(self_dot_);
62 poly_kernel(diag_, param.gamma, param.coef0, param.degree, diag_.size());
63 break;
64 case SvmParam::SIGMOID:
65 diag_.copy_from(self_dot_);
66 sigmoid_kernel(diag_, param.gamma, param.coef0, diag_.size());

Callers

nothing calls this directly

Calls 7

poly_kernelFunction · 0.85
sigmoid_kernelFunction · 0.85
resizeMethod · 0.80
copy_fromMethod · 0.80
dataMethod · 0.80
sizeMethod · 0.45
host_dataMethod · 0.45

Tested by

no test coverage detected