// Copyright (C) 2001 Jean-Marc Valin #include "FFLayer.h" #include #include "ObjectParser.h" #include "Vector.h" #include "misc.h" using namespace std; namespace FD { DECLARE_TYPE(FFLayer) DECLARE_TYPE(Vector) DECLARE_TYPE(Vector) DECLARE_TYPE2("Vector>", Vector >) //@implements FFNet FFLayer::FFLayer (int _nbNeurons, int _nbInputs, float *_weights, int _weightOffset, int _neuronOffset, string type) : nbNeurons(_nbNeurons) , nbInputs (_nbInputs) , funcType(type) , weights(_weights+_weightOffset) , weightOffset (_weightOffset) , neuronOffset (_neuronOffset) , derivOffset(0) { if (funcType == "lin") { func = lin; deriv_func = deriv_lin; } else if (funcType == "sigmoid") { func = sigmoid; deriv_func = deriv_sigmoid; } else if (funcType == "tansig") { func = tansig; deriv_func = deriv_tansig; } } void FFLayer::setupAfterRead(float *_weights, int _weightOffset, int _neuronOffset) { weightOffset=_weightOffset; neuronOffset=_neuronOffset; //cerr << "offsets: " << weightOffset << " " << neuronOffset << endl; float *tmp = weights; weights = _weights+_weightOffset; for (int i=0;i" << endl; out << "" << endl; out << "" << endl; out << "" << endl; out << ">\n"; } void FFLayer::readFrom (istream &in) { string tag; while (1) { char ch; in >> ch; if (ch == '>') break; else if (ch != '<') throw new ParsingException ("FFLayer::readFrom : Parse error: '<' expected"); in >> tag; //cerr << "layer tag = " << tag << endl; if (tag == "nbNeurons") in >> nbNeurons; else if (tag == "nbInputs") in >> nbInputs; else if (tag == "funcType") { in >> funcType; if (funcType == "lin") { func = lin; deriv_func = deriv_lin; } else if (funcType == "sigmoid") { func = sigmoid; deriv_func = deriv_sigmoid; } else if (funcType == "tansig") { func = tansig; deriv_func = deriv_tansig; } /*else if (funcType == "tanh") { func = tanh; deriv_func = deriv_tanh; }*/ } else if (tag == "weights") { weights = new float [nbNeurons*(nbInputs+1)]; for (int i=0;i> weights[i]; } } else throw new ParsingException ("FFLayer::readFrom : unknown argument: " + tag); if (!in) throw new ParsingException ("FFLayer::readFrom : Parse error trying to build " + tag); in >> tag; //cerr << "end tag = " << tag << endl; if (tag != ">") throw new ParsingException ("FFLayer::readFrom : Parse error: '>' expected "); } //cerr << "done with layer\n"; } istream &operator >> (istream &in, FFLayer &layer) { if (!isValidType(in, "FFLayer")) return in; layer.readFrom(in); return in; } }//namespace FD