Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ttt #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

ttt #19

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion C/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@
# 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.

UNAME := $(shell uname)

CC = clang
ifeq ($(UNAME), Linux)
CC_FLAGS = -Wfatal-errors -Wall -Wextra -Wpedantic -Wconversion -Wshadow -lm
else
CC_FLAGS = -Wfatal-errors -Wall -Wextra -Wpedantic -Wconversion -Wshadow
endif

# Final binary
BIN = cmain
Expand All @@ -25,7 +31,7 @@ BIN = cmain
BUILD_DIR = ./build

# List of all .c source files.
CCS = main.c $(wildcard *.c)
CCS = $(wildcard *.c)

# All .o files go to build dir.
OBJ = $(CCS:%.c=$(BUILD_DIR)/%.o)
Expand Down
149 changes: 59 additions & 90 deletions C/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,10 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "neural.h"
#include <stdlib.h>
#include "ttt.h"
#include "util.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);
#include <string.h>

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); }
Expand All @@ -42,86 +32,65 @@ 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);
}

int logical_functions(void) {
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(
"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;
"%.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]);
}

network_print(&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]);
}
int main(int argc, char** argv) {
if (argc == 1) {
logical_functions();
} else if (argc == 2 && strcmp("ttt", argv[1]) == 0) {
tic_tac_toe();
} else {
printf("Usage:\n%s <> | ttt\n", argv[0]);
}

printf("\n");
return 0;
}
Loading
Loading