Beispiel #1
0
def go(d_ens,
       f_ens,
       n_neurons=100,
       t=10,
       m=Uniform(30, 40),
       i=Uniform(-1, 0.6),
       seed=0,
       dt=0.001,
       neuron_type=nengo.LIF(),
       f=DoubleExp(1e-2, 2e-1),
       f_smooth=DoubleExp(1e-2, 2e-1),
       freq=1,
       w_ff=None,
       w_fb=None,
       e_ff=None,
       e_fb=None,
       L_ff=False,
       L_fb=False,
       L_fd=False,
       supervised=False):

    w = 2 * np.pi * freq
    A = [[0, -w], [w, 0]]
    B = [[1], [0]]
    C = [[1, 0]]
    D = [[0]]
    sys = LinearSystem((A, B, C, D))

    with nengo.Network(seed=seed) as model:

        u = nengo.Node(lambda t: [np.sin(w * t), np.cos(w * t)])

        # Ensembles
        pre = nengo.Ensemble(300,
                             2,
                             neuron_type=SpikingRectifiedLinear(),
                             seed=seed,
                             radius=2)
        ens = nengo.Ensemble(n_neurons,
                             2,
                             max_rates=m,
                             intercepts=i,
                             neuron_type=neuron_type,
                             seed=seed,
                             radius=2)
        nengo.Connection(u, pre, synapse=None, seed=seed)
        pre_ens = nengo.Connection(pre, ens, synapse=f, seed=seed)

        if L_ff:
            supv = nengo.Ensemble(n_neurons,
                                  2,
                                  max_rates=m,
                                  intercepts=i,
                                  neuron_type=LIF(),
                                  seed=seed,
                                  radius=2)
            nengo.Connection(pre, supv, synapse=f, seed=seed)
            node = LearningNode2(n_neurons, pre.n_neurons, pre_ens, k=3e-6)
            nengo.Connection(pre.neurons, node[0:pre.n_neurons], synapse=f)
            nengo.Connection(ens.neurons,
                             node[pre.n_neurons:pre.n_neurons + n_neurons],
                             synapse=f_smooth)
            nengo.Connection(supv.neurons,
                             node[pre.n_neurons + n_neurons:pre.n_neurons +
                                  2 * n_neurons],
                             synapse=f_smooth)
            nengo.Connection(u, node[-2:], synapse=f)
            p_supv = nengo.Probe(supv.neurons, synapse=None)

        if L_fb or supervised:
            supv = nengo.Ensemble(n_neurons,
                                  2,
                                  max_rates=m,
                                  intercepts=i,
                                  neuron_type=neuron_type,
                                  seed=seed,
                                  radius=2)
            #             supv2 = nengo.Ensemble(n_neurons, 2, max_rates=m, intercepts=i, neuron_type=neuron_type, seed=seed, radius=2)
            #             pre2 = nengo.Ensemble(300, 2, neuron_type=SpikingRectifiedLinear(), seed=seed, radius=2)
            #             nengo.Connection(u, pre2, synapse=f, seed=seed)
            pre_supv = nengo.Connection(pre, supv, synapse=f, seed=seed)
            #             pre2_supv2 = nengo.Connection(pre2, supv2, synapse=f, seed=seed)
            supv_ens = nengo.Connection(supv,
                                        ens,
                                        synapse=f_ens,
                                        seed=seed,
                                        solver=NoSolver(d_ens))
            p_supv = nengo.Probe(supv.neurons, synapse=None)
#             p_supv2 = nengo.Probe(supv2.neurons, synapse=None)

        if L_fb:
            node = LearningNode2(n_neurons, n_neurons, supv_ens, k=3e-6)
            nengo.Connection(supv.neurons, node[0:n_neurons], synapse=f_ens)
            nengo.Connection(ens.neurons,
                             node[n_neurons:2 * n_neurons],
                             synapse=f_smooth)
            #             nengo.Connection(supv2.neurons, node[2*n_neurons: 3*n_neurons], synapse=f_smooth)
            nengo.Connection(supv.neurons,
                             node[2 * n_neurons:3 * n_neurons],
                             synapse=f_smooth)
            nengo.Connection(u, node[-2:], synapse=f)

        if not L_ff and not L_fb and not L_fd and not supervised:
            off = nengo.Node(lambda t: (t > 1.0))
            nengo.Connection(off,
                             pre.neurons,
                             synapse=None,
                             transform=-1e3 * np.ones((pre.n_neurons, 1)))
            ens_ens = nengo.Connection(ens,
                                       ens,
                                       synapse=f_ens,
                                       seed=seed,
                                       solver=NoSolver(d_ens))

        # Probes
        p_u = nengo.Probe(u, synapse=None)
        p_ens = nengo.Probe(ens.neurons, synapse=None)

    with nengo.Simulator(model, seed=seed, dt=dt) as sim:
        if np.any(w_ff):
            for pre in range(pre.n_neurons):
                for post in range(n_neurons):
                    if L_fb or supervised:
                        pre_supv.weights[pre, post] = w_ff[pre, post]
                        pre_supv.netcons[pre,
                                         post].weight[0] = np.abs(w_ff[pre,
                                                                       post])
                        pre_supv.netcons[
                            pre,
                            post].syn().e = 0 if w_ff[pre, post] > 0 else -70


#                         pre2_supv2.weights[pre, post] = w_ff[pre, post]
#                         pre2_supv2.netcons[pre, post].weight[0] = np.abs(w_ff[pre, post])
#                         pre2_supv2.netcons[pre, post].syn().e = 0 if w_ff[pre, post] > 0 else -70
                    else:
                        pre_ens.weights[pre, post] = w_ff[pre, post]
                        pre_ens.netcons[pre,
                                        post].weight[0] = np.abs(w_ff[pre,
                                                                      post])
                        pre_ens.netcons[
                            pre,
                            post].syn().e = 0 if w_ff[pre, post] > 0 else -70
        if np.any(e_ff) and L_ff:
            pre_ens.e = e_ff
        if np.any(w_fb):
            for pre in range(n_neurons):
                for post in range(n_neurons):
                    if L_fb or supervised:
                        supv_ens.weights[pre, post] = w_fb[pre, post]
                        supv_ens.netcons[pre,
                                         post].weight[0] = np.abs(w_fb[pre,
                                                                       post])
                        supv_ens.netcons[
                            pre,
                            post].syn().e = 0 if w_fb[pre, post] > 0 else -70
                    else:
                        ens_ens.weights[pre, post] = w_fb[pre, post]
                        ens_ens.netcons[pre,
                                        post].weight[0] = np.abs(w_fb[pre,
                                                                      post])
                        ens_ens.netcons[
                            pre,
                            post].syn().e = 0 if w_fb[pre, post] > 0 else -70
        if np.any(e_fb) and L_fb:
            supv_ens.e = e_fb

        neuron.h.init()
        sim.run(t)
        reset_neuron(sim, model)

    if L_ff and hasattr(pre_ens, 'weights'):
        w_ff = pre_ens.weights
        e_ff = pre_ens.e
    if L_fb and hasattr(supv_ens, 'weights'):
        w_fb = supv_ens.weights
        e_fb = supv_ens.e

    return dict(
        times=sim.trange(),
        u=sim.data[p_u],
        ens=sim.data[p_ens],
        supv=sim.data[p_supv] if L_ff or L_fb or supervised else None,
        #         supv2=sim.data[p_supv2] if L_fb or supervised else None,
        w_ff=w_ff,
        w_fb=w_fb,
        e_ff=e_ff,
        e_fb=e_fb,
    )
Beispiel #2
0
def go(ePExc=None, eIExc=None, ePInh=None, eIInh=None, ePP=None, ePI=None, eIP=None, eII=None, wPExc=None, wIExc=None, wPInh=None, wIInh=None, wPP=None, wPI=None, wIP=None, wII=None, dNMDA=None, dAMPA=None, dGABA=None, f_NMDA=None, f_AMPA=None, f_GABA=None, f_s=None, stim=lambda t: 0, DA=lambda t: 0, n_pre=200, n_neurons=30, t=10, dt=0.001, m=Uniform(30, 40), i=Uniform(-1, 0.8), kFF=-2, kFB=-2, seed=0, stage=0):
    
    wDaInpt = kFF*np.ones((n_pre, 1))
    wDaFdbk = kFB*np.ones((n_neurons, 1))
    with nengo.Network(seed=seed) as model:
        # Stimulus and Nodes
        u = nengo.Node(stim)
        uDA = nengo.Node(DA)
        # Ensembles
        pre = nengo.Ensemble(n_pre, 1, seed=seed, label="pre")
        P = nengo.Ensemble(n_neurons, 1, max_rates=m, intercepts=i, neuron_type=BioNeuron("Pyramidal", DA=DA), seed=seed, label="P")
        I = nengo.Ensemble(n_neurons, 1, max_rates=m, intercepts=i, neuron_type=BioNeuron("Interneuron", DA=DA), seed=seed, label="I")
        supv = nengo.Ensemble(n_neurons, 1, max_rates=m, intercepts=i, neuron_type=nengo.LIF(), seed=seed)
        gate = nengo.Ensemble(n_pre, 1, seed=seed, label="gate")
        buffer = nengo.Ensemble(n_neurons, 1, max_rates=m, intercepts=i, neuron_type=nengo.LIF(), seed=seed)
        fdbk = nengo.Ensemble(n_neurons, 1, max_rates=m, intercepts=i, neuron_type=nengo.LIF(), seed=seed)
        # Connections
        uPre = nengo.Connection(u, pre, synapse=None, seed=seed)
        prePExc = nengo.Connection(pre, P, synapse=AMPA(), seed=seed)
        preIExc = nengo.Connection(pre, I, synapse=AMPA(), seed=seed)
        prePInh = nengo.Connection(pre, P, synapse=GABA(), seed=seed)
        preIInh = nengo.Connection(pre, I, synapse=GABA(), seed=seed)
        nengo.Connection(u, gate, synapse=None)
        nengo.Connection(gate, buffer, synapse=f_AMPA)
        nengo.Connection(buffer, fdbk, synapse=f_NMDA)
        nengo.Connection(fdbk, buffer, synapse=f_AMPA)
        nengo.Connection(uDA, gate.neurons, transform=wDaInpt, function=lambda x: x)
        nengo.Connection(uDA, fdbk.neurons, transform=wDaFdbk, function=lambda x: 1-x)
        # Probes
        p_u = nengo.Probe(u, synapse=None)
        p_DA = nengo.Probe(uDA, synapse=None)
        p_P = nengo.Probe(P.neurons, synapse=None)
        p_I = nengo.Probe(I.neurons, synapse=None)
        p_supv = nengo.Probe(supv.neurons, synapse=None)
        p_supv_x = nengo.Probe(supv, synapse=f_NMDA)
        p_gate = nengo.Probe(gate.neurons, synapse=None)
        p_gate_x = nengo.Probe(gate, synapse=f_AMPA)
        p_buffer = nengo.Probe(buffer.neurons, synapse=None)
        p_buffer_x = nengo.Probe(buffer, synapse=f_NMDA)
        p_fdbk = nengo.Probe(fdbk.neurons, synapse=None)
        p_fdbk_x = nengo.Probe(fdbk, synapse=f_NMDA)
        # Training
        if stage == 1:
            nengo.Connection(u, supv, synapse=f_AMPA, seed=seed)
            node = WNode(prePExc, alpha=1e-4, exc=True)
            nengo.Connection(pre.neurons, node[0:n_pre], synapse=f_AMPA)
            nengo.Connection(P.neurons, node[n_pre:n_pre+n_neurons], synapse=f_s)
            nengo.Connection(supv.neurons, node[n_pre+n_neurons: n_pre+2*n_neurons], synapse=f_s)
            node2 = WNode(preIExc, alpha=1e-5, exc=True)
            nengo.Connection(pre.neurons, node2[0:n_pre], synapse=f_AMPA)
            nengo.Connection(I.neurons, node2[n_pre:n_pre+n_neurons], synapse=f_s)
            nengo.Connection(supv.neurons, node2[n_pre+n_neurons: n_pre+2*n_neurons], synapse=f_s)
            node3 = WNode(prePInh, alpha=1e-4, inh=True)
            nengo.Connection(pre.neurons, node3[0:n_pre], synapse=f_GABA)
            nengo.Connection(P.neurons, node3[n_pre:n_pre+n_neurons], synapse=f_s)
            nengo.Connection(supv.neurons, node3[n_pre+n_neurons: n_pre+2*n_neurons], synapse=f_s)
            node4 = WNode(preIInh, alpha=1e-5, inh=True)
            nengo.Connection(pre.neurons, node4[0:n_pre], synapse=f_GABA)
            nengo.Connection(I.neurons, node4[n_pre:n_pre+n_neurons], synapse=f_s)
            nengo.Connection(supv.neurons, node4[n_pre+n_neurons: n_pre+2*n_neurons], synapse=f_s)
        if stage == 2:
            nengo.Connection(u, supv, synapse=f_AMPA, seed=seed)
        if stage == 3:
            #PTar, ITar have target activities from a gated integrator
            #PDrive, IDrive drive P, I with input from "feedback" pop
