⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 此代码经过大量使用
💻 CPP
字号:
/***************************************************************************                          main.cpp  -  description                             -------------------    copyright            : (C) 2001 by Matt Grover    email                : mgrover@amygdala.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.                                   * ***************************************************************************//***************************************************************************    Code to test the various functions of the Network and NetworkLoader    classes. ***************************************************************************/#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <iostream.h>#include <stdlib.h>#include <string.h>#include <string>//#include "../utilities/fileutils.h"//#include "../utilities/topology.h"#include <amygdala/types.h>#include <amygdala/network.h>#include <amygdala/basicneuron.h>#include <amygdala/alphaneuron.h>#include <amygdala/fastneuron.h>#include <amygdala/netloader.h>#include <amygdala/simplespikeinput.h>#include <amygdala/statisticsoutput.h>#include <amygdala/layer.h>void SetupAlphaNet(Network* mouseNet);void TestLayer();void XorTest(int testCase);void HistogramTest();string dataDir;int main(int argc, char *argv[]){    dataDir = "../data";//    string output;//    cout << "Starting the neuron test..." << endl;    XorTest(0);//    XorTest(1);//    XorTest(2);//    HistogramTest();//    TestAPI();// test the output trace//    Network* testNetwork = new Network;//    NetworkLoader<BasicNeuron>* netLoad;//    netLoad = new NetworkLoader<BasicNeuron>(testNetwork);//    netLoad = new NetworkLoader<AlphaNeuron>(testNetwork);//    netLoad->LoadXML("/home/matt/work/pacman/client/pacman/LayerTestSaveOut.xml");//    Topology top;//    top.TracePath(testNetwork);    cout << "Exiting." << endl;    return EXIT_SUCCESS;}void HistogramTest(){    string output;    string inputFile;    bool errors = false;    cout << "Starting the XOR test..." << endl;    // turn on the output    StatisticsOutput* statOut = new StatisticsOutput;    statOut->SetStepSize(50);    Neuron::CaptureOutput(OUTPUT_LAYERS);    Neuron::SetSpikeOutput(statOut);    NetLoader* netLoad;    Network* testNetwork;    testNetwork = new Network;    SimpleSpikeInput spikeIn(testNetwork);    netLoad = new NetLoader(testNetwork);    inputFile = dataDir + "/data/xor_test.xml";    cout << "Parsing " << inputFile << endl;    try {        netLoad->LoadXML(inputFile.c_str());    } catch (string e) {        cerr << e << endl;        errors = true;    }    //netLoad->LoadXML("LayerTestSaveOut.xml");    if (!errors) {        // test with input to neuron 1 only        //netLoad->ReadInputFile("xor_input_1.txt");        //spikeIn.ReadSpikeList("xor_input_1.txt");        inputFile = dataDir + "/data/xor_input_1b.xml";        cout << "Parsing " << inputFile << endl;        if (spikeIn.ReadSpikeDef(inputFile.c_str())) {            cout << "Results for input to neuron 1 only:\n";            testNetwork->Run(1000000);            // test the mean rate            float meanRate = statOut->MeanSpikeRate(9);            cout << "Mean spike rate: " << meanRate << " spikes/sec.\n";            // test the combined histogram            cout << "Testing the combined histogram...\n";            vector<unsigned int>& histogram = statOut->Histogram();            for (unsigned int i=0; i<histogram.size(); i++) {                cout << "Element " << i << ": " << histogram[i] << endl;            }        }    }}void XorTest(int testCase){    string output;    string inputFile;    bool errors = false;    cout << "Starting the XOR test..." << endl;    // turn on the output    StatisticsOutput* statOut = new StatisticsOutput;    Neuron::CaptureOutput(OUTPUT_LAYERS);    Neuron::SetSpikeOutput(statOut);    NetLoader* netLoad;    Network* testNetwork;    testNetwork = new Network;    SimpleSpikeInput spikeIn(testNetwork);    netLoad = new NetLoader(testNetwork);    inputFile = dataDir + "/data/xor_test.xml";    cout << "Parsing " << inputFile << endl;    try {        netLoad->LoadXML(inputFile.c_str());    } catch (string e) {        cerr << e << endl;        errors = true;    }    //netLoad->LoadXML("LayerTestSaveOut.xml");    if (!errors) {        switch (testCase) {            case 0:                // test with input to neuron 1 only                //netLoad->ReadInputFile("xor_input_1.txt");                //spikeIn.ReadSpikeList("xor_input_1.txt");                inputFile = dataDir + "/data/xor_input_1b.xml";                cout << "Parsing " << inputFile << endl;                if (spikeIn.ReadSpikeDef(inputFile.c_str())) {                    cout << "Results for input to neuron 1 only:\n";                    testNetwork->Run(1000000);                    float meanRate = statOut->MeanSpikeRate(9);                    cout << "Mean spike rate: " << meanRate << " spikes/sec.\n";                }                else {                    errors = true;                }                break;            case 1:                // test with input to neuron 2 only                //netLoad->ReadInputFile("xor_input_2.txt");                spikeIn.ReadSpikeList("xor_input_2.txt");                cout << "Results for input to neuron 2 only:\n";                testNetwork->Run(1000000);                //output = testNetwork->GetCachedOutput();                cout << output;                break;            case 2:                // test with input to both input neurons                //netLoad->ReadInputFile("xor_input_12.txt");                spikeIn.ReadSpikeList("xor_input_12.txt");                cout << "Results for input to neurons 1 and 2:\n";                testNetwork->Run(1000000);                //output = testNetwork->GetCachedOutput();                cout << output;                break;        }    }    if (!errors) {        netLoad->SaveXML("LayerTestSaveOut.xml");    }    else {        cout << "Exiting XorTest() with errors.\n";    }    delete netLoad;    delete testNetwork;}void SetupAlphaNet(Network* alphaNet){    AlphaNeuron* nrn;    nrn = new AlphaNeuron(1);    nrn->SetTimeConstants(2.0, 10.0);    alphaNet->AddNeuron(INPUTLAYER, nrn);    nrn = new AlphaNeuron(2);    nrn->SetTimeConstants(2.0, 10.0);    alphaNet->AddNeuron(INPUTLAYER, nrn);    nrn = new AlphaNeuron(3);    nrn->SetTimeConstants(2.0, 10.0);    alphaNet->AddNeuron(HIDDENLAYER, nrn);    nrn = new AlphaNeuron(4);    nrn->SetTimeConstants(2.0, 10.0);    alphaNet->AddNeuron(HIDDENLAYER, nrn);    nrn = new AlphaNeuron(5);    nrn->SetTimeConstants(2.0, 10.0);    alphaNet->AddNeuron(OUTPUTLAYER, nrn);    nrn = new AlphaNeuron(6);    nrn->SetTimeConstants(2.0, 10.0);    alphaNet->AddNeuron(OUTPUTLAYER, nrn);    alphaNet->ConnectNeurons(1, 3, 1.0);    alphaNet->ConnectNeurons(1, 4, 0.25);    alphaNet->ConnectNeurons(2, 3, 0.35);    alphaNet->ConnectNeurons(2, 4, 0.15);    alphaNet->ConnectNeurons(3, 5, 0.9);    alphaNet->ConnectNeurons(3, 6, 0.34);    alphaNet->ConnectNeurons(4, 5, -0.67);    alphaNet->ConnectNeurons(4, 6, -0.55);}void TestLayer(){    int startId;    LayerConstants layerConst;    Network* testNet = new Network();//    NetworkLoader<Neuron>* netLoad = new NetworkLoader<Neuron>(testNet);    NetLoader* netLoad = new NetLoader(testNet);    Layer* layer1;    Layer* layer2;    GaussConnectType conParms;    startId = testNet->MaxNeuronId() + 1;    //testLayer->SetLayerId(1);    // build a second layer with layer constants    layerConst.type = HIDDENLAYER;    layerConst.layerId = 1;    layerConst.learningConst = 1e-3;    layerConst.membraneTimeConst = 10.0;    layerConst.synapticTimeConst = 2.0;    layerConst.restPtnl = 0.0;    layerConst.thresholdPtnl = 5.0;    startId = testNet->MaxNeuronId() + 1;    layer1 = netLoad->BuildLayer(2, startId, layerConst, "AlphaNeuron");    layer1->LayerName("Layer 1");    layerConst.layerId = 2;    layer2 = netLoad->BuildLayer(2, startId + 10, layerConst, "AlphaNeuron");    layer2->LayerName("Layer 2");    // set up the connection parameters    conParms.meanWeight = 0.5;    conParms.stdDev = 0.15;    conParms.pctConnect = 100.0;    layer1->SetPercentInhibitory(10.0);    layer1->ConnectLayers(layer2, conParms);    netLoad->SaveXML("LayerTestSave.xml");//    testLayer->BuildLayer(50, 1);//    testNet->AddLayer(testLayer);    delete testNet;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -