Exemple #1
0
def test_solvers():
    check_init_args(Lstsq, ["weights", "rcond"])
    check_repr(Lstsq(weights=True, rcond=0.1))
    assert repr(Lstsq(weights=True,
                      rcond=0.1)) == "Lstsq(weights=True, rcond=0.1)"

    check_init_args(LstsqNoise, ["weights", "noise", "solver"])
    check_repr(LstsqNoise(weights=True, noise=0.2))
    assert (repr(LstsqNoise(
        weights=True, noise=0.2)) == "LstsqNoise(weights=True, noise=0.2)")

    check_init_args(LstsqL2, ["weights", "reg", "solver"])
    check_repr(LstsqL2(weights=True, reg=0.2))
    assert repr(LstsqL2(weights=True,
                        reg=0.2)) == "LstsqL2(weights=True, reg=0.2)"

    check_init_args(LstsqL2nz, ["weights", "reg", "solver"])
    check_repr(LstsqL2nz(weights=True, reg=0.2))
    assert repr(LstsqL2nz(weights=True,
                          reg=0.2)) == "LstsqL2nz(weights=True, reg=0.2)"

    check_init_args(NoSolver, ["values", "weights"])
    check_repr(NoSolver(values=np.array([[1.2, 3.4, 5.6, 7.8]]), weights=True))
    assert (repr(NoSolver([[1.2, 3.4, 5.6, 7.8]], weights=True)) ==
            "NoSolver(values=array([[1.2, 3.4, 5.6, 7.8]]), weights=True)")
Exemple #2
0
def test_nosolver_validation():
    # Must be a 2-dimensional array
    with pytest.raises(ValidationError):
        NoSolver(values=np.zeros(1))
    with pytest.raises(ValidationError):
        NoSolver(values=np.zeros((1, 1, 1)))
    # Non-numbers are not okay
    with pytest.raises(ValidationError):
        NoSolver(values="test")
    # array_likes are okay
    NoSolver(values=[[1], [1]])
Exemple #3
0
def test_ff_no_learn(params, device):
    """Feedforward test with solved decoders and no learning"""

    tol = 0.005
    if isinstance(params["neuron"], nengo.SpikingRectifiedLinear):
        tol *= 2
    stim_val = 0.25
    p_syn = 0.05

    with nengo.Network() as net:
        stim = nengo.Node([stim_val] * params["dims_in"])  # Input node

        # Nengo reference
        ens = nengo.Ensemble(
            params["n_neurons"],
            params["dims_in"],
            neuron_type=params["neuron"],
            bias=params["bias"],
            encoders=params["encoders"],
            gain=params["gain"],
        )
        output = nengo.Node(size_in=params["dims_out"])

        nengo.Connection(stim, ens)
        nengo.Connection(ens,
                         output,
                         function=params["func"],
                         solver=NoSolver(params["decoders"].T))

        p_nengo = nengo.Probe(output, synapse=p_syn)

        # FPGA
        fpga = FpgaPesEnsembleNetwork(device,
                                      params["n_neurons"],
                                      params["dims_in"],
                                      0,
                                      function=params["func"])
        fpga.connection.solver = NoSolver(params["decoders"].T)
        fpga.ensemble.neuron_type = params["neuron"]
        fpga.ensemble.encoders = params["encoders"]
        fpga.ensemble.bias = params["bias"]
        fpga.ensemble.gain = params["gain"]

        nengo.Connection(stim, fpga.input)

        p_fpga = nengo.Probe(fpga.output, synapse=p_syn)

    with nengo_fpga.Simulator(net) as sim:
        sim.run(1)

    assert fpga.config_found and fpga.using_fpga_sim  # Ensure real FPGA

    idx = 2 if device == "de1" else 1  # Compensate for DE1 off-by-one
    assert np.allclose(sim.data[p_fpga][-1], sim.data[p_nengo][-idx], atol=tol)
Exemple #4
0
def go(N=2000, d=None, f=None, t=100, l=False, neuron_type=LIF(),
       m=Uniform(30, 30), i=Uniform(-1, 1), r=30, IC=np.array([0,0,0]),
       seed=0, dt=0.001, dtSample=0.001):

    with nengo.Network(seed=seed) as model:
        inpt = nengo.Node(lambda t: IC*(t<=1.0))
        tar = nengo.Ensemble(1, 3, neuron_type=nengo.Direct())
        ens = nengo.Ensemble(N, 3, max_rates=m, intercepts=i, neuron_type=neuron_type, seed=seed, radius=r)
        dss = nengo.Node(DownsampleNode(size_in=N, size_out=N, dt=dt, dtSample=dtSample), size_in=N, size_out=N)
        nengo.Connection(inpt, tar, synapse=None)
        nengo.Connection(tar, tar, function=feedback, synapse=~s)
        if l:
            nengo.Connection(tar, ens, synapse=None, seed=seed)
        else:
            nengo.Connection(inpt, ens, synapse=None, seed=seed)
            nengo.Connection(ens, ens, synapse=f, solver=NoSolver(d), seed=seed)
        nengo.Connection(ens.neurons, dss, synapse=None)
        pTar = nengo.Probe(tar, synapse=None, sample_every=dtSample)
        pEns = nengo.Probe(dss, synapse=None, sample_every=dtSample)

    with nengo.Simulator(model, seed=seed, dt=dt, progress_bar=False) as sim:
        sim.run(t+dt, progress_bar=True)

    return dict(
        times=sim.trange(),
        tar=sim.data[pTar],
        ens=sim.data[pEns])
Exemple #5
0
def test_nosolver(values, weights, seed, Simulator, allclose):
    with nengo.Network(seed=seed) as net:
        pre = nengo.Ensemble(10, 2)
        post = nengo.Ensemble(20, 2)

        if values is not None:
            values = np.ones((pre.n_neurons, post.dimensions))

        conn = nengo.Connection(pre,
                                post,
                                solver=NoSolver(values=values,
                                                weights=weights))

    with Simulator(net) as sim:
        built_weights = sim.data[conn].weights

    if values is None:
        assert conn.solver.values is None
        assert np.all(built_weights == 0)
    else:
        assert np.all(conn.solver.values == values)
        if weights:
            assert allclose(built_weights,
                            np.dot(values, sim.data[post].scaled_encoders.T).T)
        else:
            assert np.all(built_weights == 1)

    if weights:
        assert built_weights.T.shape == (pre.n_neurons, post.n_neurons)
    else:
        assert built_weights.T.shape == (pre.n_neurons, post.dimensions)
    assert sim.data[conn].eval_points is None
Exemple #6
0
def go(d_ens, f_ens, n_neurons=3000, t=100, L=False, neuron_type=LIF(),
       m=Uniform(30, 40), i=Uniform(-1, 1), r=40, IC=np.array([1,1,1]),
       seed=0, dt=0.001, dt_sample=0.001, f=DoubleExp(1e-3, 1e-1)):

    with nengo.Network(seed=seed) as model:
        # Ensembles
        u = nengo.Node(lambda t: IC*(t<=1.0))
        x = nengo.Ensemble(1, 3, neuron_type=nengo.Direct())
        ens = nengo.Ensemble(n_neurons, 3, max_rates=m, intercepts=i, neuron_type=neuron_type, seed=seed, radius=r)
        dss = nengo.Node(DownsampleNode(size_in=n_neurons, size_out=n_neurons, dt=dt, dt_sample=dt_sample), size_in=n_neurons, size_out=n_neurons)

        # Connections
        nengo.Connection(u, x, synapse=None)
        nengo.Connection(x, x, function=feedback, synapse=~s)
        if L:
            supv = nengo.Ensemble(n_neurons, 3, neuron_type=SpikingRectifiedLinear(), radius=r, seed=seed)
            nengo.Connection(x, supv, synapse=None)
            nengo.Connection(supv, ens, synapse=f, seed=seed)
        else:
            nengo.Connection(ens, ens, synapse=f_ens, solver=NoSolver(d_ens), seed=seed)

        # Probes
        nengo.Connection(ens.neurons, dss, synapse=None)
        p_x = nengo.Probe(x, synapse=None, sample_every=dt_sample)
        p_ens = nengo.Probe(dss, synapse=None, sample_every=dt_sample)

    with nengo.Simulator(model, seed=seed, dt=dt) as sim:
        sim.run(t)

    return dict(
        times=sim.trange(),
        x=sim.data[p_x],
        ens=sim.data[p_ens])
Exemple #7
0
def go(t=10, m=Uniform(30, 30), i=Uniform(0, 0), seed=0, dt=0.001, f=DoubleExp(1e-3, 1e-1), fS=DoubleExp(1e-3, 1e-1), d1=None, f1=None, e1=None, l1=False, stim=lambda t: np.sin(t)):

    if not f1: f1=f
    with nengo.Network(seed=seed) as model:
        # Stimulus and Nodes
        inpt = nengo.Node(stim)
        tar = nengo.Ensemble(1, 1, neuron_type=nengo.Direct())
        pre = nengo.Ensemble(100, 1, max_rates=m, seed=seed, neuron_type=LIF())
        lif = nengo.Ensemble(1, 1, max_rates=m, intercepts=i, encoders=Choice([[1]]), neuron_type=LIF(), seed=seed)
        wilson = nengo.Ensemble(1, 1, max_rates=m, intercepts=i, encoders=Choice([[1]]), neuron_type=Wilson(), seed=seed)
        bio = nengo.Ensemble(1, 1, max_rates=m, intercepts=i, encoders=Choice([[1]]), neuron_type=Bio("Pyramidal"), seed=seed)
        nengo.Connection(inpt, pre, synapse=None, seed=seed)
        cLif = nengo.Connection(pre, lif, synapse=f1, seed=seed, solver=NoSolver(d1))
        cWilson = nengo.Connection(pre, wilson, synapse=f1, seed=seed, solver=NoSolver(d1))
        cBio = nengo.Connection(pre, bio, synapse=f1, seed=seed, solver=NoSolver(d1))
        pInpt = nengo.Probe(inpt, synapse=None)
        pPre = nengo.Probe(pre.neurons, synapse=None)
        pLif = nengo.Probe(lif.neurons, synapse=None)
        pWilson = nengo.Probe(wilson.neurons, synapse=None)
        pBio = nengo.Probe(bio.neurons, synapse=None)
        if l1: learnEncoders(cBio, lif, fS, alpha=3e-7) # Encoder Learning (Bio)

    with nengo.Simulator(model, seed=seed, dt=dt, progress_bar=False) as sim:
        setWeights(cBio, d1, e1)
        neuron.h.init()
        sim.run(t, progress_bar=True)
        reset_neuron(sim, model) 
      
    e1 = cBio.e if l1 else e1

    return dict(
        times=sim.trange(),
        inpt=sim.data[pInpt],
        pre=sim.data[pPre],
        lif=sim.data[pLif],
        wilson=sim.data[pWilson],
        bio=sim.data[pBio],
        e1=e1,
    )
Exemple #8
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,
    )
def go(NPre=100,
       N=100,
       N2=30,
       t=10,
       m=Uniform(30, 30),
       i=Uniform(-0.8, 0.8),
       seed=0,
       dt=0.001,
       f=DoubleExp(1e-3, 1e-1),
       fS=DoubleExp(1e-3, 1e-1),
       neuron_type=LIF(),
       d1=None,
       d2=None,
       f1=None,
       f2=None,
       e1=None,
       e2=None,
       l1=False,
       l2=False,
       stim=lambda t: [0, 0]):

    if not f1: f1 = f
    if not f2: f2 = f
    if not np.any(d2): d2 = np.zeros((N, 1))
    with nengo.Network(seed=seed) as model:
        # Stimulus and Nodes
        inpt = nengo.Node(stim)
        tar = nengo.Ensemble(1, 2, neuron_type=nengo.Direct())
        tar2 = nengo.Ensemble(1, 1, neuron_type=nengo.Direct())
        pre = nengo.Ensemble(NPre,
                             2,
                             radius=2,
                             max_rates=m,
                             seed=seed,
                             neuron_type=LIF())
        ens = nengo.Ensemble(N,
                             2,
                             radius=2,
                             max_rates=m,
                             intercepts=i,
                             neuron_type=neuron_type,
                             seed=seed)
        ens2 = nengo.Ensemble(N2,
                              1,
                              max_rates=m,
                              intercepts=i,
                              neuron_type=neuron_type,
                              seed=seed + 1)
        nengo.Connection(inpt, pre, synapse=None, seed=seed)
        nengo.Connection(inpt, tar, synapse=f, seed=seed)
        nengo.Connection(tar,
                         tar2,
                         synapse=f,
                         function=multiply,
                         seed=seed + 1)
        c1 = nengo.Connection(pre,
                              ens,
                              synapse=f1,
                              seed=seed,
                              solver=NoSolver(d1))
        if isinstance(neuron_type, Bio):
            c2 = nengo.Connection(ens,
                                  ens2,
                                  synapse=f2,
                                  seed=seed + 1,
                                  function=multiply)
        else:
            c2 = nengo.Connection(ens.neurons,
                                  ens2,
                                  synapse=f2,
                                  seed=seed + 1,
                                  transform=d2.T)
        pInpt = nengo.Probe(inpt, synapse=None)
        pPre = nengo.Probe(pre.neurons, synapse=None)
        pEns = nengo.Probe(ens.neurons, synapse=None)
        pEns2 = nengo.Probe(ens2.neurons, synapse=None)
        pTar = nengo.Probe(tar, synapse=None)
        pTar2 = nengo.Probe(tar2, synapse=None)
        # Encoder Learning (Bio)
        if l1:
            tarEns = nengo.Ensemble(N,
                                    2,
                                    radius=2,
                                    max_rates=m,
                                    intercepts=i,
                                    neuron_type=nengo.LIF(),
                                    seed=seed)
            nengo.Connection(tar, tarEns, synapse=None, seed=seed)
            learnEncoders(c1, tarEns, fS, alpha=3e-8)
            pTarEns = nengo.Probe(tarEns.neurons, synapse=None)
        if l2:
            tarEns2 = nengo.Ensemble(N2,
                                     1,
                                     max_rates=m,
                                     intercepts=i,
                                     neuron_type=nengo.LIF(),
                                     seed=seed + 1)
            nengo.Connection(tar2, tarEns2, synapse=None)
            #             nengo.Connection(ens.neurons, tarEns2, synapse=f2, transform=d2.T, seed=seed+1)
            learnEncoders(c2, tarEns2, fS, alpha=1e-7)
            pTarEns2 = nengo.Probe(tarEns2.neurons, synapse=None)
            pTarState = nengo.Probe(tarEns2, synapse=f)

    with nengo.Simulator(model, seed=seed, dt=dt, progress_bar=False) as sim:
        if isinstance(neuron_type, Bio):
            setWeights(c1, d1, e1)
            setWeights(c2, d2, e2)
            neuron.h.init()
            sim.run(t, progress_bar=True)
            reset_neuron(sim, model)
        else:
            sim.run(t, progress_bar=True)

    e1 = c1.e if l1 else e1
    e2 = c2.e if l2 else e2

    return dict(
        times=sim.trange(),
        inpt=sim.data[pInpt],
        pre=sim.data[pPre],
        ens=sim.data[pEns],
        ens2=sim.data[pEns2],
        tar=sim.data[pTar],
        tar2=sim.data[pTar2],
        tarEns=sim.data[pTarEns] if l1 else None,
        tarEns2=sim.data[pTarEns2] if l2 else None,
        tarState=sim.data[pTarState] if l2 else None,
        e1=e1,
        e2=e2,
    )
def goLIF(N=100,
          t=10,
          dt=0.001,
          stim=lambda t: 0,
          m=Uniform(30, 30),
          i=Uniform(0, 0.8),
          e=Choice([[1]]),
          i2=Uniform(0.4, 1),
          dFdfwEnsAMPA=None,
          dFdfwEnsNMDA=None,
          dEnsEnsAMPA=None,
          dEnsEnsNMDA=None,
          dEnsInhAMPA=None,
          dEnsInhNMDA=None,
          dInhFdfwGABA=None,
          dInhEnsGABA=None,
          fAMPA=DoubleExp(0.55e-3, 2.2e-3),
          fNMDA=DoubleExp(2.3e-3, 95.0e-3),
          fGABA=DoubleExp(0.5e-3, 1.5e-3),
          x0=0,
          seed=0):

    with nengo.Network(seed=seed) as model:
        inpt = nengo.Node(stim)
        intg = nengo.Ensemble(1, 1, neuron_type=nengo.Direct())
        fdfw = nengo.Ensemble(N, 1, max_rates=m, intercepts=i, seed=seed)
        inh = nengo.Ensemble(N,
                             1,
                             encoders=e,
                             max_rates=m,
                             intercepts=i2,
                             neuron_type=LIF(),
                             seed=seed)
        ens = nengo.Ensemble(N,
                             1,
                             encoders=e,
                             max_rates=m,
                             intercepts=i,
                             neuron_type=LIF(),
                             seed=seed)
        nengo.Connection(inpt, intg, synapse=1 / s)
        nengo.Connection(inpt, fdfw, synapse=None)
        nengo.Connection(fdfw,
                         ens,
                         synapse=fAMPA,
                         solver=NoSolver(dFdfwEnsAMPA))
        nengo.Connection(fdfw,
                         ens,
                         synapse=fNMDA,
                         solver=NoSolver(dFdfwEnsNMDA))
        nengo.Connection(ens, ens, synapse=fAMPA, solver=NoSolver(dEnsEnsAMPA))
        nengo.Connection(ens, ens, synapse=fNMDA, solver=NoSolver(dEnsEnsNMDA))
        nengo.Connection(ens, inh, synapse=fAMPA, solver=NoSolver(dEnsInhAMPA))
        nengo.Connection(ens, inh, synapse=fNMDA, solver=NoSolver(dEnsInhNMDA))
        nengo.Connection(inh,
                         fdfw,
                         synapse=fGABA,
                         solver=NoSolver(dInhFdfwGABA))
        nengo.Connection(inh, ens, synapse=fGABA, solver=NoSolver(dInhEnsGABA))
        pInpt = nengo.Probe(inpt, synapse=None)
        pIntg = nengo.Probe(intg, synapse=None)
        pFdfw = nengo.Probe(fdfw.neurons, synapse=None)
        pInh = nengo.Probe(inh.neurons, synapse=None)
        pEns = nengo.Probe(ens.neurons, synapse=None)
    with nengo.Simulator(model, seed=seed, progress_bar=False) as sim:
        #         init_lif(sim, ens, x0=x0)
        sim.run(t, progress_bar=True)
    return dict(times=sim.trange(),
                inpt=sim.data[pInpt],
                intg=sim.data[pIntg],
                fdfw=sim.data[pFdfw],
                inh=sim.data[pInh],
                ens=sim.data[pEns])
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,
    )
def go(NPre=100,
       N=100,
       t=10,
       m=Uniform(30, 30),
       i=Uniform(-0.8, 0.8),
       neuron_type=LIF(),
       seed=0,
       dt=0.001,
       fPre=None,
       fEns=None,
       fS=DoubleExp(2e-2, 2e-1),
       dPreA=None,
       dPreB=None,
       dEns=None,
       ePreA=None,
       ePreB=None,
       eBio=None,
       stage=None,
       alpha=1e-6,
       eMax=1e0,
       stimA=lambda t: np.sin(t),
       stimB=lambda t: 0):

    with nengo.Network(seed=seed) as model:
        inptA = nengo.Node(stimA)
        inptB = nengo.Node(stimB)
        preInptA = nengo.Ensemble(NPre, 1, radius=3, max_rates=m, seed=seed)
        preInptB = nengo.Ensemble(NPre, 1, max_rates=m, seed=seed)
        ens = nengo.Ensemble(N,
                             1,
                             max_rates=m,
                             intercepts=i,
                             neuron_type=neuron_type,
                             seed=seed)
        tarEns = nengo.Ensemble(N,
                                1,
                                max_rates=m,
                                intercepts=i,
                                neuron_type=nengo.LIF(),
                                seed=seed)
        cpa = nengo.Connection(inptA, preInptA, synapse=None, seed=seed)
        cpb = nengo.Connection(inptB, preInptB, synapse=None, seed=seed)
        pInptA = nengo.Probe(inptA, synapse=None)
        pInptB = nengo.Probe(inptB, synapse=None)
        pPreInptA = nengo.Probe(preInptA.neurons, synapse=None)
        pPreInptB = nengo.Probe(preInptB.neurons, synapse=None)
        pEns = nengo.Probe(ens.neurons, synapse=None)
        pTarEns = nengo.Probe(tarEns.neurons, synapse=None)
        if stage == 1:
            c0b = nengo.Connection(preInptB,
                                   tarEns,
                                   synapse=fPre,
                                   solver=NoSolver(dPreB),
                                   seed=seed + 1)
            c1b = nengo.Connection(preInptB,
                                   ens,
                                   synapse=fPre,
                                   solver=NoSolver(dPreB),
                                   seed=seed + 1)
            learnEncoders(c1b, tarEns, fS, alpha=alpha, eMax=eMax)
        if stage == 2:
            c0a = nengo.Connection(preInptA,
                                   tarEns,
                                   synapse=fPre,
                                   solver=NoSolver(dPreA),
                                   seed=seed)
            c0b = nengo.Connection(preInptB,
                                   tarEns,
                                   synapse=fPre,
                                   solver=NoSolver(dPreB),
                                   seed=seed + 1)
            c1a = nengo.Connection(preInptA,
                                   ens,
                                   synapse=fPre,
                                   solver=NoSolver(dPreA),
                                   seed=seed)
            c1b = nengo.Connection(preInptB,
                                   ens,
                                   synapse=fPre,
                                   solver=NoSolver(dPreB),
                                   seed=seed + 1)
            learnEncoders(c1a, tarEns, fS, alpha=alpha, eMax=eMax)
        if stage == 3:
            c1a = nengo.Connection(preInptA,
                                   ens,
                                   synapse=fPre,
                                   solver=NoSolver(dPreA),
                                   seed=seed)
            c1b = nengo.Connection(preInptB,
                                   ens,
                                   synapse=fPre,
                                   solver=NoSolver(dPreB),
                                   seed=seed + 1)
        if stage == 4:
            preInptC = nengo.Ensemble(NPre, 1, max_rates=m, seed=seed)
            ens2 = nengo.Ensemble(N,
                                  1,
                                  max_rates=m,
                                  intercepts=i,
                                  neuron_type=neuron_type,
                                  seed=seed)
            ens3 = nengo.Ensemble(N,
                                  1,
                                  max_rates=m,
                                  intercepts=i,
                                  neuron_type=neuron_type,
                                  seed=seed)
            nengo.Connection(inptB, preInptC, synapse=fEns, seed=seed)
            c1a = nengo.Connection(preInptA,
                                   ens,
                                   synapse=fPre,
                                   solver=NoSolver(dPreA),
                                   seed=seed)
            c2b = nengo.Connection(preInptB,
                                   ens2,
                                   synapse=fPre,
                                   solver=NoSolver(dPreB),
                                   seed=seed + 1)
            c3 = nengo.Connection(ens2,
                                  ens,
                                  synapse=fEns,
                                  solver=NoSolver(dEns),
                                  seed=seed)
            c4a = nengo.Connection(preInptA,
                                   ens3,
                                   synapse=fPre,
                                   solver=NoSolver(dPreA),
                                   seed=seed)
            c4b = nengo.Connection(preInptC,
                                   ens3,
                                   synapse=fPre,
                                   solver=NoSolver(dPreB),
                                   seed=seed)
            learnEncoders(c3, ens3, fS, alpha=alpha / 10, eMax=eMax)
            pTarEns = nengo.Probe(ens3.neurons, synapse=None)
        if stage == 5:
            c1a = nengo.Connection(preInptA,
                                   ens,
                                   synapse=fPre,
                                   solver=NoSolver(dPreA),
                                   seed=seed)
            c5 = nengo.Connection(ens,
                                  ens,
                                  synapse=fEns,
                                  solver=NoSolver(dEns),
                                  seed=seed)

    with nengo.Simulator(model, seed=seed, dt=dt, progress_bar=False) as sim:
        if isinstance(neuron_type, Bio):
            if stage == 1:
                setWeights(c1b, dPreB, ePreB)
            if stage == 2:
                setWeights(c1a, dPreA, ePreA)
                setWeights(c1b, dPreB, ePreB)
            if stage == 3:
                setWeights(c1a, dPreA, ePreA)
                setWeights(c1b, dPreB, ePreB)
            if stage == 4:
                setWeights(c1a, dPreA, ePreA)
                setWeights(c2b, dPreB, ePreB)
                setWeights(c4a, dPreA, ePreA)
                setWeights(c4b, dPreB, ePreB)
                setWeights(c3, dEns, eBio)
            if stage == 5:
                setWeights(c1a, dPreA, ePreA)
                setWeights(c5, dEns, eBio)
            neuron.h.init()
            sim.run(t, progress_bar=True)
            reset_neuron(sim, model)
        else:
            sim.run(t, progress_bar=True)

    ePreB = c1b.e if stage == 1 else ePreB
    ePreA = c1a.e if stage == 2 else ePreA
    eBio = c3.e if stage == 4 else eBio

    return dict(
        times=sim.trange(),
        inptA=sim.data[pInptA],
        inptB=sim.data[pInptB],
        preInptA=sim.data[pPreInptA],
        preInptB=sim.data[pPreInptB],
        ens=sim.data[pEns],
        tarEns=sim.data[pTarEns],
        ePreA=ePreA,
        ePreB=ePreB,
        eBio=eBio,
    )
Exemple #13
0
def test_feedback(params, device):
    """Feedback test with solved decoders"""

    stim_val = 0.5
    tol = 0.005
    if isinstance(params["neuron"], nengo.SpikingRectifiedLinear):
        tol *= 2
    tau = 0.05
    p_syn = 0.05

    def stim_func(t):
        """Return value for the first 1 second of simulation"""
        val = stim_val * tau if t < 1 else 0
        return [val] * params["dims_in"]

    with nengo.Network() as net:
        stim = nengo.Node(stim_func)  # Input node

        # Nengo reference
        ens = nengo.Ensemble(
            params["n_neurons"],
            params["dims_in"],
            neuron_type=params["neuron"],
            bias=params["bias"],
            encoders=params["encoders"],
            gain=params["gain"],
        )
        output = nengo.Node(size_in=params["dims_out"])

        nengo.Connection(stim, ens)
        nengo.Connection(ens,
                         output,
                         function=params["func"],
                         solver=NoSolver(params["decoders"].T))
        nengo.Connection(
            ens,
            ens,
            solver=NoSolver(
                np.concatenate((params["decoders"],
                                np.zeros(params["decoders"].shape))).T),
            synapse=tau,
        )

        p_nengo = nengo.Probe(output, synapse=p_syn)

        # FPGA
        fpga = FpgaPesEnsembleNetwork(
            device,
            params["n_neurons"],
            params["dims_in"],
            0,
            function=params["func"],
            feedback=1,
        )
        fpga.connection.solver = NoSolver(params["decoders"].T)
        fpga.ensemble.neuron_type = params["neuron"]
        fpga.ensemble.encoders = params["encoders"]
        fpga.ensemble.bias = params["bias"]
        fpga.ensemble.gain = params["gain"]
        fpga.feedback.synapse = tau
        fpga.feedback.solver = NoSolver(
            np.concatenate(
                (params["decoders"], np.zeros(params["decoders"].shape))).T)

        nengo.Connection(stim, fpga.input)

        p_fpga = nengo.Probe(fpga.output, synapse=p_syn)

    with nengo_fpga.Simulator(net) as sim:
        sim.run(1)

    assert fpga.config_found and fpga.using_fpga_sim  # Ensure real FPGA

    idx = 2 if device == "de1" else 1  # Compensate for DE1 off-by-one
    assert np.allclose(sim.data[p_fpga][-1], sim.data[p_nengo][-idx], atol=tol)
Exemple #14
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)
Exemple #15
0
def go(NPre=100, N=100, t=10, c=None, seed=0, dt=0.001, Tff=0.3, tTrans=0.01,
        stage=None, alpha=3e-7, eMax=1e-1,
        fPre=DoubleExp(1e-3, 1e-1), fNMDA=DoubleExp(10.6e-3, 285e-3), fS=DoubleExp(1e-3, 1e-1),
        dPreA=None, dPreB=None, dPreC=None, dFdfw=None, dBio=None, dNeg=None, dInh=None,
        ePreA=None, ePreB=None, ePreC=None, eFdfw=None, eBio=None, eNeg=None, eInh=None,
        stimA=lambda t: 0, stimB=lambda t: 0, stimC=lambda t: 0, DA=lambda t: 0):

    if not c: c = t
    with nengo.Network(seed=seed) as model:
        inptA = nengo.Node(stimA)
        inptB = nengo.Node(stimB)
        inptC = nengo.Node(stimC)
        preA = nengo.Ensemble(NPre, 1, max_rates=Uniform(30, 30), seed=seed)
        preB = nengo.Ensemble(NPre, 1, max_rates=Uniform(30, 30), seed=seed)
        preC = nengo.Ensemble(NPre, 1, max_rates=Uniform(30, 30), seed=seed)
        fdfw = nengo.Ensemble(N, 1, neuron_type=Bio("Pyramidal", DA=DA), seed=seed)
        ens = nengo.Ensemble(N, 1, neuron_type=Bio("Pyramidal", DA=DA), seed=seed+1)
        inh = nengo.Ensemble(N, 1, neuron_type=Bio("Interneuron", DA=DA), seed=seed+2)
        tarFdfw = nengo.Ensemble(N, 1, max_rates=Uniform(30, 30), intercepts=Uniform(-0.8, 0.8), neuron_type=nengo.LIF(), seed=seed)
        tarEns = nengo.Ensemble(N, 1, max_rates=Uniform(30, 30), intercepts=Uniform(-0.8, 0.8), neuron_type=nengo.LIF(), seed=seed+1)
        tarInh = nengo.Ensemble(N, 1, max_rates=Uniform(30, 30), intercepts=Uniform(0.2, 0.8), encoders=Choice([[1]]), neuron_type=nengo.LIF(), seed=seed+2)
        cA = nengo.Connection(inptA, preA, synapse=None, seed=seed)
        cB = nengo.Connection(inptB, preB, synapse=None, seed=seed)
        cC = nengo.Connection(inptC, preC, synapse=None, seed=seed)
        pInptA = nengo.Probe(inptA, synapse=None)
        pInptB = nengo.Probe(inptB, synapse=None)
        pInptC = nengo.Probe(inptC, synapse=None)
        pPreA = nengo.Probe(preA.neurons, synapse=None)
        pPreB = nengo.Probe(preB.neurons, synapse=None)
        pPreC = nengo.Probe(preC.neurons, synapse=None)
        pFdfw = nengo.Probe(fdfw.neurons, synapse=None)
        pTarFdfw = nengo.Probe(tarFdfw.neurons, synapse=None)
        pEns = nengo.Probe(ens.neurons, synapse=None)
        pTarEns = nengo.Probe(tarEns.neurons, synapse=None)
        pInh = nengo.Probe(inh.neurons, synapse=None)
        pTarInh = nengo.Probe(tarInh.neurons, synapse=None)
        if stage==1:
            nengo.Connection(inptA, tarFdfw, synapse=fPre, seed=seed)
            nengo.Connection(inptB, tarEns, synapse=fPre, seed=seed)
            nengo.Connection(inptC, tarInh, synapse=fPre, seed=seed)
            c1 = nengo.Connection(preA, fdfw, synapse=fPre, solver=NoSolver(dPreA), seed=seed)
            c2 = nengo.Connection(preB, ens, synapse=fPre, solver=NoSolver(dPreB), seed=seed)
            c3 = nengo.Connection(preC, inh, synapse=fPre, solver=NoSolver(dPreC), seed=seed)
            learnEncoders(c1, tarFdfw, fS, alpha=alpha, eMax=eMax, tTrans=tTrans)
            learnEncoders(c2, tarEns, fS, alpha=3*alpha, eMax=10*eMax, tTrans=tTrans)
            learnEncoders(c3, tarInh, fS, alpha=alpha/3, eMax=eMax, tTrans=tTrans)
        if stage==2:
            c1 = nengo.Connection(preA, fdfw, synapse=fPre, solver=NoSolver(dPreA), seed=seed)
            c2 = nengo.Connection(preC, inh, synapse=fPre, solver=NoSolver(dPreC), seed=seed)
        if stage==3:
            cB.synapse = fNMDA
            nengo.Connection(inptA, tarFdfw, synapse=fPre, seed=seed)
            nengo.Connection(inptB, tarEns, synapse=fPre, seed=seed)
            nengo.Connection(tarFdfw, tarEns, synapse=fNMDA, transform=Tff, seed=seed)
            c1 = nengo.Connection(preA, fdfw, synapse=fPre, solver=NoSolver(dPreA), seed=seed)
            c2 = nengo.Connection(preB, ens, synapse=fPre, solver=NoSolver(dPreB), seed=seed)
            c3 = nengo.Connection(fdfw, ens, synapse=NMDA(), solver=NoSolver(dFdfw), seed=seed)
            learnEncoders(c3, tarEns, fS, alpha=alpha, eMax=eMax, tTrans=tTrans)
        if stage==4:
            cB.synapse = fNMDA
            c1 = nengo.Connection(preA, fdfw, synapse=fPre, solver=NoSolver(dPreA), seed=seed)
            c2 = nengo.Connection(preB, ens, synapse=fPre, solver=NoSolver(dPreB), seed=seed)
            c3 = nengo.Connection(fdfw, ens, synapse=NMDA(), solver=NoSolver(dFdfw), seed=seed)
        if stage==5:
            preB2 = nengo.Ensemble(NPre, 1, max_rates=Uniform(30, 30), seed=seed)
            ens2 = nengo.Ensemble(N, 1, neuron_type=Bio("Pyramidal", DA=DA), seed=seed+1)
            ens3 = nengo.Ensemble(N, 1, neuron_type=Bio("Pyramidal", DA=DA), seed=seed+1)
            nengo.Connection(inptB, preB2, synapse=fNMDA, seed=seed)
            c1 = nengo.Connection(preA, fdfw, synapse=fPre, solver=NoSolver(dPreA), seed=seed)
            c2 = nengo.Connection(fdfw, ens, synapse=NMDA(), solver=NoSolver(dFdfw), seed=seed)
            c3 = nengo.Connection(preB, ens2, synapse=fPre, solver=NoSolver(dPreB), seed=seed)
            c4 = nengo.Connection(ens2, ens, synapse=NMDA(), solver=NoSolver(dBio), seed=seed)
            c5 = nengo.Connection(fdfw, ens3, synapse=NMDA(), solver=NoSolver(dFdfw), seed=seed)
            c6 = nengo.Connection(preB2, ens3, synapse=fPre, solver=NoSolver(dPreB), seed=seed)
            learnEncoders(c4, ens3, fS, alpha=alpha, eMax=eMax, tTrans=tTrans)
            pTarEns = nengo.Probe(ens3.neurons, synapse=None)
        if stage==6:
            preA2 = nengo.Ensemble(NPre, 1, max_rates=Uniform(30, 30), seed=seed)
            fdfw2 = nengo.Ensemble(N, 1, neuron_type=Bio("Pyramidal", DA=DA), seed=seed)
            fdfw3 = nengo.Ensemble(N, 1, neuron_type=Bio("Pyramidal", DA=DA), seed=seed)
            fdfw4 = nengo.Ensemble(N, 1, neuron_type=Bio("Pyramidal", DA=DA), seed=seed)
            tarFdfw4 = nengo.Ensemble(N, 1, max_rates=Uniform(30, 30), intercepts=Uniform(-0.8, 0.8), neuron_type=nengo.LIF(), seed=seed)
            nengo.Connection(inptA, tarFdfw4, synapse=fPre, seed=seed)
            nengo.Connection(inptB, preA2, synapse=fNMDA, seed=seed)
            nengo.Connection(inptC, tarInh, synapse=fPre, seed=seed)
            nengo.Connection(tarInh, tarFdfw4.neurons, synapse=None, transform=-1e2*np.ones((N, 1)), seed=seed)
            c1 = nengo.Connection(preB, ens, synapse=fPre, solver=NoSolver(dPreB), seed=seed)
            c2 = nengo.Connection(ens, fdfw2, synapse=NMDA(), solver=NoSolver(dNeg), seed=seed)
            c3 = nengo.Connection(preA2, fdfw3, synapse=fPre, solver=NoSolver(dPreA), seed=seed)
            c4 = nengo.Connection(preA, fdfw4, synapse=fPre, solver=NoSolver(dPreA), seed=seed)
            c5 = nengo.Connection(preC, inh, synapse=fPre, solver=NoSolver(dPreC), seed=seed)
            c6 = nengo.Connection(inh, fdfw4, synapse=GABA(), solver=NoSolver(dInh), seed=seed)
            learnEncoders(c2, fdfw3, fS, alpha=alpha, eMax=eMax, tTrans=tTrans)
            learnEncoders(c6, tarFdfw4, fS, alpha=1e3*alpha, eMax=1e3*eMax, tTrans=tTrans, inh=True)
            pFdfw2 = nengo.Probe(fdfw2.neurons, synapse=None)
            pFdfw4 = nengo.Probe(fdfw4.neurons, synapse=None)
            pTarFdfw2 = nengo.Probe(fdfw3.neurons, synapse=None)
            pTarFdfw4 = nengo.Probe(tarFdfw4.neurons, synapse=None)
        if stage==7:
            c1 = nengo.Connection(preA, fdfw, synapse=fPre, solver=NoSolver(dPreA), seed=seed)
            c2 = nengo.Connection(fdfw, ens, synapse=NMDA(), solver=NoSolver(dFdfw), seed=seed)
            c3 = nengo.Connection(ens, ens, synapse=NMDA(), solver=NoSolver(dBio), seed=seed)
            c4 = nengo.Connection(ens, fdfw, synapse=NMDA(), solver=NoSolver(dNeg), seed=seed)
            c5 = nengo.Connection(preC, inh, synapse=fPre, solver=NoSolver(dPreC), seed=seed)
            c6 = nengo.Connection(inh, fdfw, synapse=GABA(), solver=NoSolver(dInh), seed=seed)

    with nengo.Simulator(model, seed=seed, dt=dt, progress_bar=False) as sim:
        if stage==1:
            setWeights(c1, dPreA, ePreA)
            setWeights(c2, dPreB, ePreB)
            setWeights(c3, dPreC, ePreC)
        if stage==2:
            setWeights(c1, dPreA, ePreA)
            setWeights(c2, dPreC, ePreC)
        if stage==3:
            setWeights(c1, dPreA, ePreA)
            setWeights(c2, dPreB, ePreB)
            setWeights(c3, dFdfw, eFdfw)
        if stage==4:
            setWeights(c1, dPreA, ePreA)
            setWeights(c2, dPreB, ePreB)
            setWeights(c3, dFdfw, eFdfw)
        if stage==5:
            setWeights(c1, dPreA, ePreA)
            setWeights(c2, dFdfw, eFdfw)
            setWeights(c3, dPreB, ePreB)
            setWeights(c4, dBio, eBio)
            setWeights(c5, dFdfw, eFdfw)
            setWeights(c6, dPreB, ePreB)
        if stage==6:
            setWeights(c1, dPreB, ePreB)
            setWeights(c2, dNeg, eNeg)
            setWeights(c3, dPreA, ePreA)
            setWeights(c4, dPreA, ePreA)
            setWeights(c5, dPreC, ePreC)
            setWeights(c6, dInh, eInh)
        if stage==7:
            setWeights(c1, dPreA, ePreA)
            setWeights(c2, dFdfw, eFdfw)
            setWeights(c3, dBio, eBio)
            setWeights(c4, dNeg, eNeg)
            setWeights(c5, dPreC, ePreC)
            setWeights(c6, dInh, eInh)
        neuron.h.init()
        sim.run(t, progress_bar=True)
        reset_neuron(sim, model) 

    ePreA = c1.e if stage==1 else ePreA
    ePreB = c2.e if stage==1 else ePreB
    ePreC = c3.e if stage==1 else ePreC
    eFdfw = c3.e if stage==3 else eFdfw
    eBio = c4.e if stage==5 else eBio
    eNeg = c2.e if stage==6 else eNeg
    eInh = c6.e if stage==6 else eInh

    return dict(
        times=sim.trange(),
        inptA=sim.data[pInptA],
        inptB=sim.data[pInptB],
        inptC=sim.data[pInptC],
        preA=sim.data[pPreA],
        preB=sim.data[pPreB],
        preC=sim.data[pPreC],
        fdfw=sim.data[pFdfw],
        ens=sim.data[pEns],
        inh=sim.data[pInh],
        tarFdfw=sim.data[pTarFdfw],
        tarEns=sim.data[pTarEns],
        tarInh=sim.data[pTarInh],
        fdfw2=sim.data[pFdfw2] if stage==6 else None,
        fdfw4=sim.data[pFdfw4] if stage==6 else None,
        tarFdfw2=sim.data[pTarFdfw2] if stage==6 else None,
        tarFdfw4=sim.data[pTarFdfw4] if stage==6 else None,
        ePreA=ePreA,
        ePreB=ePreB,
        ePreC=ePreC,
        eFdfw=eFdfw,
        eBio=eBio,
        eNeg=eNeg,
        eInh=eInh,
    )
