This repository has been archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
model.cpp
53 lines (37 loc) · 1.39 KB
/
model.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
#include <CaberNet.h>
struct Network : public net::Model<Network> {
Network() {
optimizer.add_parameter(layers.parameters());
}
net::layer::Sequence layers {
net::layer::Linear(784, 128),
net::layer::ReLU(),
net::layer::Linear(128, 10),
net::layer::LogSoftmax(/*axis*/ 1)
};
net::Tensor<float> forward(net::Tensor<float> x) {
return layers(x);
}
net::optimizer::SGD optimizer{0.01};
};
int main() {
net::Dataset dataset(64, false);
dataset.read_targets("data/train-labels.idx1-ubyte");
dataset.read_features("data/train-images.idx3-ubyte");
net::Tensor<int> targets({64});
net::Tensor<float> input({64,784}, false, true);
Network model;
net::Tensor<float> output({64,10});
output = model(input);
net::criterion::NLLLoss criterion(output, targets);
for(int epoch = 0; epoch < 10; ++epoch) {
std::cout << "Epoch: " << epoch + 1 << std::endl;
for(int batch = 0; batch < dataset.length(); ++batch) {
input.copy(dataset.features()[batch].internal()); // I will fix this in the future so it will be prettier and without copies.
targets.copy(dataset.targets()[batch].internal());
std::cout << "loss" << criterion.loss() << std::endl;
criterion.backward();
model.optimizer.step();
}
}
}