-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautograd.cpp
More file actions
executable file
·131 lines (110 loc) · 2.72 KB
/
autograd.cpp
File metadata and controls
executable file
·131 lines (110 loc) · 2.72 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
#include "autograd.hpp"
#include <algorithm>
void Value::print(){
if (op.empty()){
std::cout << "["<< label << "]: ";
std::cout << " data =";
this->data.printShape();
std::cout << ", grad =";
this->grad.printShape();
std::cout << ", childrens = " << children.size();
std::cout << "\n";
}
else {
std::cout << "["<< label << "]: ";
std::cout << " data =";
this->data.printShape();
std::cout << ", grad =";
this->grad.printShape();
std::cout << ", childrens = " << children.size();
std::cout << ", op =" << op;
std::cout << "\n";
}
}
//there must be a way to keep track of visited nodes better
void Value::topo_sort(std::set<Value*>* visited, std::vector<Value*>* sorted){
visited->insert(this);
std::set<Value*>::iterator itr;
for(itr = children.begin();itr!=children.end();itr++){
if(!visited->count(*itr)){
(*itr)->topo_sort(visited, sorted);
}
}
sorted->push_back(this);
}
void Value::backward(){
grad = Tensor();
grad.get() = 1.0;
std::set<Value*> visited;
std::vector<Value*> sorted;
this->topo_sort(&visited,&sorted);
std::reverse(sorted.begin(),sorted.end());
for(Value* v: sorted){
v->_backward->apply();
}
}
void MultBackward::apply(){
a->grad = out->grad * b->data;
b->grad = out->grad * a->data;
}
Value* Value::mult(Value* b){
Value* out = new Value(data * b->data);
out->op = "*";
out->_backward = new MultBackward(this,b,out);
out->children = {this,b};
return out;
}
void AddBackward::apply(){
a->grad = out->grad;
b->grad = out->grad;
}
Value* Value::add(Value* b){
Value* out = new Value(data + b->data);
out->op = "+";
out->_backward = new AddBackward(this,b,out);
out->children = {this,b};
return out;
}
void MMBackward::apply(){
a->grad = b->data.transpose() * out->grad;
b->grad = a->data.transpose() * out->grad;
}
Value* Value::mm(Value* b){
Value* out = new Value(data.mm(b->data));
out->op = "mm";
out->_backward = new MMBackward(this, b, out);
out->children = {this,b};
return out;
}
void SigmoidBackward::apply(){
a->grad = a->data.sigmoidDeriv() * out->grad;
}
Value* Value::sigmoid(){
Value* out = new Value(data.sigmoid());
out->op = "sigmoid";
out->_backward = new SigmoidBackward(this, out);
out->children = {this};
return out;
}
int main(){
Value a(10., "a");
a.print();
Value b(33.,"b");
b.print();
Value* c = a.add(&b);
Value d(100.,"d");
Value f(1.33,"f");
Value* e = d.add(&f);
Value* g = c->mult(e);
c->print();
g->backward();
a.print();
b.print();
c->print();
d.print();
e->print();
g->print();
f.print();
std::cout << "[+] youpi \n";
return 0;
}