-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mnist_training.cpp
More file actions
303 lines (239 loc) · 9.85 KB
/
test_mnist_training.cpp
File metadata and controls
303 lines (239 loc) · 9.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include "allheader.h"
#include "network.h"
#include "mnist_loader.h"
using namespace std;
using namespace ml;
using namespace Utility;
/**
* MNIST Training Test Suite
*
* Tests the MNIST training functionality including:
* - Network creation and initialization
* - Batch training
* - Loss computation
* - Accuracy evaluation
* - Cross-entropy loss
*/
// Helper to check approximate equality
template <typename T>
bool approxEqual(T a, T b, T epsilon = 0.01) {
return std::abs(a - b) < epsilon;
}
void test_network_creation() {
BEGIN_TESTS("MNIST Network Creation");
typedef double T;
Network<T>* network = new Network<T>();
ILayer<T>* input = new Layer<T>(784, "Input", ActivationType::RELU);
ILayer<T>* hidden1 = new Layer<T>(128, "Hidden1", ActivationType::RELU);
ILayer<T>* output = new Layer<T>(10, "Output", ActivationType::SIGMOID);
network->setInputLayer(input);
network->connect(input, hidden1);
network->connect(hidden1, output);
network->setOutputLayer(output);
network->setOptimizerType(OptimizerType::ADAM);
network->setLossType(LossType::CROSS_ENTROPY);
network->init();
cout << "✓ Network created successfully" << endl;
cout << "✓ Optimizer set to Adam" << endl;
cout << "✓ Loss set to Cross-Entropy" << endl;
// Test forward pass with random input
ml::Mat<T> testInput(1, 784, 0.5);
ml::Mat<T> output_result = network->feed(testInput);
cout << "✓ Forward pass works" << endl;
cout << " Output size: (" << output_result.size().cy << ", " << output_result.size().cx << ")" << endl;
if (output_result.size().cy == 1 && output_result.size().cx == 10) {
cout << "✓ Output dimensions correct" << endl;
} else {
cout << "✗ Output dimensions incorrect" << endl;
}
delete network;
}
void test_batch_training() {
BEGIN_TESTS("Batch Training Functionality");
typedef double T;
// Create small network for testing
Network<T>* network = new Network<T>();
ILayer<T>* input = new Layer<T>(784, "Input", ActivationType::RELU);
ILayer<T>* hidden = new Layer<T>(64, "Hidden", ActivationType::RELU);
ILayer<T>* output = new Layer<T>(10, "Output", ActivationType::SIGMOID);
network->setInputLayer(input);
network->connect(input, hidden);
network->connect(hidden, output);
network->setOutputLayer(output);
network->setOptimizerType(OptimizerType::ADAM);
network->setLossType(LossType::CROSS_ENTROPY);
network->init();
// Create synthetic batch (batch_size=4, input_size=784)
int batchSize = 4;
ml::Mat<T> batchInputs(batchSize, 784, 0);
ml::Mat<T> batchTargets(batchSize, 10, 0);
// Fill with simple patterns
for (int i = 0; i < batchSize; i++) {
// Simple pattern for each sample
for (int j = 0; j < 784; j++) {
batchInputs.setAt(i, j, (T)(i * 0.1 + j * 0.001));
}
// One-hot target
int targetClass = i % 10;
batchTargets.setAt(i, targetClass, 1.0);
}
// Get initial loss
T initialLoss = network->evaluateLoss(batchInputs, batchTargets);
cout << "Initial loss: " << std::fixed << std::setprecision(4) << initialLoss << endl;
// Train for a few iterations
T learningRate = 0.01;
for (int iter = 0; iter < 10; iter++) {
network->trainBatch(batchInputs, batchTargets, learningRate);
}
// Get final loss
T finalLoss = network->evaluateLoss(batchInputs, batchTargets);
cout << "Final loss after 10 iterations: " << std::setprecision(4) << finalLoss << endl;
if (finalLoss < initialLoss) {
cout << "✓ Loss decreased during training" << endl;
} else {
cout << "✗ Loss did not decrease" << endl;
}
delete network;
}
void test_accuracy_computation() {
BEGIN_TESTS("Accuracy Computation");
typedef double T;
// Create test predictions and targets
ml::Mat<T> predictions(5, 10, 0);
ml::Mat<T> targets(5, 10, 0);
// Sample 0: pred=0, target=0 ✓
predictions.setAt(0, 0, 0.9);
targets.setAt(0, 0, 1.0);
// Sample 1: pred=1, target=1 ✓
predictions.setAt(1, 1, 0.8);
targets.setAt(1, 1, 1.0);
// Sample 2: pred=2, target=3 ✗
predictions.setAt(2, 2, 0.7);
targets.setAt(2, 3, 1.0);
// Sample 3: pred=4, target=4 ✓
predictions.setAt(3, 4, 0.85);
targets.setAt(3, 4, 1.0);
// Sample 4: pred=5, target=5 ✓
predictions.setAt(4, 5, 0.95);
targets.setAt(4, 5, 1.0);
T accuracy = ComputeAccuracy<T>(predictions, targets);
cout << "Accuracy: " << std::setprecision(1) << accuracy << "%" << endl;
// Expected: 4 correct out of 5 = 80%
if (approxEqual(accuracy, T(80.0), T(0.1))) {
cout << "✓ Accuracy computation correct (4/5 = 80%)" << endl;
} else {
cout << "✗ Accuracy computation incorrect (expected 80%, got " << accuracy << "%)" << endl;
}
}
void test_mnist_mini_training() {
BEGIN_TESTS("MNIST Mini Training (with actual data)");
typedef double T;
cout << "Loading small MNIST subset..." << endl;
MNISTDataset<T> trainDataset;
if (!loadMNISTDataset<T>("train-images-idx3-ubyte", "train-labels-idx1-ubyte", trainDataset)) {
cout << "MNIST data not available, skipping this test" << endl;
cout << "Download MNIST from: http://yann.lecun.com/exdb/mnist/" << endl;
return;
}
cout << "✓ MNIST data loaded" << endl;
// Create small network
Network<T>* network = new Network<T>();
ILayer<T>* input = new Layer<T>(784, "Input", ActivationType::RELU);
ILayer<T>* hidden = new Layer<T>(128, "Hidden", ActivationType::RELU);
ILayer<T>* output = new Layer<T>(10, "Output", ActivationType::SIGMOID);
network->setInputLayer(input);
network->connect(input, hidden);
network->connect(hidden, output);
network->setOutputLayer(output);
network->setOptimizerType(OptimizerType::ADAM);
network->setLossType(LossType::CROSS_ENTROPY);
network->init();
cout << "✓ Network initialized (784-128-10)" << endl;
// Use first 100 samples for quick test
int numSamples = std::min(100, trainDataset.numSamples);
ml::Mat<T> trainImages(numSamples, 784, 0);
ml::Mat<T> trainLabels(numSamples, 10, 0);
for (int i = 0; i < numSamples; i++) {
for (int j = 0; j < 784; j++) {
trainImages.setAt(i, j, trainDataset.images.getAt(i, j));
}
for (int j = 0; j < 10; j++) {
trainLabels.setAt(i, j, trainDataset.labels.getAt(i, j));
}
}
// Initial evaluation
T initialAccuracy = network->evaluateAccuracy(trainImages, trainLabels);
T initialLoss = network->evaluateLoss(trainImages, trainLabels);
cout << "Initial accuracy: " << std::setprecision(2) << initialAccuracy << "%" << endl;
cout << "Initial loss: " << std::setprecision(4) << initialLoss << endl;
// Train for 2 epochs with small batches
cout << "\nTraining for 2 epochs..." << endl;
int batchSize = 16;
T learningRate = 0.001;
for (int epoch = 0; epoch < 2; epoch++) {
int numBatches = (numSamples + batchSize - 1) / batchSize;
for (int batch = 0; batch < numBatches; batch++) {
int startIdx = batch * batchSize;
int endIdx = std::min(startIdx + batchSize, numSamples);
int actualBatchSize = endIdx - startIdx;
ml::Mat<T> batchImages(actualBatchSize, 784, 0);
ml::Mat<T> batchLabels(actualBatchSize, 10, 0);
for (int i = 0; i < actualBatchSize; i++) {
for (int j = 0; j < 784; j++) {
batchImages.setAt(i, j, trainImages.getAt(startIdx + i, j));
}
for (int j = 0; j < 10; j++) {
batchLabels.setAt(i, j, trainLabels.getAt(startIdx + i, j));
}
}
network->trainBatch(batchImages, batchLabels, learningRate);
}
T epochAccuracy = network->evaluateAccuracy(trainImages, trainLabels);
T epochLoss = network->evaluateLoss(trainImages, trainLabels);
cout << " Epoch " << (epoch + 1) << ": Accuracy=" << std::setprecision(2) << epochAccuracy
<< "%, Loss=" << std::setprecision(4) << epochLoss << endl;
}
T finalAccuracy = network->evaluateAccuracy(trainImages, trainLabels);
T finalLoss = network->evaluateLoss(trainImages, trainLabels);
cout << "\nFinal accuracy: " << std::setprecision(2) << finalAccuracy << "%" << endl;
cout << "Final loss: " << std::setprecision(4) << finalLoss << endl;
// Check if training improved performance
if (finalAccuracy > initialAccuracy) {
cout << "✓ Accuracy improved: " << std::setprecision(2)
<< (finalAccuracy - initialAccuracy) << "% gain" << endl;
} else {
cout << "⚠ Accuracy did not improve (might need more training)" << endl;
}
if (finalLoss < initialLoss) {
cout << "✓ Loss decreased" << endl;
} else {
cout << "⚠ Loss did not decrease (might need more training)" << endl;
}
// Minimum expectation: final accuracy should be > 20% (better than random 10%)
if (finalAccuracy > 20.0) {
cout << "✓ Model is learning (accuracy > 20%)" << endl;
} else {
cout << "✗ Model might not be learning properly" << endl;
}
delete network;
}
int main() {
cout << "========================================" << endl;
cout << " MNIST Training Test Suite" << endl;
cout << "========================================\n" << endl;
test_network_creation();
cout << endl;
test_batch_training();
cout << endl;
test_accuracy_computation();
cout << endl;
test_mnist_mini_training();
cout << endl;
cout << "========================================" << endl;
cout << "All tests complete!" << endl;
cout << "========================================" << endl;
return 0;
}