-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_test.cpp
More file actions
231 lines (178 loc) · 5.08 KB
/
matrix_test.cpp
File metadata and controls
231 lines (178 loc) · 5.08 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
// matrix_ops.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "tensor.h"
#include <iostream>
#include <tuple>
#include "activation.h"
#include "linear.h"
#include "loss.h"
void testLinear(){
Linear linear = Linear(4, 3);
// linear.initRange();
linear.initNormal();
linear.print();
std::vector<size_t> inputShape{3, 4};
Tensor input = Tensor(inputShape);
input.initRange();
std::cout << "Input:\n";
input.print();
std::vector<size_t> targetShape{3, 3};
Tensor targets = Tensor(targetShape);
targets.initRange();
std::cout << "Targets:\n";
targets.print();
MeanSquaredError mse;
std::cout << "===================================================================\n";
for (int i = 0; i < 1000; ++i) {
Tensor out = linear.forward(input);
// std::cout << "Output:\n";
// out.print();
// D = Y - Y*
Tensor lossGrads = mse.gradient(targets, out);
// std::cout << "Loss Gradients:\n";
// lossGrads.print();
auto [gradWeights, gradBiases] = linear.calculateGradient(input, lossGrads);
// std::cout << "\nGradients:\n" << "Weights:\n";
// gradWeights.print();
// std::cout << "Biases:\n";
// gradBiases.print();
linear.updateWeights(gradWeights, gradBiases, 0.01f);
// std::cout << "Updated:\n";
// linear.print();
out = linear.forward(input);
std::cout << "MSE: " << mse.loss(targets, out) <<
"\n=====================================================================\n";
}
}
void testActivation(){
ReLU relu;
MeanSquaredError mse;
std::vector<size_t> inputShape{3, 4};
Tensor input = Tensor(inputShape);
input.initRange();
std::cout << "Input:\n";
input.print();
std::vector<size_t> targetsShape{3, 4};
Tensor targets = Tensor(targetsShape);
targets.initRange();
std::cout << "Targets:\n";
targets.print();
Tensor out = relu.forward(input);
std::cout << "Outputs:\n";
out.print();
Tensor mseGrads = mse.gradient(targets, out);
Tensor grads = relu.gradient(input, mseGrads);
std::cout << "Gradients:\nMSE:\n";
mseGrads.print();
std::cout << "relu:\n";
grads.print();
}
void testNN(){
std::vector<size_t> inputShape{3, 4};
Tensor input = Tensor(inputShape);
input.initRange();
std::cout << "Input:\n";
input.print();
std::vector<size_t> targetsShape{3, 2};
Tensor targets = Tensor(targetsShape);
targets.initRange();
std::cout << "Targets:\n";
targets.print();
ReLU relu;
MeanSquaredError mse;
// Layers
Linear linear1 = Linear(4, 8);
linear1.initNormal();
Linear linear2 = Linear(8, 8);
linear2.initNormal();
Linear linear3 = Linear(8, 2);
linear3.initNormal();
float lr = 0.0001f;
// Training
for (int i = 0; i < 100; ++i) {
// Forward
Tensor outLinear1 = linear1.forward(input);
Tensor outRelu1 = relu.forward(outLinear1);
Tensor outLinear2 = linear2.forward(outRelu1);
Tensor outRelu2 = relu.forward(outLinear2);
Tensor outLinear3 = linear3.forward(outRelu2);
// Gradients
Tensor mseGrad = mse.gradient(targets, outLinear3);
auto [linearGradWeights3, linearGradBiases3] = linear3.calculateGradient(outRelu2, mseGrad);
Tensor reluGrad2 = relu.gradient(outLinear2, linearGradWeights3);
auto [linearGradWeights2, linearGradBiases2] = linear2.calculateGradient(outRelu1, reluGrad2);
Tensor reluGrad1 = relu.gradient(outLinear1, linearGradWeights2);
auto [linearGradWeights1, linearGradBiases1] = linear1.calculateGradient(input, reluGrad1);
// Update weights
linear1.updateWeights(linearGradWeights1, linearGradBiases1, lr);
linear2.updateWeights(linearGradWeights2, linearGradBiases2, lr);
linear3.updateWeights(linearGradWeights3, linearGradBiases3, lr);
// Loss
float loss = mse.loss(targets, outLinear3);
std::cout << loss << "\n";
}
}
void testTensorOps(){
// Constructor
std::vector<size_t> aShape{1, 2, 3};
std::vector<size_t> bShape{1, 3, 4};
Tensor a(aShape), b(bShape);
// Init Range
a.initRange();
std::cout << "A:\n";
a.print();
// Init Normal
b.initNormal();
std::cout << "B:\n";
b.print();
// Indexing
std::vector<size_t> idx{0, 1, 1};
std::cout << "A[0]: " << a[0] << "\n";
std::cout << "A[1]: " << a[1] << "\n";
std::cout << "A[2]: " << a[2] << "\n";
std::cout << "A[0, 1, 2]: " << a[&idx] << "\n";
// Reshaping
std::vector<size_t> cShape{2, 1, 3};
Tensor c = a.reshape(cShape);
std::cout << "C = A (1, 2, 3) -> (2, 1, 3):\n";
c.print();
// Matrix multiplication
Tensor d = a.matmul(b);
std::cout << "D = AB:\n";
d.print();
// Addition
Tensor e = a.add(a);
std::cout << "E = A + A:\n";
e.print();
// Addition w/ broadcasting
Tensor f = a.add(c);
std::cout << "F = A + C:\n";
f.print();
// Subtraction
Tensor g = a.sub(a);
std::cout << "G = A - A:\n";
g.print();
// Subtraction w/ broadcasting
Tensor h = a.sub(c);
std::cout << "H = A - C:\n";
h.print();
// * Operator
Tensor i = a * b;
std::cout << "I = AB\n";
i.print();
// + Operator (Mat-Vec)
Tensor j = a + c;
std::cout << "J = A + C\n";
j.print();
// - Operator (Mat-Mat)
Tensor k = a - a;
std::cout << "K = A - A\n";
k.print();
}
int main(){
std::cout << "Hello World!\n";
// testTensorOps();
// testLinear();
// testActivation();
testNN();
}