#             PTar = nengo.Ensemble(n_neurons, 1, max_rates=m, intercepts=i, neuron_type=BioNeuron("Pyramidal", DA=lambda t: 0), seed=seed, label="PTar")
#             ITar = nengo.Ensemble(n_neurons, 1, max_rates=m, intercepts=i, neuron_type=BioNeuron("Interneuron", DA=lambda t: 0), seed=seed, label="ITar")
#             PDrive = nengo.Ensemble(n_neurons, 1, max_rates=m, intercepts=i, neuron_type=BioNeuron("Pyramidal", DA=DA), seed=seed, label="PDrive")  # DA?
#             IDrive = nengo.Ensemble(n_neurons, 1, max_rates=m, intercepts=i, neuron_type=BioNeuron("Interneuron", DA=DA), seed=seed, label="IDrive")  # DA?
            # gated integrator populations drive PDrive/IDrive and PTar/ITar
#             bufferPPTar = nengo.Connection(buffer, PTar, synapse=f_AMPA, seed=seed)
#             bufferPITar = nengo.Connection(buffer, ITar, synapse=f_AMPA, seed=seed)
#             bufferIPTar = nengo.Connection(buffer, PTar, synapse=f_GABA, seed=seed)
#             bufferIITar = nengo.Connection(buffer, ITar, synapse=f_GABA, seed=seed)
#             fdbkPPDrive = nengo.Connection(fdbk, PDrive, synapse=f_AMPA, seed=seed) 
#             fdbkPIDrive = nengo.Connection(fdbk, IDrive, synapse=f_AMPA, seed=seed)
#             fdbkIPDrive = nengo.Connection(fdbk, PDrive, synapse=f_GABA, seed=seed) 
#             fdbkIIDrive = nengo.Connection(fdbk, IDrive, synapse=f_GABA, seed=seed)
#             # PDrive, IDrive drive P and I with ideal feedback activities given current gate state
#             PDriveP = nengo.Connection(PDrive, P, synapse=NMDA(), solver=NoSolver(dNMDA), seed=seed) 
#             PDriveI = nengo.Connection(PDrive, I, synapse=NMDA(), solver=NoSolver(dNMDA), seed=seed) 
#             IDriveP = nengo.Connection(IDrive, P, synapse=GABA(), solver=NoSolver(dGABA), seed=seed) 
#             IDriveI = nengo.Connection(IDrive, I, synapse=GABA(), solver=NoSolver(dGABA), seed=seed) 
            # P and I are driven with ideal feedback from fdbk
            fdbkPExc = nengo.Connection(fdbk, P, synapse=NMDA(), seed=seed) 
            fdbkIExc = nengo.Connection(fdbk, I, synapse=NMDA(), seed=seed) 
            fdbkPInh = nengo.Connection(fdbk, P, synapse=GABA(), seed=seed) 
            fdbkIInh = nengo.Connection(fdbk, I, synapse=GABA(), seed=seed) 
            # P and I have target activities from buffer
            node = WNode(fdbkPExc, alpha=1e-5, exc=True)
            nengo.Connection(fdbk.neurons, node[0:n_neurons], synapse=f_NMDA)
            nengo.Connection(P.neurons, node[n_neurons:2*n_neurons], synapse=f_s)
            nengo.Connection(buffer.neurons, node[2*n_neurons: 3*n_neurons], synapse=f_s)
            node2 = WNode(fdbkIExc, alpha=1e-6, exc=True)
            nengo.Connection(fdbk.neurons, node2[0:n_neurons], synapse=f_NMDA)
            nengo.Connection(I.neurons, node2[n_neurons:2*n_neurons], synapse=f_s)
            nengo.Connection(buffer.neurons, node2[2*n_neurons: 3*n_neurons], synapse=f_s)
            node3 = WNode(fdbkPInh, alpha=1e-5, inh=True)
            nengo.Connection(fdbk.neurons, node3[0:n_neurons], synapse=f_GABA)
            nengo.Connection(P.neurons, node3[n_neurons:2*n_neurons], synapse=f_s)
            nengo.Connection(buffer.neurons, node3[2*n_neurons: 3*n_neurons], synapse=f_s)
            node4 = WNode(fdbkIInh, alpha=1e-6, inh=True)
            nengo.Connection(fdbk.neurons, node4[0:n_neurons], synapse=f_GABA)
            nengo.Connection(I.neurons, node4[n_neurons:2*n_neurons], synapse=f_s)
            nengo.Connection(buffer.neurons, node4[2*n_neurons: 3*n_neurons], synapse=f_s)
