예제 #1
0
 def calibrate_current(self, ttfs):
     for current in range(0, 10000, 1):
         neuron = Neuron()
         for time in range(0, ttfs + 1):
             neuron.time_step(current / 10., time)
             if neuron.has_spiked(): break
         if neuron.last_spike == ttfs:
             return current / 10.
예제 #2
0
 def calibrate_current(self, rate):
     """ Rate must be less than self.t_window """
     for current in range(0, 1000, 1):
         neuron = Neuron()
         spikes = 0
         for c_time in range(1, self.t_window + 1):
             if neuron.time_step(current / 10., c_time) == 1:
                 spikes += 1
         if spikes == rate: return current / 10.
         if spikes == self.t_window: break
     return -1
예제 #3
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])
예제 #4
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,
    )
예제 #5
0
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,
    )
예제 #6
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,
    )
예제 #7
0
def run(NPre=300, N=100, t=20, tTrans=2, nTrain=1, nEnc=10, nTest=10, neuron_type=LIF(),
        dt=0.001, f=DoubleExp(1e-3, 1e-1), fS=DoubleExp(1e-3, 1e-1), freq=1, muFreq=1.0, sigmaFreq=0.1, reg=1e-1, tauRiseMax=5e-2, tDrive=0.2, base=False, load=False, file=None):

    print('\nNeuron Type: %s'%neuron_type)
    rng = np.random.RandomState(seed=0)
    if load:
        d1 = np.load(file)['d1']
        tauRise1 = np.load(file)['tauRise1']
        tauFall1 = np.load(file)['tauFall1']
        f1 = DoubleExp(tauRise1, tauFall1)
    else:
        print('readout decoders for pre')  
        spikes = np.zeros((nTrain, int(t/0.001), NPre))
        targets = np.zeros((nTrain, int(t/0.001), 2))
        for n in range(nTrain):
            data = go(NPre=NPre, N=N, t=t, dt=0.001, f=f, fS=fS, neuron_type=LIF(), freq=freq, phase=2*np.pi*(n/nTrain))
            spikes[n] = data['pre']
            targets[n] = f.filt(data['inpt'], dt=0.001)
        d1, f1, tauRise1, tauFall1, X, Y, error = decode(spikes, targets, nTrain, dt=0.001, tauRiseMax=tauRiseMax, name="oscillateNew")
        np.savez("data/oscillateNew_%s.npz"%neuron_type, d1=d1, tauRise1=tauRise1, tauFall1=tauFall1)
        times = np.arange(0, t*nTrain, 0.001)
        plotState(times, X, Y, error, "oscillateNew", "%s_pre"%neuron_type, t*nTrain)

    if load:
        e1 = np.load(file)['e1']
    elif isinstance(neuron_type, Bio):
        print("ens1 encoders")
        e1 = np.zeros((NPre, N, 2))
        for n in range(nEnc):
            data = go(d1=d1, e1=e1, f1=f1, NPre=NPre, N=N, t=t, dt=dt, f=f, fS=fS, neuron_type=neuron_type, freq=freq, phase=2*np.pi*(n/nEnc), l1=True)
            e1 = data['e1']
            np.savez("data/oscillateNew_%s.npz"%neuron_type, d1=d1, tauRise1=tauRise1, tauFall1=tauFall1, e1=e1)
            plotActivity(t, dt, fS, data['times'], data['ens'], data['tarEns'], "oscillateNew", "ens")
    else:
        e1 = np.zeros((NPre, N, 2))
        
    if load:
        d2 = np.load(file)['d2']
        tauRise2 = np.load(file)['tauRise2']
        tauFall2 = np.load(file)['tauFall2']
        f2 = DoubleExp(tauRise2, tauFall2)
    else:
        print('readout decoders for ens')
        spikes = np.zeros((nTrain, int((t-tTrans)/dt), N))
        targets = np.zeros((nTrain, int((t-tTrans)/dt), 2))
        for n in range(nTrain):
            data = go(d1=d1, e1=e1, f1=f1, NPre=NPre, N=N, t=t, dt=dt, f=f, fS=fS, neuron_type=neuron_type, freq=freq, phase=2*np.pi*(n/nTrain))
            spikes[n] = data['ens'][int(tTrans/dt):]
            targets[n] = f.filt(f.filt(data['tar'], dt=dt), dt=dt)[int(tTrans/dt):]
        d2, f2, tauRise2, tauFall2, X, Y, error = decode(spikes, targets, nTrain, dt=dt, name="oscillateNew", reg=reg, tauRiseMax=tauRiseMax)
        np.savez("data/oscillateNew_%s.npz"%neuron_type, d1=d1, tauRise1=tauRise1, tauFall1=tauFall1, e1=e1, d2=d2, tauRise2=tauRise2, tauFall2=tauFall2)
        times = np.arange(0, t*nTrain, dt)[:len(X)]
        plotState(times, X, Y, error, "oscillateNew", "%s_ens"%neuron_type, (t-tTrans)*nTrain)

    if load:
        e2 = np.load(file)['e2']
    #elif isinstance(neuron_type, Bio):
        print("ens2 encoders")
        #e2 = np.zeros((N, N, 2))
        for n in range(nEnc):
            data = go(d1=d1, e1=e1, f1=f1, d2=d2, e2=e2, NPre=NPre, N=N, t=t, dt=dt, f=f, fS=fS, neuron_type=neuron_type, freq=freq, phase=2*np.pi*(n/nEnc), l2=True)
            e2 = data['e2']
            np.savez("data/oscillateNew_%s.npz"%neuron_type, d1=d1, tauRise1=tauRise1, tauFall1=tauFall1, e1=e1, d2=d2, tauRise2=tauRise2, tauFall2=tauFall2, e2=e2)
            plotActivity(t, dt, fS, data['times'], data['ens2'], data['tarEns2'], "oscillateNew", "ens2")
    else:
        e2 = np.zeros((N, N, 2))
        np.savez("data/oscillateNew_%s.npz"%neuron_type, d1=d1, tauRise1=tauRise1, tauFall1=tauFall1, e1=e1, d2=d2, tauRise2=tauRise2, tauFall2=tauFall2, e2=e2)

    print("testing")
    errors = np.zeros((nTest))
    for test in range(nTest):
        data = go(d1=d1, e1=e1, f1=f1, d2=d2, e2=e2, f2=f2, NPre=NPre, N=N, t=t+tTrans, dt=dt, f=f, fS=fS, neuron_type=neuron_type, freq=freq, phase=2*np.pi*(test/nTest), tDrive=tDrive, test=True)
        # curve fit to a sinusoid of arbitrary frequency, phase, magnitude
        times = data['times']
        A = f2.filt(data['ens'], dt=dt)
        X = np.dot(A, d2)
        freq0, phase0, mag0, base0 = fitSinusoid(times, X[:,0], freq, int(tTrans/dt), muFreq=muFreq, sigmaFreq=sigmaFreq, base=base)
        freq1, phase1, mag1, base1 = fitSinusoid(times, X[:,1], freq, int(tTrans/dt), muFreq=muFreq, sigmaFreq=sigmaFreq, base=base)
        s0 = base0+mag0*np.sin(times*2*np.pi*freq0+phase0)
        s1 = base1+mag1*np.sin(times*2*np.pi*freq1+phase1)
#         freqError0 = np.abs(freq-freq0)
#         freqError1 = np.abs(freq-freq1)
        rmseError0 = rmse(X[int(tTrans/dt):,0], s0[int(tTrans/dt):])
        rmseError1 = rmse(X[int(tTrans/dt):,1], s1[int(tTrans/dt):])
#         error0 = (1+freqError0) * rmseError0
#         error1 = (1+freqError1) * rmseError1
        error0 = rmseError0
        error1 = rmseError1
        errors[test] = (error0 + error1)/2
        fig, (ax, ax2) = plt.subplots(nrows=2, ncols=1, sharex=True)
        ax.plot(times, X[:,0], label="estimate (dim 0)")
        ax.plot(times, s0, label="target (dim 0)")
        ax.set(ylabel='state', title='freq=%.3f, rmse=%.3f'%(freq0, error0), xlim=((0, t)), ylim=((-1.2, 1.2)))
        ax.legend(loc='upper left')
        ax2.plot(times, X[:,1], label="estimate (dim 1)")
        ax2.plot(times, s1, label="target (dim 1)")
        ax2.set(xlabel='time', ylabel='state', title='freq=%.3f, rmse=%.3f'%(freq1, error1), xlim=((0, t)), ylim=((-1.2, 1.2)))
        ax2.legend(loc='upper left')
        sns.despine()
        fig.savefig("plots/oscillateNew_%s_test%s.pdf"%(neuron_type, test))
    plt.close('all')
    print('%s errors:'%neuron_type, errors)
    np.savez("data/oscillateNew_%s.npz"%neuron_type, d1=d1, tauRise1=tauRise1, tauFall1=tauFall1, e1=e1, d2=d2, tauRise2=tauRise2, tauFall2=tauFall2, e2=e2, errors=errors)
    return errors
예제 #8
0
def run(NPre=200,
        N=100,
        N2=100,
        t=10,
        nTrain=10,
        nEnc=20,
        nTest=10,
        neuron_type=LIF(),
        dt=0.001,
        f=DoubleExp(1e-3, 1e-1),
        fS=DoubleExp(1e-3, 1e-1),
        tauRiseMax=1e-2,
        load=False,
        file=None):

    print('\nNeuron Type: %s' % neuron_type)
    if load:
        d1 = np.load(file)['d1']
        tauRise1 = np.load(file)['tauRise1']
        tauFall1 = np.load(file)['tauFall1']
        f1 = DoubleExp(tauRise1, tauFall1)
    else:
        print('readout decoders for pre')
        spikes = np.zeros((nTrain, int(t / 0.001), NPre))
        targets = np.zeros((nTrain, int(t / 0.001), 2))
        for n in range(nTrain):
            stim = makeSignal(t, f, dt=0.001, seed=n)
            data = go(NPre=NPre,
                      N=N,
                      N2=N2,
                      t=t,
                      dt=0.001,
                      f=f,
                      fS=fS,
                      neuron_type=LIF(),
                      stim=stim)
            spikes[n] = data['pre']
            targets[n] = f.filt(data['inpt'], dt=0.001)
        d1, f1, tauRise1, tauFall1, X, Y, error = decode(spikes,
                                                         targets,
                                                         nTrain,
                                                         dt=0.001,
                                                         tauRiseMax=tauRiseMax,
                                                         name="multiplyNew")
        np.savez("data/multiplyNew_%s.npz" % neuron_type,
                 d1=d1,
                 tauRise1=tauRise1,
                 tauFall1=tauFall1)
        times = np.arange(0, t * nTrain, 0.001)
        plotState(times, X, Y, error, "multiplyNew", "%s_pre" % neuron_type,
                  t * nTrain)

    if load:
        e1 = np.load(file)['e1']
    elif isinstance(neuron_type, Bio):
        print("ens1 encoders")
        e1 = np.zeros((NPre, N, 2))
        for n in range(nEnc):
            stim = makeSignal(t, f, dt=dt, seed=n)
            data = go(d1=d1,
                      e1=e1,
                      f1=f1,
                      NPre=NPre,
                      N=N,
                      N2=N2,
                      t=t,
                      dt=dt,
                      f=f,
                      fS=fS,
                      neuron_type=neuron_type,
                      stim=stim,
                      l1=True)
            e1 = data['e1']
            np.savez("data/multiplyNew_%s.npz" % neuron_type,
                     d1=d1,
                     tauRise1=tauRise1,
                     tauFall1=tauFall1,
                     e1=e1)
            plotActivity(t, dt, fS, data['times'], data['ens'], data['tarEns'],
                         "multiplyNew", "ens")
    else:
        e1 = np.zeros((NPre, N, 2))

    if load:
        d2 = np.load(file)['d2']
        tauRise2 = np.load(file)['tauRise2']
        tauFall2 = np.load(file)['tauFall2']
        f2 = DoubleExp(tauRise2, tauFall2)
    else:
        print('readout decoders for ens')
        spikes = np.zeros((nTrain, int(t / dt), N))
        targets = np.zeros((nTrain, int(t / dt), 1))
        for n in range(nTrain):
            stim = makeSignal(t, f, dt=dt, seed=n)
            data = go(d1=d1,
                      e1=e1,
                      f1=f1,
                      NPre=NPre,
                      N=N,
                      N2=N2,
                      t=t,
                      dt=dt,
                      f=f,
                      fS=fS,
                      neuron_type=neuron_type,
                      stim=stim)
            spikes[n] = data['ens']
            targets[n] = f.filt(data['tar'][:, 0] * data['tar'][:, 1],
                                dt=dt).reshape(-1, 1)
        d2, f2, tauRise2, tauFall2, X, Y, error = decode(spikes,
                                                         targets,
                                                         nTrain,
                                                         dt=dt,
                                                         tauRiseMax=tauRiseMax,
                                                         name="multiplyNew")
        np.savez("data/multiplyNew_%s.npz" % neuron_type,
                 d1=d1,
                 tauRise1=tauRise1,
                 tauFall1=tauFall1,
                 e1=e1,
                 d2=d2,
                 tauRise2=tauRise2,
                 tauFall2=tauFall2)
        times = np.arange(0, t * nTrain, dt)
        plotState(times, X, Y, error, "multiplyNew", "%s_ens" % neuron_type,
                  t * nTrain)

    if load:
        e2 = np.load(file)['e2']
    elif isinstance(neuron_type, Bio):
        print("ens2 encoders")
        e2 = np.zeros((N, N2, 1))
        for n in range(nEnc):
            stim = makeSignal(t, f, dt=dt, seed=n)
            data = go(d1=d1,
                      e1=e1,
                      f1=f1,
                      d2=d2,
                      e2=e2,
                      f2=f2,
                      NPre=NPre,
                      N=N,
                      N2=N2,
                      t=t,
                      dt=dt,
                      f=f,
                      fS=fS,
                      neuron_type=neuron_type,
                      stim=stim,
                      l2=True)
            e2 = data['e2']
            np.savez("data/multiplyNew_%s.npz" % neuron_type,
                     d1=d1,
                     tauRise1=tauRise1,
                     tauFall1=tauFall1,
                     e1=e1,
                     d2=d2,
                     tauRise2=tauRise2,
                     tauFall2=tauFall2,
                     e2=e2)
            plotActivity(t, dt, fS, data['times'], data['ens2'],
                         data['tarEns2'], "multiplyNew", "ens2")
    else:
        e2 = np.zeros((N, N2, 1))

    if load:
        d3 = np.load(file)['d3']
        tauRise3 = np.load(file)['tauRise3']
        tauFall3 = np.load(file)['tauFall3']
        f3 = DoubleExp(tauRise3, tauFall3)
    else:
        print('readout decoders for ens2')
        spikes = np.zeros((nTrain, int(t / dt), N2))
        targets = np.zeros((nTrain, int(t / dt), 1))
        for n in range(nTrain):
            stim = makeSignal(t, f, dt=dt, seed=n)
            data = go(d1=d1,
                      e1=e1,
                      f1=f1,
                      d2=d2,
                      e2=e2,
                      f2=f2,
                      NPre=NPre,
                      N=N,
                      N2=N2,
                      t=t,
                      dt=dt,
                      f=f,
                      fS=fS,
                      neuron_type=neuron_type,
                      stim=stim)
            spikes[n] = data['ens2']
            targets[n] = f.filt(data['tar2'], dt=dt)
        d3, f3, tauRise3, tauFall3, X, Y, error = decode(spikes,
                                                         targets,
                                                         nTrain,
                                                         dt=dt,
                                                         name="multiplyNew")
        np.savez("data/multiplyNew_%s.npz" % neuron_type,
                 d1=d1,
                 tauRise1=tauRise1,
                 tauFall1=tauFall1,
                 e1=e1,
                 d2=d2,
                 tauRise2=tauRise2,
                 tauFall2=tauFall2,
                 e2=e2,
                 d3=d3,
                 tauRise3=tauRise3,
                 tauFall3=tauFall3)
        times = np.arange(0, t * nTrain, dt)
        plotState(times, X, Y, error, "multiplyNew", "%s_ens2" % neuron_type,
                  t * nTrain)

    errors = np.zeros((nTest))
    print("testing")
    for test in range(nTest):
        stim = makeSignal(t, f, dt=dt, seed=100 + test)
        data = go(d1=d1,
                  e1=e1,
                  f1=f1,
                  d2=d2,
                  e2=e2,
                  f2=f2,
                  NPre=NPre,
                  N=N,
                  N2=N2,
                  t=t,
                  dt=dt,
                  f=f,
                  fS=fS,
                  neuron_type=neuron_type,
                  stim=stim)
        A = f3.filt(data['ens2'], dt=dt)
        X = np.dot(A, d3)
        Y = f.filt(data['tar2'], dt=dt)
        error = rmse(X, Y)
        errors[test] = error
        plotState(data['times'], X, Y, error, "multiplyNew",
                  "%s_test%s" % (neuron_type, test), t)
        A = f2.filt(data['ens'], dt=dt)
        X = np.dot(A, d2)
        Y = f.filt(data['tar'][:, 0] * data['tar'][:, 1], dt=dt).reshape(-1, 1)
        plotState(data['times'], X, Y, rmse(X, Y), "multiplyNew",
                  "%s_pretest%s" % (neuron_type, test), t)

    print('%s errors:' % neuron_type, errors)
    np.savez("data/multiplyNew_%s.npz" % neuron_type,
             d1=d1,
             tauRise1=tauRise1,
             tauFall1=tauFall1,
             e1=e1,
             d2=d2,
             tauRise2=tauRise2,
             tauFall2=tauFall2,
             e2=e2,
             d3=d3,
             tauRise3=tauRise3,
             tauFall3=tauFall3,
             errors=errors)
    return errors
예제 #9
0
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,
    )
예제 #10
0
    print('%s errors:' % neuron_type, errors)
    np.savez("data/identityNew_%s.npz" % neuron_type,
             d1=d1,
             tauRise1=tauRise1,
             tauFall1=tauFall1,
             e1=e1,
             d2=d2,
             tauRise2=tauRise2,
             tauFall2=tauFall2,
             e2=e2,
             d3=d3,
             tauRise3=tauRise3,
             tauFall3=tauFall3,
             errors=errors)
    return errors


errorsLIF = run(neuron_type=LIF(),
                load=False,
                file="data/identityNew_LIF().npz")
errorsALIF = run(neuron_type=ALIF(),
                 load=False,
                 file="data/identityNew_ALIF().npz")
errorsWilson = run(neuron_type=Wilson(),
                   dt=1e-4,
                   load=False,
                   file="data/identityNew_Wilson().npz")
errorsBio = run(neuron_type=Bio("Pyramidal"),
                load=False,
                file="data/identityNew_Bio().npz")
예제 #11
0
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])
예제 #12
0
        Y2 = gaussian_filter1d(targets2, sigma=f2, axis=0)[-timeStepsTrain:]
        d2, _ = nengo.solvers.LstsqL2(reg=reg2)(spikes2, targets2)
        np.savez("data/lorenzNew_%s.npz"%neuron_type, d=d, d2=d2, tauRise=tauRise, tauFall=tauFall, f2=f2)
        X2 = np.dot(A2, d2)[-timeStepsTrain:]
        error = plotLorenz(X, X2, Y2, neuron_type, "train")

    print("testing")
    tStart = int(tTransTest/dtSampleTest)
    errors = np.zeros((nTest))
    rng = np.random.RandomState(seed=100+seed)
    for test in range(nTest):
        # IC = np.array([rng.uniform(-15, 15), rng.uniform(-20, 20), rng.uniform(10, 35)])
        IC = np.array([rng.uniform(-5, 5), rng.uniform(-5, 5), rng.uniform(20, 25)])
        data = go(d=d, f=f, N=N, neuron_type=neuron_type, t=tTest, r=r, dt=dt, dtSample=dtSampleTest, seed=seed, IC=IC)
        A = f.filt(data['ens'], dt=dtSampleTest)
        A2 = gaussian_filter1d(data['ens'], sigma=f2, axis=0)[tStart:]
        X = np.dot(A, d)[tStart:]
        X2 = np.dot(A2, d2)[tStart:]
        Y = data['tar'][tStart:]
        Y2 = gaussian_filter1d(data['tar'], sigma=f2, axis=0)[tStart:]
        error = plotLorenz(X, X2, Y2, neuron_type, test)
        errors[test] = error
    _ = plotLorenz(Y, Y2, Y2, 'target', '')
    print('errors: ', errors)
    np.savez("data/lorenzNew_%s.npz"%neuron_type, d=d, d2=d2, tauRise=tauRise, tauFall=tauFall, f2=f2, errors=errors)
    return errors

