📄 netloader.cpp
字号:
void NetLoader::LoadXML(const char *filename){ SAXPass(filename, 1); SAXPass(filename, 2);}void NetLoader::SaveXML(string filename){ string cmd = "gzip -c > " + filename; FILE *netFile = popen(cmd.c_str(), "w"); if(netFile == NULL) throw runtime_error("cannot open: " + filename); fputs ("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n\n", netFile); fputs ("<!DOCTYPE network PUBLIC \"amygdala.dtd\" \"dtd/amygdala.dtd\">\n", netFile); string networktag = "<network size=\"" + Utilities::itostr(net->Size()) + "\" title=\"Amygdala Spiking Network Data File\" version=\"0.5\" timeStepSize=\""+ Utilities::itostr(net->TimeStepSize()) + "\">\n"; fputs (networktag.c_str(), netFile); if (net->Layered()) { SaveLayers(netFile); } else { } fputs("</network>\n", netFile); fclose(netFile);}void NetLoader::WriteSynapses(FILE *netFile, Neuron* preNrn){ string tmpStr; unsigned int numWeights = preNrn->GetAxonSize(); for (unsigned int j=0; j<numWeights; j++) { string synapseTag = " <synapse outputId=\"" + Utilities::itostr(preNrn->GetAxonID(j)) + "\" weight=\"" + Utilities::ftostr(preNrn->GetAxonWeight(j)) + "\" delay=\"" + Utilities::ftostr(preNrn->GetAxonDelay(j)) + "\"/>\n"; fputs(synapseTag.c_str(), netFile); }}void NetLoader::SaveLayers(FILE *netFile){ LayerConstants layerConst; Layer* currLayer; Neuron* currNeuron; Network::layer_iterator layerItr; Layer::const_iterator nrnItr; // Open Network::layer_iterator and iterate through all // of the layers. for(layerItr = net->layer_begin(); layerItr != net->layer_end(); layerItr++){ // Add a layer to the XML tree and set it's // constants, then iterate through the layer's neurons currLayer = layerItr->second; layerConst = currLayer->GetLayerConstants(); string layerTag = " <layer id=\"" + Utilities::itostr(layerConst.layerId) + "\" name=\"" + currLayer->LayerName() + "\" type=\"" + LayerTypeStr(layerConst.type) + "\" size=\"" + Utilities::itostr(currLayer->size()) + "\" "; // The rest of the constants have default values during // loading, so they aren't required. if (layerConst.learningConst) { layerTag += "learningConst=\"" + Utilities::ftostr(layerConst.learningConst) + "\" "; } if (layerConst.membraneTimeConst) { layerTag += "membraneTimeConst=\"" + Utilities::ftostr(layerConst.membraneTimeConst) + "\" "; } if (layerConst.synapticTimeConst) { layerTag += "synapticTimeConst=\"" + Utilities::ftostr(layerConst.synapticTimeConst) + "\" "; } // TODO: Rest Potential is not currently supported in Neuron // even though it appears in Layer. Leave it out of the // file for now. /* if (layerConst.restPtnl) { layerTag += "rest_potential=\"" + Utilities::ftostr(layerConst.restPtnl) + "\" "; } */ if (layerConst.thresholdPtnl) { layerTag += "thresholdPotential=\"" + Utilities::ftostr(layerConst.thresholdPtnl) + "\" "; } layerTag += ">\n"; fputs(layerTag.c_str(), netFile); for(nrnItr = currLayer->begin(); nrnItr != currLayer->end(); nrnItr++) { currNeuron = *nrnItr; string neuronTag = " <neuron id=\"" + Utilities::itostr(currNeuron->GetID()) + "\" type=\"" + currNeuron->ClassId() + "\">\n"; fputs(neuronTag.c_str(), netFile); WriteSynapses(netFile, currNeuron); fputs(" </neuron>\n", netFile); } fputs(" </layer>\n", netFile); }}string NetLoader::LayerTypeStr(LayerType lType){ switch(lType) { case INPUTLAYER: return "input"; case HIDDENLAYER: return "hidden"; case OUTPUTLAYER: return "output"; case IOLAYER: return "input/output"; } return "";}Layer* NetLoader::BuildLayer(int size, int startNrnId, LayerConstants layerConst, string neuronType){ Layer* tmpLayer = new Layer(); Neuron* tmpNeuron; int nrnCount = startNrnId; NeuronFactoryBase *factory = registry[neuronType.c_str()]; if(!factory) throw string("No factory of type ") + neuronType + " available"; tmpLayer->SetLayerConstants(layerConst); for (int i=0; i<size; i++) { tmpNeuron = factory->NewNeuron(nrnCount++); tmpNeuron->SetLearningConst(layerConst.learningConst); tmpNeuron->SetTimeConstants(layerConst.synapticTimeConst, layerConst.membraneTimeConst); tmpNeuron->SetRestPotential(layerConst.restPtnl); tmpNeuron->SetThresholdPotential(layerConst.thresholdPtnl); tmpLayer->AddNeuron(tmpNeuron); } net->AddLayer(tmpLayer); return tmpLayer;}void NetLoader::SAXPass(const char* filename, int pass){ static xmlSAXHandler firstPassHandler = { 0, /* internalSubset */ 0, /* isStandalone */ 0, /* hasInternalSubset */ 0, /* hasExternalSubset */ 0, /* resolveEntity */ 0, /* getEntity */ 0, /* entityDecl */ 0, /* notationDecl */ 0, /* attributeDecl */ 0, /* elementDecl */ 0, /* unparsedEntityDecl */ 0, /* setDocumentLocator */ 0, /* startDocument */ 0, /* endDocument */ (startElementSAXFunc)__SAXStartElement1, /* startElement */ (endElementSAXFunc)__SAXEndElement1, /* endElement */ 0, /* reference */ 0, /* characters */ 0, /* ignorableWhitespace */ 0, /* processingInstruction */ 0, /* comment */ (warningSAXFunc) __SAXError, /* warning */ (errorSAXFunc) __SAXError, /* error */ (fatalErrorSAXFunc) __SAXError, /* fatalError */ 0, 0, 0 }; static xmlSAXHandler secondPassHandler = { 0, /* internalSubset */ 0, /* isStandalone */ 0, /* hasInternalSubset */ 0, /* hasExternalSubset */ 0, /* resolveEntity */ 0, /* getEntity */ 0, /* entityDecl */ 0, /* notationDecl */ 0, /* attributeDecl */ 0, /* elementDecl */ 0, /* unparsedEntityDecl */ 0, /* setDocumentLocator */ 0, /* startDocument */ 0, /* endDocument */ (startElementSAXFunc)__SAXStartElement2, /* startElement */ (endElementSAXFunc)__SAXEndElement2, /* endElement */ 0, /* reference */ 0, /* characters */ 0, /* ignorableWhitespace */ 0, /* processingInstruction */ 0, /* comment */ (warningSAXFunc) __SAXError, /* warning */ (errorSAXFunc) __SAXError, /* error */ (fatalErrorSAXFunc) __SAXError, /* fatalError */ 0, 0, 0 }; currLayer = NULL; currNeuron = 0; saxErrors = 0; xmlParserCtxtPtr ctxt; ctxt = xmlCreateFileParserCtxt(filename); if (ctxt == NULL) throw string("can't create a parser context:\n ") + filename; switch(pass){ case 1: ctxt->sax = &firstPassHandler; break; case 2: ctxt->sax = &secondPassHandler; break; default: throw string("Pass must be either 1 or 2"); break; } ctxt->userData = this; xmlParseDocument(ctxt); ctxt->sax = NULL; xmlFreeParserCtxt(ctxt); if(saxErrors > 0 )throw string ("Errors during loading...");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -