/home/runner/work/charm/charm/src/ck-ldb/treeTest.C
Line | Count | Source |
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 | | // Test a tree |
38 | 3.20k | void TreeModel::treeTest(const DataMatrix& X, std::vector<double>& Ysoft) const { |
39 | | //[N, D]= size(X); //Returns number of rows and columns in matrix X |
40 | 3.20k | int N = X.num_rows; |
41 | 3.20k | int D = X.num_cols; |
42 | | |
43 | 3.20k | int nd = (int)pow(2, DEPTH) - 1; |
44 | | |
45 | 3.20k | int numInternals = (nd + 1) / 2 - 1; |
46 | | |
47 | | // Propagate data down the tree using weak classifiers at each node |
48 | 3.20k | std::vector<double> dataix((size_t)N * nd, 0.0); |
49 | | |
50 | 819k | for (int n = 1; n <= numInternals; n++) { |
51 | 816k | DataMatrix* reld; |
52 | 816k | DataMatrix* Xrel; |
53 | | |
54 | | // get relevant data at this node |
55 | 816k | if (n == 1) { |
56 | 3.20k | reld = new DataMatrix(N, 1, true); //=1; |
57 | 3.20k | Xrel = new DataMatrix(X.data, N, D); |
58 | 812k | } else { |
59 | 812k | int select_cols[1]; |
60 | 812k | select_cols[0] = n - 1; |
61 | 812k | DataMatrix dataix_array(dataix, N, nd); |
62 | 812k | DataMatrix reld_resp(dataix_array.num_rows, 1); |
63 | 812k | dataix_array.subset_cols(select_cols, 1, reld_resp); |
64 | 812k | reld = new DataMatrix(reld_resp.num_rows, reld_resp.num_cols); |
65 | 812k | reld_resp.findValue(1, *reld); |
66 | 812k | int* select = new int[reld->num_rows * reld->num_cols]; |
67 | 812k | int c = 0; |
68 | 1.62M | for (int i = 0; i < reld->num_rows * reld->num_cols; i++) |
69 | 812k | if (reld->data[i] == 1) select[c++] = i; |
70 | 812k | Xrel = new DataMatrix(c, X.num_cols); |
71 | 812k | X.subset_rows(select, c, *Xrel); |
72 | 812k | delete [] select; |
73 | 812k | } |
74 | | |
75 | 816k | if (Xrel->num_rows * Xrel->num_cols == 0) { |
76 | 790k | delete reld; |
77 | 790k | delete Xrel; |
78 | 790k | continue; // empty branch |
79 | 790k | } |
80 | | |
81 | 25.6k | double yhat = weakModels[n - 1].weakTest(*Xrel); |
82 | | |
83 | 51.2k | for (int i = 0; i < reld->num_rows * reld->num_cols; i++) |
84 | 25.6k | dataix[(((int)reld->data[i] - 1) * nd + 2 * n) - 1] = yhat; |
85 | | |
86 | 51.2k | for (int i = 0; i < reld->num_rows * reld->num_cols; i++) |
87 | 25.6k | dataix[(((int)reld->data[i] - 1) * nd + 2 * n + 1) - 1] = 1.0 - yhat; |
88 | | |
89 | 25.6k | delete reld; |
90 | 25.6k | delete Xrel; |
91 | 25.6k | } |
92 | | |
93 | | // Go over leafs and assign class probabilities |
94 | 822k | for (int n = ((nd + 1) / 2); n <= nd; n++) { |
95 | 819k | int select_cols[1]; |
96 | 819k | select_cols[0] = n - 1; |
97 | | |
98 | 819k | DataMatrix dataix_array(dataix, N, nd); |
99 | 819k | DataMatrix dataix_subset(dataix_array.num_rows, 1); |
100 | 819k | dataix_array.subset_cols(select_cols, 1, dataix_subset); |
101 | | |
102 | 819k | DataMatrix ff(dataix_subset.num_rows, dataix_subset.num_cols); |
103 | 819k | dataix_subset.findIndicesNE(0.0, ff); |
104 | | |
105 | 819k | int select_rows[1]; |
106 | 819k | select_rows[0] = n - (nd + 1) / 2 + 1 - 1; // c indexing |
107 | 819k | DataMatrix leafdist_array(leafdist, l_X, l_D); |
108 | 819k | DataMatrix hc(1, leafdist_array.num_cols); |
109 | 819k | leafdist_array.subset_rows(select_rows, 1, hc); |
110 | | |
111 | 819k | if (ff.num_rows * ff.num_cols > 0) { |
112 | 3.20k | DataMatrix rep_matrix(hc.num_rows * ff.num_rows * ff.num_cols, hc.num_cols); |
113 | 3.20k | hc.repmat(ff.num_rows * ff.num_cols, 1, rep_matrix); |
114 | 3.20k | std::copy(rep_matrix.data.begin(), rep_matrix.data.end(), Ysoft.begin()); |
115 | 3.20k | } |
116 | 819k | } |
117 | 3.20k | } |