-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.c
127 lines (108 loc) · 4.06 KB
/
main.c
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
/*
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 <stdlib.h>
#include <stdio.h>
uint32_t P = 2147483647;
uint32_t A = 16807;
uint32_t current = 1;
double Rand() {
current = current * A % P;
double result = (double)current / P;
return result;
}
void print_network(const Network* network);
static uint32_t xor(uint32_t i, uint32_t j) { return i ^ j; }
static uint32_t xnor(uint32_t i, uint32_t j) { return 1 - xor(i, j); }
static uint32_t or(uint32_t i, uint32_t j) { return i | j; }
static uint32_t and(uint32_t i, uint32_t j) { return i & j; }
static uint32_t nor(uint32_t i, uint32_t j) { return 1 - or(i, j); }
static uint32_t nand(uint32_t i, uint32_t j) { return 1 - and(i, j); }
const int ITERS = 4000;
int main() {
Network network = {0};
network_init(&network, 2, 2, 6, Rand);
Trainer trainer = {0};
trainer_init(&trainer, &network);
double inputs[4][2] = {
{0, 0},
{0, 1},
{1, 0},
{1, 1}
};
double outputs[4][6] = {
{ xor(0, 0), xnor(0, 0), or(0, 0), and(0, 0), nor(0, 0), nand(0, 0) },
{ xor(0, 1), xnor(0, 1), or(0, 1), and(0, 1), nor(0, 1), nand(0, 1) },
{ xor(1, 0), xnor(1, 0), or(1, 0), and(1, 0), nor(1, 0), nand(1, 0) },
{ xor(1, 1), xnor(1, 1), or(1, 1), and(1, 1), nor(1, 1), nand(1, 1) }
};
for (size_t i = 0; i < ITERS; i++) {
double* input = inputs[i % 4];
double* output = outputs[i % 4];
trainer_train(&trainer, &network, input, output, 1.0);
}
printf(
"Result after %d iterations\n XOR XNOR OR AND NOR NAND\n",
ITERS);
for (size_t i = 0; i < 4; i++)
{
double* input = inputs[i % 4];
network_predict(&network, input);
printf(
"%.0f,%.0f = %.3f %.3f %.3f %.3f %.3f %.3f\n",
input[0],
input[1],
network.output[0],
network.output[1],
network.output[2],
network.output[3],
network.output[4],
network.output[5]);
}
print_network(&network);
trainer_free(&trainer);
network_free(&network);
return 0;
}
void print_network(const Network* network) {
printf("weights hidden:\n");
for (size_t i = 0; i < network->n_inputs; i++) {
for (size_t j = 0; j < network->n_hidden; j++) {
printf(" %9.6f", network->weights_hidden[network->n_inputs * i + j]);
}
printf("\n");
}
printf("biases hidden:\n");
for (size_t i = 0; i < network->n_hidden; i++) {
printf(" %9.6f", network->biases_hidden[i]);
}
printf("\n");
printf("weights output:\n");
for (size_t i = 0; i < network->n_hidden; i++) {
for (size_t j = 0; j < network->n_outputs; j++) {
printf(" %9.6f", network->weights_output[i * network->n_outputs + j]);
}
printf("\n");
}
printf("biases output:\n");
for (size_t i = 0; i < network->n_outputs; i++) {
printf(" %9.6f", network->biases_output[i]);
}
printf("\n");
}