-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
123 lines (71 loc) · 2.41 KB
/
Copy pathmain.cpp
File metadata and controls
123 lines (71 loc) · 2.41 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
#include "tensor/tensor2d.h"
#include "neural/layer/affine1d.h"
#include "tensor/vector.h"
#include "utils/frisbee.h"
#include <iostream>
#include "neural/layer/softmax.h"
#include "neural/loss/categoricalCrossEntropy.h"
#include "utils/fileReading.h"
#include "neural/layer/relu.h"
#include "utils/timer.h"
#include "neural/layer/sigmoid.h"
#include "neural/layer/tanh.h"
#include "neural/layer/leakyRelu.h"
#include "tensor/tensor.h"
#include "neural/layer/convolution2d.h"
#include "neural/layer/affine_t.h"
#include "neural/layer/relu.h"
#include "neural/layer/softmax.h"
#include "neural/layer/flatten.h"
int main() {
using namespace SgNet;
int maxRows = 11;
Tensor data(std::vector<int>{maxRows,1,28,28});
Vector labels(maxRows);
loadMnistConvData(data,labels,maxRows);
data -= 127.5;
data/=127.5;
double learningRate = 0.005;
int kernelWidth = 3;
std::array<int, 2> kernelDims = { kernelWidth,kernelWidth };
int inputChannels = 1;
int numFilters = 1;
int strideLength = 1;
int padding = 0;
int convOutDim = (28 - kernelWidth+2*padding)/strideLength + 1;
labels.print();
Convolution2d conv1 = Convolution2d(kernelDims, inputChannels, numFilters, learningRate, strideLength, padding);
Relu r1 = Relu();
Flatten f1 = Flatten(2);
Affine1d a2 = Affine1d(2,std::vector<int>{676,10},learningRate);
Softmax s2 = Softmax(0);
CCE cce = CCE();
int nIters = 10;
for(int i=0;i<nIters;i++){
std::cout << "begin loop\n";
std::cout << "4445\n";
Tensor out1 = conv1.forward(data);
std::cout << "45\n";
Tensor rOut1 = r1.forward(out1);
Tensor flat1 = f1.forward(rOut1);
std::cout << "345\n";
Tensor out2 = a2.forward(flat1);
std::cout << "455\n";
Tensor sOut2 = s2.forward(out2,1);
std::cout << "145\n";
double loss = cce.calculate(sOut2,labels);
std::cout << "Loss: " << loss << "\n";
Tensor gradients = cce.backward(sOut2,labels);
std::cout << "here\n";
Tensor sBack2 = s2.backward(gradients);
std::cout << "here\n";
Tensor back2 = a2.backward(sBack2);
std::cout << "here\n";
Tensor flatBack1 = f1.backward(back2);
Tensor rBack1 = r1.backward(flatBack1);
std::cout << "here\n";
Tensor back1 = conv1.backward(rBack1);
std::cout << "here\n";
std::cout << "end loop\n";
}
}