| 67 | } |
| 68 | |
| 69 | class KernelMatrixTest : public ::testing::Test { |
| 70 | DataSet dataSet; |
| 71 | unsigned n_rows = 10; |
| 72 | SyncArray<int> rows; |
| 73 | DataSet::node2d instances; |
| 74 | SyncArray<kernel_type> kernel_rows; |
| 75 | protected: |
| 76 | SvmParam param; |
| 77 | |
| 78 | void SetUp() override { |
| 79 | param = SvmParam(); |
| 80 | dataSet.load_from_file(DATASET_DIR "test_dataset.txt"); |
| 81 | rows.resize(n_rows); |
| 82 | kernel_rows.resize(n_rows * dataSet.n_instances()); |
| 83 | for (unsigned i = 0; i < n_rows; ++i) { |
| 84 | rows.host_data()[i] = i * 3 + 4; |
| 85 | } |
| 86 | instances = DataSet::node2d(dataSet.instances().begin(), dataSet.instances().begin() + n_rows); |
| 87 | } |
| 88 | |
| 89 | void TearDown() override { |
| 90 | KernelMatrix *kernelMatrix = new KernelMatrix(dataSet.instances(), param); |
| 91 | kernelMatrix->get_rows(rows, kernel_rows); |
| 92 | |
| 93 | //test diagonal elements |
| 94 | for (unsigned i = 0; i < kernelMatrix->diag().size(); ++i) { |
| 95 | float_type cpu_kernel = get_cpu_kernel(dataSet.instances(), i, i, param); |
| 96 | float_type gpu_kernel = kernelMatrix->diag().host_data()[i]; |
| 97 | EXPECT_NEAR(gpu_kernel, cpu_kernel, 1e-4) << i; |
| 98 | } |
| 99 | |
| 100 | //test get rows with index |
| 101 | for (unsigned i = 0; i < rows.size(); ++i) { |
| 102 | for (unsigned j = 0; j < kernelMatrix->n_instances(); ++j) { |
| 103 | float_type gpu_kernel = kernel_rows.host_data()[i * kernelMatrix->n_instances() + j]; |
| 104 | float_type cpu_kernel = get_cpu_kernel(dataSet.instances(), rows.host_data()[i], j, param); |
| 105 | EXPECT_NEAR(gpu_kernel, cpu_kernel, 1e-4) << rows.host_data()[i] << "," << j; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | //test get rows with instances |
| 110 | kernelMatrix->get_rows(instances, kernel_rows); |
| 111 | |
| 112 | for (unsigned i = 0; i < n_rows; ++i) { |
| 113 | for (unsigned j = 0; j < kernelMatrix->n_instances(); ++j) { |
| 114 | float_type gpu_kernel = kernel_rows.host_data()[i * kernelMatrix->n_instances() + j]; |
| 115 | float_type cpu_kernel = get_cpu_kernel(dataSet.instances(), i, j, param); |
| 116 | EXPECT_NEAR(gpu_kernel, cpu_kernel, 1e-4) << i << "," << j; |
| 117 | } |
| 118 | } |
| 119 | delete kernelMatrix; |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | TEST_F(KernelMatrixTest, test_rbf) { |
| 124 | param.gamma = 0.5; |
nothing calls this directly
no outgoing calls
no test coverage detected