/** * xor_backprop.c * * This is an example of a backpropagation network to solve the XOR problem. * This can also be used as a template for other programs using backpropagation * networks. * * For more information on the backpropagation network, see backprop.h * * To compile this program with LINUX, first compile the backprop.c file into * an object with * * gcc backprop.c -c -DLINUX * * Then compile the xor_backprop.c program with * * gcc xor_backprop.c backprop.o -o xor_backprop * * written by Joshua Petitt * Center for Intelligent Information Processing (CIIPS) * University of Western Australia * 2003 */ #include "backprop.h" #include #include #define IN_SIZE 2 // network input layer size #define OUT_SIZE 1 // network output layer size #define HID_SIZE 5 // network hidden layer size #define MAX_EPOCHS 100000 // maximum number of epochs #define TOLERANCE 0.05 // maximum allowed error in output vector #define LEARNING_RATE 0.1 // network learning rate (use small number // for more stable training, but longer // training time. Use number near 1 for // faster training, but network may not converge. #define TRAINING_SET_SIZE 4 int main(int argc, char* argv[]) { int i,j,k; float error, tolerance; int max_epochs; char* filename; // Allocate memory for the network. float x1[IN_SIZE]; float W1[HID_SIZE][IN_SIZE]; float y1[HID_SIZE]; float g1[HID_SIZE]; float x2[HID_SIZE]; float W2[OUT_SIZE][HID_SIZE]; float y2[OUT_SIZE]; float g2[OUT_SIZE]; // Create both layers. Note that this intializes the layers // with references to the memory allocated above. layer_t layers[2] = { {IN_SIZE,HID_SIZE,x1,&W1[0][0],y1,g1}, {HID_SIZE,OUT_SIZE,x2,&W2[0][0],y2,g2} }; // Create the network and initialize with the layers previously // allocated. network_t network = {2,LEARNING_RATE,layers}; // The input training set (A XOR B). // All enumerations of two binary numbers. float X[TRAINING_SET_SIZE][IN_SIZE] = { {0,0}, {0,1}, {1,0}, {1,1} }; // The output training set (A XOR B). // The desired output of the network. float Y[TRAINING_SET_SIZE][OUT_SIZE] = { {0}, {1}, {1}, {0} }; // Randomize weights RandomizeNetwork(&network); // Tnput data to network and activate printf("Initial state\n"); for(j=0;j tolerance && k=max_epochs) { printf("warning: maximum epochs reached\n"); } // Input data to network and activate printf("\nAfter training\n"); for(j=0;j0) { filename=NULL; for(i=0;i