-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigitrec.cpp
192 lines (166 loc) · 4.86 KB
/
digitrec.cpp
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
#include "mnist.h"
#include <stdio.h>
#include "ANDnet.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
void setupMNIST() {
int32_t TESTING_LABEL_MAGIC_NUM = 2049;
auto testingLabels = new MNISTLabels(TESTING_LABEL_MAGIC_NUM);
testingLabels->loadFile("digitsdata/t10k-labels-idx1-ubyte");
printf("Loaded %d testing labels.\n", testingLabels->numItems);
int32_t TESTING_IMAGES_MAGIC_NUM = 2051;
auto testingImages = new MNISTImages(TESTING_IMAGES_MAGIC_NUM);
testingImages->loadFile("digitsdata/t10k-images-idx3-ubyte");
printf("Loaded %d testing images.\n", testingImages->numImages);
NeuralNetwork* net = new NeuralNetwork();
net->inputs = new Layer(0,784);
net->hidden = new Layer(1,32);
net->outputs = new Layer(2,10);
cout << "Connect layers \n";
net->connectLayers();
cout << "OK\n";
auto trainer = new Trainer(net, 0.005);
int rows = 28;
int cols = 28;
vector<vector<float>> inputs,expected;
vector<vector<float>> inputsx,expectedx;
for (int index=0; index<230; index++) {
vector<float> inp, exp;
char* pixels = testingImages->images[index];
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
int index = row*cols+col;
uint8_t darkness = pixels[index];
inp.push_back(darkness/255.0);
}
}
if (index < 30) inputs.push_back(inp);
inputsx.push_back(inp);
for (int l=0;l<10;l++) exp.push_back(0);
int which = testingLabels->labels[index];
cout << "which = " << which << "\n";
exp[which] = 1.0;
if (index < 30) expected.push_back(exp);
expectedx.push_back(exp);
}
string d;
getline(cin,d);
float lowmse = 100;
for (int k=0; k<100; k++) {
cout << ">>> " << k << "\n";
cout << "Randomizing weights\n";
net->randomWeights();
cout << "Calculating MSE\n";
float mse = trainer->meanSquaredError(inputs, expected);
cout << "Got MSE\n";
if (mse < lowmse) {
lowmse = mse;
cout << "--------------------------------------------------\n";
cout << "mse: " << mse << "\n";
net->outputs->print();
net->hidden->printWeights();
net->outputs->printWeights();
}
}
for (int i=0; i<1500; i++) {
if (i%100==0) cout << "calc gradients: " << i << "\n";
trainer->calcGradients(inputs, expected);
}
cout << "Calculating MSE\n";
float mse = trainer->meanSquaredError(inputs, expected);
cout << "Got MSE\n";
lowmse = mse;
cout << "--------------------------------------------------\n";
cout << "mse: " << mse << "\n";
string xx;
for (int t=200; t<214;t++) {
testingImages->printImage(t);
net->assignInputs(inputsx[t]);
net->computeOutputs();
int c = net->classify();
//net->print();
cout << "Class = " << c << "\n";
getline(cin, xx);
}
}
int main(int argc, const char* argv[]) {
srand (time(NULL));
setupMNIST();
/*
auto net = new ANDNetwork();
auto trainer = new Trainer(net, 0.11);
vector<vector<float>> exp;
vector<float> exp_;
exp_.push_back(0);
exp_.push_back(1);
exp.push_back(exp_);
vector<float> exp2_;
exp2_.push_back(1);
exp2_.push_back(0);
exp.push_back(exp2_);
vector<float> exp3_;
exp3_.push_back(1);
exp3_.push_back(0);
exp.push_back(exp3_);
vector<vector<float>>inputs;
vector<float> inp1;
inp1.push_back(1);
inp1.push_back(1);
inputs.push_back(inp1);
vector<float> inp2;
inp2.push_back(0);
inp2.push_back(1);
inputs.push_back(inp2);
vector<float> inp3;
inp3.push_back(1);
inp3.push_back(0);
inputs.push_back(inp3);
for (int nn=0; nn<5; nn++) {
exp.push_back(exp_);
inputs.push_back(inp1);
}
float lowmse = 100;
for (int k=0; k<2000; k++) {
net->randomWeights();
float mse = trainer->meanSquaredError(inputs, exp);
if (mse < lowmse) {
lowmse = mse;
cout << "--------------------------------------------------\n";
cout << "mse: " << mse << "\n";
net->outputs->print();
net->hidden->printWeights();
net->outputs->printWeights();
}
}
for (int i=0; i<25000; i++) {
trainer->calcGradients(inputs, exp);
}
//cout << "Difference from expected:\n";
//trainer->mean(exp);
//net->assignInput(0,1);
//net->assignInput(1,1);
cout << "/////////////////////////////////////////////////\n";
float mse = trainer->meanSquaredError(inputs, exp);
cout << "mse: " << mse << "\n";
net->assignInput(0,0);
net->assignInput(1,0);
net->computeOutputs();
cout << "Network:\n";
net->print();
cout << "class: " << net->classify() << "\n"; */
}
/*
Standard knowledge encoding format
32x32 bytes = grid
as a type of language
high-level shapes described
as 32 x 32
ways to compose multiple 32x32 grids
to describe larger or more complex shapes
mapping to images or visual fields
apply grids to eachother as mapping
standard ways to go from images to grid arrays
blowing up or expanding grids
*/