errorsLIF = run(neuron_type=LIF(), load=False, file="data/lorenzNew_LIF().npz")
errorsALIF = run(neuron_type=ALIF(), load=False, file="data/lorenzNew_ALIF().npz")
errorsWilson = run(neuron_type=Wilson(), dt=1e-4, load=False, file="data/lorenzNew_Wilson().npz")
예제 #13
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,
    )
예제 #14
0
def run(NPre=100,
        N=30,
        t=10,
        nTrain=5,
        nEnc=5,
        nTest=10,
        dt=0.001,
        neuron_type=LIF(),
        fPre=DoubleExp(1e-3, 1e-1),
        fS=DoubleExp(2e-2, 2e-1),
        Tff=0.3,
        reg=1e-1,
        tauRiseMax=1e-1,
        tauFallMax=3e-1,
        load=[],
        file="data/integrate"):

    print('\nNeuron Type: %s' % neuron_type)
    file = file + f"{neuron_type}.npz"

    if 0 in load:
        dPreA = np.load(file)['dPreA']  # fix indexing of decoders
        dPreB = np.load(file)['dPreB']
    else:
        print('readout decoders for preInptA and preInptB')
        spikesInptA = np.zeros((nTrain, int(t / 0.001), NPre))
        spikesInptB = np.zeros((nTrain, int(t / 0.001), NPre))
        targetsInptA = np.zeros((nTrain, int(t / 0.001), 1))
        targetsInptB = np.zeros((nTrain, int(t / 0.001), 1))
        for n in range(nTrain):
            stimA, stimB = makeSignal(t, fPre, dt=0.001, seed=n)
            data = go(NPre=NPre,
                      N=N,
                      t=t,
                      dt=0.001,
                      neuron_type=neuron_type,
                      fPre=fPre,
                      fS=fS,
                      stimA=stimA,
                      stimB=stimB)
            spikesInptA[n] = data['preInptA']
            spikesInptB[n] = data['preInptB']
            targetsInptA[n] = fPre.filt(Tff * data['inptA'], dt=0.001)
            targetsInptB[n] = fPre.filt(data['inptB'], dt=0.001)
        dPreA, X1a, Y1a, error1a = decodeNoF(spikesInptA,
                                             targetsInptA,
                                             nTrain,
                                             fPre,
                                             dt=0.001,
                                             reg=reg)
        dPreB, X1b, Y1b, error1b = decodeNoF(spikesInptB,
                                             targetsInptB,
                                             nTrain,
                                             fPre,
                                             dt=0.001,
                                             reg=reg)
        np.savez(file, dPreA=dPreA, dPreB=dPreB)
        times = np.arange(0, t * nTrain, 0.001)
        plotState(times, X1a, Y1a, error1a, "integrate",
                  "%s_preInptA" % neuron_type, t * nTrain)
        plotState(times, X1b, Y1b, error1b, "integrate",
                  "%s_preInptB" % neuron_type, t * nTrain)

    if 1 in load:
        ePreB = np.load(file)['ePreB']
    elif isinstance(neuron_type, Bio):
        print("encoders for preInptB-to-ens")
        ePreB = np.zeros((NPre, N, 1))
        for n in range(nEnc):
            stimA, stimB = makeSignal(t, fPre, dt=dt, seed=n)
            data = go(dPreA=dPreA,
                      dPreB=dPreB,
                      ePreB=ePreB,
                      NPre=NPre,
                      N=N,
                      t=t,
                      dt=dt,
                      neuron_type=neuron_type,
                      fPre=fPre,
                      fS=fS,
                      stimA=stimA,
                      stimB=stimB,
                      stage=1)
            ePreB = data['ePreB']
            plotActivity(t, dt, fS, data['times'], data['ens'], data['tarEns'],
                         "integrate", "preIntgToEns")
            np.savez(file, dPreA=dPreA, dPreB=dPreB, ePreB=ePreB)
    else:
        ePreB = np.zeros((NPre, N, 1))

    if 2 in load:
        ePreA = np.load(file)['ePreA']
    elif isinstance(neuron_type, Bio):
        print("encoders for preInptA-to-ens")
        ePreA = np.zeros((NPre, N, 1))
        for n in range(nEnc):
            stimA, stimB = makeSignal(t, fPre, dt=dt, seed=n)
            data = go(dPreA=dPreA,
                      dPreB=dPreB,
                      ePreA=ePreA,
                      ePreB=ePreB,
                      fPre=fPre,
                      NPre=NPre,
                      N=N,
                      t=t,
                      dt=dt,
                      neuron_type=neuron_type,
                      fS=fS,
                      stimA=stimA,
                      stimB=stimB,
                      stage=2)
            ePreA = data['ePreA']
            plotActivity(t, dt, fS, data['times'], data['ens'], data['tarEns'],
                         "integrate", "preInptToEns")
            np.savez(file, dPreA=dPreA, dPreB=dPreB, ePreA=ePreA, ePreB=ePreB)
    else:
        ePreA = np.zeros((NPre, N, 1))

    if 3 in load:
        dEns = np.load(file)['dEns']
        tauRiseEns = np.load(file)['tauRiseEns']
        tauFallEns = np.load(file)['tauFallEns']
        fEns = DoubleExp(tauRiseEns, tauFallEns)
    else:
        print('readout decoders for ens')
        spikes = np.zeros((nTrain, int(t / dt), N))
        targets = np.zeros((nTrain, int(t / dt), 1))
        for n in range(nTrain):
            stimA, stimB = makeSignal(t, fPre, dt=dt, seed=n)
            data = go(dPreA=dPreA,
                      dPreB=dPreB,
                      ePreA=ePreA,
                      ePreB=ePreB,
                      NPre=NPre,
                      N=N,
                      t=t,
                      dt=dt,
                      neuron_type=neuron_type,
                      fPre=fPre,
                      fS=fS,
                      stimA=stimA,
                      stimB=stimB,
                      stage=3)
            spikes[n] = data['ens']
            targets[n] = fPre.filt(data['inptB'], dt=dt)
        dEns, fEns, tauRiseEns, tauFallEns, X2, Y2, error2 = decode(
            spikes,
            targets,
            nTrain,
            dt=dt,
            reg=reg,
            tauRiseMax=tauRiseMax,
            tauFallMax=tauFallMax,
            name="integrate")
        np.savez(file,
                 dPreA=dPreA,
                 dPreB=dPreB,
                 ePreA=ePreA,
                 ePreB=ePreB,
                 dEns=dEns,
                 tauRiseEns=tauRiseEns,
                 tauFallEns=tauFallEns)
        times = np.arange(0, t * nTrain, dt)
        plotState(times, X2, Y2, error2, "integrate", "%s_ens" % neuron_type,
                  t * nTrain)

    if 4 in load:
        eBio = np.load(file)['eBio']
    elif isinstance(neuron_type, Bio):
        print("encoders from ens2 to ens")
        eBio = np.zeros((N, N, 1))
        for n in range(nEnc):
            stimA, stimB = makeSignal(t, fPre, dt=dt, seed=n)
            data = go(dPreA=dPreA,
                      dPreB=dPreB,
                      dEns=dEns,
                      ePreA=ePreA,
                      ePreB=ePreB,
                      eBio=eBio,
                      NPre=NPre,
                      N=N,
                      t=t,
                      dt=dt,
                      neuron_type=neuron_type,
                      fPre=fPre,
                      fEns=fEns,
                      fS=fS,
                      stimA=stimA,
                      stimB=stimB,
                      stage=4)
            eBio = data['eBio']
            plotActivity(t, dt, fS, data['times'], data['ens'], data['tarEns'],
                         "integrate", "Ens2Ens")
            np.savez(file,
                     dPreA=dPreA,
                     dPreB=dPreB,
                     ePreA=ePreA,
                     ePreB=ePreB,
                     dEns=dEns,
                     tauRiseEns=tauRiseEns,
                     tauFallEns=tauFallEns,
                     eBio=eBio)
    else:
        eBio = np.zeros((N, N, 1))

    print("testing")
    errors = np.zeros((nTest))
    for test in range(nTest):
        stimA, stimB = makeSignal(t, fPre, dt=dt, seed=200 + test)
        data = go(dPreA=dPreA,
                  dEns=dEns,
                  ePreA=ePreA,
                  eBio=eBio,
                  NPre=NPre,
                  N=N,
                  t=t,
                  dt=dt,
                  neuron_type=neuron_type,
                  fPre=fPre,
                  fEns=fEns,
                  fS=fS,
                  stimA=stimA,
                  stimB=stimB,
                  stage=5)
        A = fEns.filt(data['ens'], dt=dt)
        X = np.dot(A, dEns)
        Y = fPre.filt(data['inptB'], dt=dt)
        U = fPre.filt(Tff * data['inptA'], dt=dt)
        error = rmse(X, Y)
        errorU = rmse(X, U)
        errors[test] = error
        fig, ax = plt.subplots()
        #         ax.plot(data['times'], U, label="input")
        ax.plot(data['times'], X, label="estimate")
        ax.plot(data['times'], Y, label="target")
        ax.set(xlabel='time',
               ylabel='state',
               title='rmse=%.3f' % error,
               xlim=((0, t)),
               ylim=((-1, 1)))
        ax.legend(loc='upper left')
        sns.despine()
        fig.savefig("plots/integrate_%s_test%s.pdf" % (neuron_type, test))
        plt.close('all')
    # print('errors:', errors)
    # np.savez(file, errors=errors, dPreA=dPreA, dPreB=dPreB, ePreA=ePreA, ePreB=ePreB, dEns=dEns, tauRiseEns=tauRiseEns, tauFallEns=tauFallEns, eBio=eBio)

    return data['times'], X, Y
