-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
439 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# 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. | ||
|
||
CC = clang | ||
CC_FLAGS = -Wfatal-errors -Wall -Wextra -Wpedantic -Wconversion -Wshadow | ||
|
||
# Final binary | ||
BIN = cmain | ||
|
||
# Put all auto generated stuff to this build dir. | ||
BUILD_DIR = ./build | ||
|
||
# List of all .c source files. | ||
CCS = main.c $(wildcard *.c) | ||
|
||
# All .o files go to build dir. | ||
OBJ = $(CCS:%.c=$(BUILD_DIR)/%.o) | ||
|
||
# Gcc/Clang will create these .d files containing dependencies. | ||
DEP = $(OBJ:%.o=%.d) | ||
|
||
# Default target named after the binary. | ||
$(BIN) : $(BUILD_DIR)/$(BIN) | ||
|
||
# Actual target of the binary - depends on all .o files. | ||
$(BUILD_DIR)/$(BIN) : $(OBJ) | ||
mkdir -p $(@D) | ||
$(CC) $(CC_FLAGS) $^ -o $@ | ||
|
||
# Include all .d files | ||
-include $(DEP) | ||
|
||
# Build target for every single object file. | ||
# The potential dependency on header files is covered | ||
# by calling `-include $(DEP)`. | ||
$(BUILD_DIR)/%.o : %.c | ||
mkdir -p $(@D) | ||
$(CC) $(CC_FLAGS) -MMD -c $< -o $@ | ||
|
||
.PHONY : clean | ||
clean : | ||
-rm $(BUILD_DIR)/$(BIN) $(OBJ) $(DEP) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
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 size_t xor(size_t i, size_t j) { return i ^ j; } | ||
static size_t xnor(size_t i, size_t j) { return 1 - xor(i, j); } | ||
static size_t or(size_t i, size_t j) { return i | j; } | ||
static size_t and(size_t i, size_t j) { return i & j; } | ||
static size_t nor(size_t i, size_t j) { return 1 - or(i, j); } | ||
static size_t nand(size_t i, size_t j) { return 1 - and(i, j); } | ||
|
||
const int ITERS = 4000; | ||
|
||
int main() { | ||
Network network = network_create(2, 2, 6, Rand); | ||
Trainer trainer = trainer_create(&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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
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 <math.h> | ||
|
||
static double sigmoid(double f) { return 1.0 / (1.0 + exp(-f)); } | ||
static double sigmoid_prim(double f) { return f * (1.0 - f); } | ||
|
||
Network network_create(uint32_t n_inputs, uint32_t n_hidden, uint32_t n_outputs, RandFcn rand) { | ||
Network network = {0}; | ||
network.n_inputs = n_inputs; | ||
network.n_hidden = n_hidden; | ||
network.n_outputs = n_outputs; | ||
|
||
network.weights_hidden = calloc( | ||
n_inputs * n_outputs, | ||
sizeof(*network.weights_hidden)); | ||
network.biases_hidden = calloc( | ||
n_hidden, | ||
sizeof(*network.biases_hidden)); | ||
network.weights_output = calloc( | ||
n_hidden * n_outputs, | ||
sizeof(*network.weights_output)); | ||
network.biases_output = calloc( | ||
n_outputs, | ||
sizeof(*network.biases_output)); | ||
network.hidden = calloc( | ||
n_hidden, | ||
sizeof(*network.hidden)); | ||
network.output = calloc( | ||
n_outputs, | ||
sizeof(*network.output)); | ||
|
||
// initialize everything but the biases | ||
for (size_t i = 0; i < n_inputs * n_hidden; i++) { | ||
network.weights_hidden[i] = rand() - 0.5; | ||
} | ||
|
||
for (size_t i = 0; i < n_hidden * n_outputs; i++) { | ||
network.weights_output[i] = rand() - 0.5; | ||
} | ||
|
||
return network; | ||
} | ||
|
||
void network_free(Network* network) { | ||
free(network->weights_hidden); | ||
free(network->biases_hidden); | ||
free(network->weights_output); | ||
free(network->biases_output); | ||
free(network->hidden); | ||
free(network->output); | ||
} | ||
|
||
void network_predict(Network* network, double* input) { | ||
for (uint32_t c = 0; c < network->n_hidden; c++) { | ||
double sum = 0; | ||
for (uint32_t r = 0; r < network->n_inputs; r++) { | ||
sum += input[r] * network->weights_hidden[r * network->n_hidden + c]; | ||
} | ||
|
||
network->hidden[c] = sigmoid(sum + network->biases_hidden[c]); | ||
} | ||
|
||
for (uint32_t c = 0; c < network->n_outputs; c++) { | ||
double sum = 0; | ||
for (uint32_t r = 0; r < network->n_hidden; r++) { | ||
sum += network->hidden[r] * network->weights_output[r * network->n_outputs + c]; | ||
} | ||
|
||
network->output[c] = sigmoid(sum + network->biases_output[c]); | ||
} | ||
} | ||
|
||
/* trainer */ | ||
|
||
Trainer trainer_create(Network* network) { | ||
Trainer trainer = {0}; | ||
trainer.grad_hidden = calloc(network->n_hidden, sizeof(*trainer.grad_hidden)); | ||
trainer.grad_output = calloc(network->n_outputs, sizeof(*trainer.grad_output)); | ||
return trainer; | ||
} | ||
|
||
void trainer_train(Trainer* trainer, Network* network, double* input, double* y, double lr) { | ||
network_predict(network, input); | ||
for (uint32_t c = 0; c < network->n_outputs; c++) { | ||
trainer->grad_output[c] = (network->output[c] - y[c]) * sigmoid_prim(network->output[c]); | ||
} | ||
|
||
for (uint32_t r = 0; r < network->n_hidden; r++) { | ||
double sum = 0.0; | ||
for (uint32_t c = 0; c < network->n_outputs; c++) { | ||
sum += trainer->grad_output[c] * network->weights_output[r * network->n_outputs + c]; | ||
} | ||
|
||
trainer->grad_hidden[r] = sum * sigmoid_prim(network->hidden[r]); | ||
} | ||
|
||
for (size_t r = 0; r < network->n_hidden; r++) { | ||
for (size_t c = 0; c < network->n_outputs; c++) { | ||
network->weights_output[r * network->n_outputs + c] -= lr * trainer->grad_output[c] * network->hidden[r]; | ||
} | ||
} | ||
|
||
for (size_t r = 0; r < network->n_inputs; r++) { | ||
for (size_t c = 0; c < network->n_hidden; c++) { | ||
network->weights_hidden[r * network->n_hidden + c] -= lr * trainer->grad_hidden[c] * input[r]; | ||
} | ||
} | ||
|
||
for (size_t c = 0; c < network->n_outputs; c++) { | ||
network->biases_output[c] -= lr * trainer->grad_output[c]; | ||
} | ||
|
||
for (size_t c = 0; c < network->n_hidden; c++) { | ||
network->biases_hidden[c] -= lr * trainer->grad_hidden[c]; | ||
} | ||
} | ||
|
||
void trainer_free(Trainer* trainer) { | ||
free(trainer->grad_hidden); | ||
free(trainer->grad_output); | ||
} |
Oops, something went wrong.