#             p_PDrive = nengo.Probe(PDrive.neurons, synapse=None)
#             p_IDrive = nengo.Probe(IDrive.neurons, synapse=None)
#             p_PTar = nengo.Probe(PTar.neurons, synapse=None)
#             p_ITar = nengo.Probe(ITar.neurons, synapse=None)
        if stage == 4:
            PP = nengo.Connection(P, P, synapse=f_NMDA, seed=seed, solver=NoSolver(dNMDA))
            PI = nengo.Connection(P, I, synapse=f_NMDA, seed=seed, solver=NoSolver(dNMDA))
            IP = nengo.Connection(I, P, synapse=f_GABA, seed=seed, solver=NoSolver(dGABA))
            II = nengo.Connection(I, I, synapse=f_GABA, seed=seed, solver=NoSolver(dGABA))

    with nengo.Simulator(model, seed=seed, progress_bar=False) as sim:
        if stage > 1:
            for pre in range(n_pre):
                for post in range(n_neurons):
                    prePExc.weights[pre, post] = wPExc[pre, post]
                    prePExc.netcons[pre, post].weight[0] = np.abs(wPExc[pre, post])
                    prePExc.netcons[pre, post].syn().e = 0 if wPExc[pre, post] > 0 else -70
                    preIExc.weights[pre, post] = wIExc[pre, post]
                    preIExc.netcons[pre, post].weight[0] = np.abs(wIExc[pre, post])
                    preIExc.netcons[pre, post].syn().e = 0 if wIExc[pre, post] > 0 else -70
                    prePInh.weights[pre, post] = wPInh[pre, post]
                    prePInh.netcons[pre, post].weight[0] = np.abs(wPInh[pre, post])
                    prePInh.netcons[pre, post].syn().e = 0 if wPInh[pre, post] > 0 else -70
                    preIInh.weights[pre, post] = wIInh[pre, post]
                    preIInh.netcons[pre, post].weight[0] = np.abs(wIInh[pre, post])
                    preIInh.netcons[pre, post].syn().e = 0 if wIInh[pre, post] > 0 else -70
#         if stage==3:
#             for pre in range(n_pre):
#                 for post in range(n_neurons):
#                     bufferPPTar.weights[pre, post] = wpPP[pre, post]
#                     bufferPPTar.netcons[pre, post].weight[0] = np.abs(wpPP[pre, post])
#                     bufferPPTar.netcons[pre, post].syn().e = 0 if wpPP[pre, post] > 0 else -70
#                     bufferPITar.weights[pre, post] = wpPI[pre, post]
#                     bufferPITar.netcons[pre, post].weight[0] = np.abs(wpPI[pre, post])
#                     bufferPITar.netcons[pre, post].syn().e = 0 if wpPI[pre, post] > 0 else -70
#                     bufferIPTar.weights[pre, post] = wpIP[pre, post]
#                     bufferIPTar.netcons[pre, post].weight[0] = np.abs(wpIP[pre, post])
#                     bufferIPTar.netcons[pre, post].syn().e = 0 if wpIP[pre, post] > 0 else -70
#                     bufferIITar.weights[pre, post] = wpII[pre, post]
#                     bufferIITar.netcons[pre, post].weight[0] = np.abs(wpII[pre, post])
#                     bufferIITar.netcons[pre, post].syn().e = 0 if wpII[pre, post] > 0 else -70
#                     fdbkPPDrive.weights[pre, post] = wpPP[pre, post]
#                     fdbkPPDrive.netcons[pre, post].weight[0] = np.abs(wpPP[pre, post])
#                     fdbkPPDrive.netcons[pre, post].syn().e = 0 if wpPP[pre, post] > 0 else -70
#                     fdbkPIDrive.weights[pre, post] = wpPI[pre, post]
#                     fdbkPIDrive.netcons[pre, post].weight[0] = np.abs(wpPI[pre, post])
#                     fdbkPIDrive.netcons[pre, post].syn().e = 0 if wpPI[pre, post] > 0 else -70
#                     fdbkIPDrive.weights[pre, post] = wpIP[pre, post]
#                     fdbkIPDrive.netcons[pre, post].weight[0] = np.abs(wpIP[pre, post])
#                     fdbkIPDrive.netcons[pre, post].syn().e = 0 if wpIP[pre, post] > 0 else -70
#                     fdbkIIDrive.weights[pre, post] = wpII[pre, post]
#                     fdbkIIDrive.netcons[pre, post].weight[0] = np.abs(wpII[pre, post])
#                     fdbkIIDrive.netcons[pre, post].syn().e = 0 if wpII[pre, post] > 0 else -70
        if stage==4:
            for pre in range(n_neurons):
                for post in range(n_neurons):
                    PP.weights[pre, post] = wPP[pre, post]
                    PP.netcons[pre, post].weight[0] = np.abs(wPP[pre, post])
                    PP.netcons[pre, post].syn().e = 0 if wPP[pre, post] > 0 else -70
                    PI.weights[pre, post] = wPI[pre, post]
                    PI.netcons[pre, post].weight[0] = np.abs(wPI[pre, post])
                    PI.netcons[pre, post].syn().e = 0 if wPI[pre, post] > 0 else -70
                    IP.weights[pre, post] = wIP[pre, post]
                    IP.netcons[pre, post].weight[0] = np.abs(wIP[pre, post])
                    IP.netcons[pre, post].syn().e = 0 if wIP[pre, post] > 0 else -70
                    II.weights[pre, post] = wII[pre, post]
                    II.netcons[pre, post].weight[0] = np.abs(wII[pre, post])
                    II.netcons[pre, post].syn().e = 0 if wII[pre, post] > 0 else -70 
        if stage==1:
            if np.any(ePExc): prePExc.e = ePExc
            if np.any(eIExc): preIExc.e = eIExc
            if np.any(ePInh): prePInh.e = ePInh
            if np.any(eIInh): preIInh.e = eIInh
        if stage==3:
            if np.any(ePP): fdbkPExc.e = ePP
            if np.any(ePI): fdbkIExc.e = ePI
            if np.any(eIP): fdbkPInh.e = eIP
            if np.any(eII): fdbkIInh.e = eII
        neuron.h.init()
        sim.run(t, progress_bar=True)
        reset_neuron(sim, model) 
        
    if stage == 1:
        ePExc = prePExc.e
        wPExc = prePExc.weights
        eIExc = preIExc.e
        wIExc = preIExc.weights
        ePInh = prePInh.e
        wPInh = prePInh.weights
        eIInh = preIInh.e
        wIInh = preIInh.weights
    if stage == 3:
        ePP = fdbkPExc.e
        wPP = fdbkPExc.weights
        ePI = fdbkIExc.e
        wPI = fdbkIExc.weights
        eIP = fdbkPInh.e
        wIP = fdbkPInh.weights
        eII = fdbkIInh.e
        wII = fdbkIInh.weights
    return dict(
        times=sim.trange(),
        u=sim.data[p_u],
        uDA=sim.data[p_DA],
        P=sim.data[p_P],
        I=sim.data[p_I],
        supv=sim.data[p_supv],
        supv_x=sim.data[p_supv_x],
        gate=sim.data[p_gate],
        gate_x=sim.data[p_gate_x],
        buffer=sim.data[p_buffer],
        buffer_x=sim.data[p_buffer_x],
        fdbk=sim.data[p_fdbk],
        fdbk_x=sim.data[p_fdbk_x],
        ePExc=ePExc,
        wPExc=wPExc,
        eIExc=eIExc,
        wIExc=wIExc,
        ePInh=ePInh,
        wPInh=wPInh,
        eIInh=eIInh,
        wIInh=wIInh,
        ePP=ePP,
        wPP=wPP,
        ePI=ePI,
        wPI=wPI,
        eIP=eIP,
        wIP=wIP,
        eII=eII,
        wII=wII,
#         PDrive=sim.data[p_PDrive] if stage==3 else None,
#         IDrive=sim.data[p_IDrive] if stage==3 else None,
#         PTar=sim.data[p_PTar] if stage==3 else None,
#         ITar=sim.data[p_ITar] if stage==3 else None,
    )
Beispiel #3
0
def go(d_ens, f_ens, n_neurons=100, n_pre=100, t=10, m=Uniform(30, 40), i=Uniform(-1, 0.6), seed=1, dt=0.001,
        f=DoubleExp(1e-3, 3e-2), f_smooth=DoubleExp(1e-2, 2e-1), stim_func1=lambda t: np.sin(t), stim_func2=lambda t: np.sin(t),
        neuron_type=LIF(), w_ens=None, e_ens=None, w_ens2=None, e_ens2=None, L=False, L2=False):

    with nengo.Network(seed=seed) as model:

        # Stimulus and Nodes
        u = nengo.Node(stim_func1)
        u2 = nengo.Node(stim_func2)

        # Ensembles
        pre = nengo.Ensemble(n_pre, 2, radius=2, seed=seed)
        ens = nengo.Ensemble(n_neurons, 2, radius=2, max_rates=m, intercepts=i, neuron_type=neuron_type, seed=seed)
        supv = nengo.Ensemble(n_neurons, 2, radius=2, max_rates=m, intercepts=i, neuron_type=LIF(), seed=seed)
        ens2 = nengo.Ensemble(30, 1, max_rates=m, intercepts=i, neuron_type=neuron_type, seed=seed+1)
        supv2 = nengo.Ensemble(30, 1, max_rates=m, intercepts=i, neuron_type=LIF(), seed=seed+1)
        x = nengo.Ensemble(1, 2, neuron_type=nengo.Direct())
        x2 = nengo.Ensemble(1, 1, neuron_type=nengo.Direct())

        # Connections
        nengo.Connection(u, pre[0], synapse=None, seed=seed)
        nengo.Connection(u2, pre[1], synapse=None, seed=seed)
        nengo.Connection(u, x[0], synapse=f, seed=seed)
        nengo.Connection(u2, x[1], synapse=f, seed=seed)
        conn = nengo.Connection(pre, ens, synapse=f, seed=seed, label='pre_ens')
        nengo.Connection(x, supv, synapse=None, seed=seed)
        nengo.Connection(x2, supv2, synapse=None, seed=seed)
        nengo.Connection(x, x2, synapse=f, function=multiply, seed=seed+1)
#         nengo.Connection(x, x2, synapse=f_ens, function=multiply, seed=seed+1)
        if isinstance(neuron_type, DurstewitzNeuron): # todo: fix nosolver bug
            conn2 = nengo.Connection(ens[0], ens2, synapse=f_ens, seed=seed+1, solver=NoSolver(d_ens), label='ens-ens2')
        else:
            conn2 = nengo.Connection(ens.neurons, ens2, synapse=f_ens, seed=seed+1, transform=d_ens.T, label='ens-ens2')

        if L:
            node = LearningNode2(n_neurons, n_pre, conn, k=3e-6)
            nengo.Connection(pre.neurons, node[0:pre.n_neurons], synapse=f)
            nengo.Connection(ens.neurons, node[pre.n_neurons:pre.n_neurons+n_neurons], synapse=f_smooth)
            nengo.Connection(supv.neurons, node[pre.n_neurons+n_neurons: pre.n_neurons+2*n_neurons], synapse=f_smooth)
            nengo.Connection(x, node[-2:], synapse=None)

        if L2:
            node2 = LearningNode2(30, n_neurons, conn2, k=3e-6)
            nengo.Connection(ens.neurons, node2[0:n_neurons], synapse=f_ens) 
            nengo.Connection(ens2.neurons, node2[n_neurons:n_neurons+30], synapse=f_smooth)
            nengo.Connection(supv2.neurons, node2[n_neurons+30: n_neurons+60], synapse=f_smooth)
            nengo.Connection(x2, node2[-1], synapse=None)

        # Probes
        p_u = nengo.Probe(u, synapse=None)
        p_u2 = nengo.Probe(u2, synapse=None)
        p_ens = nengo.Probe(ens.neurons, synapse=None)
        p_ens2 = nengo.Probe(ens2.neurons, synapse=None)
        p_supv = nengo.Probe(supv.neurons, synapse=None)
        p_supv2 = nengo.Probe(supv2.neurons, synapse=None)
        p_x = nengo.Probe(x, synapse=None)
        p_x2 = nengo.Probe(x2, synapse=None)

    with nengo.Simulator(model, seed=seed, dt=dt) as sim:
        if np.any(w_ens):
            for pre in range(pre.n_neurons):
                for post in range(n_neurons):
                    conn.weights[pre, post] = w_ens[pre, post]
                    conn.netcons[pre, post].weight[0] = np.abs(w_ens[pre, post])
                    conn.netcons[pre, post].syn().e = 0 if w_ens[pre, post] > 0 else -70
        if np.any(w_ens2):
            for pre in range(n_neurons):
                for post in range(30):
                    conn2.weights[pre, post] = w_ens2[pre, post]
                    conn2.netcons[pre, post].weight[0] = np.abs(w_ens2[pre, post])
                    conn2.netcons[pre, post].syn().e = 0 if w_ens2[pre, post] > 0 else -70
        if np.any(e_ens):
            conn.e = e_ens
        if np.any(e_ens2):
            conn2.e = e_ens2
        neuron.h.init()
        sim.run(t)
        reset_neuron(sim, model) 

    if L and hasattr(conn, 'weights'):
        w_ens = conn.weights
        e_ens = conn.e
    if L2 and hasattr(conn2, 'weights'):
        w_ens2 = conn2.weights
        e_ens2 = conn2.e
        
    return dict(
        times=sim.trange(),
        u=sim.data[p_u],
        u2=sim.data[p_u2],
        ens=sim.data[p_ens],
        ens2=sim.data[p_ens2],
        x=sim.data[p_x],
        x2=sim.data[p_x2],
        supv=sim.data[p_supv],
        supv2=sim.data[p_supv2],
        enc=sim.data[supv].encoders,
        enc2=sim.data[supv2].encoders,
        w_ens=w_ens,
        e_ens=e_ens,
        w_ens2=w_ens2,
        e_ens2=e_ens2)
Beispiel #4
0
def go(d_ens,
       f_ens,
       n_pre=100,
       n_neurons=30,
       t=10,
       m=Uniform(30, 40),
       i=Uniform(-1, 0.6),
       seed=1,
       dt=0.001,
       f=Lowpass(0.01),
       f_smooth=DoubleExp(1e-2, 2e-1),
       neuron_type=LIF(),
       w_ens=None,
       e_ens=None,
       w_ens2=None,
       e_ens2=None,
       L=False,
       L2=False,
       stim_func=lambda t: np.sin(t)):

    with nengo.Network(seed=seed) as model:

        # Stimulus and Nodes
        u = nengo.Node(stim_func)

        # Ensembles
        pre = nengo.Ensemble(n_pre, 1, radius=1.5, seed=seed)
        ens = nengo.Ensemble(n_neurons,
                             1,
                             max_rates=m,
                             intercepts=i,
                             neuron_type=neuron_type,
                             seed=seed)
        ens2 = nengo.Ensemble(n_neurons,
                              1,
                              max_rates=m,
                              intercepts=i,
                              neuron_type=neuron_type,
                              seed=seed + 1)
        supv = nengo.Ensemble(n_neurons,
                              1,
                              max_rates=m,
                              intercepts=i,
                              neuron_type=LIF(),
                              seed=seed)
        supv2 = nengo.Ensemble(n_neurons,
                               1,
                               max_rates=m,
                               intercepts=i,
                               neuron_type=LIF(),
                               seed=seed + 1)
        x = nengo.Ensemble(1, 1, neuron_type=nengo.Direct())
        x2 = nengo.Ensemble(1, 1, neuron_type=nengo.Direct())

        # Connections
        nengo.Connection(u, pre, synapse=None, seed=seed)
        conn = nengo.Connection(pre, ens, synapse=f, seed=seed)
        conn2 = nengo.Connection(ens,
                                 ens2,
                                 synapse=f_ens,
                                 seed=seed + 1,
                                 solver=NoSolver(d_ens))
        nengo.Connection(x, supv, synapse=None, seed=seed)
        nengo.Connection(x2, supv2, synapse=None, seed=seed + 1)
        nengo.Connection(u, x, synapse=f, seed=seed)
        nengo.Connection(x, x2, synapse=f, seed=seed + 1)

        # Probes
        p_u = nengo.Probe(u, synapse=None)
        p_ens = nengo.Probe(ens.neurons, synapse=None)
        p_v = nengo.Probe(ens.neurons, 'voltage', synapse=None)
        p_x = nengo.Probe(x, synapse=None)
        p_ens2 = nengo.Probe(ens2.neurons, synapse=None)
        p_v2 = nengo.Probe(ens2.neurons, 'voltage', synapse=None)
        p_x2 = nengo.Probe(x2, synapse=None)
        p_supv = nengo.Probe(supv.neurons, synapse=None)
        p_supv2 = nengo.Probe(supv2.neurons, synapse=None)

        # Bioneurons
        if L and isinstance(neuron_type, DurstewitzNeuron):
            node = LearningNode2(n_neurons, n_pre, conn, k=3e-6)
            nengo.Connection(pre.neurons, node[0:n_pre], synapse=f)
            nengo.Connection(ens.neurons,
                             node[n_pre:n_pre + n_neurons],
                             synapse=f_smooth)
            nengo.Connection(supv.neurons,
                             node[n_pre + n_neurons:n_pre + 2 * n_neurons],
                             synapse=f_smooth)
        if L2 and isinstance(neuron_type, DurstewitzNeuron):
            node2 = LearningNode2(n_neurons, n_neurons, conn2, k=3e-6)
            nengo.Connection(ens.neurons, node2[0:n_neurons], synapse=f_ens)
            nengo.Connection(ens2.neurons,
                             node2[n_neurons:2 * n_neurons],
                             synapse=f_smooth)
            nengo.Connection(supv2.neurons,
                             node2[2 * n_neurons:3 * n_neurons],
                             synapse=f_smooth)

    with nengo.Simulator(model, seed=seed, dt=dt) as sim:
        if np.any(w_ens):
            for pre in range(n_pre):
                for post in range(n_neurons):
                    conn.weights[pre, post] = w_ens[pre, post]
                    conn.netcons[pre, post].weight[0] = np.abs(w_ens[pre,
                                                                     post])
                    conn.netcons[pre,
                                 post].syn().e = 0 if w_ens[pre,
                                                            post] > 0 else -70
        if np.any(w_ens2):
            for pre in range(n_neurons):
                for post in range(n_neurons):
                    conn2.weights[pre, post] = w_ens2[pre, post]
                    conn2.netcons[pre, post].weight[0] = np.abs(w_ens2[pre,
                                                                       post])
                    conn2.netcons[
                        pre,
                        post].syn().e = 0 if w_ens2[pre, post] > 0 else -70
        if np.any(e_ens):
            conn.e = e_ens
        if np.any(e_ens2):
            conn2.e = e_ens2
        neuron.h.init()
        sim.run(t)
        reset_neuron(sim, model)

    if L and hasattr(conn, 'weights'):
        w_ens = conn.weights
        e_ens = conn.e
    if L2 and hasattr(conn2, 'weights'):
        w_ens2 = conn2.weights
        e_ens2 = conn2.e

    return dict(
        times=sim.trange(),
        u=sim.data[p_u],
        ens=sim.data[p_ens],
        v=sim.data[p_v],
        x=sim.data[p_x],
        ens2=sim.data[p_ens2],
        v2=sim.data[p_v2],
        x2=sim.data[p_x2],
        supv=sim.data[p_supv],
        supv2=sim.data[p_supv2],
        w_ens=w_ens,
        e_ens=e_ens,
        w_ens2=w_ens2,
        e_ens2=e_ens2,
    )
Beispiel #5
0
def go(d_ens,
       f_ens,
       n_neurons=30,
       n_pre=300,
       t=10,
       m=Uniform(30, 40),
       i=Uniform(-1, 0.8),
       seed=0,
       dt=0.001,
       T=0.2,
       neuron_type=LIF(),
       f=DoubleExp(1e-3, 2e-1),
       f_smooth=DoubleExp(1e-2, 2e-1),
       stim_func=lambda t: np.sin(t),
       stim_func_base=None,
       w_x=None,
       e_x=None,
       w_u=None,
       e_u=None,
       w_fb=None,
       e_fb=None,
       L_x=False,
       L_u=False,
       L_fd=False,
       L_fb=False,
       supervised=False):

    with nengo.Network(seed=seed) as model:

        # Stimulus and Nodes
        u = nengo.Node(stim_func)
        x = nengo.Ensemble(1, 1, neuron_type=nengo.Direct())

        # Ensembles
        pre_u = nengo.Ensemble(n_pre, 1, radius=3, seed=seed)
        pre_x = nengo.Ensemble(n_pre, 1, seed=seed)
        ens = nengo.Ensemble(n_neurons,
                             1,
                             max_rates=m,
                             intercepts=i,
                             neuron_type=neuron_type,
                             seed=seed)
        supv = nengo.Ensemble(n_neurons,
                              1,
                              max_rates=m,
                              intercepts=i,
                              neuron_type=nengo.LIF(),
                              seed=seed)

        # Connections
        u_pre = nengo.Connection(u, pre_u, synapse=None, seed=seed)
        x_pre = nengo.Connection(x, pre_x, synapse=None, seed=seed)
        nengo.Connection(u, x, synapse=1 / s, seed=seed)
        if not L_x:
            pre_u_ens = nengo.Connection(pre_u,
                                         ens,
                                         synapse=f,
                                         transform=T,
                                         seed=seed)
            pre_u_supv = nengo.Connection(pre_u,
                                          supv,
                                          synapse=f,
                                          transform=T,
                                          seed=seed)
        if L_x or L_fd:
            #         if L_x or L_u or L_fd:
            pre_x_ens = nengo.Connection(pre_x, ens, synapse=f, seed=seed)
            pre_x_supv = nengo.Connection(pre_x, supv, synapse=f, seed=seed)
        if L_u:
            base = nengo.Node(stim_func_base)
            pre_base = nengo.Ensemble(n_pre, 1, seed=seed)
            nengo.Connection(base, pre_base, synapse=None, seed=seed)
            pre_base_ens = nengo.Connection(pre_base,
                                            ens,
                                            synapse=f,
                                            seed=seed)
            pre_base_supv = nengo.Connection(pre_base,
                                             supv,
                                             synapse=f,
                                             seed=seed)

        if L_fb or supervised:
            u_pre.synapse = f
            x_pre.synapse = None
            ens2 = nengo.Ensemble(n_neurons,
                                  1,
                                  max_rates=m,
                                  intercepts=i,
                                  neuron_type=neuron_type,
                                  seed=seed)
            supv2 = nengo.Ensemble(n_neurons,
                                   1,
                                   max_rates=m,
                                   intercepts=i,
                                   neuron_type=nengo.LIF(),
                                   seed=seed)
            pre_x_ens2 = nengo.Connection(pre_x, ens2, synapse=f, seed=seed)
            #             pre_x_supv2 = nengo.Connection(pre_x, supv2, synapse=f, seed=seed)
            pre_x_supv2 = nengo.Connection(pre_x,
                                           supv2,
                                           synapse=f_ens,
                                           seed=seed)
            ens2_ens = nengo.Connection(ens2,
                                        ens,
                                        synapse=f_ens,
                                        seed=seed,
                                        solver=NoSolver(d_ens))
            supv2_supv = nengo.Connection(supv2, supv, synapse=f, seed=seed)
            p_ens2 = nengo.Probe(ens2.neurons, synapse=None)
            p_supv2 = nengo.Probe(supv2.neurons, synapse=None)
            p_supv_state = nengo.Probe(supv, synapse=f)
            p_supv2_state = nengo.Probe(supv2, synapse=f)
        else:
            ens_ens = nengo.Connection(ens,
                                       ens,
                                       synapse=f_ens,
                                       solver=NoSolver(d_ens),
                                       seed=seed)
            if not (L_x or L_u or L_fd):
                supv_supv = nengo.Connection(supv, supv, synapse=f, seed=seed)

        if L_x:
            node = LearningNode2(n_neurons, n_pre, pre_x_ens, k=3e-6)
            nengo.Connection(pre_x.neurons, node[0:n_pre], synapse=f)
            nengo.Connection(ens.neurons,
                             node[n_pre:n_pre + n_neurons],
                             synapse=f_smooth)
            nengo.Connection(supv.neurons,
                             node[n_pre + n_neurons:n_pre + 2 * n_neurons],
                             synapse=f_smooth)
        if L_u:
            node = LearningNode2(n_neurons, n_pre, pre_u_ens, k=3e-6)
            nengo.Connection(pre_u.neurons, node[0:n_pre], synapse=f)
            nengo.Connection(ens.neurons,
                             node[n_pre:n_pre + n_neurons],
                             synapse=f_smooth)
            nengo.Connection(supv.neurons,
                             node[n_pre + n_neurons:n_pre + 2 * n_neurons],
                             synapse=f_smooth)
        if L_fb:
            node = LearningNode2(n_neurons,
                                 n_neurons,
                                 ens2_ens,
                                 conn_supv=pre_x_ens2,
                                 k=3e-6)
            nengo.Connection(ens2.neurons, node[0:n_neurons], synapse=f_ens)
            nengo.Connection(ens.neurons,
                             node[n_neurons:2 * n_neurons],
                             synapse=f_smooth)
            nengo.Connection(ens2.neurons,
                             node[2 * n_neurons:3 * n_neurons],
                             synapse=f_smooth)


