📄 netloader.cpp
字号:
/*************************************************************************** netloader.cpp - description ------------------- begin : Mon Apr 29 2002 copyright : (C) 2002 by R黡iger Koch email : rkoch@rkoch.org ***************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/using namespace std;#include "amygdala/netloader.h"#include "amygdala/layer.h"#include "amygdala/fastneuron.h"#include "amygdala/alphaneuron.h"#include "amygdala/basicneuron.h"#include "amygdala/utilities.h"#include <string.h>#include <stdexcept>#include <iostream>//libxml#include <libxml/parser.h>#include <libxml/parserInternals.h>extern "C" { void __SAXStartElement1(NetLoader *netLoader, const char *name, const char **attrs){ netLoader->SAXStartElement1(name, attrs); } void __SAXEndElement1(NetLoader *netLoader, const char *name){ netLoader->SAXEndElement1(name); } void __SAXStartElement2(NetLoader *netLoader, const char *name, const char **attrs){ netLoader->SAXStartElement2(name, attrs); } void __SAXEndElement2(NetLoader *netLoader, const char *name){ netLoader->SAXEndElement2(name); } void __SAXError(NetLoader *netLoader, const char *msg, ...){ cerr << msg << endl; netLoader->SAXError(); }} // extern "C"NetLoader::NetLoader(){}NetLoader::NetLoader(Network *_net){ net = _net; NeuronFactory<AlphaNeuron> *alpha = new NeuronFactory<AlphaNeuron> (); NeuronFactory<FastNeuron> *fast = new NeuronFactory<FastNeuron> (); NeuronFactory<BasicNeuron> *basic = new NeuronFactory<BasicNeuron> (); RegisterNeuronClass(alpha); RegisterNeuronClass(fast); RegisterNeuronClass(basic);}NetLoader::~NetLoader(){ for(hash_map <const char*, NeuronFactoryBase*, hash<const char*>, eqstr >::iterator factory = registry.begin(); factory != registry.end(); factory++){ delete factory->second; }}void NetLoader::SAXError(){ saxErrors++;}void NetLoader::SAXStartElement1(const char* name, const char **attrs){ string tag = name; if(tag == "neuron"){ AmIdInt id = 0; NeuronFactoryBase *factory = NULL; for (unsigned int i = 0; attrs[i] != NULL; i++) { string aname = attrs[i++]; const char* adata = attrs[i]; if(aname == "id"){ id = atoi(adata); if(id == 0) { cerr << "Invalid Neuron ID: " << adata << endl; saxErrors++; return; } } else if(aname == "type"){ factory = registry[adata]; if(factory == NULL){ cerr << "Invalid Neuron type: " << adata << endl; saxErrors++; return; } } } if(factory == NULL){ cerr << "No factory available" << endl; saxErrors++; return; } if(id == 0){ cerr << "No ID given for neuron" << endl; saxErrors++; return; } Neuron *n = factory->NewNeuron(id); if(currLayer == NULL) { cerr << "No layer defined" << endl; saxErrors++; return; } currLayer->AddNeuron(n); return; } else if(tag == "layer"){ LayerConstants lc; char *layerName = NULL; ParseLayerConstants(&lc, &layerName, attrs); currLayer = new Layer(); if(layerName) currLayer->LayerName(layerName); currLayer->SetLayerConstants(lc); return; } else if(tag == "network"){ for (unsigned int i = 0; attrs[i] != NULL; i++) { string aname = attrs[i++]; const char* adata = attrs[i]; if(aname == "timeStepSize") { AmTimeInt timeStepSize = atoi(adata); if(timeStepSize == 0) { cerr << "Invalid timeStepSize: 0" << endl; saxErrors++; } net->SetTimeStepSize(timeStepSize); } } return; }}void NetLoader::SAXEndElement1(const char* name){ if(string(name) == "layer"){ try { net->AddLayer(currLayer); } catch (string e) { cerr << e << endl; saxErrors++; } currLayer = NULL; }}void NetLoader::SAXEndElement2(const char* name){ if(name == string("neuron")) { currNeuron = 0; }}void NetLoader::SAXStartElement2(const char* name, const char **attrs){ if(string("synapse") == name){ AmIdInt outputID = 0; float weight = 0.; float delay = 0.; for (unsigned int i = 0; attrs[i] != NULL; i++) { string aname = attrs[i++]; const char* adata = attrs[i]; if(aname == "outputId") { outputID = atoi(adata); } else if(aname == "weight") { weight = atof(adata); } else if(aname == "delay") { delay = atof(adata); } } net->ConnectNeurons(currNeuron, outputID, weight, delay); } else if(string("neuron") == name){ for (unsigned int i = 0; attrs[i] != NULL; i++) { string aname = attrs[i++]; const char* adata = attrs[i]; if(aname == "id") { currNeuron = atoi(adata); } } }}void NetLoader::ParseLayerConstants(LayerConstants *layerConst, char **layerName, const char **attrs){ for (unsigned int i = 0; attrs[i] != NULL; i++) { string aname = attrs[i++]; const char* adata = attrs[i]; if(aname == "type"){ if (string("input") == adata) { layerConst->type = INPUTLAYER; } else if (string("hidden") == adata) { layerConst->type = HIDDENLAYER; } else if (string("output") == adata) { layerConst->type = OUTPUTLAYER; } else { // Something's wrong -- handle the error } } else if (aname == "id") { layerConst->layerId = atoi(adata); } else if (aname == "learningConst") { layerConst->learningConst = atof(adata); } else if (aname == "membraneTimeConst") { layerConst->membraneTimeConst = atof(adata); } else if (aname == "synapticTimeConst") { layerConst->synapticTimeConst = atof(adata); } else if (aname == "thresholdPotential") { layerConst->thresholdPtnl = atof(adata); } else if (aname == "name"){ *layerName = strdup(adata); }// TODO: Rest Potential is not currently supported in Neuron// even though it appears in Layer. Leave it out of the// file for now. //else if (aname == "restPotential"){ // layerConst- >restPtnl = atof(adata); //} }}//////////////////////// End SAX callback stuff //////////////////////7void NetLoader::RegisterNeuronClass(NeuronFactoryBase *factory){ const char *neuronType = strdup(factory->GetType().c_str()); registry[neuronType] = factory;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -