-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.cpp
138 lines (121 loc) · 4.24 KB
/
main.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
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
130
131
132
133
134
135
136
137
138
/*
Licensed under the MIT License given below.
Copyright 2023 Daniel Lidstrom
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the “Software”), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "neural.h"
#include <iomanip>
#include <iostream>
using namespace Neural;
namespace {
const int ITERS = 4000;
const double lr = 1.0;
u_int32_t P = 2147483647;
u_int32_t A = 16807;
u_int32_t current = 1;
double Rand() {
current = current * A % P;
double result = (double)current / P;
return result;
}
size_t Xor(size_t i, size_t j) { return i ^ j; }
size_t Xnor(size_t i, size_t j) { return 1 - Xor(i, j); }
size_t Or(size_t i, size_t j) { return i | j; }
size_t And(size_t i, size_t j) { return i & j; }
size_t Nor(size_t i, size_t j) { return 1 - Or(i, j); }
size_t Nand(size_t i, size_t j) { return 1 - And(i, j); }
}
void show_weights(const Network& network);
int main() {
Matrix inputs = Matrix();
Matrix outputs = Matrix();
for (size_t i = 0; i < 2; i++) {
for (size_t j = 0; j < 2; j++) {
inputs.push_back({ (double)i, (double)j});
outputs.push_back({
(double)Xor(i, j),
(double)Xnor(i, j),
(double)Or(i, j),
(double)And(i, j),
(double)Nor(i, j),
(double)Nand(i, j)
});
}
}
Trainer trainer = Trainer::Create(2, 2, 6, Rand);
for (size_t i = 0; i < ITERS; i++) {
const Vector& input = inputs[i % inputs.size()];
const Vector& output = outputs[i % outputs.size()];
trainer.Train(input, output, lr);
}
std::cout
<< "Result after "
<< ITERS
<< " iterations\n"
<< " XOR XNOR OR AND NOR NAND\n";
const Network& network = trainer.network;
for (size_t i = 0; i < inputs.size(); i++) {
const Vector& input = inputs[i];
Vector pred = network.Predict(input);
std::cout
<< std::fixed
<< std::setprecision(0)
<< input[0]
<< ','
<< input[1]
<< " = "
<< std::setprecision(3)
<< pred[0]
<< " "
<< pred[1]
<< " "
<< pred[2]
<< " "
<< pred[3]
<< " "
<< pred[4]
<< " "
<< pred[5]
<< '\n';
}
show_weights(trainer.network);
return 0;
}
void show_weights(const Network& network) {
std::cout << "WeightsHidden:\n" << std::setprecision(6);
for (size_t i = 0; i < network.inputCount; i++) {
for (size_t j = 0; j < network.hiddenCount; j++) {
std::cout << network.weightsHidden[network.inputCount * i + j] << ' ';
}
std::cout << '\n';
}
std::cout << "BiasesHidden:\n";
for (auto c : network.biasesHidden) {
std::cout << c << ' ';
}
std::cout << "\nWeightsOutput:\n";
for (size_t i = 0; i < network.hiddenCount; i++) {
for (size_t j = 0; j < network.outputCount; j++) {
std::cout << network.weightsOutput[network.outputCount * i + j] << ' ';
}
std::cout << '\n';
}
std::cout << "BiasesOutput:\n";
for (auto c : network.biasesOutput) {
std::cout << c << ' ';
}
std::cout << '\n';
}