Exemple #16
0
def go(NPre=100,
       N=100,
       t=10,
       m=Uniform(30, 30),
       i=Uniform(-0.8, 0.8),
       seed=0,
       dt=0.001,
       f=DoubleExp(1e-3, 1e-2),
       fS=DoubleExp(1e-3, 1e-1),
       neuron_type=LIF(),
       d1a=None,
       d1b=None,
       d2=None,
       f1a=None,
       f1b=None,
       f2=None,
       e1a=None,
       e1b=None,
       e2=None,
       l1a=False,
       l1b=False,
       l2=False,
       l3=False,
       test=False,
       stim=lambda t: np.sin(t),
       stim2=lambda t: 0):

    with nengo.Network(seed=seed) as model:
        inpt = nengo.Node(stim)
        intg = nengo.Ensemble(1, 1, neuron_type=nengo.Direct())
        preInpt = nengo.Ensemble(NPre, 1, radius=3, max_rates=m, seed=seed)
        preIntg = nengo.Ensemble(NPre, 1, max_rates=m, seed=seed)
        ens = nengo.Ensemble(N,
                             1,
                             max_rates=m,
                             intercepts=i,
                             neuron_type=neuron_type,
                             seed=seed)
        nengo.Connection(inpt, intg, synapse=1 / s, seed=seed)
        c0a = nengo.Connection(inpt, preInpt, synapse=None, seed=seed)
        c0b = nengo.Connection(intg, preIntg, synapse=None, seed=seed)
        c1a = nengo.Connection(preInpt,
                               ens,
                               synapse=f1a,
                               solver=NoSolver(d1a),
                               seed=seed)
        pInpt = nengo.Probe(inpt, synapse=None)
        pIntg = nengo.Probe(intg, synapse=None)
        pPreInpt = nengo.Probe(preInpt.neurons, synapse=None)
        pPreIntg = nengo.Probe(preIntg.neurons, synapse=None)
        pEns = nengo.Probe(ens.neurons, synapse=None)
        if l1b:  # preIntg-to-ens
            tarEns = nengo.Ensemble(N,
                                    1,
                                    max_rates=m,
                                    intercepts=i,
                                    neuron_type=nengo.LIF(),
                                    seed=seed)
            nengo.Connection(preIntg,
                             tarEns,
                             synapse=f1b,
                             solver=NoSolver(d1b),
                             seed=seed + 1)
            c1b = nengo.Connection(preIntg,
                                   ens,
                                   synapse=f1b,
                                   solver=NoSolver(d1b),
                                   seed=seed + 1)
            learnEncoders(c1b, tarEns, fS)
            pTarEns = nengo.Probe(tarEns.neurons, synapse=None)
        if l1a:  # preInpt-to-ens, given preIntg-to-ens
            inpt2 = nengo.Node(stim2)
            tarEns = nengo.Ensemble(N,
                                    1,
                                    max_rates=m,
                                    intercepts=i,
                                    neuron_type=nengo.LIF(),
                                    seed=seed)
            nengo.Connection(preInpt,
                             tarEns,
                             synapse=f1a,
                             solver=NoSolver(d1a),
                             seed=seed)
            c0b.transform = 0
            nengo.Connection(inpt2, preIntg, synapse=None, seed=seed)
            nengo.Connection(preIntg,
                             tarEns,
                             synapse=f1b,
                             solver=NoSolver(d1b),
                             seed=seed + 1)
            c1b = nengo.Connection(preIntg,
                                   ens,
                                   synapse=f1b,
                                   solver=NoSolver(d1b),
                                   seed=seed + 1)
            learnEncoders(c1a, tarEns, fS)
            pTarEns = nengo.Probe(tarEns.neurons, synapse=None)
        if l2:  # ens readout, given preIntg and preInpt
            c1b = nengo.Connection(preIntg,
                                   ens,
                                   synapse=f1b,
                                   solver=NoSolver(d1b),
                                   seed=seed + 1)
        if l3:  # ens2-to-ens, given preInpt-ens and preIntg-ens2
            ens2 = nengo.Ensemble(N,
                                  1,
                                  max_rates=m,
                                  intercepts=i,
                                  neuron_type=neuron_type,
                                  seed=seed)
            c0a.synapse = f
            c2a = nengo.Connection(preInpt,
                                   ens2,
                                   synapse=f1a,
                                   solver=NoSolver(d1a),
                                   seed=seed)
            c2b = nengo.Connection(preIntg,
                                   ens2,
                                   synapse=f1b,
                                   solver=NoSolver(d1b),
                                   seed=seed + 1)
            c3 = nengo.Connection(ens2,
                                  ens,
                                  synapse=f2,
                                  solver=NoSolver(d2),
                                  seed=seed)
            learnEncoders(c3, ens2, fS)
            pTarEns2 = nengo.Probe(ens2.neurons, synapse=None)
        if test:
            c5 = nengo.Connection(ens,
                                  ens,
                                  synapse=f2,
                                  solver=NoSolver(d2),
                                  seed=seed)

    with nengo.Simulator(model, seed=seed, dt=dt, progress_bar=False) as sim:
        if isinstance(neuron_type, Bio):
            if l1b:
                setWeights(c1b, d1b, e1b)
            if l1a:
                setWeights(c1a, d1a, e1a)
                setWeights(c1b, d1b, e1b)
            if l2:
                setWeights(c1a, d1a, e1a)
                setWeights(c1b, d1b, e1b)
            if l3:
                setWeights(c1a, d1a, e1a)
                setWeights(c2a, d1a, e1a)
                setWeights(c2b, d1b, e1b)
                setWeights(c3, d2, e2)
            if test:
                setWeights(c1a, d1a, e1a)
                setWeights(c5, d2, e2)
            neuron.h.init()
            sim.run(t, progress_bar=True)
            reset_neuron(sim, model)
        else:
            sim.run(t, progress_bar=True)

    e1a = c1a.e if l1a else e1a
    e1b = c1b.e if l1b else e1b
    e2 = c3.e if l3 else e2

    return dict(
        times=sim.trange(),
        inpt=sim.data[pInpt],
        intg=sim.data[pIntg],
        preInpt=sim.data[pPreInpt],
        preIntg=sim.data[pPreIntg],
        ens=sim.data[pEns],
        tarEns=sim.data[pTarEns] if l1a or l1b else None,
        tarEns2=sim.data[pTarEns2] if l3 else None,
        e1a=e1a,
        e1b=e1b,
        e2=e2,
    )
