/home/runner/work/charm/charm/src/ck-ldb/weakTest.C
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * The file is based on the Random-Forest-Matlab at |
3 | | * https://github.com/karpathy/Random-Forest-Matlab available under BSD license |
4 | | |
5 | | * Copyright (c) <2014>, <Andrej Karpathy> |
6 | | * All rights reserved. |
7 | | |
8 | | * Redistribution and use in source and binary forms, with or without |
9 | | * modification, are permitted provided that the following conditions are met: |
10 | | |
11 | | * 1. Redistributions of source code must retain the above copyright notice, this |
12 | | * list of conditions and the following disclaimer. |
13 | | * 2. Redistributions in binary form must reproduce the above copyright notice, |
14 | | * this list of conditions and the following disclaimer in the documentation |
15 | | * and/or other materials provided with the distribution. |
16 | | |
17 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
18 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
19 | | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
20 | | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
21 | | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
22 | | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
23 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
24 | | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
26 | | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | | |
28 | | * The views and conclusions contained in the software and documentation are those |
29 | | * of the authors and should not be interpreted as representing official policies, |
30 | | * purpose. |
31 | | */ |
32 | | |
33 | | #include "RandomForestModel.h" |
34 | | |
35 | | using namespace rfmodel; |
36 | | |
37 | 0 | double Model::weakTest(const DataMatrix& X) const { |
38 | | // X is NxD |
39 | 0 | double final_yhat = 1.0; |
40 | 0 | int N = X.num_rows; |
41 | | #ifdef DEBUG_RF |
42 | | if (classifierID == 2.0) |
43 | | printf("\nTesting on weakmodel r1[%d], r2[%d], w[%lf,%lf,%lf]\n", r1, r2, w[0], w[1], |
44 | | w[2]); |
45 | | #endif |
46 | | // Retaining pseudo code for decision stump classifier, distance based learner |
47 | | // as placeholders to be potentially implemented later |
48 | | #if 0 |
49 | | if (classifierID == 1) { |
50 | | // Decision stump classifier |
51 | | |
52 | | yhat = (double)X(:, model->r) < model->t; |
53 | | |
54 | | int* select_cols; |
55 | | select_cols = new int[1]; |
56 | | select_cols[0] = r; |
57 | | |
58 | | DataMatrix *yhat_op = MatrixOp::_subset_cols(X, num_rows, num_cols, select_cols, |
59 | | 1); |
60 | | yhat = yhat_op->data; |
61 | | final_yhat = MatrixOp::findIndicesLT(yhat,N, t); |
62 | | } else |
63 | | #endif |
64 | 0 | if (classifierID == 2) { |
65 | | // 2-D linear classifier stump |
66 | |
|
67 | 0 | int select_cols[2]; |
68 | 0 | select_cols[0] = r1 - 1; |
69 | 0 | select_cols[1] = r2 - 1; |
70 | |
|
71 | 0 | DataMatrix subset_arr(X.num_rows, 2); |
72 | 0 | X.subset_cols(select_cols, 2, subset_arr); |
73 | 0 | const DataMatrix ones_arr(N, 1, true); |
74 | 0 | DataMatrix combined_arr(subset_arr.num_rows, subset_arr.num_cols + ones_arr.num_rows); |
75 | 0 | combined_arr.combine((const DataMatrix)subset_arr, ones_arr); |
76 | 0 | const DataMatrix w_arr(w, 3, 1); |
77 | 0 | DataMatrix mm(combined_arr.num_rows, w_arr.num_cols); |
78 | 0 | mm.matrix_multiply(combined_arr, w_arr); |
79 | |
|
80 | 0 | double zero = 0.0; |
81 | 0 | DataMatrix yhat(mm.num_rows, mm.num_cols); |
82 | 0 | mm.findIndicesLT(zero, yhat); |
83 | 0 | if (yhat.data.size() == 0) |
84 | 0 | final_yhat = 0.0; |
85 | 0 | else |
86 | 0 | final_yhat = yhat.data[0] + 1; |
87 | 0 | } |
88 | |
|
89 | 0 | return final_yhat; |
90 | 0 | } |