void NeuralPatternArray::read(TextFileReader const& _file) throw() { TextFileReader file = _file; Text text = file.readLine(); if(text.contains("NeuralPatternArray:v1")) { //printf("is valid NeuralPatternArray file\n"); while(file.isEof() == false) { NeuralPattern pat; pat.read(file); if((pat.getNumInputs() > 0) && (pat.getNumOutputs() > 0)) { //printf("..adding a pattern..\n"); this->add(pat); } } } }
void NeuralPattern::read(TextFileReader const& _file) throw() { TextFileReader file = _file; Text text; int line = 0; int numInputs = -1, numOutputs = -1; NumericalArray<float> inputVector; NumericalArray<float> outputVector; while((text = file.readLine()).size() != 0) { if(line == 0) { // should be the format/version string if(text.contains("NeuralPatternSimple:v1") == false) { printf("NeuralPattern::read wrong version type\n"); return; } } else if(numInputs < 0) { // should be the number of inputs sscanf(text.getArray(), "Inputs:%d", &numInputs); if(numInputs < 1) { printf("NeuralPattern::read invalid number of inputs\n"); return; } //printf("ins: %d\n", numInputs); inputVector = NumericalArray<float>::withSize(numInputs); } else if(numInputs > 0) { int index; float value; sscanf(text.getArray(), "%d %f", &index, &value); numInputs--; //printf("in[%3d] = %f\n", index, value); inputVector[index] = value; } else if(numOutputs < 0) { // should be the number of outputs sscanf(text.getArray(), "Outputs:%d", &numOutputs); if(numOutputs < 1) { printf("NeuralPattern::read invalid number of numOutputs\n"); return; } //printf("outs: %d\n", numOutputs); outputVector = NumericalArray<float>::withSize(numOutputs); } else if(numOutputs > 0) { int index; float value; sscanf(text.getArray(), "%d %f", &index, &value); numOutputs--; //printf("out[%3d] = %f\n", index, value); outputVector[index] = value; } if((numInputs == 0) && (numOutputs == 0)) { //printf("CREATING PATTERN\n"); setInternal(new NeuralPatternSimpleInternal(inputVector, outputVector)); return; } line++; } }
void NeuralNetwork::read(TextFileReader const& _file) throw() { TextFileReader file = _file; Text text; int line = 0; int numLayers = -1; float learnRate, actFuncOffset; IntArray nodes; while(file.isEof() == false) { text = file.readLine(); if(line == 0) { // should be the format/version string if(text.contains("NeuralNetworkSimple:v1") == false) { printf("NeuralNetwork::read wrong version type\n"); return; } } else if(line == 1) { // should be learn rate and act func offset sscanf(text.getArray(), "learnRate: %f actFuncOffset: %f", &learnRate, &actFuncOffset); } else if(line == 2) { // should be the number of layers sscanf(text.getArray(), "Layers:%d", &numLayers); if(numLayers < 2) { printf("NeuralNetwork::read invalid number of layers\n"); return; } nodes = IntArray::withSize(numLayers, false); } else if(numLayers > 0) { int layer, numNodes; sscanf(text.getArray(), "Layer %d:%d", &layer, &numNodes); numLayers--; nodes[layer] = numNodes; if(numLayers == 0) { if(nodes == getStructure()) { setLearnRate(learnRate); setActFuncOffset(actFuncOffset); } else { setInternal(new NeuralNetworkSimpleInternal(nodes, learnRate, actFuncOffset)); } } } else { // it's a weight or a threshold int layer, node, index; float value; sscanf(text.getArray(), "%d %d %d %f", &layer, &node, &index, &value); if(index < 0) setThreshold(layer, node, value); else setWeight(layer, node, index, value); } line++; } }