-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnn.cpp
66 lines (56 loc) · 1.05 KB
/
nn.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
#include <vector>
#include <iostream>
struct Node {
struct Connection {
Node &to;
double weight;
void send(double value) {
to.inputsFired++;
to.value += value * weight;
if (to.inputsFired == to.inputs) {
to.fire(to.fired() ? 1 : 0);
}
}
};
unsigned int inputs = 0;
unsigned int inputsFired = 0;
double value = 0;
double bias;
std::vector<Connection> outputs;
Node(double _bias) :
bias(_bias)
{}
void connect(Node &to, double weight) {
outputs.push_back(Connection{to, weight});
to.inputs++;
}
void fire(double value) {
for (auto &output : outputs) {
output.send(value);
}
}
bool fired() {
return value + bias > 0;
}
};
int main() {
Node in1{0};
Node in2{0};
Node out{3};
Node carry{3};
Node m1{3};
Node m2{3};
Node m3{3};
in1.connect(m1, -2);
in2.connect(m1, -2);
in1.connect(m2, -2);
in2.connect(m3, -2);
m1.connect(m2, -2);
m1.connect(m3, -2);
m2.connect(out, -2);
m3.connect(out, -2);
m1.connect(carry, -4);
in1.fire(1);
in2.fire(1);
std::cout << carry.fired() << out.fired() << std::endl;
}