def go(NPre=300,
       NBias=100,
       N=30,
       t=10,
       seed=1,
       dt=0.001,
       Tff=0.3,
       tTrans=0.01,
       stage=None,
       alpha=3e-7,
       eMax=1e-1,
       fPre=DoubleExp(1e-3, 1e-1),
       fNMDA=DoubleExp(10.6e-3, 285e-3),
       fGABA=DoubleExp(0.5e-3, 1.5e-3),
       fS=DoubleExp(1e-3, 1e-1),
       dPre=None,
       dFdfw=None,
       dEns=None,
       dBias=None,
       ePreFdfw=None,
       ePreEns=None,
       ePreBias=None,
       eFdfwEns=None,
       eBiasEns=None,
       eEnsEns=None,
       stimA=lambda t: 0,
       stimB=lambda t: 0,
       stimC=lambda t: 0.01,
       DA=lambda t: 0):

    with nengo.Network(seed=seed) as model:
        inptA = nengo.Node(stimA)
        inptB = nengo.Node(stimB)
        inptC = nengo.Node(stimC)
        preA = nengo.Ensemble(NPre, 1, max_rates=Uniform(30, 30), seed=seed)
        preB = nengo.Ensemble(NPre, 1, max_rates=Uniform(30, 30), seed=seed)
        preC = nengo.Ensemble(NPre, 1, max_rates=Uniform(30, 30), seed=seed)
        fdfw = nengo.Ensemble(N,
                              1,
                              neuron_type=Bio("Pyramidal", DA=DA),
                              seed=seed)
        ens = nengo.Ensemble(N,
                             1,
                             neuron_type=Bio("Pyramidal", DA=DA),
                             seed=seed + 1)
        bias = nengo.Ensemble(NBias,
                              1,
                              neuron_type=Bio("Interneuron", DA=DA),
                              seed=seed + 2)
        tarFdfw = nengo.Ensemble(N,
                                 1,
                                 max_rates=Uniform(30, 30),
                                 intercepts=Uniform(-0.8, 0.8),
                                 seed=seed)
        tarEns = nengo.Ensemble(N,
                                1,
                                max_rates=Uniform(30, 30),
                                intercepts=Uniform(-0.8, 0.8),
                                seed=seed + 1)
        tarBias = nengo.Ensemble(NBias,
                                 1,
                                 max_rates=Uniform(30, 30),
                                 intercepts=Uniform(-0.8, -0.2),
                                 encoders=Choice([[1]]),
                                 seed=seed + 2)
        cA = nengo.Connection(inptA, preA, synapse=None, seed=seed)
        cB = nengo.Connection(inptB, preB, synapse=None, seed=seed)
        cC = nengo.Connection(inptC, preC, synapse=None, seed=seed)
        pInptA = nengo.Probe(inptA, synapse=None)
        pInptB = nengo.Probe(inptB, synapse=None)
        pInptC = nengo.Probe(inptC, synapse=None)
        pPreA = nengo.Probe(preA.neurons, synapse=None)
        pPreB = nengo.Probe(preB.neurons, synapse=None)
        pPreC = nengo.Probe(preC.neurons, synapse=None)
        pFdfw = nengo.Probe(fdfw.neurons, synapse=None)
        pTarFdfw = nengo.Probe(tarFdfw.neurons, synapse=None)
        pEns = nengo.Probe(ens.neurons, synapse=None)
        pTarEns = nengo.Probe(tarEns.neurons, synapse=None)
        pBias = nengo.Probe(bias.neurons, synapse=None)
        pTarBias = nengo.Probe(tarBias.neurons, synapse=None)
        if stage == 0:  # readout decoders for [preA, preB, preC]
            pass
        if stage == 1:  # encoders for [preA, preC] to [fdfw, bias]
            nengo.Connection(inptA, tarFdfw, synapse=fPre, seed=seed)
            nengo.Connection(inptC, tarBias, synapse=fPre, seed=seed)
            c1 = nengo.Connection(preA,
                                  fdfw,
                                  synapse=fPre,
                                  solver=NoSolver(dPre),
                                  seed=seed)
            c2 = nengo.Connection(preC,
                                  bias,
                                  synapse=fPre,
                                  solver=NoSolver(dPre),
                                  seed=seed)
            learnEncoders(c1,
                          tarFdfw,
                          fS,
                          alpha=3 * alpha,
                          eMax=3 * eMax,
                          tTrans=tTrans)
            learnEncoders(c2,
                          tarBias,
                          fS,
                          alpha=alpha,
                          eMax=eMax,
                          tTrans=tTrans)
        if stage == 2:  # readout decoders for fdfw and bias
            c1 = nengo.Connection(preA,
                                  fdfw,
                                  synapse=fPre,
                                  solver=NoSolver(dPre),
                                  seed=seed)
            c2 = nengo.Connection(preC,
                                  bias,
                                  synapse=fPre,
                                  solver=NoSolver(dPre),
                                  seed=seed)
        if stage == 3:  # encoders for [bias] to ens
            # nengo.Connection(inptC, tarBias, synapse=fPre, seed=seed)
            nengo.Connection(inptC, tarEns, synapse=fGABA, seed=seed)
            c1 = nengo.Connection(preC,
                                  bias,
                                  synapse=fPre,
                                  solver=NoSolver(dPre),
                                  seed=seed)
            c2 = nengo.Connection(bias,
                                  ens,
                                  synapse=GABA(),
                                  solver=NoSolver(dBias),
                                  seed=seed)
            learnEncoders(c2,
                          tarEns,
                          fS,
                          alpha=1e3 * alpha,
                          eMax=1e3 * eMax,
                          tTrans=tTrans)
        if stage == 4:  # encoders for [preB] to [ens]
            # nengo.Connection(inptC, tarBias, synapse=fPre, seed=seed)
            nengo.Connection(inptC, tarEns, synapse=fGABA, seed=seed)
            nengo.Connection(inptB, tarEns, synapse=fPre, seed=seed)
            c1 = nengo.Connection(preC,
                                  bias,
                                  synapse=fPre,
                                  solver=NoSolver(dPre),
                                  seed=seed)
            c2 = nengo.Connection(bias,
                                  ens,
                                  synapse=GABA(),
                                  solver=NoSolver(dBias),
                                  seed=seed)
            c3 = nengo.Connection(preB,
                                  ens,
                                  synapse=fPre,
                                  solver=NoSolver(dPre),
                                  seed=seed)
            learnEncoders(c3,
                          tarEns,
                          fS,
                          alpha=3 * alpha,
                          eMax=3 * eMax,
                          tTrans=tTrans)
        if stage == 5:  # encoders for [fdfw] to ens
            cB.synapse = fNMDA
            tarPreA = nengo.Ensemble(1, 1, neuron_type=nengo.Direct())
            tarPreB = nengo.Ensemble(1, 1, neuron_type=nengo.Direct())
            tarPreC = nengo.Ensemble(1, 1, neuron_type=nengo.Direct())
            nengo.Connection(inptA, tarPreA, synapse=fPre, seed=seed)
            nengo.Connection(inptB, tarPreB, synapse=fNMDA, seed=seed)
            nengo.Connection(inptC, tarPreC, synapse=fPre, seed=seed)
            nengo.Connection(tarPreA,
                             tarEns,
                             synapse=fNMDA,
                             transform=Tff,
                             seed=seed)
            nengo.Connection(tarPreB, tarEns, synapse=fPre, seed=seed)
            nengo.Connection(tarPreC, tarEns, synapse=fGABA, seed=seed)
            c1 = nengo.Connection(preA,
                                  fdfw,
                                  synapse=fPre,
                                  solver=NoSolver(dPre),
                                  seed=seed)
            c2 = nengo.Connection(preB,
                                  ens,
                                  synapse=fPre,
                                  solver=NoSolver(dPre),
                                  seed=seed)
            c3 = nengo.Connection(preC,
                                  bias,
                                  synapse=fPre,
                                  solver=NoSolver(dPre),
                                  seed=seed)
            c4 = nengo.Connection(fdfw,
                                  ens,
                                  synapse=NMDA(),
                                  solver=NoSolver(dFdfw),
                                  seed=seed)
            c5 = nengo.Connection(bias,
                                  ens,
                                  synapse=GABA(),
                                  solver=NoSolver(dBias),
                                  seed=seed)
            learnEncoders(c4,
                          tarEns,
                          fS,
                          alpha=3 * alpha,
                          eMax=3 * eMax,
                          tTrans=tTrans)
        if stage == 6:  # readout decoders for ens
            cB.synapse = fNMDA
            c1 = nengo.Connection(preA,
                                  fdfw,
                                  synapse=fPre,
                                  solver=NoSolver(dPre),
                                  seed=seed)
            c2 = nengo.Connection(preB,
                                  ens,
                                  synapse=fPre,
                                  solver=NoSolver(dPre),
                                  seed=seed)
            c3 = nengo.Connection(preC,
                                  bias,
                                  synapse=fPre,
                                  solver=NoSolver(dPre),
                                  seed=seed)
            c4 = nengo.Connection(fdfw,
                                  ens,
                                  synapse=NMDA(),
                                  solver=NoSolver(dFdfw),
                                  seed=seed)
            c5 = nengo.Connection(bias,
                                  ens,
                                  synapse=GABA(),
                                  solver=NoSolver(dBias),
                                  seed=seed)
        if stage == 7:  # encoders from ens to ens
            preB2 = nengo.Ensemble(NPre,
                                   1,
                                   max_rates=Uniform(30, 30),
                                   seed=seed)
            ens2 = nengo.Ensemble(N,
                                  1,
                                  neuron_type=Bio("Pyramidal", DA=DA),
                                  seed=seed + 1)  # acts as preB input to ens
            ens3 = nengo.Ensemble(N,
                                  1,
                                  neuron_type=Bio("Pyramidal", DA=DA),
                                  seed=seed + 1)  # acts as tarEns
            nengo.Connection(inptB, preB2, synapse=fNMDA, seed=seed)
            pTarEns = nengo.Probe(ens3.neurons, synapse=None)
            c1 = nengo.Connection(preA,
                                  fdfw,
                                  synapse=fPre,
                                  solver=NoSolver(dPre),
                                  seed=seed)
            c2 = nengo.Connection(preB,
                                  ens2,
                                  synapse=fPre,
                                  solver=NoSolver(dPre),
                                  seed=seed)
            c3 = nengo.Connection(preB2,
                                  ens3,
                                  synapse=fPre,
                                  solver=NoSolver(dPre),
                                  seed=seed)
            c4 = nengo.Connection(preC,
                                  bias,
                                  synapse=fPre,
                                  solver=NoSolver(dPre),
                                  seed=seed)
            c5 = nengo.Connection(fdfw,
                                  ens,
                                  synapse=NMDA(),
                                  solver=NoSolver(dFdfw),
                                  seed=seed)
            c6 = nengo.Connection(fdfw,
                                  ens3,
                                  synapse=NMDA(),
                                  solver=NoSolver(dFdfw),
                                  seed=seed)
            c7 = nengo.Connection(bias,
                                  ens,
                                  synapse=GABA(),
                                  solver=NoSolver(dBias),
                                  seed=seed)
            c8 = nengo.Connection(bias,
                                  ens2,
                                  synapse=GABA(),
                                  solver=NoSolver(dBias),
                                  seed=seed)
            c9 = nengo.Connection(bias,
                                  ens3,
                                  synapse=GABA(),
                                  solver=NoSolver(dBias),
                                  seed=seed)
            c10 = nengo.Connection(ens2,
                                   ens,
                                   synapse=NMDA(),
                                   solver=NoSolver(dEns),
                                   seed=seed)
            learnEncoders(c10, ens3, fS, alpha=alpha, eMax=eMax, tTrans=tTrans)
        if stage == 8:  # test
            c1 = nengo.Connection(preA,
                                  fdfw,
                                  synapse=fPre,
                                  solver=NoSolver(dPre),
                                  seed=seed)
            c2 = nengo.Connection(preC,
                                  bias,
                                  synapse=fPre,
                                  solver=NoSolver(dPre),
                                  seed=seed)
            c3 = nengo.Connection(fdfw,
                                  ens,
                                  synapse=NMDA(),
                                  solver=NoSolver(dFdfw),
                                  seed=seed)
            c4 = nengo.Connection(bias,
                                  ens,
                                  synapse=GABA(),
                                  solver=NoSolver(dBias),
                                  seed=seed)
            c5 = nengo.Connection(ens,
                                  ens,
                                  synapse=NMDA(),
                                  solver=NoSolver(dEns),
                                  seed=seed)

    with nengo.Simulator(model, seed=seed, dt=dt, progress_bar=False) as sim:
        if stage == 0:
            pass
        if stage == 1:
            setWeights(c1, dPre, ePreFdfw)
            setWeights(c2, dPre, ePreBias)
        if stage == 2:
            setWeights(c1, dPre, ePreFdfw)
            setWeights(c2, dPre, ePreBias)
        if stage == 3:
            setWeights(c1, dPre, ePreBias)
            setWeights(c2, dBias, eBiasEns)
        if stage == 4:
            setWeights(c1, dPre, ePreBias)
            setWeights(c2, dBias, eBiasEns)
            setWeights(c3, dPre, ePreEns)
        if stage == 5:
            setWeights(c1, dPre, ePreFdfw)
            setWeights(c2, dPre, ePreEns)
            setWeights(c3, dPre, ePreBias)
            setWeights(c4, dFdfw, eFdfwEns)
            setWeights(c5, dBias, eBiasEns)
        if stage == 6:
            setWeights(c1, dPre, ePreFdfw)
            setWeights(c2, dPre, ePreEns)
            setWeights(c3, dPre, ePreBias)
            setWeights(c4, dFdfw, eFdfwEns)
            setWeights(c5, dBias, eBiasEns)
        if stage == 7:
            setWeights(c1, dPre, ePreFdfw)
            setWeights(c2, dPre, ePreEns)
            setWeights(c3, dPre, ePreEns)
            setWeights(c4, dPre, ePreBias)
            setWeights(c5, dFdfw, eFdfwEns)
            setWeights(c6, dFdfw, eFdfwEns)
            setWeights(c7, dBias, eBiasEns)
            setWeights(c8, dBias, eBiasEns)
            setWeights(c9, dBias, eBiasEns)
            setWeights(c10, dEns, eEnsEns)
        if stage == 8:
            setWeights(c1, dPre, ePreFdfw)
            setWeights(c2, dPre, ePreBias)
            setWeights(c3, dFdfw, eFdfwEns)
            setWeights(c4, dBias, eBiasEns)
            setWeights(c5, dEns, eEnsEns)

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

    ePreFdfw = c1.e if stage == 1 else ePreFdfw
    ePreBias = c2.e if stage == 1 else ePreBias
    eBiasEns = c2.e if stage == 3 else eBiasEns
    ePreEns = c3.e if stage == 4 else ePreEns
    eFdfwEns = c4.e if stage == 5 else eFdfwEns
    eEnsEns = c10.e if stage == 7 else eEnsEns

    return dict(
        times=sim.trange(),
        inptA=sim.data[pInptA],
        inptB=sim.data[pInptB],
        inptC=sim.data[pInptC],
        preA=sim.data[pPreA],
        fdfw=sim.data[pFdfw],
        ens=sim.data[pEns],
        bias=sim.data[pBias],
        tarFdfw=sim.data[pTarFdfw],
        tarEns=sim.data[pTarEns],
        tarBias=sim.data[pTarBias],
        ePreFdfw=ePreFdfw,
        ePreEns=ePreEns,
        ePreBias=ePreBias,
        eFdfwEns=eFdfwEns,
        eBiasEns=eBiasEns,
        eEnsEns=eEnsEns,
    )
Exemple #18
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,
    )
Exemple #19
0
def go(NPre=100, N=30, t=10, m=Uniform(30, 30), i=Uniform(-0.8, 0.8), seed=0, dt=0.001, f=DoubleExp(1e-3, 1e-1), fS=DoubleExp(1e-3, 1e-1), neuron_type=LIF(), d1=None, d2=None, f1=None, f2=None, e1=None, e2=None, l1=False, l2=False, test=False, freq=1, phase=0, tDrive=0.2):

    A = [[1, 1e-1*2*np.pi*freq], [-1e-1*2*np.pi*freq, 1]]  # tau*A + I
    if isinstance(neuron_type, Bio) and not f1: f1=DoubleExp(1e-3, 1e-1)
    if isinstance(neuron_type, Bio) and not f2: f2=DoubleExp(1e-3, 1e-1)
    stim = lambda t: [np.sin(2*np.pi*freq*t+phase), np.cos(2*np.pi*freq*t+phase)]

    with nengo.Network(seed=seed) as model:          
        inpt = nengo.Node(stim)
        tar = nengo.Ensemble(1, 2, neuron_type=nengo.Direct())
        pre = nengo.Ensemble(NPre, 2, max_rates=m, neuron_type=nengo.SpikingRectifiedLinear(), radius=2, seed=seed)
        ens = nengo.Ensemble(N, 2, max_rates=m, intercepts=i, neuron_type=neuron_type, radius=2, seed=seed)
        nengo.Connection(inpt, tar, synapse=None, transform=A, seed=seed)
        nengo.Connection(inpt, pre, synapse=None, seed=seed)
        c1 = nengo.Connection(pre, ens, synapse=f1, seed=seed, solver=NoSolver(d1))
        pInpt = nengo.Probe(inpt, synapse=None)
        pTar = nengo.Probe(tar, synapse=None)
        pPre = nengo.Probe(pre.neurons, synapse=None)
        pEns = nengo.Probe(ens.neurons, synapse=None)
        # Encoder Learning (Bio)
        if l1:
            tarEns = nengo.Ensemble(N, 2, max_rates=m, intercepts=i, neuron_type=nengo.LIF(), seed=seed)
            nengo.Connection(inpt, tarEns, synapse=None, seed=seed)
            learnEncoders(c1, tarEns, fS)
            pTarEns = nengo.Probe(tarEns.neurons, synapse=None)
        if l2:
            pre2 = nengo.Ensemble(NPre, 2, max_rates=m, neuron_type=nengo.LIF(), seed=seed, radius=2)
            tarEns2 = nengo.Ensemble(N, 2, max_rates=m, intercepts=i, neuron_type=nengo.LIF(), seed=seed)
            ens2 = nengo.Ensemble(N, 2, max_rates=m, intercepts=i, neuron_type=neuron_type, seed=seed, radius=2)
            
