-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtensor-convo.cc
161 lines (127 loc) · 4.69 KB
/
tensor-convo.cc
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
#include <iostream>
#include <vector>
#include <fstream>
#include <memory>
#include <optional>
#include "tensor2.hh"
#include "mnistreader.hh"
#include "misc.hh"
#include "vizi.hh"
#include <fenv.h>
#include "tensor-layers.hh"
#include "convo-alphabet.hh"
#include <time.h>
using namespace std;
template<typename M, typename S>
void testModel(SQLiteWriter& sqw, S& s, const MNISTReader& mn, unsigned int startID, int batchno)
{
M m;
m.init(s, true); // production
Batcher b(mn.num());
auto batch = b.getBatch(128);
float totalLoss=0;
int corrects=0, wrongs=0;
auto topo = m.loss.getTopo();
DTime dt;
dt.start();
for(const auto& idx : batch) {
m.loss.zerograd(topo);
mn.pushImage(idx, m.img);
// normalize
m.img.normalize(0.172575, 0.25);
int label = mn.getLabel(idx) - 1;
m.expected.zero();
m.expected(0, label) = 1;
totalLoss += m.modelloss(0,0); // turns it into a float
int predicted = m.scores.maxValueIndexOfColumn(0);
if(corrects + wrongs == 0) {
printImgTensor(m.img);
cout<<"predicted: "<<(char)(predicted+'a')<<", actual: "<<(char)('a'+label)<<", loss: "<<m.modelloss<<"\n";
}
if(predicted == label)
corrects++;
else wrongs++;
}
double perc=100.0*corrects/(corrects+wrongs);
cout<<"Validation batch average loss: "<<totalLoss/batch.size()<<", percentage correct: "<<perc<<", took "<<dt.lapUsec()/1000<<" ms for "<<batch.size()<<" images\n";
sqw.addValue({
{"startID", startID},
{"batchno", batchno},
{"cputime", (double) clock()/CLOCKS_PER_SEC},
{"corperc", perc},
{"avgloss", totalLoss/batch.size()}}, "validation");
}
int main(int argc, char **argv)
{
feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW );
MNISTReader mn("gzip/emnist-letters-train-images-idx3-ubyte.gz", "gzip/emnist-letters-train-labels-idx1-ubyte.gz");
MNISTReader mntest("gzip/emnist-letters-test-images-idx3-ubyte.gz", "gzip/emnist-letters-test-labels-idx1-ubyte.gz");
cout<<"Have "<<mn.num()<<" training images and "<<mntest.num()<<" test images"<<endl;
ConvoAlphabetModel m;
ConvoAlphabetModel::State s;
if(argc==2) {
cout<<"Loading model state from file '"<<argv[1]<<"'\n";
loadModelState(s, argv[1]);
}
else
s.randomize();
m.init(s);
auto topo = m.loss.getTopo();
cout<<"Topo.size(): "<<topo.size()<<endl;
SQLiteWriter sqw("convo-vals.sqlite3");
int64_t startID=time(0);
int batchno = 0;
for(;;) {
Batcher batcher(mn.num());
DTime dt;
for(unsigned int tries = 0 ;; ++tries) {
auto batch = batcher.getBatch(64);
if(batch.empty())
break;
if(!(tries % 32)) {
testModel<ConvoAlphabetModel>(sqw, s, mntest, startID, batchno);
saveModelState(s, "tensor-convo.state");
}
if(batchno < 32 || !(tries%32)) {
s.emit(sqw, startID, batchno, batch.size());
}
dt.start();
batchno++;
float totalLoss = 0, totalWeightsLoss=0;
unsigned int corrects=0, wrongs=0;
m.loss.zeroAccumGrads(topo);
for(const auto& idx : batch) {
mn.pushImage(idx, m.img);
// normalize
m.img.normalize(0.172575, 0.25);
int label = mn.getLabel(idx) -1; // they count from 1 over at NIST!
m.expected.oneHotColumn(label);
totalLoss += m.modelloss(0,0); // turns it into a float
totalWeightsLoss += m.weightsloss(0,0);
int predicted = m.scores.maxValueIndexOfColumn(0);
if(corrects + wrongs == 0) {
cout<<"predicted: "<<(char)(predicted+'a')<<", actual: "<<(char)(label+'a')<<", loss: "<<m.modelloss<<"\n";
printImgTensor(m.img);
}
if(predicted == label)
corrects++;
else wrongs++;
// backward the thing
m.loss.backward(topo);
m.loss.accumGrads(topo);
// clear grads & havevalue
m.loss.zerograd(topo);
}
double perc = 100.0*corrects/(corrects+wrongs);
cout<<"Batch "<<batchno<<" average loss " << totalLoss/batch.size()<<", weightsloss " <<totalWeightsLoss/batch.size()<<", percent batch correct "<<perc<<"%, "<<dt.lapUsec()/1000<<"ms/batch"<<endl;
double lr=0.010 / batch.size(); // 0.010 works well at the beginning
double momentum = 0.9;
s.learn(lr, momentum);
// tcsv<<"batchno,cputime,corperc,avgloss,batchsize,lr,momentum"<<endl;
sqw.addValue({
{"startID", startID}, {"batchno", batchno}, {"cputime", (double)clock()/CLOCKS_PER_SEC},
{"corperc", perc}, {"avgloss", totalLoss/batch.size()},
{"batchsize", (int)batch.size()}, {"lr", lr*batch.size()}, {"momentum", momentum}}, "training");
}
}
}