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 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++; } }