#             nengo.Connection(supv.neurons, node[2*n_neurons: 3*n_neurons], synapse=f_smooth)

# Probes
        p_u = nengo.Probe(u, synapse=None)
        p_x = nengo.Probe(x, synapse=None)
        p_ens = nengo.Probe(ens.neurons, synapse=None)
        p_v = nengo.Probe(ens.neurons, 'voltage', synapse=None)
        p_supv = nengo.Probe(supv.neurons, synapse=None)

    with nengo.Simulator(model, seed=seed, dt=dt, progress_bar=False) as sim:
        if np.any(w_x):
            for pre in range(n_pre):
                for post in range(n_neurons):
                    if L_u:
                        pre_base_ens.weights[pre, post] = w_x[pre, post]
                        pre_base_ens.netcons[pre, post].weight[0] = np.abs(
                            w_x[pre, post])
                        pre_base_ens.netcons[
                            pre,
                            post].syn().e = 0 if w_x[pre, post] > 0 else -70
                    elif L_x or L_fd:
                        #                     if L_u or L_fd:
                        pre_x_ens.weights[pre, post] = w_x[pre, post]
                        pre_x_ens.netcons[pre,
                                          post].weight[0] = np.abs(w_x[pre,
                                                                       post])
                        pre_x_ens.netcons[
                            pre,
                            post].syn().e = 0 if w_x[pre, post] > 0 else -70
                    elif L_fb or supervised:
                        pre_x_ens2.weights[pre, post] = w_x[pre, post]
                        pre_x_ens2.netcons[pre,
                                           post].weight[0] = np.abs(w_x[pre,
                                                                        post])
                        pre_x_ens2.netcons[
                            pre,
                            post].syn().e = 0 if w_x[pre, post] > 0 else -70
        if np.any(w_u):
            for pre in range(n_pre):
                for post in range(n_neurons):
                    pre_u_ens.weights[pre, post] = w_u[pre, post]
                    pre_u_ens.netcons[pre, post].weight[0] = np.abs(w_u[pre,
                                                                        post])
                    pre_u_ens.netcons[
                        pre, post].syn().e = 0 if w_u[pre, post] > 0 else -70
        if np.any(w_fb):
            if L_fb or supervised:
                for pre in range(n_neurons):
                    for post in range(n_neurons):
                        ens2_ens.weights[pre, post] = w_fb[pre, post]
                        ens2_ens.netcons[pre,
                                         post].weight[0] = np.abs(w_fb[pre,
                                                                       post])
                        ens2_ens.netcons[
                            pre,
                            post].syn().e = 0 if w_fb[pre, post] > 0 else -70
            else:
                for pre in range(n_neurons):
                    for post in range(n_neurons):
                        ens_ens.weights[pre, post] = w_fb[pre, post]
                        ens_ens.netcons[pre,
                                        post].weight[0] = np.abs(w_fb[pre,
                                                                      post])
                        ens_ens.netcons[
                            pre,
                            post].syn().e = 0 if w_fb[pre, post] > 0 else -70
        if np.any(e_x) and L_x:
            pre_x_ens.e = e_x
        if np.any(e_u) and L_u:
            pre_u_ens.e = e_u
        if np.any(e_fb) and L_fb:
            ens2_ens.e = e_fb

        neuron.h.init()
        sim.run(t, progress_bar=True)
        reset_neuron(sim, model)

    if L_x and hasattr(pre_x_ens, 'weights'):
        w_x = pre_x_ens.weights
        e_x = pre_x_ens.e
    if L_u and hasattr(pre_u_ens, 'weights'):
        w_u = pre_u_ens.weights
        e_u = pre_u_ens.e
    if L_fb and hasattr(ens2_ens, 'weights'):
        w_fb = ens2_ens.weights
        e_fb = ens2_ens.e

    return dict(
        times=sim.trange(),
        u=sim.data[p_u],
        x=sim.data[p_x],
        ens=sim.data[p_ens],
        ens2=sim.data[p_ens2] if L_fb or supervised else None,
        supv=sim.data[p_supv],
        supv2=sim.data[p_supv2] if L_fb or supervised else None,
        w_x=w_x,
        w_u=w_u,
        w_fb=w_fb,
        e_x=e_x if L_x else None,
        e_u=e_u if L_u else None,
        e_fb=e_fb if L_fb else None,
        supv_state=sim.data[p_supv_state] if L_fb or supervised else None,
        supv2_state=sim.data[p_supv2_state] if L_fb or supervised else None,
    )