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

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

	t_layer* inputLayer = CreateLayer(2, NULL);
	t_layer* currentLayer = CreateLayer(2, inputLayer);
	currentLayer = CreateActivationLayer(currentLayer, Sigmoid, DSigmoid);
	currentLayer = CreateLayer(1, currentLayer);
	t_layer* outputLayer = CreateActivationLayer(currentLayer, Tanh, DTanh);

	printf("%f\t%f\t%f\t%f\t%f\t%f\n\n", inputLayer->next->weight[0],inputLayer->next->weight[1],inputLayer->next->weight[2],inputLayer->next->weight[3], outputLayer->prev->weight[0], outputLayer->prev->weight[1]);


	float in[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
	float expected[4] = { 0, 1, 1, 0 };

	float errNum = 0;

	for(int epoch = 0; epoch < 20000; epoch++)
	{
		int j = epoch % 4;
		inputLayer->neuron[0] = in[2 * j];
		inputLayer->neuron[1] = in[2 * j + 1];
		FeedForward(inputLayer);

		errNum += MeanSquaredError(outputLayer, expected + j);
		BackPropagation(outputLayer, MeanSquaredError_Derivative(outputLayer, expected + j), 0.05);
		//if((epoch % 4) == 3)
		//{
		if(epoch % 1000 == 0)
		{
			printf("Epoch %-5d => ErrorRate = %f\n", epoch, errNum / 4);
		}
		errNum = 0;
		//}
	}

	inputLayer->neuron[0] = 0;
	inputLayer->neuron[1] = 0;
	FeedForward(inputLayer);
	printf("Pattern n°0 : 0 | Input 1 : 0 | Input 2 : 0 | Ouput : %f\n", outputLayer->neuron[0]);
	inputLayer->neuron[0] = 0;
	inputLayer->neuron[1] = 1;
	FeedForward(inputLayer);
	printf("Pattern n°1 : 1 | Input 1 : 0 | Input 2 : 1 | Ouput : %f\n", outputLayer->neuron[0]);

	inputLayer->neuron[0] = 1;
	inputLayer->neuron[1] = 0;
	FeedForward(inputLayer);
	printf("Pattern n°2 : 1 | Input 1 : 1 | Input 2 : 0 | Ouput : %f\n", outputLayer->neuron[0]);

	inputLayer->neuron[0] = 1;
	inputLayer->neuron[1] = 1;
	FeedForward(inputLayer);
	printf("Pattern n°3 : 0 | Input 1 : 1 | Input 2 : 1 | Ouput : %f\n", outputLayer->neuron[0]);


	FreeNetwork(inputLayer);
}