Create a high dimensions test case and fix the build error

This commit is contained in:
Sangyeon
2019-09-28 23:37:51 +09:00
parent be40024ed6
commit 259a2afd8d
7 changed files with 548 additions and 8 deletions
-3
View File
@@ -1,16 +1,13 @@
### mlpack ?.?.?
###### ????-??-??
<<<<<<< HEAD
* Enforce CMake version check for ensmallen (#2032).
* Fix CMake check for Armadillo version (#2029).
### mlpack 3.2.0
###### 2019-09-25
=======
* Fix some potential infinity errors in Naive Bayes Classifier (#2022).
>>>>>>> a16d55f... Edit according to comments of Marcus and Ryan.
* Fix occasionally-failing RADICAL test (#1924).
* Fix gcc 9 OpenMP compilation issue (#1970).
@@ -272,8 +272,9 @@ void NaiveBayesClassifier<ModelMatType>::Classify(
// To prevent underflow in log of sum of exp of x operation (where x is a
// small negative value), we use logsumexp(x - max(x)) + max(x).
const double maxValue = arma::max(logLikelihoods);
probabilities = exp(logLikelihoods - log(arma::accu(exp(logLikelihoods -
maxValue))) + maxValue); // log(exp(value)) == value.
const double logProbX = log(arma::accu(exp(logLikelihoods - maxValue))) +
maxValue;
probabilities = exp(logLikelihoods - logProbX); // log(exp(value)) == value.
arma::uword maxIndex = 0;
logLikelihoods.max(maxIndex);
@@ -324,7 +325,7 @@ void NaiveBayesClassifier<ModelMatType>::Classify(
LogLikelihood(data, logLikelihoods);
predictionProbs.set_size(arma::size(logLikelihoods));
double maxValue;
double maxValue, logProbX;
for (size_t j = 0; j < data.n_cols; ++j)
{
// The LogLikelihood() gives us the unnormalized log likelihood which is
@@ -332,8 +333,9 @@ void NaiveBayesClassifier<ModelMatType>::Classify(
// Besides, to prevent underflow in log of sum of exp of x operation (where
// x is a small negative value), we use logsumexp(x - max(x)) + max(x).
maxValue = arma::max(logLikelihoods.col(j));
predictionProbs.col(j) = arma::exp(logLikelihoods.col(j) -
log(arma::accu(exp(logLikelihoods.col(j) - maxValue))) + maxValue);
logProbX = log(arma::accu(exp(logLikelihoods.col(j) -
maxValue))) + maxValue;
predictionProbs.col(j) = arma::exp(logLikelihoods.col(j) - logProbX);
}
// Now calculate maximum probabilities for each point.
File diff suppressed because one or more lines are too long
@@ -0,0 +1,50 @@
3
2
0
0
0
1
2
3
3
2
4
2
1
2
3
1
2
4
4
1
3
0
2
0
0
2
0
1
3
3
2
2
2
3
3
3
3
3
0
0
4
3
3
0
3
2
3
2
1
1
1 3
2 2
3 0
4 0
5 0
6 1
7 2
8 3
9 3
10 2
11 4
12 2
13 1
14 2
15 3
16 1
17 2
18 4
19 4
20 1
21 3
22 0
23 2
24 0
25 0
26 2
27 0
28 1
29 3
30 3
31 2
32 2
33 2
34 3
35 3
36 3
37 3
38 3
39 0
40 0
41 4
42 3
43 3
44 0
45 3
46 2
47 3
48 2
49 1
50 1
File diff suppressed because one or more lines are too long
@@ -0,0 +1,200 @@
1
4
2
2
1
0
1
0
0
4
0
4
3
4
3
2
4
2
2
2
4
1
2
1
3
0
4
1
4
4
4
0
3
4
3
1
3
2
3
0
4
1
4
1
4
2
1
4
2
1
2
0
2
2
4
2
0
2
0
3
3
3
0
2
1
4
3
1
2
2
4
0
1
3
4
4
4
2
4
2
3
4
4
3
2
3
3
4
3
4
2
4
0
3
3
1
3
4
2
1
2
3
1
3
3
0
4
0
0
3
2
1
0
3
2
1
0
0
1
0
2
2
4
2
3
1
4
4
2
3
4
0
2
2
0
4
0
3
1
4
4
2
0
0
0
0
3
4
3
2
0
4
3
3
4
0
3
1
3
4
3
2
2
4
0
0
0
0
1
4
0
3
4
3
1
4
0
1
4
3
2
1
3
2
4
3
2
0
1
4
2
0
2
3
0
0
2
1
3
1
1 1
2 4
3 2
4 2
5 1
6 0
7 1
8 0
9 0
10 4
11 0
12 4
13 3
14 4
15 3
16 2
17 4
18 2
19 2
20 2
21 4
22 1
23 2
24 1
25 3
26 0
27 4
28 1
29 4
30 4
31 4
32 0
33 3
34 4
35 3
36 1
37 3
38 2
39 3
40 0
41 4
42 1
43 4
44 1
45 4
46 2
47 1
48 4
49 2
50 1
51 2
52 0
53 2
54 2
55 4
56 2
57 0
58 2
59 0
60 3
61 3
62 3
63 0
64 2
65 1
66 4
67 3
68 1
69 2
70 2
71 4
72 0
73 1
74 3
75 4
76 4
77 4
78 2
79 4
80 2
81 3
82 4
83 4
84 3
85 2
86 3
87 3
88 4
89 3
90 4
91 2
92 4
93 0
94 3
95 3
96 1
97 3
98 4
99 2
100 1
101 2
102 3
103 1
104 3
105 3
106 0
107 4
108 0
109 0
110 3
111 2
112 1
113 0
114 3
115 2
116 1
117 0
118 0
119 1
120 0
121 2
122 2
123 4
124 2
125 3
126 1
127 4
128 4
129 2
130 3
131 4
132 0
133 2
134 2
135 0
136 4
137 0
138 3
139 1
140 4
141 4
142 2
143 0
144 0
145 0
146 0
147 3
148 4
149 3
150 2
151 0
152 4
153 3
154 3
155 4
156 0
157 3
158 1
159 3
160 4
161 3
162 2
163 2
164 4
165 0
166 0
167 0
168 0
169 1
170 4
171 0
172 3
173 4
174 3
175 1
176 4
177 0
178 1
179 4
180 3
181 2
182 1
183 3
184 2
185 4
186 3
187 2
188 0
189 1
190 4
191 2
192 0
193 2
194 3
195 0
196 0
197 2
198 1
199 3
200 1
+41
View File
@@ -314,4 +314,45 @@ BOOST_AUTO_TEST_CASE(SeparateTrainIndividualIncrementalTest)
}
}
/**
* Check if NaiveBayesClassifier::Classify() works properly for a high
* dimension datasets.
*/
BOOST_AUTO_TEST_CASE(NaiveBayesClassifierHighDimensionsTest)
{
// Set file names of dataset of training and test.
// The training dataset has 5 classes and each class has 1,000 dimensions.
const char* trainFilename = "nbc_high_dim_train.csv";
const char* testFilename = "nbc_high_dim_test.csv";
const char* trainLabelsFileName = "nbc_high_dim_train_labels.csv";
const char* testLabelsFilename = "nbc_high_dim_test_labels.csv";
size_t classes = 5;
// Create variables for training and assign data to them.
arma::mat trainData;
arma::Row<size_t> trainLabels;
data::Load(trainFilename, trainData, true);
data::Load(trainLabelsFileName, trainLabels, true);
// Initialize and train a NBC model.
NaiveBayesClassifier<> nbcTest(trainData, trainLabels, classes);
// Create variables for test and assign data to them.
arma::mat testData, calcProbs;
arma::Row<size_t> testLabels;
arma::Row<size_t> calcVec;
data::Load(testFilename, testData, true);
data::Load(testLabelsFilename, testLabels, true);
// Classify observations in the test dataset. To use Classify() method with
// a parameter for probabilities of predictions, we pass 'calcProbs' to the
// method.
nbcTest.Classify(testData, calcVec, calcProbs);
// Check the results.
for (size_t i = 0; i < calcVec.n_cols; i++)
BOOST_REQUIRE_EQUAL(calcVec(i), testLabels(i));
}
BOOST_AUTO_TEST_SUITE_END();