home *** CD-ROM | disk | FTP | other *** search
- // Xor1Temp.cpp
- // Automatically generated from breadboard by NeuroSolutions.
-
- #include "NSLib.h"
-
-
- void saveAllWeights(char *weightsFilePath);
- void file35Access(NSFloat *, int, int);
- void dataWriterAccess(NSFloat *, int, int);
- BOOL networkStopped=FALSE;
- unsigned short int weightFileVersion=100;
-
- // Component Construction
- Axon axon1;
- FunctionIO file35;
- FullSynapse fullSynapse2;
- TanhAxon tanhAxon6;
- FullSynapse fullSynapse10;
- TanhAxon tanhAxon14;
- FunctionIO dataWriter;
-
- int main() {
- srand((unsigned)time(NULL));
-
- // Component Initialization
- axon1.setRows(2);
- file35.setFunction((void*)file35Access);
- file35.setMode(READ);
- file35.setSpatialDimension(2,1);
- tanhAxon6.setRows(4);
- tanhAxon14.setRows(1);
- dataWriter.setFunction((void*)dataWriterAccess);
- dataWriter.setMode(WRITE);
- dataWriter.setSpatialDimension(1,1);
- FILE *loadStream = fopen("Xor.nsw","r");
- if (!loadStream) {
- fprintf(stderr, "Could not open weight file Xor.nsw");
- exit(1);
- }
- weightFileVersion = getWeightFileVersion(loadStream);
-
- // Load Normalization Coefficients of Files
- file35.loadWeights(seekComponent(loadStream, "File", "file35"),weightFileVersion);
-
- // Component Interconnection
- axon1.setPreActivityAccess(&file35);
- tanhAxon14.setActivityAccess(&dataWriter);
- axon1.setNext(&fullSynapse2);
- fullSynapse2.setLast(&axon1);
- fullSynapse2.setNext(&tanhAxon6);
- tanhAxon6.setLast(&fullSynapse2);
- tanhAxon6.setNext(&fullSynapse10);
- fullSynapse10.setLast(&tanhAxon6);
- fullSynapse10.setNext(&tanhAxon14);
- tanhAxon14.setLast(&fullSynapse10);
-
- // Load Axon Weights
- axon1.loadWeights(seekComponent(loadStream, "Axon", "axon1"),weightFileVersion);
- tanhAxon6.loadWeights(seekComponent(loadStream, "TanhAxon", "tanhAxon6"),weightFileVersion);
- tanhAxon14.loadWeights(seekComponent(loadStream, "TanhAxon", "tanhAxon14"),weightFileVersion);
-
- // Load Synapse Weights
- fullSynapse2.loadWeights(seekComponent(loadStream, "FullSynapse", "fullSynapse2"),weightFileVersion);
- fullSynapse10.loadWeights(seekComponent(loadStream, "FullSynapse", "fullSynapse10"),weightFileVersion);
- fclose(loadStream);
-
- // Get Ready to Run Network
-
- // Run Network
- for (unsigned int epoch=0; epoch<100; epoch++) {
- for (unsigned int exemplar=0; exemplar<4; exemplar++) {
- axon1.fire();
- if (networkStopped)
- goto ConcludeFiring;
- }
- }
-
- ConcludeFiring:
- saveAllWeights("Xor1Temp.nsw");
-
- return 0;
- }
-
- void saveAllWeights(char *weightsFilePath)
- { FILE *saveStream = fopen(weightsFilePath,"w");
- if (!saveStream) {
- fprintf(stderr, "Could not open weight file %s\n", weightsFilePath);
- exit(1);
- }
- fprintf(saveStream,"#NSWeightFileVersion 135\n\n");
- file35.saveWeights(putComponent(saveStream, "File", "file35"));
- axon1.saveWeights(putComponent(saveStream, "Axon", "axon1"));
- fullSynapse2.saveWeights(putComponent(saveStream, "FullSynapse", "fullSynapse2"));
- tanhAxon6.saveWeights(putComponent(saveStream, "TanhAxon", "tanhAxon6"));
- fullSynapse10.saveWeights(putComponent(saveStream, "FullSynapse", "fullSynapse10"));
- tanhAxon14.saveWeights(putComponent(saveStream, "TanhAxon", "tanhAxon14"));
- fclose(saveStream);
- }
-
- void file35Access(NSFloat *data, int rows, int cols) {
- // This function is called each time the axon1's Pre-Activity data changes
- // This function's code was written after NeuroSolutions generated the code for the BB
- // (Begin)
- int length,
- i;
- char response[128];
-
- length = rows*cols;
- printf("Enter the space-delimited data samples ('exit' to quit)):\n");
- scanf("%s",response);
- if (strcmp(response,"exit") != 0) {
- sscanf(response,"%f", data);
- for (i=1; i<length; i++)
- scanf("%f", data+i);
- }
- else
- networkStopped=TRUE;
- // (End)
- }
-
- void dataWriterAccess(NSFloat *data, int rows, int cols) {
- // This function is called each time the tanhAxon14's Activity data should be changed
- // This function's code was written after NeuroSolutions generated the code for the BB
- // (Begin)
- int j,
- i;
-
- printf("Network output:\n");
- for (i=0; i<rows; i++) {
- for (j=0; j<cols; j++)
- printf("%f ", data[i*cols + j]);
- printf("\n");
- }
- // (End)
- }
-
-