#             ens3 = nengo.Ensemble(N, 2, max_rates=m, intercepts=i, neuron_type=neuron_type, seed=seed, radius=2)
#             nengo.Connection(tar, pre2, synapse=f)
#             c3 = nengo.Connection(ens, ens2, synapse=f2, seed=seed)
#             c4 = nengo.Connection(pre2, ens3, synapse=f1, seed=seed)
#             learnEncoders(c3, ens3, fS)
#             pTarEns2 = nengo.Probe(ens3.neurons, synapse=None)
#             pEns2 = nengo.Probe(ens2.neurons, synapse=None)

            nengo.Connection(inpt, pre2, synapse=f)
            nengo.Connection(pre2, tarEns2, synapse=f, seed=seed)
            c3 = nengo.Connection(ens, ens2, synapse=f2, seed=seed)
            learnEncoders(c3, tarEns2, fS, alpha=3e-7)
            pTarEns2 = nengo.Probe(tarEns2.neurons, synapse=None)
            pEns2 = nengo.Probe(ens2.neurons, synapse=None)
        if test:
            c2 = nengo.Connection(ens, ens, synapse=f2, seed=seed, solver=NoSolver(d2))
            off = nengo.Node(lambda t: 1 if t>tDrive else 0)
            nengo.Connection(off, pre.neurons, synapse=None, transform=-1e4*np.ones((NPre, 1)))

    with nengo.Simulator(model, seed=seed, dt=dt, progress_bar=False) as sim:
        if isinstance(neuron_type, Bio):
            setWeights(c1, d1, e1)
            if l2: setWeights(c3, d2, e2)
#             if l2: setWeights(c4, d1, e1)
            if test: setWeights(c2, d2, e2)
            neuron.h.init()
            sim.run(t, progress_bar=True)
            reset_neuron(sim, model) 
        else:
            sim.run(t, progress_bar=True)
      
    e1 = c1.e if l1 else e1
    e2 = c3.e if l2 else e2

    return dict(
        times=sim.trange(),
        inpt=sim.data[pInpt],
        tar=sim.data[pTar],
        pre=sim.data[pPre],
        ens=sim.data[pEns],
        tarEns=sim.data[pTarEns] if l1 else None,
        tarEns2=sim.data[pTarEns2] if l2 else None,
        ens2=sim.data[pEns2] if l2 else None,
        e1=e1,
        e2=e2,
    )
def go(NPre=100,
       N=100,
       t=10,
       c=None,
       dt=0.001,
       m=Uniform(30, 30),
       i=Uniform(-0.8, 0.8),
       seed=0,
       neuron_type=LIF(),
       fPre=None,
       fNMDA=None,
       dPre=None,
       dDiff=None,
       dEns=None,
       dNeg=None,
       Tff=1.0,
       stim=lambda t: 0,
       train=False):

    if not c: c = t
    dFF = dDiff * Tff if np.any(dDiff) else None
    with nengo.Network(seed=seed) as model:
        inpt = nengo.Node(stim)
        intg = nengo.Ensemble(1, 1, neuron_type=nengo.Direct())
        cut = nengo.Node(lambda t: 0 if t < c else 1)
        pre = nengo.Ensemble(NPre, 1, max_rates=m, seed=seed)
        pre2 = nengo.Ensemble(NPre, 1, max_rates=m, seed=seed)
        diff = nengo.Ensemble(N,
                              1,
                              max_rates=m,
                              intercepts=i,
                              neuron_type=neuron_type,
                              seed=seed)
        ens = nengo.Ensemble(N,
                             1,
                             max_rates=m,
                             intercepts=i,
                             neuron_type=neuron_type,
                             seed=seed)
        nengo.Connection(inpt, intg, synapse=1 / s)
        nengo.Connection(inpt, pre, synapse=None, seed=seed)
        nengo.Connection(intg, pre2, synapse=fNMDA, seed=seed)
        c1 = nengo.Connection(pre,
                              diff,
                              synapse=fPre,
                              solver=NoSolver(dPre),
                              seed=seed)
        c2 = nengo.Connection(diff,
                              ens,
                              synapse=fNMDA,
                              solver=NoSolver(dFF),
                              seed=seed)
        c3 = nengo.Connection(ens,
                              ens,
                              synapse=fNMDA,
                              solver=NoSolver(dEns),
                              seed=seed)
        c4 = nengo.Connection(ens,
                              diff,
                              synapse=fNMDA,
                              solver=NoSolver(dNeg),
                              seed=seed)
        c5 = nengo.Connection(cut,
                              diff.neurons,
                              synapse=None,
                              transform=-1e2 * np.ones((N, 1)))
        if train:
            nengo.Connection(pre2,
                             ens,
                             synapse=fPre,
                             solver=NoSolver(dPre),
                             seed=seed)
        pInpt = nengo.Probe(inpt, synapse=None)
        pIntg = nengo.Probe(intg, synapse=None)
        pPre = nengo.Probe(pre.neurons, synapse=None)
        pDiff = nengo.Probe(diff.neurons, synapse=None)
        pEns = nengo.Probe(ens.neurons, synapse=None)

    with nengo.Simulator(model, seed=seed, dt=dt, progress_bar=False) as sim:
        sim.run(t, progress_bar=True)

    return dict(
        times=sim.trange(),
        inpt=sim.data[pInpt],
        intg=sim.data[pIntg],
        pre=sim.data[pPre],
        diff=sim.data[pDiff],
        ens=sim.data[pEns],
    )
Exemple #21
0
def test_ff_learn(params, device):
    """Feedforward test with decoders initialized to 0 and learning"""

    l_rate = 0.001
    stim_val = 0.25
    tol = 0.001
    if isinstance(params["neuron"], nengo.SpikingRectifiedLinear):
        tol *= 2
    p_syn = 0.05

    with nengo.Network() as net:
        stim = nengo.Node([stim_val] * params["dims_in"])  # Input node

        # Nengo reference
        ens = nengo.Ensemble(
            params["n_neurons"],
            params["dims_in"],
            neuron_type=params["neuron"],
            bias=params["bias"],
            encoders=params["encoders"],
            gain=params["gain"],
        )
        output = nengo.Node(size_in=params["dims_out"])
        nengo_err = nengo.Node(size_in=params["dims_out"])

        nengo.Connection(stim, ens)
        conn = nengo.Connection(
            ens,
            output,
            solver=NoSolver(np.zeros(
                (params["n_neurons"], params["dims_out"]))),
            function=params["func"],
            learning_rule_type=nengo.PES(l_rate),
        )
        nengo.Connection(stim,
                         nengo_err,
                         function=params["func"],
                         transform=-1)
        nengo.Connection(output, nengo_err)
        nengo.Connection(nengo_err, conn.learning_rule)

        p_nengo = nengo.Probe(output, synapse=p_syn)

        # FPGA
        fpga = FpgaPesEnsembleNetwork(
            device,
            params["n_neurons"],
            params["dims_in"],
            l_rate,
            function=params["func"],
        )
        fpga.connection.solver = NoSolver(np.zeros(params["decoders"].shape).T)
        fpga.ensemble.neuron_type = params["neuron"]
        fpga.ensemble.encoders = params["encoders"]
        fpga.ensemble.bias = params["bias"]
        fpga.ensemble.gain = params["gain"]
        fpga_err = nengo.Node(size_in=params["dims_out"])

        nengo.Connection(stim, fpga.input)
        nengo.Connection(stim, fpga_err, function=params["func"], transform=-1)
        nengo.Connection(fpga.output, fpga_err)
        nengo.Connection(fpga_err, fpga.error)

        p_fpga = nengo.Probe(fpga.output, synapse=p_syn)

    with nengo_fpga.Simulator(net) as sim:
        sim.run(1)

    assert fpga.config_found and fpga.using_fpga_sim  # Ensure real FPGA

    idx = 2 if device == "de1" else 1  # Compensate for DE1 off-by-one
    assert np.allclose(sim.data[p_fpga][-1], sim.data[p_nengo][-idx], atol=tol)