예제 #15
0
        ax.set(xlabel='time',
               ylabel='state',
               title='rmse=%.3f' % error,
               xlim=((0, t)),
               ylim=((-1, 1)))
        ax.legend(loc='upper left')
        sns.despine()
        fig.savefig("plots/integrate_%s_test%s.pdf" % (neuron_type, test))
        plt.close('all')
    # print('errors:', errors)
    # np.savez(file, errors=errors, dPreA=dPreA, dPreB=dPreB, ePreA=ePreA, ePreB=ePreB, dEns=dEns, tauRiseEns=tauRiseEns, tauFallEns=tauFallEns, eBio=eBio)

    return data['times'], X, Y


times, XLIF, Y = run(neuron_type=LIF(), dt=1e-4, load=[0, 1, 2, 3, 4], nTest=1)
times, XALIF, Y = run(neuron_type=ALIF(),
                      dt=1e-4,
                      load=[0, 1, 2, 3, 4],
                      nTest=1)
times, XWilson, Y = run(neuron_type=Wilson(),
                        dt=1e-4,
                        load=[0, 1, 2, 3, 4],
                        nTest=1)
times, XBio, Y = run(neuron_type=Bio("Pyramidal"),
                     dt=1e-4,
                     load=[0, 1, 2, 3, 4],
                     nTest=1)
eLIF = rmse(XLIF, Y)
eALIF = rmse(XALIF, Y)
eWilson = rmse(XWilson, Y)
예제 #16
0
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],
    )
