/* mlayer.h * Multilayer Feed-Forward Neural Network Library * (c) Logan Kearsley */ #ifndef MLAYER_H #define MLAYER_H #include typedef float (*transform)(float x); struct net { float*** L; // Weight Matrix for the neural net float*** C; // Weight Change Matrix for momentum float** P; //data passed between layers float** I; //swapped inputs float* E; //Expected output float eta; //learning speed float mu; //momentum int* D; //dimensions int depth; //number of layers in the net int icount; int bits; //flag variable transform act; transform der; }; typedef struct net neuro_t; void mprntnet(neuro_t* w); void printout(float* o, int l); /**************************** * Querying & Teaching Nets * ****************************/ void loadInput(neuro_t* w, float* I); void loadTarget(neuro_t* w, float* T); void copyOutput(neuro_t* w, float* O); #define getInput(w) w->P[0] #define getOutput(w) w->P[w->depth] #define getInputSize(w) w->D[0] #define getOutputSize(w) w->D[w->depth] #define getLayerDepth(w) w->depth #define setLearningSpeed(w,s) w->eta=s #define setMomentum(w,s) w->mu=s void momentumOn(neuro_t* w); void momentumOff(neuro_t* w); void swapOn(neuro_t* w); void swapOff(neuro_t* w); #define swapInput(w, i) w->P[0]=w->I[i] int addInput(neuro_t* w); /* mquery- query a multilayer net and put data in the passing structure * Arguments: * neuro_t* w- pointer to network data * */ void mquery(neuro_t* w); /* backprop- modify weights in the input or hidden layers * Requires that loadInput, loadTarget, and mquery be run previously * Arguments: * neuro_t* w- pointer to network data * */ float backprop(neuro_t* w); void mprntnet(neuro_t* w); /****************************** * Saving and Generating Nets * ******************************/ #define mknet(w) w=(neuro_t*)malloc(sizeof(neuro_t)); /* mldnet- load the weights for a multilayer network from a file * * Argumnets: * FILE* f- a pointer to a filestream opened for reading * in which the net is stored * neuro_t* w- pointer to location for storing network data */ void mldnet(FILE* f, neuro_t* w); void bldnet(FILE* f, neuro_t* w); int mrldnet(FILE* f, neuro_t* w); int brldnet(FILE* f, neuro_t* w); /* mrndnet- generate a multilayer network with random initial weights * * Arguments: * neuro_t* w- pointer to location for storing network data * int* dim- a pointer to a list in which the required layer dimensions are stored * int dep- the number of layers in the network */ void mrndnet(neuro_t* w, int* dim, int dep); /* msvnet- save the weights for a multilayer network to a file * * Arguments- * FILE* f- a pointer to a filestream opened for writing * neuro_t* w- pointer to location for storing network data */ void msvnet(FILE* f, neuro_t* w); void bsvnet(FILE* f, neuro_t* w); void mdstrynet(neuro_t* w); #endif // MLAYER_H