-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequential_main.cpp
More file actions
43 lines (30 loc) · 1.07 KB
/
sequential_main.cpp
File metadata and controls
43 lines (30 loc) · 1.07 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
#include <tuple>
#include <iostream>
#include "Tensor.hpp"
#include "SGD.hpp"
#include "./layers/Layer.hpp"
#include "./layers/Linear.hpp"
#include "./layers/Sequential.hpp"
int main() {
double d[8] = {0,0,0,1,1,0,1,1};
double t[4] = {0, 1, 0, 1};
Linear layer1 = Linear( 2, 3 );
Linear layer2 = Linear( 3, 1 );
Layer* layers[2] = { &layer1, &layer2 };
Sequential model = Sequential( 2, layers );
Tensor data = Tensor( std::tuple<int, int>{4, 2}, d, true );
Tensor target = Tensor( std::tuple<int, int>{4, 1}, t, true );
SGD optim = SGD( 2, model.getParameters(), 0.1 );
for( int i=0; i < 10; i++ ) {
Tensor prediction = model.forward( data );
std::cout << "prediction is: " << prediction.to_string() << std::endl;
std::cout << "target is: " << target.to_string() << std::endl;
Tensor delta = prediction - target;
Tensor loss = delta * delta;
Tensor lossSum = loss.sum();
lossSum.backward( Tensor{ std::vector<double>{ 1 } } );
optim.step( false );
optim.zero();
std::cout << "Loss is: " << lossSum.to_string() << std::endl << std::endl;
}
}