lsmtopology.cpp

来自「amygdata的神经网络算法源代码」· C++ 代码 · 共 478 行 · 第 1/2 页

CPP
478
字号
        for (int y = 0; y < Y; y++){            for (int z = 0; z < Z; z++){                pProps->SetPosition(x, y, z);                Neuron * n = nf->MakeNeuron(Network::GetNetworkRef()->GetNewNeuronId(), nProps, this);                OutputManager::AddNeuronToGroup(n, HIDDENGROUP);                if(float(rand())/RAND_MAX <= 0.2 ){                    n->Inhibitory(true);                    LOGGER(4, "Inhibitory neuron at " << x << ", " << y << ", " << z);                } else {                    n->Inhibitory(false);                    LOGGER(4, "Excitatory neuron at " << x << ", " << y << ", " << z);                }            }        }    }}void LSMTopology::MakeCube(NFactory* nf, NeuronProperties* np, unsigned int lx, unsigned int ly, unsigned int lz){    X=lx; Y=ly; Z=lz;    LOGGER(1, "Making Cube of size " << X << ", " << Y << ", " << Z);        //NeuronProperties * nProps = nf->MakeNeuronProperties(true);    PhysicalProperties * pProps = np->GetPhysicalProps();        for (int x = 0; x < X; x++){        for (int y = 0; y < Y; y++){            for (int z = 0; z < Z; z++){                pProps->SetPosition(x, y, z);                Neuron * n = nf->MakeNeuron(Network::GetNetworkRef()->GetNewNeuronId(), np, this);                OutputManager::AddNeuronToGroup(n, HIDDENGROUP);                if(float(rand())/RAND_MAX <= 0.2 ){                    n->Inhibitory(true);                    LOGGER(4, "Inhibitory neuron at " << x << ", " << y << ", " << z);                } else {                    n->Inhibitory(false);                    LOGGER(4, "Excitatory neuron at " << x << ", " << y << ", " << z);                }            }        }    }}void LSMTopology::Connect(const float lambda,                           const EI & A,                           const EI & delay,                          const EI & C,                          const EI & U,                          const EI & D,                          const EI & F,                          const  string & sType,						  const float likelyhood){    LOGGER(1, "Connecting the liquid with synapse type " << sType);    int cType;    ConnectorRegistry & cr = ConnectorRegistry::GetRegistry();    NConnector * connector = cr.GetConnector(sType);    if(!connector) throw runtime_error("No connector for synapse type " + sType);    for (iterator itPre=begin(); itPre != end(); itPre++){       for (iterator itPost=begin(); itPost != end(); itPost++){           Neuron * pre = *itPre;           SpikingNeuron * post = dynamic_cast <SpikingNeuron*> (*itPost);           if(!post) continue; // Postneuron is an InputNeuron           if(pre == post) continue;  // no self - connection                      if(!pre->Inhibitory() && !post->Inhibitory()) cType = 0;           else if(!pre->Inhibitory() && post->Inhibitory()) cType = 1;           else if(pre->Inhibitory() && !post->Inhibitory()) cType = 2;           else  cType = 3;                      PhysicalProperties * pPre = pre->Properties()->GetPhysicalProps();           PhysicalProperties * pPost = post->Properties()->GetPhysicalProps();           float dd = (pPre->GetX() - pPost->GetX()) * (pPre->GetX() - pPost->GetX()) +                      (pPre->GetY() - pPost->GetY()) * (pPre->GetY() - pPost->GetY()) +                      (pPre->GetZ() - pPost->GetZ()) * (pPre->GetZ() - pPost->GetZ());           float cChance = C.EIV[cType] * exp(-dd/(lambda*lambda));           if(float(rand())/RAND_MAX < cChance ){  // connect               LOGGER(4, "Connecting " << pre->GetId() << " --> " << post-> GetId());               DynamicSynapseProperties * sProps = dynamic_cast < DynamicSynapseProperties * >                                                                  (connector->GetDefaultProperties());               if(!sProps){                   cerr << __FILE__ << ":" << __LINE__ << ": Cannot convert SynapseProperties" << endl;                   continue;               }               sProps->SetDelay(AmTimeInt(delay.EIV[cType]));            // TODO: The weight is gamma distributed               float tmp=-1;               /*while (tmp<0) {                   tmp = Utilities::GaussRand(A.EIV[cType], A.EIV[cType]);               }*/               sProps->SetDynaParam('A', A.EIV[cType]);            // TODO: The following values are Gauss distributed               /*tmp=-1;               while (tmp<0) {                   tmp = Utilities::GaussRand(U.EIV[cType], U.EIV[cType]/2);               }*/               sProps->SetDynaParam('U', U.EIV[cType]);               /*tmp=-1;               while (tmp<0) {                   tmp = Utilities::GaussRand(D.EIV[cType], D.EIV[cType]/2);               }*/               sProps->SetDynaParam('D', D.EIV[cType]);               /*tmp=-1;               while (tmp<0) {                   tmp = Utilities::GaussRand(F.EIV[cType], F.EIV[cType]/2);               }*/               sProps->SetDynaParam('F', F.EIV[cType]);               connector->Connect(pre, post, *sProps);           }       }    }        if(output.size()){        for (unsigned int i=0; i<output.size(); ++i) {            NConnector * connectorOut = ConnectorRegistry::GetRegistry().GetConnector("StaticSynapse");            if(!connectorOut) throw runtime_error("No connector for synapse type StaticSynapse" );            for (iterator itPre=begin(); itPre != end(); itPre++){                for (iterator itPost=output[i]->begin(); itPost != output[i]->end(); itPost++){                    SpikingNeuron * pre = dynamic_cast <SpikingNeuron*> (*itPre);                    if(!pre) throw runtime_error(string("Neuron: ") +  Utilities::itostr(pre->GetId()) +                                                        ": Not a SpikingNeuron");                     SpikingNeuron * post = dynamic_cast <SpikingNeuron*> (*itPost);                    if(!post) throw runtime_error(string("Neuron: ") +  Utilities::itostr(post->GetId()) +                                                        ": Not a SpikingNeuron");                     StaticSynapseProperties * sProps = dynamic_cast < StaticSynapseProperties * >                                                                     (connectorOut->GetDefaultProperties());                    float w = ((float)(rand())/RAND_MAX)*0.2;                    if (rand()%2) {                        w = w * -1.;    // make half the connections inhibitory                    }                    sProps->SetWeight(w);                    connectorOut->Connect(pre, post, *sProps);                                }            }        }    } else {        LOGGER(1, "No output defined");    }    if(input.size()){        for (unsigned int i=0; i<input.size(); ++i) {            NConnector * connectorIn = ConnectorRegistry::GetRegistry().GetConnector("StaticSynapse");            if(!connectorIn) throw runtime_error("No connector for synapse type StaticSynapse" );            for (iterator itPre=input[i]->begin(); itPre != input[i]->end(); itPre++){                for (iterator itPost = begin(); itPost != end(); itPost++){                    if(float(rand())/RAND_MAX > likelyhood) continue;  // draw random neurons from the liquid                    SpikingNeuron * post = dynamic_cast <SpikingNeuron*> (*itPost);                    if(!post) throw runtime_error(string("Neuron: ") +  Utilities::itostr(post->GetId()) + ": Not a SpikingNeuron");                    Neuron * pre = dynamic_cast <Neuron*> (*itPre);                    StaticSynapseProperties * sProps = dynamic_cast < StaticSynapseProperties * >                                                                     (connectorIn->GetDefaultProperties());                    if (post->Inhibitory()) {                        sProps->SetWeight(0.6);                    }                    else {                        sProps->SetWeight(1.2);                    }                    connectorIn->Connect(pre, post, *sProps);                                }            }            /*for (iterator itPost = begin(); itPost != end(); itPost++){                if(float(rand())/RAND_MAX > likelyhood) continue;  // draw random neurons from the liquid                SpikingNeuron * post = dynamic_cast <SpikingNeuron*> (*itPost);                if(!post) throw runtime_error(string("Neuron: ") +  Utilities::itostr(post->GetId()) + ": Not a SpikingNeuron");                for (iterator itPre=input[i]->begin(); itPre != input[i]->end(); itPre++){                    Neuron * pre = dynamic_cast <Neuron*> (*itPre);                    StaticSynapseProperties * sProps = dynamic_cast < StaticSynapseProperties * >                                                                     (connectorIn->GetDefaultProperties());                    sProps->SetWeight(1.2);                    connectorIn->Connect(pre, post, *sProps);                                }            }*/        }    } else {        LOGGER(1, "No input defined");    }}// Local class OutputLSMTopology::Output::Output(string name) : Topology(name){}LSMTopology::Output::~Output(){}/*void LSMTopology::Output::MakeReadout(const string & nType, unsigned int X, unsigned int Y, unsigned int oNeurons){    LOGGER(3, "Making Outputs");    unsigned int neuronCounter = 1;    NFactory * nf =  dynamic_cast<NFactory*> (Factory::GetRegistry().GetFactory(nType));    if (nf == NULL) throw runtime_error("No neuron type " + nType + " registered!");    NeuronProperties * nProps = nf->MakeNeuronProperties(true);    PhysicalProperties * pProps = nProps->GetPhysicalProps();    // distribute the OutputNeurons alongside at the Y-Z side of the cube starting at X+1, 0, 0    for (int z=0; true; z++){        for (int y = 0; y < Y; y++){            pProps->SetPosition(X + 1, y, z);            Neuron * n = nf->MakeNeuron(Network::GetNetworkRef()->GetNewNeuronId(), nProps, this);            OutputManager::AddNeuronToGroup(n, OUTPUTGROUP);            LOGGER(4, "Output neuron at " << X+1 << ", " << y << ", " << z);            if(++neuronCounter > oNeurons) return;        }    }}*/// Local class InputLSMTopology::Input::Input(string name) : Topology(name){}LSMTopology::Input::~Input(){}InputNeuron * LSMTopology::Input::MakeInputNeuron(int x, int y, int z){    LOGGER(5, "Making Input Neuron at " << x << ", " << y << ", " << z);    NFactory * iFactory = dynamic_cast<NFactory*>                        (Factory::GetRegistry().GetFactory("InputNeuron"));    if(iFactory == NULL) throw runtime_error("Cannot get a NeuronFactory for input");    NeuronProperties * iProps = iFactory->MakeNeuronProperties(true);    iProps->GetPhysicalProps()->SetPosition(x, y, z);    AmIdInt id = Network::GetNetworkRef()->GetNewNeuronId();    InputNeuron * n = dynamic_cast <InputNeuron *> (iFactory->MakeNeuron(id, iProps, this));	OutputManager::AddNeuronToGroup(n, INPUTGROUP);    if(n == NULL) throw runtime_error(string("Creating InputNeuron id ") + Utilities::itostr(id) + "failed");    return n;}   }; // namespace Amygdala

⌨️ 快捷键说明

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