OCR / XOR / NN / network.c
network.c
Raw
#include "network.h"

#define KRED  "\x1B[31m"
#define KGRN  "\x1B[32m"
#define KWHT  "\x1B[37m"

void OCR()
{
	srand(time(NULL));

	t_layer* inputLayer = CreateLayer(28 * 28, NULL);
	t_layer* currentLayer = CreateLayer(20, inputLayer);
	currentLayer = CreateActivationLayer(currentLayer, Sigmoid, DSigmoid);
	currentLayer = CreateLayer(52, currentLayer);
	t_layer* outputLayer = CreateActivationLayer(currentLayer, Tanh, DTanh);

	float errNum = 0;

	float **goal = goalMatrix();
	float **letters = lettersMatrix();

	for (int epoch = 0; epoch < 5000; epoch++)
	{
		int j = epoch % 4;
		
		for (int i = 0; i < inputLayer->nbNeuron; i++)
		{
			inputLayer->neuron = letters[i];
		}

		printf("TEST3\n");
		FeedForward(inputLayer);
		printf("TEST4\n");
		errNum += MeanSquaredError(outputLayer, goal[j]);
		BackPropagation(outputLayer, MeanSquaredError_Derivative(outputLayer, goal[j]), 0.05);
		printf("TEST5\n");
		if(epoch % 100 == 0)
		{
			printf("TEST6\n");
			//printf("Epoch %-5d => ErrorRate = %f\n", epoch, errNum / 4);
			if(outputLayer->neuron == goal[0])
				printf("Position Found = %f Expected %f %sOK \n", outputLayer->neuron[0], goal[0], KGRN);
			else
				printf("Position Found = %f Expected %f %sKO \n", outputLayer->neuron[0], goal[0] ,KRED);
		}
		errNum = 0;
	}
	FreeNetwork(inputLayer);
}