-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
106 lines (92 loc) · 3.88 KB
/
main.cpp
File metadata and controls
106 lines (92 loc) · 3.88 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
#include <iostream>
#include <vector>
#include <iomanip>
#include "network.cpp"
int main() {
std::cout << "neural network demo" << std::endl << std::endl;
std::cout << "=== test 1: xor classification ===" << std::endl;
{
NeuralNet model;
model.add_layer(new DenseLayer(2, 4));
model.add_layer(new ReLULayer());
model.add_layer(new DenseLayer(4, 1));
std::vector<std::vector<double>> X = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
std::vector<std::vector<double>> y = {{0}, {1}, {1}, {0}};
for (int epoch = 0; epoch < 5000; epoch++) {
double total_loss = 0.0;
for (size_t i = 0; i < X.size(); i++) {
auto logits = model.predict(X[i]);
auto preds = apply_sigmoid(logits);
Loss loss = compute_bce(preds, y[i]);
total_loss += loss.value;
model.backward_pass(loss.gradient, 0.1);
}
if (epoch % 1000 == 0) {
std::cout << "epoch " << epoch << " loss " << total_loss / X.size() << std::endl;
}
}
std::cout << std::fixed << std::setprecision(6);
for (const auto& input : X) {
auto logit = model.predict(input);
auto output = apply_sigmoid(logit);
std::cout << (int)input[0] << " " << (int)input[1] << " -> " << output[0] << std::endl;
}
}
std::cout << std::endl << "=== test 2: and gate ===" << std::endl;
{
NeuralNet model;
model.add_layer(new DenseLayer(2, 3));
model.add_layer(new TanhLayer());
model.add_layer(new DenseLayer(3, 1));
std::vector<std::vector<double>> X = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
std::vector<std::vector<double>> y = {{0}, {0}, {0}, {1}};
for (int epoch = 0; epoch < 3000; epoch++) {
double total = 0.0;
for (size_t i = 0; i < X.size(); i++) {
auto out = model.predict(X[i]);
auto sig = apply_sigmoid(out);
Loss l = compute_bce(sig, y[i]);
total += l.value;
model.backward_pass(l.gradient, 0.1);
}
if (epoch % 1000 == 0) {
std::cout << "epoch " << epoch << " loss " << total / X.size() << std::endl;
}
}
std::cout << std::fixed << std::setprecision(6);
for (const auto& input : X) {
auto out = model.predict(input);
auto sig = apply_sigmoid(out);
std::cout << (int)input[0] << " " << (int)input[1] << " -> " << sig[0] << std::endl;
}
}
std::cout << std::endl << "=== test 3: or gate ===" << std::endl;
{
NeuralNet model;
model.add_layer(new DenseLayer(2, 2));
model.add_layer(new LeakyReLULayer(0.01));
model.add_layer(new DenseLayer(2, 1));
std::vector<std::vector<double>> X = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
std::vector<std::vector<double>> y = {{0}, {1}, {1}, {1}};
for (int epoch = 0; epoch < 2000; epoch++) {
double total = 0.0;
for (size_t i = 0; i < X.size(); i++) {
auto out = model.predict(X[i]);
auto sig = apply_sigmoid(out);
Loss l = compute_bce(sig, y[i]);
total += l.value;
model.backward_pass(l.gradient, 0.15);
}
if (epoch % 500 == 0) {
std::cout << "epoch " << epoch << " loss " << total / X.size() << std::endl;
}
}
std::cout << std::fixed << std::setprecision(6);
for (const auto& input : X) {
auto out = model.predict(input);
auto sig = apply_sigmoid(out);
std::cout << (int)input[0] << " " << (int)input[1] << " -> " << sig[0] << std::endl;
}
}
return 0;
}