-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_debug_weights.cpp
More file actions
108 lines (88 loc) · 3.69 KB
/
test_debug_weights.cpp
File metadata and controls
108 lines (88 loc) · 3.69 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
#include <iostream>
#include "allheader.h"
#include "network.h"
using namespace std;
using namespace ml;
void printWeights(const char* label, Mat<double> w) {
cout << label << " (" << w.size().cy << "x" << w.size().cx << "):" << endl;
for (int i = 0; i < min(w.size().cy, 3); ++i) {
cout << " ";
for (int j = 0; j < min(w.size().cx, 5); ++j) {
cout << w.getAt(i, j) << " ";
}
cout << endl;
}
}
int main() {
typedef double T;
cout << "Creating 2-2-1 network..." << endl;
Network<T>* net = new Network<T>();
ILayer<T>* input = new Layer<T>(2, "I");
ILayer<T>* hidden = new Layer<T>(2, "H");
ILayer<T>* output = new Layer<T>(1, "O");
net->setInputLayer(input);
net->connect(input, hidden);
net->connect(hidden, output);
net->setOutputLayer(output);
net->init();
// Single training example: [1, 0] -> 1
Mat<T> x(1, 2, 0);
x.setAt(0, 0, 1.0);
x.setAt(0, 1, 0.0);
Mat<T> y(1, 1, 1.0);
T lr = 0.1;
printWeights("\nInitial I->H weights", input->getWeights(hidden));
printWeights("Initial H->O weights", hidden->getWeights(output));
for (int iter = 0; iter < 5; ++iter) {
cout << "\n=== Iteration " << iter << " ===" << endl;
// Forward
Mat<T> pred = net->feed(x);
cout << "Prediction: " << pred.getAt(0, 0) << endl;
// Compute error
Mat<T> err = Diff(y, pred);
cout << "Error: " << err.getAt(0, 0) << " (target - pred = " << y.getAt(0, 0) << " - " << pred.getAt(0, 0) << ")" << endl;
// Get activations before backprop
Mat<T> hiddenAct = hidden->getActivatedInput();
cout << "Hidden activations: ";
for (int i = 0; i < hiddenAct.size().cx; ++i) {
cout << hiddenAct.getAt(0, i) << " ";
}
cout << endl;
// Backprop
output->setErrors(err);
net->backprop();
// Check propagated errors
Mat<T> hiddenErr = hidden->getErrors();
cout << "Hidden errors after backprop: ";
for (int i = 0; i < hiddenErr.size().cx; ++i) {
cout << hiddenErr.getAt(0, i) << " ";
}
cout << endl;
// Get weights before update
Mat<T> oldWeightsHO = hidden->getWeights(output);
// MANUAL weight update check
cout << "Manual check - what SHOULD happen:" << endl;
Mat<T> errCopy = err.Copy();
cout << " Error dimensions: (" << errCopy.size().cy << "," << errCopy.size().cx << ")" << endl;
cout << " Hidden act dimensions: (" << hiddenAct.size().cy << "," << hiddenAct.size().cx << ")" << endl;
cout << " Expected delta[0,0] = " << err.getAt(0,0) << " * " << hiddenAct.getAt(0,0) << " = " << (err.getAt(0,0) * hiddenAct.getAt(0,0)) << endl;
cout << " Expected delta[0,1] = " << err.getAt(0,0) << " * " << hiddenAct.getAt(0,1) << " = " << (err.getAt(0,0) * hiddenAct.getAt(0,1)) << endl;
cout << " Expected delta[0,2] (bias) = " << err.getAt(0,0) << " * 1.0 = " << err.getAt(0,0) << endl;
// Update
net->updateWeights(lr);
// Check weight changes
Mat<T> newWeightsHO = hidden->getWeights(output);
cout << "Actual H->O weight changes: ";
for (int i = 0; i < min(newWeightsHO.size().cy, 2); ++i) {
for (int j = 0; j < min(newWeightsHO.size().cx, 3); ++j) {
T delta = newWeightsHO.getAt(i, j) - oldWeightsHO.getAt(i, j);
cout << "(" << i << "," << j << "):" << delta << " ";
}
}
cout << endl;
}
printWeights("\nFinal I->H weights", input->getWeights(hidden));
printWeights("Final H->O weights", hidden->getWeights(output));
delete net;
return 0;
}