def go(NPre=100,
       N=100,
       t=10,
       c=None,
       seed=1,
       dt=0.001,
       tTrans=0.01,
       stage=None,
       alpha=3e-7,
       eMax=1e-1,
       Tff=0.3,
       fPre=DoubleExp(1e-3, 1e-1),
       fNMDA=DoubleExp(10.6e-3, 285e-3),
       fGABA=DoubleExp(0.5e-3, 1.5e-3),
       fS=DoubleExp(1e-3, 1e-1),
       dPreA=None,
       dPreB=None,
       dPreC=None,
       dPreD=None,
       dFdfw=None,
       dEns=None,
       dOff=None,
       ePreAFdfw=None,
       ePreBEns=None,
       ePreCOff=None,
       eFdfwEns=None,
       eEnsEns=None,
       ePreDEns=None,
       eOffFdfw=None,
       stimA=lambda t: 0,
       stimB=lambda t: 0,
       stimC=lambda t: 0,
       stimD=lambda t: 0,
       DA=lambda t: 0):

    if not c: c = t
    with nengo.Network(seed=seed) as model:
        inptA = nengo.Node(stimA)
        inptB = nengo.Node(stimB)
        inptC = nengo.Node(stimC)
        inptD = nengo.Node(stimD)
        preA = nengo.Ensemble(NPre, 1, max_rates=Uniform(30, 30), seed=seed)
        preB = nengo.Ensemble(NPre, 1, max_rates=Uniform(30, 30), seed=seed)
        preC = nengo.Ensemble(NPre, 1, max_rates=Uniform(30, 30), seed=seed)
        preD = nengo.Ensemble(NPre, 1, max_rates=Uniform(30, 30), seed=seed)
        fdfw = nengo.Ensemble(N,
                              1,
                              neuron_type=Bio("Pyramidal", DA=DA),
                              seed=seed)
        ens = nengo.Ensemble(N,
                             1,
                             neuron_type=Bio("Pyramidal", DA=DA),
                             seed=seed + 1)
        off = nengo.Ensemble(N,
                             1,
                             neuron_type=Bio("Interneuron", DA=DA),
                             seed=seed + 3)
        tarFdfw = nengo.Ensemble(N,
                                 1,
                                 max_rates=Uniform(30, 30),
                                 intercepts=Uniform(-0.8, 0.8),
                                 seed=seed)
        tarEns = nengo.Ensemble(N,
                                1,
                                max_rates=Uniform(30, 30),
                                intercepts=Uniform(-0.8, 0.8),
                                seed=seed + 1)
        tarOff = nengo.Ensemble(N,
                                1,
                                max_rates=Uniform(30, 30),
                                intercepts=Uniform(0.2, 0.8),
                                encoders=Choice([[1]]),
                                seed=seed + 3)
        cA = nengo.Connection(inptA, preA, synapse=None, seed=seed)
        cB = nengo.Connection(inptB, preB, synapse=None, seed=seed)
        cC = nengo.Connection(inptC, preC, synapse=None, seed=seed)
        cD = nengo.Connection(inptD, preD, synapse=None, seed=seed)
        pInptA = nengo.Probe(inptA, synapse=None)
        pInptB = nengo.Probe(inptB, synapse=None)
        pInptC = nengo.Probe(inptC, synapse=None)
        pInptD = nengo.Probe(inptD, synapse=None)
        pPreA = nengo.Probe(preA.neurons, synapse=None)
        pPreB = nengo.Probe(preB.neurons, synapse=None)
        pPreC = nengo.Probe(preC.neurons, synapse=None)
        pPreD = nengo.Probe(preD.neurons, synapse=None)
        pFdfw = nengo.Probe(fdfw.neurons, synapse=None)
        pTarFdfw = nengo.Probe(tarFdfw.neurons, synapse=None)
        pEns = nengo.Probe(ens.neurons, synapse=None)
        pTarEns = nengo.Probe(tarEns.neurons, synapse=None)
        pOff = nengo.Probe(off.neurons, synapse=None)
        pTarOff = nengo.Probe(tarOff.neurons, synapse=None)
        if stage == 0:
            nengo.Connection(preD, tarEns, synapse=fPre, seed=seed)
            c0 = nengo.Connection(preD,
                                  ens,
                                  synapse=fPre,
                                  solver=NoSolver(dPreD),
                                  seed=seed)
            learnEncoders(c0,
                          tarEns,
                          fS,
                          alpha=10 * alpha,
                          eMax=10 * eMax,
                          tTrans=tTrans)
        if stage == 1:
            nengo.Connection(inptA, tarFdfw, synapse=fPre, seed=seed)
            nengo.Connection(inptB, tarEns, synapse=fPre, seed=seed)
            nengo.Connection(inptC, tarOff, synapse=fPre, seed=seed)
            c0 = nengo.Connection(preD,
                                  ens,
                                  synapse=fPre,
                                  solver=NoSolver(dPreD),
                                  seed=seed)
            c1 = nengo.Connection(preA,
                                  fdfw,
                                  synapse=fPre,
                                  solver=NoSolver(dPreA),
                                  seed=seed)
            c2 = nengo.Connection(preB,
                                  ens,
                                  synapse=fPre,
                                  solver=NoSolver(dPreB),
                                  seed=seed)
            c3 = nengo.Connection(preC,
                                  off,
                                  synapse=fPre,
                                  solver=NoSolver(dPreC),
                                  seed=seed)
            learnEncoders(c1,
                          tarFdfw,
                          fS,
                          alpha=3 * alpha,
                          eMax=3 * eMax,
                          tTrans=tTrans)
            learnEncoders(c2,
                          tarEns,
                          fS,
                          alpha=10 * alpha,
                          eMax=10 * eMax,
                          tTrans=tTrans)
            learnEncoders(c3,
                          tarOff,
                          fS,
                          alpha=3 * alpha,
                          eMax=3 * eMax,
                          tTrans=tTrans)
        if stage == 2:
            c1 = nengo.Connection(preA,
                                  fdfw,
                                  synapse=fPre,
                                  solver=NoSolver(dPreA),
                                  seed=seed)
            c2 = nengo.Connection(preC,
                                  off,
                                  synapse=fPre,
                                  solver=NoSolver(dPreC),
                                  seed=seed)
        if stage == 3:
            cB.synapse = fNMDA
            ff = nengo.Ensemble(1, 1, neuron_type=nengo.Direct())
            fb = nengo.Ensemble(1, 1, neuron_type=nengo.Direct())
            nengo.Connection(inptA, ff, synapse=fPre, seed=seed)
            nengo.Connection(inptB, fb, synapse=fNMDA, seed=seed)
            nengo.Connection(fb, tarEns, synapse=fPre, seed=seed)
            nengo.Connection(ff,
                             tarEns,
                             synapse=fNMDA,
                             transform=Tff,
                             seed=seed)
            c0 = nengo.Connection(preD,
                                  ens,
                                  synapse=fPre,
                                  solver=NoSolver(dPreD),
                                  seed=seed)
            c1 = nengo.Connection(preA,
                                  fdfw,
                                  synapse=fPre,
                                  solver=NoSolver(dPreA),
                                  seed=seed)
            c2 = nengo.Connection(preB,
                                  ens,
                                  synapse=fPre,
                                  solver=NoSolver(dPreB),
                                  seed=seed)
            c3 = nengo.Connection(fdfw,
                                  ens,
                                  synapse=NMDA(),
                                  solver=NoSolver(dFdfw),
                                  seed=seed)
            learnEncoders(c3,
                          tarEns,
                          fS,
                          alpha=3 * alpha,
                          eMax=3 * eMax,
                          tTrans=tTrans)
        if stage == 4:
            cB.synapse = fNMDA
            c0 = nengo.Connection(preD,
                                  ens,
                                  synapse=fPre,
                                  solver=NoSolver(dPreD),
                                  seed=seed)
            c1 = nengo.Connection(preA,
                                  fdfw,
                                  synapse=fPre,
                                  solver=NoSolver(dPreA),
                                  seed=seed)
            c2 = nengo.Connection(preB,
                                  ens,
                                  synapse=fPre,
                                  solver=NoSolver(dPreB),
                                  seed=seed)
            c3 = nengo.Connection(fdfw,
                                  ens,
                                  synapse=NMDA(),
                                  solver=NoSolver(dFdfw),
                                  seed=seed)
        if stage == 5:
            preB2 = nengo.Ensemble(NPre,
                                   1,
                                   max_rates=Uniform(30, 30),
                                   seed=seed)
            ens2 = nengo.Ensemble(N,
                                  1,
                                  neuron_type=Bio("Pyramidal", DA=DA),
                                  seed=seed + 1)
            ens3 = nengo.Ensemble(N,
                                  1,
                                  neuron_type=Bio("Pyramidal", DA=DA),
                                  seed=seed + 1)
            nengo.Connection(inptB, preB2, synapse=fNMDA, seed=seed)
            c0a = nengo.Connection(preD,
                                   ens,
                                   synapse=fPre,
                                   solver=NoSolver(dPreD),
                                   seed=seed)
            c0b = nengo.Connection(preD,
                                   ens2,
                                   synapse=fPre,
                                   solver=NoSolver(dPreD),
                                   seed=seed)
            c0c = nengo.Connection(preD,
                                   ens3,
                                   synapse=fPre,
                                   solver=NoSolver(dPreD),
                                   seed=seed)
            c1 = nengo.Connection(preA,
                                  fdfw,
                                  synapse=fPre,
                                  solver=NoSolver(dPreA),
                                  seed=seed)
            c2 = nengo.Connection(fdfw,
                                  ens,
                                  synapse=NMDA(),
                                  solver=NoSolver(dFdfw),
                                  seed=seed)
            c3 = nengo.Connection(preB,
                                  ens2,
                                  synapse=fPre,
                                  solver=NoSolver(dPreB),
                                  seed=seed)
            c4 = nengo.Connection(ens2,
                                  ens,
                                  synapse=NMDA(),
                                  solver=NoSolver(dEns),
                                  seed=seed)
            c5 = nengo.Connection(fdfw,
                                  ens3,
                                  synapse=NMDA(),
                                  solver=NoSolver(dFdfw),
                                  seed=seed)
            c6 = nengo.Connection(preB2,
                                  ens3,
                                  synapse=fPre,
                                  solver=NoSolver(dPreB),
                                  seed=seed)
            learnEncoders(c4, ens3, fS, alpha=alpha, eMax=eMax, tTrans=tTrans)
            pTarEns = nengo.Probe(ens3.neurons, synapse=None)
        if stage == 9:
            c0 = nengo.Connection(preD,
                                  ens,
                                  synapse=fPre,
                                  solver=NoSolver(dPreD),
                                  seed=seed)
            c1 = nengo.Connection(preA,
                                  fdfw,
                                  synapse=fPre,
                                  solver=NoSolver(dPreA),
                                  seed=seed)
            c2 = nengo.Connection(fdfw,
                                  ens,
                                  synapse=NMDA(),
                                  solver=NoSolver(dFdfw),
                                  seed=seed)
            c3 = nengo.Connection(ens,
                                  ens,
                                  synapse=NMDA(),
                                  solver=NoSolver(dEns),
                                  seed=seed)
            c6 = nengo.Connection(preC,
                                  off,
                                  synapse=fPre,
                                  solver=NoSolver(dPreC),
                                  seed=seed)
            c7 = nengo.Connection(off,
                                  fdfw,
                                  synapse=GABA(),
                                  solver=NoSolver(dOff),
                                  seed=seed)

    with nengo.Simulator(model, seed=seed, dt=dt, progress_bar=False) as sim:
        if stage == 0:
            setWeights(c0, dPreD, ePreDEns)
        if stage == 1:
            setWeights(c0, dPreD, ePreDEns)
            setWeights(c1, dPreA, ePreAFdfw)
            setWeights(c2, dPreB, ePreBEns)
            setWeights(c3, dPreC, ePreCOff)
        if stage == 2:
            setWeights(c1, dPreA, ePreAFdfw)
            setWeights(c2, dPreC, ePreCOff)
        if stage == 3:
            setWeights(c0, dPreD, ePreDEns)
            setWeights(c1, dPreA, ePreAFdfw)
            setWeights(c2, dPreB, ePreBEns)
            setWeights(c3, dFdfw, eFdfwEns)
        if stage == 4:
            setWeights(c0, dPreD, ePreDEns)
            setWeights(c1, dPreA, ePreAFdfw)
            setWeights(c2, dPreB, ePreBEns)
            setWeights(c3, dFdfw, eFdfwEns)
        if stage == 5:
            setWeights(c0a, dPreD, ePreDEns)
            setWeights(c0b, dPreD, ePreDEns)
            setWeights(c0c, dPreD, ePreDEns)
            setWeights(c1, dPreA, ePreAFdfw)
            setWeights(c2, dFdfw, eFdfwEns)
            setWeights(c3, dPreB, ePreBEns)
            setWeights(c4, dEns, eEnsEns)
            setWeights(c5, dFdfw, eFdfwEns)
            setWeights(c6, dPreB, ePreBEns)
        if stage == 9:
            setWeights(c0, dPreD, ePreDEns)
            setWeights(c1, dPreA, ePreAFdfw)
            setWeights(c2, dFdfw, eFdfwEns)
            setWeights(c3, dEns, eEnsEns)
            setWeights(c6, dPreC, ePreCOff)
            setWeights(c7, dOff, eOffFdfw)

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

    ePreDEns = c0.e if stage == 0 else ePreDEns
    ePreAFdfw = c1.e if stage == 1 else ePreAFdfw
    ePreBEns = c2.e if stage == 1 else ePreBEns
    ePreCOff = c3.e if stage == 1 else ePreCOff
    eFdfwEns = c3.e if stage == 3 else eFdfwEns
    eEnsEns = c4.e if stage == 5 else eEnsEns

    return dict(
        times=sim.trange(),
        inptA=sim.data[pInptA],
        inptB=sim.data[pInptB],
        inptC=sim.data[pInptC],
        inptD=sim.data[pInptD],
        preA=sim.data[pPreA],
        preB=sim.data[pPreB],
        preC=sim.data[pPreC],
        preD=sim.data[pPreD],
        fdfw=sim.data[pFdfw],
        ens=sim.data[pEns],
        off=sim.data[pOff],
        tarFdfw=sim.data[pTarFdfw],
        tarEns=sim.data[pTarEns],
        tarOff=sim.data[pTarOff],
        ePreAFdfw=ePreAFdfw,
        ePreBEns=ePreBEns,
        ePreCOff=ePreCOff,
        ePreDEns=ePreDEns,
        eFdfwEns=eFdfwEns,
        eEnsEns=eEnsEns,
        eOffFdfw=eOffFdfw,
    )
Exemple #23
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,
    )