예제 #17
0
def run(NPre=100,
        N=100,
        t=10,
        c=None,
        nTrain=10,
        nTest=5,
        dt=0.001,
        neuron_type=LIF(),
        fPre=DoubleExp(1e-3, 1e-1),
        fNMDA=DoubleExp(10.6e-3, 285e-3),
        reg=1e-2,
        load=[],
        file=None,
        neg=True):

    if not c: c = t
    if 0 in load:
        dPre = np.load(file)['dPre']
    else:
        print('readout decoders for pre')
        spikesInpt = np.zeros((nTrain, int(t / dt), NPre))
        targetsInpt = np.zeros((nTrain, int(t / dt), 1))
        for n in range(nTrain):
            stim = makeSignal(t, fPre, fNMDA, seed=n)
            data = go(NPre=NPre,
                      N=N,
                      t=t,
                      fPre=fPre,
                      fNMDA=fNMDA,
                      neuron_type=neuron_type,
                      stim=stim)
            spikesInpt[n] = data['pre']
            targetsInpt[n] = fPre.filt(data['inpt'], dt=dt)
        dPre, X, Y, error = decodeNoF(spikesInpt,
                                      targetsInpt,
                                      nTrain,
                                      fPre,
                                      reg=reg)
        np.savez("data/diffMemory_%s.npz" % neuron_type, dPre=dPre)
        times = np.arange(0, t * nTrain, 0.001)
        plotState(times, X, Y, error, "diffMemory_%s" % neuron_type, "pre",
                  t * nTrain)

    if 1 in load:
        dDiff = np.load(file)['dDiff']
    else:
        print('readout decoders for diff')
        spikes = np.zeros((nTrain, int(t / dt), N))
        targets = np.zeros((nTrain, int(t / dt), 1))
        for n in range(nTrain):
            stim = makeSignal(t, fPre, fNMDA, seed=n)
            data = go(NPre=NPre,
                      N=N,
                      t=t,
                      fPre=fPre,
                      fNMDA=fNMDA,
                      neuron_type=neuron_type,
                      stim=stim,
                      dPre=dPre)
            spikes[n] = data['diff']
            targets[n] = fNMDA.filt(fPre.filt(data['inpt'], dt=dt), dt=dt)
        dDiff, X, Y, error = decodeNoF(spikes, targets, nTrain, fNMDA, reg=reg)
        np.savez("data/diffMemory_%s.npz" % neuron_type,
                 dPre=dPre,
                 dDiff=dDiff)
        times = np.arange(0, t * nTrain, dt)
        plotState(times, X, Y, error, "diffMemory_%s" % neuron_type, "diff",
                  t * nTrain)

    if 2 in load:
        dEns = np.load(file)['dEns']
        dNeg = np.load(file)['dNeg']
    else:
        print('readout decoders for ens')
        spikes = np.zeros((nTrain, int(t / dt), N))
        targets = np.zeros((nTrain, int(t / dt), 1))
        for n in range(nTrain):
            stim = makeSignal(t, fPre, fNMDA, seed=n)
            # data = go(NPre=NPre, N=N, t=t, fPre=fPre, fNMDA=fNMDA, neuron_type=neuron_type, stim=stim, dPre=dPre, dDiff=dDiff)
            data = go(NPre=NPre,
                      N=N,
                      t=t,
                      fPre=fPre,
                      fNMDA=fNMDA,
                      neuron_type=neuron_type,
                      stim=stim,
                      dPre=dPre,
                      dDiff=dDiff,
                      train=True,
                      Tff=0.3)
            spikes[n] = data['ens']
            # targets[n] = fNMDA.filt(fNMDA.filt(fPre.filt(data['inpt'], dt=dt), dt=dt), dt=dt)
            targets[n] = fNMDA.filt(fPre.filt(data['intg'], dt=dt), dt=dt)
        dEns, X, Y, error = decodeNoF(spikes, targets, nTrain, fNMDA, reg=reg)
        dNeg = -np.array(dEns)
        np.savez("data/diffMemory_%s.npz" % neuron_type,
                 dPre=dPre,
                 dDiff=dDiff,
                 dEns=dEns,
                 dNeg=dNeg)
        times = np.arange(0, t * nTrain, dt)
        plotState(times, X, Y, error, "diffMemory_%s" % neuron_type, "ens",
                  t * nTrain)

    print('testing')
    vals = np.linspace(-1, 1, nTest)
    for test in range(nTest):
        if neg:
            stim = lambda t: vals[test] if t < c else 0
            data = go(NPre=NPre,
                      N=N,
                      t=t,
                      fPre=fPre,
                      fNMDA=fNMDA,
                      neuron_type=neuron_type,
                      stim=stim,
                      dPre=dPre,
                      dDiff=dDiff,
                      dEns=dEns,
                      dNeg=dNeg,
                      c=c)
        else:
            stim = makeSignal(t, fPre, fNMDA, seed=test)
            data = go(NPre=NPre,
                      N=N,
                      t=t,
                      fPre=fPre,
                      fNMDA=fNMDA,
                      neuron_type=neuron_type,
                      stim=stim,
                      dPre=dPre,
                      dDiff=dDiff,
                      dEns=dEns,
                      dNeg=None,
                      c=None,
                      Tff=0.3)
        aDiff = fNMDA.filt(fPre.filt(data['diff'], dt=dt), dt=dt)
        aEns = fNMDA.filt(fNMDA.filt(fPre.filt(data['ens'], dt=dt), dt=dt),
                          dt=dt)
        xhatDiff = np.dot(aDiff, dDiff)
        xhatEns = np.dot(aEns, dEns)
        u = fNMDA.filt(fPre.filt(data['inpt'], dt=dt), dt=dt)
        u2 = fNMDA.filt(fNMDA.filt(fPre.filt(data['inpt'], dt=dt), dt=dt),
                        dt=dt)
        x = fNMDA.filt(fNMDA.filt(fPre.filt(data['intg'], dt=dt), dt=dt),
                       dt=dt)
        error = rmse(xhatEns, x)
        fig, ax = plt.subplots()
        if neg:
            ax.plot(data['times'], u2, alpha=0.5, label="input (delayed)")
            ax.axvline(c, label="cutoff")
        else:
            # ax.plot(data['times'], 0.3*u2, alpha=0.5, label="input")
            ax.plot(data['times'], x, alpha=0.5, label="integral")
        # ax.plot(data['times'], xhatDiff, label="diff")
        ax.plot(data['times'], xhatEns, label="ens")
        ax.set(xlabel='time',
               ylabel='state',
               title="rmse=%.3f" % error,
               xlim=((0, t)),
               ylim=((-1, 1)))
        ax.legend(loc='upper left')
        sns.despine()
        fig.savefig("plots/diffMemory_%s_test%s.pdf" % (neuron_type, test))
예제 #18
0
def run(N=3000, neuron_type=LIF(), tTrain=200, tTest=100, tTransTrain=20, tTransTest=20,
    nTrain=1, nTest=10, dt=0.001, dtSampleTrain=0.003, dtSampleTest=0.01, seed=0,
    f2=10, reg=1e-3, reg2=1e-3, evals=100, r=30, load=False, file=None,
    tauRiseMin=3e-2, tauRiseMax=6e-2, tauFallMin=2e-1, tauFallMax=3e-1):

    print('\nNeuron Type: %s'%neuron_type)
    rng = np.random.RandomState(seed=seed)
    timeStepsTrain = int((tTrain-tTransTrain)/dtSampleTrain)
    tStart = int(tTransTrain/dtSampleTrain)
    tFlat = int((tTrain-tTransTrain)/dtSampleTrain)*nTrain
    if load:
        d = np.load(file)['d']
        d2 = np.load(file)['d2']
        tauRise = np.load(file)['tauRise']
        tauFall = np.load(file)['tauFall']
        f = DoubleExp(tauRise, tauFall)
    else:
        print('decoders for ens')
        spikes = np.zeros((nTrain, timeStepsTrain, N))
        spikes2 = np.zeros((nTrain, timeStepsTrain, N))
        targets = np.zeros((nTrain, timeStepsTrain, 3))
        targets2 = np.zeros((nTrain, timeStepsTrain, 3))
        for n in range(nTrain):
            # IC = np.array([rng.uniform(-15, 15), rng.uniform(-20, 20), rng.uniform(10, 35)])
            IC = np.array([rng.uniform(-5, 5), rng.uniform(-5, 5), rng.uniform(20, 25)])
            data = go(N=N, neuron_type=neuron_type, l=True, t=tTrain, r=r, dt=dt, dtSample=dtSampleTrain, seed=seed, IC=IC)
            spikes[n] = data['ens'][-timeStepsTrain:]
            spikes2[n] = gaussian_filter1d(data['ens'], sigma=f2, axis=0)[-timeStepsTrain:]
            targets[n] = data['tar'][-timeStepsTrain:]
            targets2[n] = gaussian_filter1d(data['tar'], sigma=f2, axis=0)[-timeStepsTrain:]
        d, f, tauRise, tauFall, X, Y, error = decode(
            spikes, targets, nTrain, dt=dtSampleTrain, dtSample=dtSampleTrain, name="lorenzNew", evals=evals, reg=reg,
            tauRiseMin=tauRiseMin, tauRiseMax=tauRiseMax, tauFallMin=tauFallMin, tauFallMax=tauFallMax)
        spikes2 = spikes2.reshape((tFlat, N))
        targets2 = targets2.reshape((tFlat, 3))
        A2 = gaussian_filter1d(spikes2, sigma=f2, axis=0)[-timeStepsTrain:]
        Y2 = gaussian_filter1d(targets2, sigma=f2, axis=0)[-timeStepsTrain:]
        d2, _ = nengo.solvers.LstsqL2(reg=reg2)(spikes2, targets2)
        np.savez("data/lorenzNew_%s.npz"%neuron_type, d=d, d2=d2, tauRise=tauRise, tauFall=tauFall, f2=f2)
        X2 = np.dot(A2, d2)[-timeStepsTrain:]
        error = plotLorenz(X, X2, Y2, neuron_type, "train")

    print("testing")
    tStart = int(tTransTest/dtSampleTest)
    errors = np.zeros((nTest))
    rng = np.random.RandomState(seed=100+seed)
    for test in range(nTest):
        # IC = np.array([rng.uniform(-15, 15), rng.uniform(-20, 20), rng.uniform(10, 35)])
        IC = np.array([rng.uniform(-5, 5), rng.uniform(-5, 5), rng.uniform(20, 25)])
        data = go(d=d, f=f, N=N, neuron_type=neuron_type, t=tTest, r=r, dt=dt, dtSample=dtSampleTest, seed=seed, IC=IC)
        A = f.filt(data['ens'], dt=dtSampleTest)
        A2 = gaussian_filter1d(data['ens'], sigma=f2, axis=0)[tStart:]
        X = np.dot(A, d)[tStart:]
        X2 = np.dot(A2, d2)[tStart:]
        Y = data['tar'][tStart:]
        Y2 = gaussian_filter1d(data['tar'], sigma=f2, axis=0)[tStart:]
        error = plotLorenz(X, X2, Y2, neuron_type, test)
        errors[test] = error
    _ = plotLorenz(Y, Y2, Y2, 'target', '')
    print('errors: ', errors)
    np.savez("data/lorenzNew_%s.npz"%neuron_type, d=d, d2=d2, tauRise=tauRise, tauFall=tauFall, f2=f2, errors=errors)
    return errors
