-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.cpp
104 lines (75 loc) · 2.33 KB
/
demo.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
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
#include "doubleflow.h"
#include <iostream>
#include <iomanip>
#include <random>
using namespace std;
/*
trains a logistic regression model that learns the binary AND operation
*/
void logistic_regression() {
mt19937 generator;
std::normal_distribution<double> normal(0, 0.1);
Graph g;
const int m = 4, n = 2;
Variable* x[m][n];
Variable* r[m];
// this is our training data
x[0][0] = g.variable(0); x[0][1] = g.variable(0); r[0] = g.variable(0);
x[1][0] = g.variable(0); x[1][1] = g.variable(1); r[1] = g.variable(0);
x[2][0] = g.variable(1); x[2][1] = g.variable(0); r[2] = g.variable(0);
x[3][0] = g.variable(1); x[3][1] = g.variable(1); r[3] = g.variable(1);
// weights ans bias
Variable* w[n];
Variable* b = g.variable(0);
// initialize weights normally distrubuted
for (int i = 0; i < n; i++) {
w[i] = g.variable(normal(generator));
}
// predictions
Operation* y[m];
for (int i = 0; i < m; i++) {
y[i] = g.variable(0);
// multiply the term with weights and add them
for (int j = 0; j < n; j++) {
y[i] = g.add(y[i], g.mul(w[j], x[i][j]));
}
// add the bias
y[i] = g.add(b, y[i]);
// sigmoid operation
y[i] = g.div(g.variable(1), g.add(g.variable(1), g.exp(g.mul(g.variable(-1), y[i]))));
}
// calculate cross-entropy loss
Operation* loss = g.variable(0);
for (int i = 0; i < m; i++) {
auto t1 = g.mul(g.mul(g.variable(-1), r[i]), g.log(y[i]));
auto t2 = g.mul(g.sub(r[i], g.variable(1)), g.log(g.sub(g.variable(1), y[i])));
loss = g.add(loss, g.add(t1, t2));
}
loss = g.div(loss, g.variable(m));
// we defined our computation graph so far
// now its time to train our model
// we are using the gradient descent algorithm to train our parameters
double lr = 10;
cout << fixed << setprecision(4);
for (int epoch = 1; epoch <= 1000; epoch++) {
// calculate loss and gradients
double cur_loss = g.run(loss);
if (epoch % 10 == 0) {
cout << "epoch=" << epoch << " loss=" << loss->result << endl;
}
// update parameters
for (int i = 0; i < n; i++) {
w[i]->set(w[i]->result - lr * w[i]->grad);
}
b->set(b->result - lr * b->grad);
}
cout << "w: " << w[0]->result << " " << w[1]->result << endl;
cout << "b: " << b->result << endl;
for (int i = 0; i < m; i++) {
cout << i << "th prediction: " << y[i]->result << endl;
}
}
int main() {
logistic_regression();
return 0;
}