예제 #19
0
import matplotlib.pyplot as plt
from matplotlib.ticker import NullFormatter

CURRENTS = []
RESISTANCE = 2
CAPACITANCE = 2.5
REST_VOLTAGE = 10
SPIKE_VOLTAGE = 75


print('Problem 1')
plt.figure(1)
for x in range(10, 90, 20):
    CURRENTS.append(x)
for i in range(1, len(CURRENTS)+1):
    neuron = LIF(CAPACITANCE, RESISTANCE, SPIKE_VOLTAGE, REST_VOLTAGE)
    current = CURRENTS[i-1]
    plt.subplot(810+i)
    plt.title('Current: {}'.format(current))
    for i in range(TIME_STEPS-1):
        neuron.time_step(current, i)
    neuron.add_plot(plt)
    x1, x2, y1, y2 = plt.axis()
    plt.axis((x1, x2, 0, SPIKE_VOLTAGE*SPIKE_MULTIPLIER))
    plt.gca().yaxis.set_minor_formatter(NullFormatter())
    plt.grid(True)
    plt.minorticks_on()
    plt.gca().set_yticks([REST_VOLTAGE, SPIKE_VOLTAGE])
    plt.gca().set_xticks([])
for i in range(1, len(CURRENTS)+1):
    neuron = LIF(CAPACITANCE, RESISTANCE, SPIKE_VOLTAGE, REST_VOLTAGE)
예제 #20
0
def run(NPre=100,
        N=100,
        t=10,
        nTrain=10,
        nTest=30,
        nEnc=10,
        neuron_type=LIF(),
        dt=0.001,
        f=DoubleExp(1e-3, 1e-1),
        fS=DoubleExp(1e-3, 1e-1),
        Tff=0.1,
        Tfb=1.0,
        reg=1e-3,
        tauRiseMax=5e-2,
        tauFallMax=3e-1,
        load=False,
        file=None):

    print('\nNeuron Type: %s' % neuron_type)
    if load:
        d1a = np.load(file)['d1a']
        d1b = np.load(file)['d1b']
        tauRise1a = np.load(file)['tauRise1a']
        tauRise1b = np.load(file)['tauRise1b']
        tauFall1a = np.load(file)['tauFall1a']
        tauFall1b = np.load(file)['tauFall1b']
        f1a = DoubleExp(tauRise1a, tauFall1a)
        f1b = DoubleExp(tauRise1b, tauFall1b)
    else:
        print('readout decoders for preInpt and preIntg')
        spikesInpt = np.zeros((nTrain, int(t / 0.001), NPre))
        spikesIntg = np.zeros((nTrain, int(t / 0.001), NPre))
        targetsInpt = np.zeros((nTrain, int(t / 0.001), 1))
        targetsIntg = np.zeros((nTrain, int(t / 0.001), 1))
        for n in range(nTrain):
            stim = makeSignal(t, f, dt=0.001, seed=n)
            data = go(NPre=NPre,
                      N=N,
                      t=t,
                      dt=0.001,
                      f=f,
                      fS=fS,
                      neuron_type=LIF(),
                      stim=stim)
            spikesInpt[n] = data['preInpt']
            spikesIntg[n] = data['preIntg']
            targetsInpt[n] = f.filt(Tff * data['inpt'], dt=0.001)
            targetsIntg[n] = f.filt(data['intg'], dt=0.001)
        d1a, f1a, tauRise1a, tauFall1a, X1a, Y1a, error1a = decode(
            spikesInpt,
            targetsInpt,
            nTrain,
            dt=0.001,
            reg=reg,
            name="integrateNew",
            tauRiseMax=tauRiseMax,
            tauFallMax=tauFallMax)
        d1b, f1b, tauRise1b, tauFall1b, X1b, Y1b, error1b = decode(
            spikesIntg,
            targetsIntg,
            nTrain,
            dt=0.001,
            reg=reg,
            name="integrateNew",
            tauRiseMax=tauRiseMax,
            tauFallMax=tauFallMax)
        np.savez("data/integrateNew_%s.npz" % neuron_type,
                 d1a=d1a,
                 d1b=d1b,
                 tauRise1a=tauRise1a,
                 tauRise1b=tauRise1b,
                 tauFall1a=tauFall1a,
                 tauFall1b=tauFall1b)
        times = np.arange(0, t * nTrain, 0.001)
        plotState(times, X1a, Y1a, error1a, "integrateNew",
                  "%s_preInpt" % neuron_type, t * nTrain)
        plotState(times, X1b, Y1b, error1b, "integrateNew",
                  "%s_preIntg" % neuron_type, t * nTrain)

    if load:
        e1a = np.load(file)['e1a']
        e1b = np.load(file)['e1b']
    elif isinstance(neuron_type, Bio):
        e1a = np.zeros((NPre, N, 1))
        e1b = np.zeros((NPre, N, 1))
        print("encoders for preIntg-to-ens")
        for n in range(nEnc):
            stim = makeSignal(t, f, dt=dt, seed=n)
            data = go(d1a=d1a,
                      d1b=d1b,
                      e1a=e1a,
                      e1b=e1b,
                      f1a=f1a,
                      f1b=f1b,
                      NPre=NPre,
                      N=N,
                      t=t,
                      dt=dt,
                      f=f,
                      fS=fS,
                      neuron_type=neuron_type,
                      stim=stim,
                      l1b=True)
            e1b = data['e1b']
            plotActivity(t, dt, fS, data['times'], data['ens'], data['tarEns'],
                         "integrateNew", "preIntgToEns")
            np.savez("data/integrateNew_%s.npz" % neuron_type,
                     d1a=d1a,
                     d1b=d1b,
                     tauRise1a=tauRise1a,
                     tauRise1b=tauRise1b,
                     tauFall1a=tauFall1a,
                     tauFall1b=tauFall1b,
                     e1a=e1a,
                     e1b=e1b)
        print("encoders for preInpt-to-ens")
        for n in range(nEnc):
            stim = makeSignal(t, f, dt=dt, seed=n)
            # stim2 = makeSignal(t, f, dt=dt, seed=n, value=0.5)
            #             stim2 = makeSignal(t, f, dt=dt, norm='u', freq=0.25, value=0.8, seed=100+n)
            stim2 = makeSin(t, f, dt=dt, seed=n)
            data = go(d1a=d1a,
                      d1b=d1b,
                      e1a=e1a,
                      e1b=e1b,
                      f1a=f1a,
                      f1b=f1b,
                      NPre=NPre,
                      N=N,
                      t=t,
                      dt=dt,
                      f=f,
                      fS=fS,
                      neuron_type=neuron_type,
                      stim=stim,
                      l1a=True,
                      stim2=stim2)
            e1a = data['e1a']
            plotActivity(t, dt, fS, data['times'], data['ens'], data['tarEns'],
                         "integrateNew", "preInptToEns")
            np.savez("data/integrateNew_%s.npz" % neuron_type,
                     d1a=d1a,
                     d1b=d1b,
                     tauRise1a=tauRise1a,
                     tauRise1b=tauRise1b,
                     tauFall1a=tauFall1a,
                     tauFall1b=tauFall1b,
                     e1a=e1a,
                     e1b=e1b)
    else:
        e1a = np.zeros((NPre, N, 1))
        e1b = np.zeros((NPre, N, 1))

    if load:
        d2 = np.load(file)['d2']
        tauRise2 = np.load(file)['tauRise2']
        tauFall2 = np.load(file)['tauFall2']
        f2 = DoubleExp(tauRise2, tauFall2)
    else:
        print('readout decoders for ens')
        spikes = np.zeros((nTrain, int(t / dt), N))
        targets = np.zeros((nTrain, int(t / dt), 1))
        for n in range(nTrain):
            stim = makeSignal(t, f, dt=dt, seed=n)
            data = go(d1a=d1a,
                      d1b=d1b,
                      e1a=e1a,
                      e1b=e1b,
                      f1a=f1a,
                      f1b=f1b,
                      NPre=NPre,
                      N=N,
                      t=t,
                      dt=dt,
                      f=f,
                      fS=fS,
                      neuron_type=neuron_type,
                      stim=stim,
                      l2=True)
            spikes[n] = data['ens']
            targets[n] = f.filt(Tfb * data['intg'], dt=dt)
        d2, f2, tauRise2, tauFall2, X, Y, error = decode(spikes,
                                                         targets,
                                                         nTrain,
                                                         dt=dt,
                                                         reg=reg,
                                                         tauRiseMax=tauRiseMax,
                                                         tauFallMax=tauFallMax,
                                                         name="integrateNew")
        np.savez("data/integrateNew_%s.npz" % neuron_type,
                 d1a=d1a,
                 d1b=d1b,
                 tauRise1a=tauRise1a,
                 tauRise1b=tauRise1b,
                 tauFall1a=tauFall1a,
                 tauFall1b=tauFall1b,
                 e1a=e1a,
                 e1b=e1b,
                 d2=d2,
                 tauRise2=tauRise2,
                 tauFall2=tauFall2)
        times = np.arange(0, t * nTrain, dt)
        plotState(times, X, Y, error, "integrateNew", "%s_ens" % neuron_type,
                  t * nTrain)

    if load:
        e2 = np.load(file)['e2']
    elif isinstance(neuron_type, Bio):
        print("encoders from ens2 to ens")
        e2 = np.zeros((N, N, 1))
        for n in range(nEnc):
            stim = makeSignal(t, f, dt=dt, seed=n)
            #stim = makeSin(t, f, dt=dt, seed=n)
            data = go(d1a=d1a,
                      d1b=d1b,
                      d2=d2,
                      e1a=e1a,
                      e1b=e1b,
                      e2=e2,
                      f1a=f1a,
                      f1b=f1b,
                      f2=f2,
                      NPre=NPre,
                      N=N,
                      t=t,
                      dt=dt,
                      f=f,
                      fS=fS,
                      neuron_type=neuron_type,
                      stim=stim,
                      l3=True)
            e2 = data['e2']
            plotActivity(t, dt, fS, data['times'], data['ens'],
                         data['tarEns2'], "integrateNew", "Ens2Ens")
            np.savez("data/integrateNew_%s.npz" % neuron_type,
                     d1a=d1a,
                     d1b=d1b,
                     tauRise1a=tauRise1a,
                     tauRise1b=tauRise1b,
                     tauFall1a=tauFall1a,
                     tauFall1b=tauFall1b,
                     e1a=e1a,
                     e1b=e1b,
                     d2=d2,
                     tauRise2=tauRise2,
                     tauFall2=tauFall2,
                     e2=e2)
    else:
        e2 = np.zeros((N, N, 1))

    print("testing")
    errors = np.zeros((nTest))
    for test in range(nTest):
        stim = makeSignal(t, f, dt=dt, seed=200 + test)
        data = go(d1a=d1a,
                  d2=d2,
                  e1a=e1a,
                  e2=e2,
                  f1a=f1a,
                  f2=f2,
                  NPre=NPre,
                  N=N,
                  t=t,
                  dt=dt,
                  f=f,
                  fS=fS,
                  neuron_type=neuron_type,
                  stim=stim,
                  test=True)
        A = f2.filt(data['ens'], dt=dt)
        X = np.dot(A, d2)
        Y = f.filt(data['intg'], dt=dt)
        U = f.filt(f.filt(Tff * data['inpt'], dt=dt))
        error = rmse(X, Y)
        errorU = rmse(X, U)
        errors[test] = error
        plotState(data['times'], X, Y, error, "integrateNew",
                  "%s_test%s" % (neuron_type, test), t)
        #plotState(data['times'], X, U, errorU, "integrateNew", "%s_inpt%s"%(neuron_type, test), t)
    print('%s errors:' % neuron_type, errors)
    np.savez("data/integrateNew_%s.npz" % neuron_type,
             d1a=d1a,
             d1b=d1b,
             tauRise1a=tauRise1a,
             tauRise1b=tauRise1b,
             tauFall1a=tauFall1a,
             tauFall1b=tauFall1b,
             e1a=e1a,
             e1b=e1b,
             d2=d2,
             tauRise2=tauRise2,
             tauFall2=tauFall2,
             e2=e2,
             errors=errors)
    return errors