예제 #1
0
def Run(T,
        v0,
        u0,
        bench,
        number,
        input_neurons,
        liquid_neurons,
        hidden_neurons,
        output_neurons,
        Sin,
        Sliq,
        Sa,
        Sb,
        M,
        Mv,
        Mu,
        S_in,
        S_hidden,
        S_out,
        train=False,
        letter=None):

    br.forget(Sin, Sliq)
    for i in range(len(Sa)):
        br.forget(Sa[i])
    br.forget(Sb)
    br.reinit(states=False)
    br.recall(Sin, Sliq)
    for i in range(len(Sa)):
        br.recall(Sa[i])
    br.recall(Sb)

    img, label = ReadImg(number=number, bench=bench, letter=letter)
    spikes = GetInSpikes(img, bench=bench)
    if number >= 0 and number < 4:
        input_neurons.set_spiketimes(spikes)
    else:
        input_neurons.set_spiketimes([])

    for i in range(len(liquid_neurons)):
        liquid_neurons[i].v = v0
        liquid_neurons[i].u = u0
        liquid_neurons[i].I = 0
        liquid_neurons[i].ge = 0

    for i in range(len(hidden_neurons)):
        hidden_neurons[i].v = v0
        hidden_neurons[i].u = u0
        hidden_neurons[i].I = 0
        hidden_neurons[i].ge = 0

    #pudb.set_trace()
    output_neurons.v = v0
    output_neurons.u = u0
    output_neurons.I = 0
    output_neurons.ge = 0
    br.run(T * br.msecond, report='text')

    return label
예제 #2
0
def run_sim(number_neurons=default_number_neurons,
            connection_probability=default_connection_probability,
            synaptic_weights=default_synaptic_weights,
            synaptic_time_constant=default_synaptic_time_constant,
            tend=300):
    '''run a simulation of a population of leaky integrate-and-fire excitatory neurons that are
    randomly connected. The population is injected with a transient current.'''

    from brian.units import mvolt, msecond, namp, Mohm
    import brian
    brian.clear()

    El = 0 * mvolt
    tau_m = 30 * msecond
    tau_syn = synaptic_time_constant * msecond
    R = 20 * Mohm
    v_threshold = 30 * mvolt
    v_reset = 0 * mvolt
    tau_refractory = 4 * msecond

    eqs = brian.Equations('''
    dv/dt = (-(v - El) + R*I)/tau_m : volt
    I = I_syn + I_stim : amp
    dI_syn/dt = -I_syn/tau_syn : amp
    I_stim : amp
    ''')

    external_current = np.zeros(tend)
    external_current[np.arange(0, 100)] = 5 * namp

    group = brian.NeuronGroup(
        model=eqs,
        N=number_neurons, threshold=v_threshold, reset=v_reset, refractory=tau_refractory)

    group.I_stim = brian.TimedArray(external_current, dt=1*msecond)

    connections = brian.Connection(group, group, 'I_syn')
    connections.connect_random(sparseness=connection_probability, weight=synaptic_weights*namp)

    spike_monitor = brian.SpikeMonitor(group)
    population_rate_monitor = brian.PopulationRateMonitor(group, bin=10*msecond)

    brian.reinit()
    brian.run(tend * msecond)

    return spike_monitor, population_rate_monitor
예제 #3
0
def poisson_input(active, N_input, r, time_input, t1, t2):
    "function to built a poisson input"
    "with no zero firing rates"
    from brian import PoissonGroup,SpikeMonitor,reinit,clear,run,Hz,second
    import random as pyrandom

    reinit(states = True)
    clear(erase   = True, all = True)

    N  = 1000
    P  = PoissonGroup(N)
    S = SpikeMonitor(P)

    run(t1)

    P.rate = r
    run(time_input)

    P.rate = 0*Hz
    run(t2)

    s = []
    remove = []
    for i in xrange(N):
        s.append(len(S[i]))
        if len(S[i]) == 0:
            remove.append(i)

    pos = [x for x in range(N) if x not in remove]
    pos = pyrandom.sample(pos, len(active))

    C =  []
    for ii in xrange(len(pos)):
        D = list(S.spiketimes[pos[ii]])
        D = [(active[ii],x) for x in D]
        C += D

    C = [(i,t *second) for (i,t) in C]
    C = sorted(C, key=lambda x: x[1])

    reinit(states = True)
    clear(erase   = True, all = True)

    return C
예제 #4
0
import brian
import numpy as np
import os, sys

nruns = int(sys.argv[1])

for nrun in xrange(1, nruns + 1):
    brian.seed(nrun)
    print 'RUN: ' + str(nrun)
    brian.reinit(states=True)
    brian.clear(erase=True, all=True)
    rate = int(sys.argv[2])
    foldername = 'rate' + str(rate) + '/run_' + str(nrun)
    os.system('mkdir -p -v ' + foldername)

    N = 1000
    time_input = 23000 * brian.ms
    P = brian.PoissonGroup(N)
    S = brian.SpikeMonitor(P)

    P.rate = rate * brian.Hz
    brian.run(time_input, report='text', report_period=10 * brian.second)

    fname = 'noise_'
    for s in xrange(len(S.spiketimes)):
        spiketimes = [round(1000 * x, 1) + 50 for x in list(S.spiketimes[s])]
        np.savetxt(foldername + '/' + fname + str(s) + '.txt',
                   spiketimes,
                   fmt='%10.1f',
                   newline='\n')
예제 #5
0
#------------------------------------------------------------------------------ 
# run the simulation and set inputs
#------------------------------------------------------------------------------ 
previous_spike_count = np.zeros(n_e)
assignments = np.zeros(n_e)
input_numbers = [0] * num_examples
outputNumbers = np.zeros((num_examples, 10))
if not test_mode:
    input_weight_monitor, fig_weights = plot_2d_input_weights()
    fig_num += 1
if do_plot_performance:
    performance_monitor, performance, fig_num, fig_performance = plot_performance(fig_num)
for i,name in enumerate(input_population_names):
    input_groups[name+'e'].rate = 0
b.run(0)
j = 0
while j < (int(num_examples)):
    if test_mode:
        if use_testing_set:
            rates = testing['x'][j%10000,:,:].reshape((n_input)) / 8. *  input_intensity
        else:
            rates = training['x'][j%60000,:,:].reshape((n_input)) / 8. *  input_intensity
    else:
        normalize_weights()
        rates = training['x'][j%60000,:,:].reshape((n_input)) / 8. *  input_intensity
    input_groups['Xe'].rate = rates
#     print 'run number:', j+1, 'of', int(num_examples)
    b.run(single_example_time, report='text')
            
    if j % update_interval == 0 and j > 0:
예제 #6
0
def hodgkin_huxley(duration=100, num=10, percent_excited=0.7, sample=1):
    """
    The hodgkin_huxley function takes the following parameters:

      duration - is the timeperiod to model for measured in milliseconds.
      num - an integer value represeting the number of neurons you want to model.
      percent_excited - a float between 0 and 1 representing the percentage of excited neurons.
      sample - gives the number of random neurons you would like plotted (default is 1)
    """
    # Assert that we are getting valid input
    assert(percent_excited >= 0 and percent_excited <= 1.0)
    assert(duration > 0)
    assert(num > 0)
    assert(sample > 0)
    assert(num >= sample)

    # Constants used in the modeling equation
    area = 20000*umetre**2
    Cm = (1*ufarad*cm**-2)*area
    gl = (5e-5*siemens*cm**-2)*area
    El = -60*mV
    EK = -90*mV
    ENa = 50*mV
    g_na = (100*msiemens*cm**-2)*area
    g_kd = (30*msiemens*cm**-2)*area
    VT = -63*mV

    # Time constants
    taue = 5*ms       # excitatory
    taui = 10*ms      # inhibitory
    
    # Reversal potentials
    Ee = 0*mV         # excitatory
    Ei = -80*mV       # inhibitory
    
    # Synaptic weights
    we = 6*nS         # excitatory
    wi = 67*nS        # inhibitory

    # The model equations
    eqs = Equations(
        '''
        dv/dt = (gl*(El-v)+ge*(Ee-v)+gi*(Ei-v)-g_na*(m*m*m)*h*(v-ENa)-g_kd*(n*n*n*n)*(v-EK))/Cm : volt
        dm/dt = alpham*(1-m)-betam*m : 1
        dn/dt = alphan*(1-n)-betan*n : 1
        dh/dt = alphah*(1-h)-betah*h : 1
        dge/dt = -ge*(1./taue) : siemens
        dgi/dt = -gi*(1./taui) : siemens
        alpham = 0.32*(mV**-1)*(13*mV-v+VT)/(exp((13*mV-v+VT)/(4*mV))-1.)/ms : Hz
        betam = 0.28*(mV**-1)*(v-VT-40*mV)/(exp((v-VT-40*mV)/(5*mV))-1)/ms : Hz
        alphah = 0.128*exp((17*mV-v+VT)/(18*mV))/ms : Hz
        betah = 4./(1+exp((40*mV-v+VT)/(5*mV)))/ms : Hz
        alphan = 0.032*(mV**-1)*(15*mV-v+VT)/(exp((15*mV-v+VT)/(5*mV))-1.)/ms : Hz
        betan = .5*exp((10*mV-v+VT)/(40*mV))/ms : Hz
        '''
    )

    # Build the neuron group
    neurons = NeuronGroup(
        num,
        model=eqs,
        threshold=EmpiricalThreshold(threshold=-20*mV,refractory=3*ms),
        implicit=True,
        freeze=True
    )

    num_excited = int(num * percent_excited)
    num_inhibited = num - num_excited
    excited = neurons.subgroup(num_excited)
    inhibited = neurons.subgroup(num_inhibited)
    excited_conn = Connection(excited, neurons, 'ge', weight=we, sparseness=0.02)
    inhibited_conn = Connection(inhibited, neurons, 'gi', weight=wi, sparseness=0.02)
    
    # Initialization
    neurons.v = El+(randn(num)*5-5)*mV
    neurons.ge = (randn(num)*1.5+4)*10.*nS
    neurons.gi = (randn(num)*12+20)*10.*nS
    
    # Record a few trace and run
    recorded = choice(num, sample)
    trace = StateMonitor(neurons, 'v', record=recorded)
    run(duration * msecond)

    for i in recorded:
        plot(trace.times/ms, trace[i]/mV)
        
    show()
def fft_nostd(qee, run_num, new_connectivity, osc, rep):

    #bn.seed(int(time.time()))
    #  bn.seed(1412958308+2)
    bn.reinit_default_clock()
    bn.defaultclock.dt = 0.5 * bn.ms

    #==============================================================================
    # Define constants for the model.
    #==============================================================================
    fft_file = './nostd_fft_p20_'
    rate_file = './nostd_rate_p20_'

    if osc:
        T = 8.0 * bn.second
    else:
        T = 3.5 * bn.second
    n_tsteps = T / bn.defaultclock.dt
    fft_start = 3.0 * bn.second / bn.defaultclock.dt  # Time window for the FFT computation
    #run_num = 10
    ro = 1.2 * bn.Hz
    #==============================================================================
    #   Need to do all others besides 0.2 and 0.5
    #==============================================================================
    print qee
    print run_num
    print new_connectivity
    print rep
    qie = 0.3  # Fraction of NMDA receptors for e to i connections

    k = 0.65
    Jeo_const = 1.0  #*bn.mV # Base strength of o (external) to e connections

    Ne = 3200  # number of excitatory neurons
    Ni = 800  # number of inhibitory neurons
    No = 2000  # number of external neurons
    N = Ne + Ni

    pcon = 0.2  # probability of connection

    Jee = 5.0 / (Ne * pcon)
    Jie = 5.0 / (Ne * pcon)
    Jii = k * 5.0 / (Ni * pcon)
    Jei = k * 5.0 / (Ni * pcon)
    Jeo = 1.0

    El = -60.0 * bn.mV  # leak reversal potential
    Vreset = -52.0 * bn.mV  # reversal potential
    Vthresh = -40.0 * bn.mV  # spiking threshold

    tref = 2.0 * bn.ms  # refractory period
    te = 20.0 * bn.ms  # membrane time constant of excitatory neurons
    ti = 10.0 * bn.ms  # membrane time constant of inhibitory neruons
    tee_ampa = 10.0 * bn.ms  # time const of ampa currents at excitatory neurons
    tee_nmda = 100.0 * bn.ms  # time const of nmda currents at excitatory neurons
    tie_ampa = 10.0 * bn.ms  # time const of ampa currents at inhibitory neurons
    tie_nmda = 100.0 * bn.ms  # time const of nmda currents at inhibitory neurons
    tii_gaba = 10.0 * bn.ms  # time const of GABA currents at inhibitory neurons
    tei_gaba = 10.0 * bn.ms  # time const of GABA currents at excitatory neurons
    teo_input = 100.0 * bn.ms

    #==============================================================================
    # Define model structure
    #==============================================================================

    model = '''
  dV/dt = (-(V-El)+J_ampa*I_ampa+J_nmda*I_nmda-J_gaba*I_gaba+J_input*I_input+eta+eta_corr)/tm : bn.volt
  dI_ampa/dt = -I_ampa/t_ampa : bn.volt
  dI_nmda/dt = -I_nmda/t_nmda : bn.volt
  dI_gaba/dt = -I_gaba/t_gaba : bn.volt
  dI_input/dt = (-I_input+mu)/t_input : bn.volt
  J_ampa : 1
  J_nmda : 1
  J_gaba : 1
  J_input : 1
  mu : bn.volt
  eta : bn.volt
  eta_corr : bn.volt
  tm : bn.second
  t_ampa : bn.second
  t_nmda : bn.second
  t_gaba : bn.second
  t_input : bn.second
  '''

    P_reset = "V=-52*bn.mV"

    Se_model = '''
  we_ampa : bn.volt
  we_nmda : bn.volt
  '''

    Se_pre = ('I_ampa += we_ampa', 'I_nmda += we_nmda')

    Si_model = '''
  wi_gaba : bn.volt
  '''

    Si_pre = 'I_gaba += wi_gaba'

    So_model = '''
  wo_input : bn.volt
  '''

    So_pre = 'I_input += wo_input'

    #==============================================================================
    # Define populations
    #==============================================================================

    P = bn.NeuronGroup(N,
                       model,
                       threshold=Vthresh,
                       reset=P_reset,
                       refractory=tref)

    Pe = P[0:Ne]
    Pe.tm = te
    Pe.t_ampa = tee_ampa
    Pe.t_nmda = tee_nmda
    Pe.t_gaba = tei_gaba
    Pe.t_input = teo_input
    Pe.I_ampa = 0 * bn.mV
    Pe.I_nmda = 0 * bn.mV
    Pe.I_gaba = 0 * bn.mV
    Pe.I_input = 0 * bn.mV
    Pe.V = (np.random.rand(Pe.V.size) * 12 - 52) * bn.mV

    Pi = P[Ne:(Ne + Ni)]
    Pi.tm = ti
    Pi.t_ampa = tie_ampa
    Pi.t_nmda = tie_nmda
    Pi.t_gaba = tii_gaba
    Pi.t_input = teo_input
    Pi.I_ampa = 0 * bn.mV
    Pi.I_nmda = 0 * bn.mV
    Pi.I_gaba = 0 * bn.mV
    Pi.I_input = 0 * bn.mV
    Pi.V = (np.random.rand(Pi.V.size) * 12 - 52) * bn.mV

    Pe.J_ampa = Jee * (1 - qee)  #*SEE1
    Pe.J_nmda = Jee * qee  #*SEE1

    Pi.J_ampa = Jie * (1 - qie)  #*SEE1
    Pi.J_nmda = Jie * qie  #*SEE1

    Pe.J_gaba = Jei
    Pi.J_gaba = Jii

    Pe.J_input = Jeo
    Pi.J_input = Jeo

    #==============================================================================
    # Define inputs
    #==============================================================================
    if osc:
        Pe.mu = 2.0 * bn.mV
        holder = np.zeros((n_tsteps, ))
        t_freq = np.linspace(0, 10, n_tsteps)

        fo = 0.2  # Smallest frequency in the signal
        fe = 10.0  # Largest frequency in the signal
        F = int(fe / 0.2)
        for m in range(1, F + 1):
            holder = holder + np.cos(2 * np.pi * m * fo * t_freq - m *
                                     (m - 1) * np.pi / F)
        holder = holder / np.max(holder)
        Pe.eta = bn.TimedArray(0.0 * bn.mV * holder)  #, dt=0.5*bn.ms)
        Pe.eta_corr = 0 * bn.mV

        Background_eo = bn.PoissonInput(Pe,
                                        N=1000,
                                        rate=1.0 * bn.Hz,
                                        weight=0.2 * bn.mV,
                                        state='I_input')
        Background_io = bn.PoissonInput(Pi,
                                        N=1000,
                                        rate=1.05 * bn.Hz,
                                        weight=0.2 * bn.mV,
                                        state='I_input')

        Pi.mu = 0 * bn.mV
        Pi.eta = 0 * bn.mV  #, dt=0.5*bn.ms)
        Pi.eta_corr = 0 * bn.mV

        Po = bn.PoissonGroup(No, rates=0 * bn.Hz)

    else:
        Background_eo = bn.PoissonInput(Pe,
                                        N=1000,
                                        rate=1.0 * bn.Hz,
                                        weight=0.2 * bn.mV,
                                        state='I_input')
        Background_io = bn.PoissonInput(Pi,
                                        N=1000,
                                        rate=1.05 * bn.Hz,
                                        weight=0.2 * bn.mV,
                                        state='I_input')

        holder_pe = np.zeros((n_tsteps, ))
        time_steps = np.linspace(0, T / bn.second, n_tsteps)
        holder_pe[time_steps < 0.5] = 0.0 * bn.mV
        holder_pe[time_steps >= 0.5] = 3.0 * bn.mV  # 35.0/Jeo *bn.mV #25
        Pe.mu = bn.TimedArray(holder_pe)

        def firing_function(t, ro):
            if t > 0.5 * bn.second and t < 3.5 * bn.second:
                return 0.0 * bn.Hz
            else:
                return 0.0 * bn.Hz

#    Pe.mu = 0*bn.mV

        Pe.eta = 0 * bn.mV  #, dt=0.5*bn.ms)
        Pi.mu = 0.0 * bn.mV
        Pi.eta = 0 * bn.mV  #, dt=0.5*bn.ms)

        Po = bn.PoissonGroup(No, rates=lambda t: firing_function(t, ro))

#==============================================================================
# Define synapses
#==============================================================================

    See = bn.Synapses(Pe, Pe, model=Se_model, pre=Se_pre)
    Sie = bn.Synapses(Pe, Pi, model=Se_model, pre=Se_pre)

    Sei = bn.Synapses(Pi, Pe, model=Si_model, pre=Si_pre)
    Sii = bn.Synapses(Pi, Pi, model=Si_model, pre=Si_pre)

    Seo = bn.Synapses(Po, Pe, model=So_model, pre=So_pre)

    #==============================================================================
    #  Define monitors
    #==============================================================================

    Pe_mon_V = bn.StateMonitor(Pe, 'V', timestep=1, record=True)
    Pe_mon_eta = bn.StateMonitor(Pe, 'eta', timestep=1, record=True)
    Pe_mon_ampa = bn.StateMonitor(Pe, 'I_ampa', timestep=1, record=True)
    Pe_mon_nmda = bn.StateMonitor(Pe, 'I_nmda', timestep=1, record=True)
    Pe_mon_gaba = bn.StateMonitor(Pe, 'I_gaba', timestep=1, record=True)
    Pe_ratemon = bn.PopulationRateMonitor(Pe, bin=10.0 * bn.ms)

    #==============================================================================
    # Define random connections
    #==============================================================================

    if new_connectivity:
        See.connect_random(Pe, Pe, sparseness=pcon)
        Sie.connect_random(Pe, Pi, sparseness=pcon)
        Sii.connect_random(Pi, Pi, sparseness=pcon)
        Sei.connect_random(Pi, Pe, sparseness=pcon)
        Seo.connect_random(Po, Pe, sparseness=pcon)

        print 'Saving'
        See.save_connectivity('./See_connections_nostd_saver_p20' +
                              str(run_num))
        Sie.save_connectivity('./Sie_connections_nostd_saver_p20' +
                              str(run_num))
        Sii.save_connectivity('./Sii_connections_nostd_saver_p20' +
                              str(run_num))
        Sei.save_connectivity('./Sei_connections_nostd_saver_p20' +
                              str(run_num))
        Seo.save_connectivity('./Seo_connections_nostd_saver_p20' +
                              str(run_num))

    else:
        print 'Loading'
        See.load_connectivity('./See_connections_nostd_saver_p20' +
                              str(run_num))
        Sie.load_connectivity('./Sie_connections_nostd_saver_p20' +
                              str(run_num))
        Sii.load_connectivity('./Sii_connections_nostd_saver_p20' +
                              str(run_num))
        Sei.load_connectivity('./Sei_connections_nostd_saver_p20' +
                              str(run_num))
        Seo.load_connectivity('./Seo_connections_nostd_saver_p20' +
                              str(run_num))

    See.we_ampa = 1.0 * bn.mV / tee_ampa
    See.we_nmda = 1.0 * bn.mV / tee_nmda

    Sie.we_ampa = 1.0 * bn.mV / tie_ampa
    Sie.we_nmda = 1.0 * bn.mV / tie_nmda

    Sei.wi_gaba = 1.0 * bn.mV / tei_gaba
    Sii.wi_gaba = 1.0 * bn.mV / tii_gaba

    Seo.wo_input = 1.0 * bn.mV / teo_input

    #==============================================================================
    # Run model
    #==============================================================================

    timer = 0 * bn.second
    t_start = time.time()
    bn.run(T, report='graphical')
    timer = timer + T
    print '-------------------------------------------------------'
    print 'Time is ' + str(timer) + ' seconds'
    t_end = time.time()
    print 'Time to compute last ' +str(T)+' seconds is: ' + \
          str(t_end - t_start) + ' seconds'
    print '-------------------------------------------------------\n'

    #==============================================================================
    # Save into a Matlab file
    #==============================================================================

    if osc:
        Pe_output = Pe.J_ampa[0] * Pe_mon_ampa.values + Pe.J_nmda[
            0] * Pe_mon_nmda.values - Pe.J_gaba[0] * Pe_mon_gaba.values
        Pe_output = Pe_output[:, fft_start:, ]
        Pe_glut = Pe.J_ampa[0] * Pe_mon_ampa.values + Pe.J_nmda[
            0] * Pe_mon_nmda.values
        Pe_glut = Pe_glut[:, fft_start:, ]
        Pe_gaba = Pe.J_gaba[0] * Pe_mon_gaba.values[:, fft_start:, ]

        Pe_V = Pe_mon_V.values[:, fft_start:, ]
        Pe_input = Pe_mon_eta[:, fft_start:, ]
        T_step = bn.defaultclock.dt

        holder = {
            'Pe_output': Pe_output,
            'Pe_input': Pe_input,
            'Pe_V': Pe_V,
            'Pe_glut': Pe_glut,
            'Pe_gaba': Pe_gaba,
            'T_step': T_step
        }
        scipy.io.savemat(fft_file + 'qee' + str(qee) + '_' + str(rep),
                         mdict=holder)

    else:
        holder = {'Pe_rate': Pe_ratemon.rate, 'Pe_time': Pe_ratemon.times}
        scipy.io.savemat(rate_file + 'qee_' + str(qee) + '_' + str(run_num) +
                         'rep' + str(rep),
                         mdict=holder)
def runsim(neuron_model, 
           # sim params
           dt, simtime, prerun, monitors, recvars,
           # stimulation params
           fstim, r0_bg, r0_stim, stim_starts, stim_stops, stim_odors, stim_amps, stim_start_var,
           # network params
           beeid, N_glu, N_KC, ORNperGlu, PNperKC, PN_I0, LN_I0,
           # network weights
           wi, wORNLN, wORNPN, wPNKC,
           # default params
           V0min, inh_struct=None, Winh=None, timestep=500, report=None):

    np.random.seed() #needed for numpy/brian when runing parallel sims
    define_default_clock(dt=dt)    
    
    inh_on_off = 0 if (wi == 0) or (wi is None) or (wORNLN is None) else 1    
    
    
    
    #########################     NEURONGROUPS     #########################
    NG = dict()

    # ORN Input
    
    # For each glumerolus, random temporal response jitter can be added.
    # The jitter is added to the response onset. Maximum jitter is given by stim_start_var.
    # stim_start_jittered is a vector containing the jittered stim start tims
    
    # orn_activation returns a booolean vector of stim presence given time t
    
    # Total ORN rate: Baseline componenent equal for all units,
    # and individual activationa.
    
    jitter = np.random.uniform(0,stim_start_var,N_glu)
    
    stim_tun       = lambda odorN: fstim(N_glu=N_glu, odorN=odorN) * r0_stim
    orn_activation = lambda t: np.sum([
                     a*stim_tun(odorN=o)*np.logical_and(np.greater(t,prerun+stim_start+jitter), np.less(t,prerun+stim_stop))
                     for stim_start,stim_stop,o,a in zip(stim_starts, stim_stops, stim_odors, stim_amps)], 0)                         
    orn_rates      = lambda t: np.repeat(r0_bg + orn_activation(t),repeats = ORNperGlu)
    
    NG['ORN'] = PoissonGroup(ORNperGlu*N_glu, rates=orn_rates)
    NG['PN'] = NeuronGroup(N_glu, **neuron_model)
    NG['LN'] = NeuronGroup(N_glu*inh_on_off, **neuron_model)
    if 'KC' in monitors: NG['KC'] = NeuronGroup(N_KC, **neuron_model)

    #########################     CONNECTIONS       #########################
    c = dict()
    
    c['ORNPN'] = Connection(NG['ORN'],NG['PN'],'ge')
    
    for i in np.arange(N_glu): c['ORNPN'].connect_full(NG['ORN'].subgroup(ORNperGlu),NG['PN'][i],weight=wORNPN)

    if inh_on_off:
        print('-- inhibiting --',wi)
        
        c['ORNLN'] = Connection(NG['ORN'],NG['LN'],'ge')
        c['LNPN'] = Connection(NG['LN'],NG['PN'],'gi',weight=(wi*35)/N_glu)
        
        for i in np.arange(N_glu):
            c['ORNLN'].connect_full(NG['ORN'][ i*ORNperGlu : (i+1)*ORNperGlu ],
                                NG['LN'][i],
                                weight = wORNLN)
        if inh_struct: c['LNPN'].connect(NG['LN'],NG['PN'],Winh)
    
    if 'KC' in monitors:
        c['KC'] = Connection(NG['PN'],NG['KC'],'ge')
        c['KC'].connect_random(NG['PN'],NG['KC'],p=PNperKC/float(N_glu),weight=wPNKC,seed=beeid)
    
    #########################     INITIAL VALUES     #########################
    VT = neuron_model['threshold']
    
    NG['PN'].vm    = np.random.uniform(V0min,VT,size=len(NG['PN']))
    if inh_on_off:
        NG['LN'].vm= np.random.uniform(V0min,VT,size=len(NG['LN']))
    if 'KC' in monitors:
        NG['KC'].vm= np.random.uniform(V0min,VT,size=len(NG['KC']))
    
    net = Network(NG.values(), c.values())
    
    #### Compensation currents ###
    NG['PN'].I0 = PN_I0
    NG['LN'].I0 = LN_I0
    ##########################################################################

    #########################         PRE-RUN        #########################    
    net.run(prerun)
    #########################     MONITORS     #########################
    spmons = [SpikeMonitor(NG[mon], record=True) for mon in monitors]
    net.add(spmons)
    
    if len(recvars) > 0:
        mons = [MultiStateMonitor(NG[mon], vars=recvars, record=True, timestep=timestep) for mon in monitors]
        net.add(mons)
    else:
        mons = None
    #########################           RUN          #########################
    net = run(simtime, report=report)
    

    out_spikes = dict( (monitors[i],np.array(sm.spikes)) for i,sm in enumerate(spmons) )
    
    if mons is not None:
        out_mons = dict( (mon,dict((var,statemon.values) for var,statemon in m.iteritems())) for mon,m in zip(monitors,mons))
    else:
        out_mons = None

    #subtract the prerun from spike times, if there are any
    for spikes in out_spikes.itervalues():
        if len(spikes) != 0:
            spikes[:,1] -= prerun
    
    return out_spikes, out_mons
	def run_simulation():

		# weight updates and progress printing intervals
		weight_update_interval = 10
		print_progress_interval = 10

		# plot input weights
		if not test_mode and do_plot:
			input_weight_monitor, fig_weights = plot_2d_input_weights()
			fig_num += 1
			patch_weight_monitor, fig2_weights = plot_patch_weights()
			fig_num += 1
			# neuron_vote_monitor, fig_neuron_votes = plot_neuron_votes(assignments, rates)
			# fig_num += 1

		# plot input intensities
		if do_plot:
			input_image_monitor, input_image = plot_input(rates)
			fig_num += 1

		# plot performance
		if do_plot_performance and do_plot:
			performance_monitor, all_performance, most_spiked_performance, top_percent_performance, fig_num, fig_performance = plot_performance(fig_num)
		else:
			all_performance, most_spiked_performance, top_percent_performance = get_current_performance(np.zeros(int(num_examples / update_interval)), np.zeros(int(num_examples / update_interval)), np.zeros(int(num_examples / update_interval)), 0)

		# set firing rates to zero initially
		for name in input_population_names:
			input_groups[name + 'e'].rate = 0

		# initialize network
		j = 0
		num_retries = 0
		b.run(0)

		# start recording time
		start_time = timeit.default_timer()

		while j < num_examples:
			# getting firing rates based on training or test phase
			if test_mode:
				rates = (this.data['x'][j % 10000, :, :] / 8.0) * input_intensity
			else:
				# ensure weights don't grow without bound
				normalize_weights()
				# get the firing rates of the next input example
				rates = (this.data['x'][j % 60000, :, :] / 8.0) * input_intensity
			
			# plot the input at this step
			if do_plot:
				input_image_monitor = update_input(rates, input_image_monitor, input_image)
			
			# sets the input firing rates
			input_groups['Xe'].rate = rates.reshape(n_input)
			
			# run the network for a single example time
			b.run(single_example_time)
			
			# get new neuron label assignments every 'update_interval'
			if j % update_interval == 0 and j > 0:
				assignments = get_new_assignments(result_monitor[:], input_numbers[j - update_interval : j])
			
			# get count of spikes over the past iteration
			current_spike_count = np.copy(spike_counters['Ae'].count[:]).reshape((conv_features, n_e)) - previous_spike_count
			previous_spike_count = np.copy(spike_counters['Ae'].count[:]).reshape((conv_features, n_e))

			# set weights to those of the most-fired neuron
			if not test_mode and weight_sharing == 'weight_sharing':
				set_weights_most_fired()

			# update weights every 'weight_update_interval'
			if j % weight_update_interval == 0 and not test_mode and do_plot:
				update_2d_input_weights(input_weight_monitor, fig_weights)
				update_patch_weights(patch_weight_monitor, fig2_weights)
				# update_neuron_votes(neuron_vote_monitor, fig_neuron_votes)

			# if the neurons in the network didn't spike more than four times
			if np.sum(current_spike_count) < 5 and num_retries < 3:
				# increase the intensity of input
				input_intensity += 2
				num_retries += 1
				
				# set all network firing rates to zero
				for name in input_population_names:
					input_groups[name + 'e'].rate = 0

				# let the network relax back to equilibrium
				b.run(resting_time)
			# otherwise, record results and continue simulation
			else:
				num_retries = 0
				# record the current number of spikes
				result_monitor[j % update_interval, :] = current_spike_count
				
				# decide whether to evaluate on test or training set
				if test_mode:
					input_numbers[j] = this.data['y'][j % 10000][0]
				else:
					input_numbers[j] = this.data['y'][j % 60000][0]
				
				# get the output classifications of the network
				all_output_numbers[j, :], most_spiked_output_numbers[j, :], top_percent_output_numbers[j, :] = get_recognized_number_ranking(assignments, result_monitor[j % update_interval, :])
				
				# print progress
				if j % print_progress_interval == 0 and j > 0:
					print 'runs done:', j, 'of', int(num_examples), '(time taken for past', print_progress_interval, 'runs:', str(timeit.default_timer() - start_time) + ')'
					start_time = timeit.default_timer()
				
				# plot performance if appropriate
				if j % update_interval == 0 and j > 0:
					if do_plot_performance and do_plot:
						# updating the performance plot
						perf_plot, all_performance, most_spiked_performance, top_percent_performance = update_performance_plot(performance_monitor, all_performance, most_spiked_performance, top_percent_performance, j, fig_performance)
					else:
						all_performance, most_spiked_performance, top_percent_performance = get_current_performance(all_performance, most_spiked_performance, top_percent_performance, j)

					# printing out classification performance results so far
					print '\nClassification performance (all vote): ', all_performance[:int(j / float(update_interval)) + 1], '\n', 'Average performance:', sum(all_performance[:int(j / float(update_interval)) + 1]) / float(len(all_performance[:int(j / float(update_interval)) + 1])), '\n'
					print '\nClassification performance (most-spiked vote): ', most_spiked_performance[:int(j / float(update_interval)) + 1], '\n', 'Average performance:', sum(most_spiked_performance[:int(j / float(update_interval)) + 1]) / float(len(most_spiked_performance[:int(j / float(update_interval)) + 1])), '\n'
					print '\nClassification performance (top', str(top_percent), 'percentile vote): ', top_percent_performance[:int(j / float(update_interval)) + 1], '\n', 'Average performance:', sum(top_percent_performance[:int(j / float(update_interval)) + 1]) / float(len(top_percent_performance[:int(j / float(update_interval)) + 1])), '\n'			

					target = open('../performance/conv_patch_connectivity_performance/' + ending + '.txt', 'w')
					target.truncate()
					target.write('Iteration ' + str(j) + '\n')
					target.write(str(all_performance[:int(j / float(update_interval)) + 1]))
					target.write('\n')
					target.write(str(sum(all_performance[:int(j / float(update_interval)) + 1]) / float(len(all_performance[:int(j / float(update_interval)) + 1]))))
					target.write('\n')
					target.write(str(most_spiked_performance[:int(j / float(update_interval)) + 1]))
					target.write('\n')
					target.write(str(sum(most_spiked_performance[:int(j / float(update_interval)) + 1]) / float(len(most_spiked_performance[:int(j / float(update_interval)) + 1]))))
					target.write('\n')
					target.write(str(top_percent_performance[:int(j / float(update_interval)) + 1]))
					target.write('\n')
					target.write(str(sum(top_percent_performance[:int(j / float(update_interval)) + 1]) / float(len(top_percent_performance[:int(j / float(update_interval)) + 1]))))
					target.close()
						
				# set input firing rates back to zero
				for name in input_population_names:
					input_groups[name + 'e'].rate = 0
				
				# run the network for 'resting_time' to relax back to rest potentials
				b.run(resting_time)
				# reset the input firing intensity
				input_intensity = start_input_intensity
				# increment the example counter
				j += 1

		# set weights to those of the most-fired neuron
		if not test_mode and weight_sharing == 'weight_sharing':
			set_weights_most_fired()
예제 #10
0
#------------------------------------------------------------------------------
# run the simulation and set inputs
#------------------------------------------------------------------------------
previous_spike_count = np.zeros(n_e)
assignments = np.zeros(n_e)
input_numbers = [0] * num_examples
outputNumbers = np.zeros((num_examples, 10))
if not test_mode:
    input_weight_monitor, fig_weights = plot_2d_input_weights()
    fig_num += 1
if do_plot_performance:
    performance_monitor, performance, fig_num, fig_performance = plot_performance(fig_num)
for i,name in enumerate(input_population_names):
    input_groups[name+'e'].rate = 0
b.run(0)
j = 0
while j < (int(num_examples)):
    if test_mode:
        if use_testing_set:
            rates = testing['x'][j%10000,:,:].reshape((n_input)) / 8. *  input_intensity
        else:
            rates = training['x'][j%60000,:,:].reshape((n_input)) / 8. *  input_intensity
    else:
        normalize_weights()
        rates = training['x'][j%60000,:,:].reshape((n_input)) / 8. *  input_intensity
    input_groups['Xe'].rate = rates
#     print 'run number:', j+1, 'of', int(num_examples)
    b.run(single_example_time, report=None)

    if j % update_interval == 0 and j > 0:
예제 #11
0
def fft_std(delta_u, run_num, new_connectivity, osc, rep):
    #bn.seed(int(time.time()))
    bn.reinit_default_clock()
    #bn.seed(1412958308+2)
    bn.defaultclock.dt = 0.5 * bn.ms

    #==============================================================================
    # Define constants for the model.
    #==============================================================================
    fft_file = './std_fft_p20_'
    rate_file = './std_rate_p20_'
    print delta_u
    print run_num
    print new_connectivity
    print rep

    if osc:
        T = 5.5 * bn.second
    else:
        T = 2.5 * bn.second
    n_tsteps = T / bn.defaultclock.dt
    fft_start = 0.5 * bn.second / bn.defaultclock.dt  # Time window for the FFT computation
    ro = 1.2 * bn.Hz

    SEE1 = 1.0
    SEE2 = 1.0
    qee1 = 1.00  # Fraction of NMDA receptors for e to e connections
    qee2 = 0.00
    qie1 = 1.00  # Fraction of NMDA receptors for e to i connections
    qie2 = 0.00

    uee1 = 0.2 - delta_u
    uee2 = 0.2 + delta_u
    uie1 = 0.2
    uie2 = 0.2
    trec1 = 1000.0 * bn.ms
    trec2 = 1000.0 * bn.ms

    k = 0.65
    #Jeo_const = 1.0#*bn.mV # Base strength of o (external) to e connections

    Ne = 3200  # number of excitatory neurons
    Ni = 800  # number of inhibitory neurons
    No = 20000  # number of external neurons
    N = Ne + Ni

    pcon = 0.2  # probability of connection

    Jee = 10.0 / (Ne * pcon)
    Jie = 10.0 / (Ne * pcon)
    Jii = k * 10.0 / (Ni * pcon)
    Jei = k * 10.0 / (Ni * pcon)
    Jeo = 1.0

    El = -60.0 * bn.mV  # leak reversal potential
    Vreset = -52.0 * bn.mV  # reversal potential
    Vthresh = -40.0 * bn.mV  # spiking threshold

    tref = 2.0 * bn.ms  # refractory period
    te = 20.0 * bn.ms  # membrane time constant of excitatory neurons
    ti = 10.0 * bn.ms  # membrane time constant of inhibitory neruons
    tee_ampa = 10.0 * bn.ms  # time const of ampa currents at excitatory neurons
    tee_nmda = 100.0 * bn.ms  # time const of nmda currents at excitatory neurons
    tie_ampa = 10.0 * bn.ms  # time const of ampa currents at inhibitory neurons
    tie_nmda = 100.0 * bn.ms  # time const of nmda currents at inhibitory neurons
    tii_gaba = 10.0 * bn.ms  # time const of GABA currents at inhibitory neurons
    tei_gaba = 10.0 * bn.ms  # time const of GABA currents at excitatory neurons
    teo_input = 100.0 * bn.ms

    #==============================================================================
    # Define model structure
    #==============================================================================

    model = '''
  dV/dt = (-(V-El)+J_ampa1*I_ampa1+J_nmda1*I_nmda1+J_ampa2*I_ampa2+J_nmda2*I_nmda2-J_gaba*I_gaba+J_input*I_input+eta)/tm : bn.volt
  dI_ampa1/dt = -I_ampa1/t_ampa : bn.volt
  dI_nmda1/dt = -I_nmda1/t_nmda : bn.volt
  dI_ampa2/dt = -I_ampa2/t_ampa : bn.volt
  dI_nmda2/dt = -I_nmda2/t_nmda : bn.volt
  dI_gaba/dt = -I_gaba/t_gaba : bn.volt
  dI_input/dt = (-I_input+mu)/t_input : bn.volt
  dx1/dt = (1-x1)/t1_rec : 1
  dx2/dt = (1-x2)/t2_rec : 1
  u1 : 1
  t1_rec : bn.second
  u2 : 1
  t2_rec : bn.second
  mu : bn.volt
  eta : bn.volt
  J_ampa1 : 1
  J_nmda1 : 1
  J_ampa2 : 1
  J_nmda2 : 1
  J_gaba : 1
  J_input : 1
  tm : bn.second
  t_ampa : bn.second
  t_nmda : bn.second
  t_gaba : bn.second
  t_input : bn.second
  '''

    P_reset = "V=-52*bn.mV;x1+=-u1*x1;x2+=-u2*x2"

    Se_model = '''
  we_ampa1 : bn.volt
  we_nmda1 : bn.volt
  we_ampa2 : bn.volt
  we_nmda2 : bn.volt
  '''

    Se_pre = ('I_ampa1 += x1_pre*we_ampa1', 'I_nmda1 += x1_pre*we_nmda1',
              'I_ampa2 += x2_pre*we_ampa2', 'I_nmda2 += x2_pre*we_nmda2')

    Si_model = '''
  wi_gaba : bn.volt
  '''

    Si_pre = 'I_gaba += wi_gaba'

    So_model = '''
  wo_input : bn.volt
  '''

    So_pre = 'I_input += wo_input'

    #==============================================================================
    # Define populations
    #==============================================================================

    P = bn.NeuronGroup(N,
                       model,
                       threshold=Vthresh,
                       reset=P_reset,
                       refractory=tref)

    Pe = P[0:Ne]
    Pe.tm = te
    Pe.t_ampa = tee_ampa
    Pe.t_nmda = tee_nmda
    Pe.t_gaba = tei_gaba
    Pe.t_input = teo_input
    Pe.I_ampa1 = 0 * bn.mV
    Pe.I_nmda1 = 0 * bn.mV
    Pe.I_ampa2 = 0 * bn.mV
    Pe.I_nmda2 = 0 * bn.mV
    Pe.I_gaba = 0 * bn.mV
    Pe.I_input = 0 * bn.mV
    Pe.V = (np.random.rand(Pe.V.size) * 12 - 52) * bn.mV

    Pe.x1 = 1.0
    Pe.x2 = 1.0
    Pe.u1 = uee1
    Pe.u2 = uee2
    Pe.t1_rec = trec1
    Pe.t2_rec = trec2

    Pi = P[Ne:(Ne + Ni)]
    Pi.tm = ti
    Pi.t_ampa = tie_ampa
    Pi.t_nmda = tie_nmda
    Pi.t_gaba = tii_gaba
    Pi.t_input = teo_input
    Pi.I_ampa1 = 0 * bn.mV
    Pi.I_nmda1 = 0 * bn.mV
    Pi.I_ampa2 = 0 * bn.mV
    Pi.I_nmda2 = 0 * bn.mV
    Pi.I_gaba = 0 * bn.mV
    Pi.I_input = 0 * bn.mV
    Pi.V = (np.random.rand(Pi.V.size) * 12 - 52) * bn.mV

    Pi.x1 = 1.0
    Pi.x2 = 1.0
    Pi.u1 = 0.0
    Pi.u2 = 0.0
    Pi.t1_rec = 1.0
    Pi.t2_rec = 1.0

    Pe.J_ampa1 = Jee * (1 - qee1)  #*SEE1
    Pe.J_nmda1 = Jee * qee1  #*SEE1
    Pe.J_ampa2 = Jee * (1 - qee2)  #*SEE2
    Pe.J_nmda2 = Jee * qee2  #*SEE2

    Pi.J_ampa1 = Jie * (1 - qie2)  #*SEE2
    Pi.J_nmda1 = Jie * qie2  #*SEE2
    Pi.J_ampa2 = Jie * (1 - qie1)  #*SEE1
    Pi.J_nmda2 = Jie * qie1  #*SEE1

    Pe.J_gaba = Jei
    Pi.J_gaba = Jii

    Pe.J_input = Jeo
    Pi.J_input = Jeo

    #==============================================================================
    # Define inputs
    #==============================================================================

    if osc:
        Pe.mu = 12.0 * bn.mV
        holder = np.zeros((n_tsteps, ))
        t_freq = np.linspace(0, 10, n_tsteps)

        fo = 0.2  # Smallest frequency in the signal
        fe = 10.0  # Largest frequency in the signal
        F = int(fe / 0.2)
        for m in range(1, F + 1):
            holder = holder + np.cos(2 * np.pi * m * fo * t_freq - m *
                                     (m - 1) * np.pi / F)
        holder = holder / np.max(holder)
        Pe.eta = bn.TimedArray(0.0 * bn.mV * holder)  #, dt=0.5*bn.ms)

        Background_eo = bn.PoissonInput(Pe,
                                        N=1000,
                                        rate=1.05 * bn.Hz,
                                        weight=0.2 * bn.mV,
                                        state='I_input')
        Background_io = bn.PoissonInput(Pi,
                                        N=1000,
                                        rate=1.0 * bn.Hz,
                                        weight=0.2 * bn.mV,
                                        state='I_input')

        Pi.mu = 0 * bn.mV
        Pi.eta = 0 * bn.mV  #, dt=0.5*bn.ms)

        Po = bn.PoissonGroup(No, rates=0 * bn.Hz)
    else:

        Background_eo = bn.PoissonInput(Pe,
                                        N=1000,
                                        rate=1.05 * bn.Hz,
                                        weight=0.2 * bn.mV,
                                        state='I_input')
        Background_io = bn.PoissonInput(Pi,
                                        N=1000,
                                        rate=1.0 * bn.Hz,
                                        weight=0.2 * bn.mV,
                                        state='I_input')
        holder_pe = np.zeros((n_tsteps, ))
        time_steps = np.linspace(0, T / bn.second, n_tsteps)
        holder_pe[time_steps < 0.5] = 0.0 * bn.mV
        holder_pe[time_steps >= 0.5] = 6.0 * bn.mV  #25
        holder_pe[time_steps > 1.5] = 0.0 * bn.mV  #25
        Pe.mu = bn.TimedArray(holder_pe)

        def firing_function(t, ro):
            if t > 0.5 * bn.second and t < 3.5 * bn.second:
                return 0.0 * bn.Hz
            else:
                return 0.0 * bn.Hz

        Pe.eta = 0 * bn.mV  #, dt=0.5*bn.ms)
        Pi.mu = 0.0 * bn.mV
        Pi.eta = 0 * bn.mV  #, dt=0.5*bn.ms)

        Po = bn.PoissonGroup(No, rates=lambda t: firing_function(t, ro))

    #==============================================================================
    # Define synapses
    #==============================================================================

    See1 = bn.Synapses(Pe, Pe, model=Se_model, pre=Se_pre)
    See2 = bn.Synapses(Pe, Pe, model=Se_model, pre=Se_pre)
    Sie1 = bn.Synapses(Pe, Pi, model=Se_model, pre=Se_pre)
    Sie2 = bn.Synapses(Pe, Pi, model=Se_model, pre=Se_pre)

    Sei = bn.Synapses(Pi, Pe, model=Si_model, pre=Si_pre)
    Sii = bn.Synapses(Pi, Pi, model=Si_model, pre=Si_pre)

    Seo = bn.Synapses(Po, Pe, model=So_model, pre=So_pre)

    #==============================================================================
    # Define random connections
    #==============================================================================

    if new_connectivity:
        See1.connect_random(Pe, Pe, sparseness=pcon / 2.0)
        See2.connect_random(Pe, Pe, sparseness=pcon / 2.0)
        Sie1.connect_random(Pe, Pi, sparseness=pcon / 2.0)
        Sie2.connect_random(Pe, Pi, sparseness=pcon / 2.0)
        Sii.connect_random(Pi, Pi, sparseness=pcon)
        Sei.connect_random(Pi, Pe, sparseness=pcon)
        Seo.connect_random(Po, Pe, sparseness=pcon)

        print 'Saving'
        See1.save_connectivity('./See1_connections_std_saver_p20_' +
                               str(run_num))
        See2.save_connectivity('./See2_connections_std_saver_p20_' +
                               str(run_num))
        Sie1.save_connectivity('./Sie1_connections_std_saver_p20_' +
                               str(run_num))
        Sie2.save_connectivity('./Sie2_connections_std_saver_p20_' +
                               str(run_num))
        Sii.save_connectivity('./Sii_connections_std_saver_p20_' +
                              str(run_num))
        Sei.save_connectivity('./Sei_connections_std_saver_p20_' +
                              str(run_num))
        Seo.save_connectivity('./Seo_connections_std_saver_p20_' +
                              str(run_num))
    else:
        print 'Loading'
        See1.load_connectivity('./See1_connections_std_saver_p20_' +
                               str(run_num))
        See2.load_connectivity('./See2_connections_std_saver_p20_' +
                               str(run_num))
        Sie1.load_connectivity('./Sie1_connections_std_saver_p20_' +
                               str(run_num))
        Sie2.load_connectivity('./Sie2_connections_std_saver_p20_' +
                               str(run_num))
        Sii.load_connectivity('./Sii_connections_std_saver_p20_' +
                              str(run_num))
        Sei.load_connectivity('./Sei_connections_std_saver_p20_' +
                              str(run_num))
        Seo.load_connectivity('./Seo_connections_std_saver_p20_' +
                              str(run_num))

    See1.we_ampa1 = SEE1 * 1.0 * bn.mV / tee_ampa
    See1.we_nmda1 = SEE1 * 1.0 * bn.mV / tee_nmda
    See1.we_ampa2 = 0.0 * bn.mV / tee_ampa
    See1.we_nmda2 = 0.0 * bn.mV / tee_nmda

    See2.we_ampa1 = 0.0 * bn.mV / tee_ampa
    See2.we_nmda1 = 0.0 * bn.mV / tee_nmda
    See2.we_ampa2 = SEE2 * 1.0 * bn.mV / tee_ampa
    See2.we_nmda2 = SEE2 * 1.0 * bn.mV / tee_nmda

    Sie1.we_ampa1 = 0.0 * bn.mV / tie_ampa
    Sie1.we_nmda1 = 0.0 * bn.mV / tie_nmda
    Sie1.we_ampa2 = SEE1 * 1.0 * bn.mV / tie_ampa
    Sie1.we_nmda2 = SEE1 * 1.0 * bn.mV / tie_nmda

    Sie2.we_ampa1 = SEE2 * 1.0 * bn.mV / tie_ampa
    Sie2.we_nmda1 = SEE2 * 1.0 * bn.mV / tie_nmda
    Sie2.we_ampa2 = 0.0 * bn.mV / tie_ampa
    Sie2.we_nmda2 = 0.0 * bn.mV / tie_nmda

    Sei.wi_gaba = 1.0 * bn.mV / tei_gaba
    Sii.wi_gaba = 1.0 * bn.mV / tii_gaba

    Seo.wo_input = 1.0 * bn.mV / teo_input

    #==============================================================================
    #  Define monitors
    #==============================================================================

    Pe_mon_V = bn.StateMonitor(Pe, 'V', timestep=10, record=True)
    Pe_mon_eta = bn.StateMonitor(Pe, 'eta', timestep=1, record=True)
    Pe_mon_ampa1 = bn.StateMonitor(Pe, 'I_ampa1', timestep=1, record=True)
    Pe_mon_nmda1 = bn.StateMonitor(Pe, 'I_nmda1', timestep=1, record=True)
    Pe_mon_ampa2 = bn.StateMonitor(Pe, 'I_ampa2', timestep=1, record=True)
    Pe_mon_nmda2 = bn.StateMonitor(Pe, 'I_nmda2', timestep=1, record=True)
    Pe_mon_gaba = bn.StateMonitor(Pe, 'I_gaba', timestep=1, record=True)
    Pe_mon_input = bn.StateMonitor(Pe, 'I_input', timestep=10, record=True)
    See1_mon_x = bn.StateMonitor(Pe, 'x1', timestep=10, record=True)
    See2_mon_x = bn.StateMonitor(Pe, 'x2', timestep=10, record=True)

    Pe_ratemon = bn.PopulationRateMonitor(Pe, bin=10.0 * bn.ms)
    Pi_ratemon = bn.PopulationRateMonitor(Pi, bin=10.0 * bn.ms)

    #==============================================================================
    # Run model
    #==============================================================================
    timer = 0 * bn.second
    t_start = time.time()
    bn.run(T, report='graphical')
    timer = timer + T
    print '-------------------------------------------------------'
    print 'Time is ' + str(timer) + ' seconds'
    t_end = time.time()
    print 'Time to compute last ' +str(T)+' seconds is: ' + \
          str(t_end - t_start) + ' seconds'
    print '-------------------------------------------------------\n'

    Pe_mon_ampa1_vals = Pe.J_ampa1[0] * np.mean(Pe_mon_ampa1.values.T, axis=1)
    Pe_mon_nmda1_vals = Pe.J_nmda1[0] * np.mean(Pe_mon_nmda1.values.T, axis=1)
    Pe_mon_ampa2_vals = Pe.J_ampa2[0] * np.mean(Pe_mon_ampa2.values.T, axis=1)
    Pe_mon_nmda2_vals = Pe.J_nmda2[0] * np.mean(Pe_mon_nmda2.values.T, axis=1)
    Pe_mon_ampa_vals = Pe_mon_ampa1_vals + Pe_mon_ampa2_vals
    Pe_mon_nmda_vals = Pe_mon_nmda1_vals + Pe_mon_nmda2_vals

    Pe_mon_gaba_vals = Pe.J_gaba[0] * np.mean(Pe_mon_gaba.values.T, axis=1)
    Pe_mon_input_vals = Pe.J_input[0] * np.mean(Pe_mon_input.values.T, axis=1)
    Pe_mon_V_vals = np.mean(Pe_mon_V.values.T, axis=1)

    Pe_mon_all_vals = Pe_mon_ampa_vals + Pe_mon_nmda_vals - Pe_mon_gaba_vals

    See1_mon_x_vals = np.mean(See1_mon_x.values.T, axis=1)
    See2_mon_x_vals = np.mean(See2_mon_x.values.T, axis=1)

    #==============================================================================
    # Save into a Matlab file
    #==============================================================================

    if osc:

        Pe_output = Pe.J_ampa1[0]*Pe_mon_ampa1.values+Pe.J_nmda1[0]*Pe_mon_nmda1.values + \
        Pe.J_ampa2[0]*Pe_mon_ampa2.values+Pe.J_nmda2[0]*Pe_mon_nmda2.values-Pe.J_gaba[0]*Pe_mon_gaba.values
        Pe_output = Pe_output[:, fft_start:, ]
        Pe_V = Pe_mon_V.values[:, fft_start:, ]
        Pe_glut = Pe.J_ampa1[0]*Pe_mon_ampa1.values+Pe.J_nmda1[0]*Pe_mon_nmda1.values + \
        Pe.J_ampa2[0]*Pe_mon_ampa2.values+Pe.J_nmda2[0]*Pe_mon_nmda2.values
        Pe_glut = Pe_glut[:, fft_start:, ]
        Pe_gaba = Pe.J_gaba[0] * Pe_mon_gaba.values
        Pe_gaba = Pe_gaba[:, fft_start:, ]

        Pe_input = Pe_mon_eta[:, fft_start:, ]
        T_step = bn.defaultclock.dt

        holder = {
            'Pe_output': Pe_output,
            'Pe_input': Pe_input,
            'Pe_V': Pe_V,
            'Pe_glut': Pe_glut,
            'Pe_gaba': Pe_gaba,
            'T_step': T_step
        }
        scipy.io.savemat(fft_file + 'delta_u' + str(delta_u) + '_' + str(rep),
                         mdict=holder)
    else:
        holder = {
            'Pe_rate': Pe_ratemon.rate,
            'Pe_time': Pe_ratemon.times,
            'uee1': uee1,
            'uee2': uee2,
            'uie1': uie1,
            'uie2': uie2
        }
        scipy.io.savemat(rate_file + 'delta_q_' + str(delta_u) + '_' +
                         str(run_num) + 'rep' + str(rep),
                         mdict=holder)
    bn.clear(erase=True, all=True)
예제 #12
0
            pre=eqs_stdp_pre_ee,
            post=eqs_stdp_post_ee,
            wmin=0.,
            wmax=wmax_ee)

#------------------------------------------------------------------------------
# run the simulation and set inputs
#------------------------------------------------------------------------------
previous_spike_count = np.zeros(n_e)
assignments = np.zeros(n_e)
input_numbers = [0] * num_examples
outputNumbers = np.zeros((num_examples, 10))

for i, name in enumerate(input_population_names):
    input_groups[name + 'e'].rate = 0
'''
b.run(0)

j = 0
while j < (int(num_examples)):

    print(j)

    rates = testing['x'][j % 10000, :, :].reshape((n_input)) / 8. *  input_intensity

    input_groups['Xe'].rate = rates
    b.run(single_example_time, report='text')
            
    current_spike_count = np.asarray(spike_counters['Ae'].count[:]) - previous_spike_count
    previous_spike_count = np.copy(spike_counters['Ae'].count[:])
예제 #13
0
def run_train():
    global fig_num, input_intensity, previous_spike_count, rates, assignments, clusters, cluster_assignments, \
       simple_clusters, simple_cluster_assignments, index_matrix, accumulated_rates, \
       accumulated_inputs, spike_proportions

    if plot:
        input_image_monitor, input_image = plot_input(rates)
        fig_num += 1

        weights_assignments_figure, weights_axes, assignments_axes, weights_image, \
            assignments_image = plot_weights_and_assignments(assignments)
        fig_num += 1

    # set up performance recording and plotting
    num_evaluations = int(num_examples / update_interval) + 1
    performances = {
        voting_scheme: np.zeros(num_evaluations)
        for voting_scheme in voting_schemes
    }
    num_weight_updates = int(num_examples / weight_update_interval)

    if plot:
        performance_monitor, fig_num, fig_performance = plot_performance(
            fig_num, performances, num_evaluations)
    else:
        performances, wrong_idxs, wrong_labels = get_current_performance(
            performances, 0)

    # initialize network
    j = 0
    num_retries = 0
    b.run(0)

    if save_best_model:
        best_performance = 0.0

    start_time = timeit.default_timer()
    while j < num_examples:
        # get the firing rates of the next input example
        rates = ((data['x'][j % data_size, :, :] / 8.0) *
                 input_intensity).ravel()
        for n in xrange(n_e):
            if rates[convolution_locations[n]].sum() < 1:
                continue

            # sets the input firing rates
            input_groups['Xe'].rate = rates[convolution_locations[n]]

            # plot the input at this step
            if plot:
                input_image_monitor = update_input(
                    rates[convolution_locations[n]], input_image_monitor,
                    input_image)

            # run the network for a single example time
            b.run(single_example_time)

            # make sure synapse weights don't grow too large
            normalize_weights()

            # let the network relax back to equilibrium
            b.run(resting_time)

        # get new neuron label assignments every 'update_interval'
        if j % update_interval == 0 and j > 0:
            if j % data_size == 0:
                assignments, accumulated_rates, spike_proportions = assign_labels(
                    result_monitor,
                    input_numbers[data_size - update_interval:data_size],
                    accumulated_rates, accumulated_inputs)
            else:
                assignments, accumulated_rates, spike_proportions = assign_labels(
                    result_monitor,
                    input_numbers[(j % data_size) - update_interval:j %
                                  data_size], accumulated_rates,
                    accumulated_inputs)

        # get count of spikes over the past iteration
        current_spike_count = np.copy(
            spike_counters['Ae'].count[:]) - previous_spike_count
        previous_spike_count = np.copy(spike_counters['Ae'].count[:])

        if plot:
            spike_show = -np.log(spike_counters['Ae'].count[:].reshape(
                [n_neurons_sqrt, n_neurons_sqrt]).T /
                                 float(np.sum(spike_counters['Ae'].count[:])))
            theta_show = neuron_groups['e'].theta.reshape(
                [n_neurons_sqrt, n_neurons_sqrt]).T
            voltage_show = np.copy(neuron_groups['e'].v).reshape(
                [n_neurons_sqrt, n_neurons_sqrt]).T

            print theta_show.shape

            if j == 0:
                spike_fig, spike_axes = plt.subplots()
                spike_im = spike_axes.matshow(spike_show, vmin=0, vmax=10)
                plt.colorbar(spike_im)
                spike_axes.set_title('Spikes')

                theta_fig, theta_axes = plt.subplots()
                theta_im = theta_axes.matshow(theta_show)
                plt.colorbar(theta_im)
                theta_axes.set_title('Theta')

                voltage_fig, voltage_axes = plt.subplots()
                voltage_axes.set_title('Voltage')
                voltage_im = voltage_axes.matshow(voltage_show)
            else:
                spike_im.set_array(spike_show)
                spike_fig.canvas.draw()

                theta_im.set_array(theta_show)
                theta_fig.canvas.draw()

                voltage_im.set_array(voltage_show)
                voltage_fig.canvas.draw()

        # update weights every 'weight_update_interval'
        if j % weight_update_interval == 0 and plot:
            update_weights_and_assignments(weights_assignments_figure,
                                           weights_axes, assignments_axes,
                                           weights_image, assignments_image,
                                           assignments)

        # if the neurons in the network didn't spike more than four times
        if np.sum(current_spike_count) < 5 and num_retries < 3:
            # increase the intensity of input
            input_intensity += 2
            num_retries += 1

            # set all network firing rates to zero
            for name in input_population_names:
                input_groups[name + 'e'].rate = 0

        # otherwise, record results and continue simulation
        else:
            num_retries = 0

            # record the current number of spikes
            result_monitor[j % update_interval, :] = current_spike_count

            # get true label of last input example
            input_numbers[j] = data['y'][j % data_size][0]

            # get the output classifications of the network
            for scheme, outputs in predict_label(
                    assignments, result_monitor[j % update_interval, :],
                    accumulated_rates, spike_proportions).items():
                output_numbers[scheme][j, :] = outputs

            if j % print_progress_interval == 0 and j > 0:
                print 'Progress: (%d / %d) - Elapsed time: %.4f' % (
                    j, num_examples, timeit.default_timer() - start_time)
                start_time = timeit.default_timer()

            # plot performance if appropriate
            if (j % update_interval == 0 or j == num_examples - 1) and j > 0:
                if plot:
                    # updating the performance plot
                    perf_plot, performances, wrong_idxs, wrong_labels = update_performance_plot(
                        performance_monitor, performances, j, fig_performance)
                else:
                    performances, wrong_idxs, wrong_labels = get_current_performance(
                        performances, j)

                # pickling performance recording and iteration number
                p.dump((j, performances),
                       open(os.path.join(performance_dir, ending + '.p'),
                            'wb'))

                # Save the best model's weights and theta parameters (if so specified)
                if save_best_model:
                    for performance in performances:
                        if performances[performance][int(
                                j /
                                float(update_interval))] > best_performance:
                            print '\n', 'Best model thus far! Voting scheme:', performance, '\n'

                            best_performance = performances[performance][int(
                                j / float(update_interval))]
                            save_connections(best_weights_dir, connections,
                                             input_connections, ending, 'best')
                            save_theta(best_weights_dir, population_names,
                                       neuron_groups, ending, 'best')

                            np.save(
                                os.path.join(
                                    best_assignments_dir,
                                    '_'.join(['assignments', ending, 'best'])),
                                assignments)
                            np.save(
                                os.path.join(
                                    best_misc_dir, '_'.join(
                                        ['accumulated_rates', ending,
                                         'best'])), accumulated_rates)
                            np.save(
                                os.path.join(
                                    best_misc_dir, '_'.join(
                                        ['spike_proportions', ending,
                                         'best'])), spike_proportions)

                # Print out performance progress intermittently
                for performance in performances:
                    print '\nClassification performance (' + performance + ')', performances[performance][1:int(j / float(update_interval)) + 1], \
                       '\nAverage performance:', sum(performances[performance][1:int(j / float(update_interval)) + 1]) / \
                        float(len(performances[performance][1:int(j / float(update_interval)) + 1])), \
                        '\nBest performance:', max(performances[performance][1:int(j / float(update_interval)) + 1]), '\n'

            # set input firing rates back to zero
            for name in input_population_names:
                input_groups[name + 'e'].rate = 0

            # run the network for 'resting_time' to relax back to rest potentials
            b.run(resting_time)

            # bookkeeping
            input_intensity = start_input_intensity
            j += 1

    # ensure weights don't grow without bound
    normalize_weights()

    print '\n'
                     model="w : siemens", pre="gExc_post += w")
synapse_B[:,:] = 1
synapse_B.w = WExc
synapse_B.delay[0] = B1



vtrace = StateMonitor(neuron, 'V', record=True)
mtrace = StateMonitor(neuron, 'm', record=True)
htrace = StateMonitor(neuron, 'h', record=True)
ntrace = StateMonitor(neuron, 'n', record=True)
exc_trace = StateMonitor(neuron, 'gExc', record=True)

spike_monitor = SpikeMonitor(neuron)

run(1.0*second)

print("Number of spikes fired: %i" % (spike_monitor.nspikes))
if spike_monitor.nspikes <= 10:
    print("Spike times follow:")
    print(', '.join([str(sp) for sp in spike_monitor[0]]))


plt.subplot(211)
plt.plot(vtrace.times, vtrace[0], label="V(t)")
plt.legend()
#subplot(312)
#plot(mtrace.times, mtrace[0], label="m(t)")
#plot(htrace.times, htrace[0], label="h(t)")
#plot(ntrace.times, ntrace[0], label="n(t)")
#legend()
                # negative inputs
                times = np.linspace(0, num_timesteps-num_layers-1, np.clip(-1*raw_vector[input_neuron]*num_timesteps, 0, 1./timestep))
                spikes = zip([input_neuron+n_input]*len(times), times)
#                 print 'spiketimes', spiketimes
#                 print 'spikes', spikes
                if spikes != []:
                    if spiketimes == []:
                        spiketimes = spikes
                    else:
#                         print spiketimes
                        spiketimes = np.concatenate((spiketimes, spikes), axis=0)
            input_groups['input'].spiketimes = [(y,z) for y,z in spiketimes]
            print input_groups['input'].spiketimes
            
                
        b.run(single_example_time)#, report='text')
                            
        current_spike_count = np.asarray(spike_counters[num_layers-1].count[:]) - previous_spike_count
        #     print current_spike_count,  np.asarray(spike_counters['Ce'].count[:]), previous_spike_count
        previous_spike_count = np.copy(spike_counters[num_layers-1].count[:])
        print "Number of output spikes:", current_spike_count, '\n'
        imshow(img_dict[(int(round(layer1_output)))])
        draw()
            
        #     b.figure()
        #     b.plot(state_monitors[0].times/b.second, state_monitors[0]['v'][0], label = ' v 0')
        #     b.title('membrane voltages of population ')
        #     b.show()
    
    except KeyError:
        print word, ' is not/rarely mentioned in Wikipedia. Please choose something more common...\n'
예제 #16
0
                                   post=stdp_post_ee,
                                   wmin=0.,
                                   wmax=wmax_ee)

# RUN TRAINING
number_epochs = 1
number_samples = training_x.shape[0]
total_samples = number_epochs * number_samples

input_intensity = 2.0
default_input_intensity = input_intensity

total_connection_weight = 78.

neuron_groups['poisson'].rate = 0
b.run(0 * b.ms)

spike_counter = b.SpikeCounter(neuron_groups['spiking'])
previous_spike_count = np.zeros(spiking_neurons)
result_spike_activity = np.zeros((total_samples, spiking_neurons))

total_start = time.time()
start = time.time()
i = 0
while i < total_samples:

    # normalize input connection weights
    total_input_weight_neuron = np.sum(connections['input'].W, axis=0)
    connections[
        'input'].W *= total_connection_weight / total_input_weight_neuron
예제 #17
0
#------------------------------------------------------------------------------ 
# run the simulation and set inputs
#------------------------------------------------------------------------------ 
previous_spike_count = np.zeros(n_e)
assignments = np.zeros(n_e)
input_numbers = [0] * num_examples
outputNumbers = np.zeros((num_examples, 10))
if not test_mode:
    input_weight_monitor, fig_weights = plot_2d_input_weights()
    fig_num += 1
if do_plot_performance:
    performance_monitor, performance, fig_num, fig_performance = plot_performance(fig_num)
for i,name in enumerate(input_population_names):
    input_groups[name+'e'].rate = 0
b.run(0)

j = 0
if test_mode:
   temp=360
else:
   temp=18000

while j < (temp):                                              #CHANGED
    start =time.time()
    if  j==0:
     st1=time.time()
    if test_mode:
        if use_testing_set:
            rates = testing['x'][j%10000,:,:].reshape((n_input)) / 8. *  input_intensity
        else:
예제 #18
0
def run_train():
    global fig_num, input_intensity, previous_spike_count, rates, assignments, clusters, cluster_assignments, \
       simple_clusters, simple_cluster_assignments, index_matrix, accumulated_rates, \
       accumulated_inputs, spike_proportions

    if plot:
        input_image_monitor, input_image = plot_input(rates)
        fig_num += 1

        weights_assignments_figure, weights_axes, assignments_axes, weights_image, \
            assignments_image = plot_weights_and_assignments(assignments)
        fig_num += 1

    # set up performance recording and plotting
    num_evaluations = int(num_examples / update_interval) + 1
    performances = {
        voting_scheme: np.zeros(num_evaluations)
        for voting_scheme in voting_schemes
    }
    num_weight_updates = int(num_examples / weight_update_interval)

    if plot:
        performance_monitor, fig_num, fig_performance = plot_performance(
            fig_num, performances, num_evaluations)
    else:
        performances, wrong_idxs, wrong_labels = get_current_performance(
            performances, 0)

    # initialize network
    j = 0
    num_retries = 0
    b.run(0)

    if save_best_model:
        best_performance = 0.0

    # start recording time
    start_time = timeit.default_timer()

    last_weights = input_connections['XeAe'][:].todense()

    while j < num_examples:
        # get the firing rates of the next input example
        rates = (data['x'][j % data_size, :, :] / 8.0) * input_intensity

        # sets the input firing rates
        input_groups['Xe'].rate = rates.reshape(n_input)

        # plot the input at this step
        if plot:
            input_image_monitor = update_input(rates, input_image_monitor,
                                               input_image)

        # run the network for a single example time
        b.run(single_example_time)

        # get new neuron label assignments every 'update_interval'
        if j % update_interval == 0 and j > 0:
            if j % data_size == 0:
                assignments, accumulated_rates, spike_proportions = assign_labels(
                    result_monitor,
                    input_numbers[data_size - update_interval:data_size],
                    accumulated_rates, accumulated_inputs)
            else:
                assignments, accumulated_rates, spike_proportions = assign_labels(
                    result_monitor,
                    input_numbers[(j % data_size) - update_interval:j %
                                  data_size], accumulated_rates,
                    accumulated_inputs)

        # get count of spikes over the past iteration
        current_spike_count = np.copy(spike_counters['Ae'].count[:]).reshape(
            (conv_features, n_e)) - previous_spike_count
        previous_spike_count = np.copy(spike_counters['Ae'].count[:]).reshape(
            (conv_features, n_e))

        # make sure synapse weights don't grow too large
        normalize_weights()

        if j % weight_update_interval == 0:
            last_weights = input_connections['XeAe'][:].todense()

        # update weights every 'weight_update_interval'
        if j % weight_update_interval == 0 and plot:
            update_weights_and_assignments(weights_assignments_figure, weights_axes, assignments_axes, \
                   weights_image, assignments_image, assignments, current_spike_count)

        # if the neurons in the network didn't spike more than four times
        if np.sum(current_spike_count) < 5 and num_retries < 3:
            # increase the intensity of input
            input_intensity += 2
            num_retries += 1

            # set all network firing rates to zero
            for name in input_population_names:
                input_groups[name + 'e'].rate = 0

            # let the network relax back to equilibrium
            b.run(resting_time)

        # otherwise, record results and continue simulation
        else:
            num_retries = 0

            # record the current number of spikes
            result_monitor[j % update_interval, :] = current_spike_count

            # get true label of last input example
            input_numbers[j] = data['y'][j % data_size][0]

            activity = result_monitor[j % update_interval, :] / np.sum(
                result_monitor[j % update_interval, :])

            # get network filter weights
            filters = input_connections['XeAe'][:].todense()

            # get the output classifications of the network
            for scheme, outputs in predict_label(assignments, result_monitor[j % update_interval, :], \
                        accumulated_rates, spike_proportions).items():
                if scheme != 'distance':
                    output_numbers[scheme][j, :] = outputs
                elif scheme == 'distance':
                    current_input = (
                        rates * (weight['ee_input'] / np.sum(rates))).ravel()
                    output_numbers[scheme][j, 0] = assignments[np.argmin([ euclidean(current_input, \
                            filters[:, i]) for i in xrange(conv_features) ])]

            # print progress
            if j % print_progress_interval == 0 and j > 0:
                print 'runs done:', j, 'of', int(
                    num_examples
                ), '(time taken for past', print_progress_interval, 'runs:', str(
                    timeit.default_timer() - start_time) + ')'
                start_time = timeit.default_timer()

            # plot performance if appropriate
            if (j % update_interval == 0 or j == num_examples - 1) and j > 0:
                if plot:
                    # updating the performance plot
                    perf_plot, performances, wrong_idxs, wrong_labels = update_performance_plot(
                        performance_monitor, performances, j, fig_performance)
                else:
                    performances, wrong_idxs, wrong_labels = get_current_performance(
                        performances, j)

                # pickling performance recording and iteration number
                p.dump((j, performances),
                       open(os.path.join(performance_dir, ending + '.p'),
                            'wb'))

                # Save the best model's weights and theta parameters (if so specified)
                if save_best_model:
                    for performance in performances:
                        if performances[performance][int(
                                j /
                                float(update_interval))] > best_performance:
                            print '\n', 'Best model thus far! Voting scheme:', performance, '\n'

                            best_performance = performances[performance][int(
                                j / float(update_interval))]
                            save_connections(best_weights_dir, connections,
                                             input_connections, ending, 'best')
                            save_theta(best_weights_dir, population_names,
                                       neuron_groups, ending, 'best')

                            np.save(
                                os.path.join(
                                    best_assignments_dir,
                                    '_'.join(['assignments', ending, 'best'])),
                                assignments)
                            np.save(
                                os.path.join(
                                    best_misc_dir, '_'.join(
                                        ['accumulated_rates', ending,
                                         'best'])), accumulated_rates)
                            np.save(
                                os.path.join(
                                    best_misc_dir, '_'.join(
                                        ['spike_proportions', ending,
                                         'best'])), spike_proportions)

                # Print out performance progress intermittently
                for performance in performances:
                    print '\nClassification performance (' + performance + ')', performances[performance][1:int(j / float(update_interval)) + 1], \
                       '\nAverage performance:', sum(performances[performance][1:int(j / float(update_interval)) + 1]) / \
                        float(len(performances[performance][1:int(j / float(update_interval)) + 1])), \
                        '\nBest performance:', max(performances[performance][1:int(j / float(update_interval)) + 1]), '\n'

            # set input firing rates back to zero
            for name in input_population_names:
                input_groups[name + 'e'].rate = 0

            # run the network for 'resting_time' to relax back to rest potentials
            b.run(resting_time)

            # bookkeeping
            input_intensity = start_input_intensity
            j += 1

    # ensure weights don't grow without bound
    normalize_weights()

    print '\n'
def run_activity_plotting():
    global fig_num, input_intensity, previous_spike_count, rates, assignments, clusters, cluster_assignments, \
       simple_clusters, simple_cluster_assignments, index_matrix, accumulated_rates, \
       accumulated_inputs, spike_proportions, ending

    ending = '_'.join([
        ending,
        str(inhibition_level),
        str(test_time),
        str(test_rest),
        str(num_tests)
    ])

    if not os.path.isdir(os.path.join(spike_activity_dir, ending)):
        os.makedirs(os.path.join(spike_activity_dir, ending))

    if do_plot:
        assignments_image = plot_assignments(assignments)
        fig_num += 1

    # set up performance recording and plotting
    num_evaluations = int(num_examples / update_interval) + 1
    performances = {
        voting_scheme: np.zeros(num_evaluations)
        for voting_scheme in voting_schemes
    }
    num_weight_updates = int(num_examples / weight_update_interval)
    all_deltas = np.zeros((num_weight_updates, (conv_size**2) * n_e_total))
    deltas = np.zeros(num_weight_updates)

    # initialize network
    j = 0
    num_retries = 0
    b.run(0)

    # get network filter weights
    filters = input_connections['XeAe'][:].todense()

    # start recording time
    start_time = timeit.default_timer()

    votes = {}
    for scheme in voting_schemes:
        votes[scheme] = np.zeros(num_tests)

    correct = {}
    for scheme in voting_schemes:
        correct[scheme] = np.zeros(1)

    while j < num_examples * num_tests:
        # get the firing rates of the next input example
        np.random.seed(random_seed + (j % num_tests))

        # get the firing rates of the next input example
        rates = (data['x'][(j // num_tests) % data_size, :, :] /
                 8.0) * input_intensity

        # sets the input firing rates
        input_groups['Xe'].rate = rates.reshape(n_input)

        # run the network for a single example time
        b.run(single_example_time)

        # get count of spikes over the past iteration
        current_spike_count = np.copy(spike_counters['Ae'].count[:]).reshape(
            (conv_features, n_e)) - previous_spike_count
        previous_spike_count = np.copy(spike_counters['Ae'].count[:]).reshape(
            (conv_features, n_e))

        # if the neurons in the network didn't spike more than four times
        if np.sum(current_spike_count) < 5 and num_retries < 3:
            # increase the intensity of input
            input_intensity += 2
            num_retries += 1

            # set all network firing rates to zero
            for name in input_population_names:
                input_groups[name + 'e'].rate = 0

            # let the network relax back to equilibrium
            if not reset_state_vars:
                b.run(resting_time)
            else:
                for neuron_group in neuron_groups:
                    neuron_groups[neuron_group].v = v_reset_e
                    neuron_groups[neuron_group].ge = 0
                    neuron_groups[neuron_group].gi = 0

        # otherwise, record results and continue simulation
        else:
            num_retries = 0

            # record the current number of spikes
            result_monitor[j % update_interval, :] = current_spike_count

            # get true label of the past input example
            input_numbers[j // num_tests] = data['y'][(j // num_tests) %
                                                      data_size][0]

            # get the output classifications of the network
            for scheme, outputs in predict_label(assignments, result_monitor[j % update_interval, :], \
                        accumulated_rates, spike_proportions).items():
                votes[scheme][j % num_tests] = outputs[0]

            # Get the predicted labels (from our voting schemes) as well as the ground truth label for this example.
            predicted_labels = {
                scheme: int(output_numbers[scheme][j, 0])
                for scheme in voting_schemes
            }
            true_label = int(input_numbers[j])

            plot_labels_and_spikes(assignments, current_spike_count, ending, j, data['x']\
                   [(j // num_tests) % data_size, :, :], predicted_labels, true_label)

            # print progress
            if j % print_progress_interval == 0 and j > 0:
                print 'runs done:', j, 'of', int(num_examples * num_tests), '(time taken for past', print_progress_interval, \
                              'runs:', str(timeit.default_timer() - start_time) + ')'
                start_time = timeit.default_timer()

            # set input firing rates back to zero
            for name in input_population_names:
                input_groups[name + 'e'].rate = 0

            # run the network for 'resting_time' to relax back to rest potentials
            if not reset_state_vars:
                b.run(resting_time)
            else:
                for neuron_group in neuron_groups:
                    neuron_groups[neuron_group].v = v_reset_e
                    neuron_groups[neuron_group].ge = 0
                    neuron_groups[neuron_group].gi = 0

            if (j + 1) % num_tests == 0:
                for scheme in voting_schemes:
                    output_numbers[scheme][j // num_tests, 0] = np.argmax(
                        np.bincount(votes[scheme].astype(int), minlength=10))
                    if output_numbers[scheme][j // num_tests,
                                              0] == input_numbers[j //
                                                                  num_tests]:
                        correct[scheme] += 1

                for scheme in voting_schemes:
                    votes[scheme] = np.zeros(num_tests)

            # bookkeeping
            input_intensity = start_input_intensity
            j += 1

    print '\n'
예제 #20
0
def run_test():
    global fig_num, input_intensity, previous_spike_count, rates, assignments, clusters, cluster_assignments, \
       simple_clusters, simple_cluster_assignments, index_matrix, accumulated_rates, \
       accumulated_inputs, spike_proportions

    # set up performance recording and plotting
    num_evaluations = int(num_examples / update_interval) + 1
    performances = {
        voting_scheme: np.zeros(num_evaluations)
        for voting_scheme in voting_schemes
    }
    num_weight_updates = int(num_examples / weight_update_interval)

    # initialize network
    j = 0
    num_retries = 0
    b.run(0)

    # get network filter weights
    filters = input_connections['XeAe'][:].todense()

    # start recording time
    start_time = timeit.default_timer()

    while j < num_examples:
        # get the firing rates of the next input example
        rates = (data['x'][j % data_size, :, :] / 8.0) * input_intensity

        # sets the input firing rates
        input_groups['Xe'].rate = rates.reshape(n_input)

        # run the network for a single example time
        b.run(single_example_time)

        # get count of spikes over the past iteration
        current_spike_count = np.copy(spike_counters['Ae'].count[:]).reshape(
            (conv_features, n_e)) - previous_spike_count
        previous_spike_count = np.copy(spike_counters['Ae'].count[:]).reshape(
            (conv_features, n_e))

        # if the neurons in the network didn't spike more than four times
        if np.sum(current_spike_count) < 5 and num_retries < 3:
            # increase the intensity of input
            input_intensity += 2
            num_retries += 1

            # set all network firing rates to zero
            for name in input_population_names:
                input_groups[name + 'e'].rate = 0

            # let the network relax back to equilibrium
            b.run(resting_time)

        # otherwise, record results and continue simulation
        else:
            num_retries = 0

            # record the current number of spikes
            result_monitor[j % update_interval, :] = current_spike_count

            # get true label of the past input example
            input_numbers[j] = data['y'][j % data_size][0]

            # get the output classifications of the network
            for scheme, outputs in predict_label(
                    assignments, result_monitor[j % update_interval, :],
                    accumulated_rates, spike_proportions).items():
                output_numbers[scheme][j, :] = outputs

            # print progress
            if j % print_progress_interval == 0 and j > 0:
                print 'runs done:', j, 'of', int(
                    num_examples
                ), '(time taken for past', print_progress_interval, 'runs:', str(
                    timeit.default_timer() - start_time) + ')'
                start_time = timeit.default_timer()

            # set input firing rates back to zero
            for name in input_population_names:
                input_groups[name + 'e'].rate = 0

            # run the network for 'resting_time' to relax back to rest potentials
            b.run(resting_time)

            # bookkeeping
            input_intensity = start_input_intensity
            j += 1

    print '\n'
#! /usr/bin/python
# -*- coding: utf-8 -*-

import brian

tau = 20 * brian.msecond 
Vt = -50 * brian.mvolt
Vr = -60 * brian.mvolt
El = -60 * brian.mvolt

N = 40

G = brian.NeuronGroup(N=N, model='dV/dt = -(V-El)/tau : volt' , threshold=Vt, reset=Vr)
M = brian.SpikeMonitor(G)

G.V = Vr + 2*(Vt-Vr)*brian.rand(N)

brian.run(1 * brian.second)

print M.nspikes
예제 #22
0
#------------------------------------------------------------------------------
# run the simulation and set inputs
#------------------------------------------------------------------------------
previous_spike_count = np.zeros(n_e)
assignments = np.zeros(n_e)
input_numbers = [0] * num_examples
outputNumbers = np.zeros((num_examples, 10))
# if not test_mode:
#     input_weight_monitor, fig_weights = plot_2d_input_weights()
#     fig_num += 1
# if do_plot_performance:
#     performance_monitor, performance, fig_num, fig_performance = plot_performance(fig_num)
for i, name in enumerate(input_population_names):
    input_groups[name + 'e'].rate = 0
b.run(0)
j = 0
prev_xeae_weights = connections['XeAe'][:]
while j < (int(num_examples)):
    print '@@@@@@@@', np.allclose(prev_xeae_weights, connections['XeAe'][:])
    prev_xeae_weights = connections['XeAe'][:]
    if test_mode:
        if use_testing_set:
            rates = testing['x'][j % 10000, :, :].reshape(
                (n_input)) / 8. * input_intensity
        else:
            rates = training['x'][j % 60000, :, :].reshape(
                (n_input)) / 8. * input_intensity
    else:
        normalize_weights()
        rates = training['x'][j % 60000, :, :].reshape(
예제 #23
0
connections['inhibitory'] = b.Connection(neuron_groups['inhibitory'],
                                         neuron_groups['spiking'],
                                         structure='dense',
                                         state='gi')
connections['inhibitory'].connect(neuron_groups['inhibitory'],
                                  neuron_groups['spiking'],
                                  weight_matrix_inhibitory)

# RUN LABELING
number_samples = training_x.shape[0]

input_intensity = 2.0
default_input_intensity = input_intensity

neuron_groups['poisson'].rate = 0
b.run(0 * b.ms)

spike_counter = b.SpikeCounter(neuron_groups['spiking'])
previous_spike_count = np.zeros(spiking_neurons)
result_spike_activity = np.zeros((number_samples, spiking_neurons))

total_start = time.time()
start = time.time()

i = 0
while i < number_samples:

    # present one image
    neuron_groups['poisson'].rate = training_x[i, :] * input_intensity

    b.run(input_image_time)
예제 #24
0
# plot input weights
if not test_mode:
    input_weight_monitor, fig_weights = plot_2d_input_weights()
    fig_num += 1

# plot performance
if do_plot_performance:
    performance_monitor, performance, fig_num, fig_performance = plot_performance(
        fig_num)

# set firing rates to zero initially
for name in input_population_names:
    input_groups[name + 'e'].rate = 0

# initialize network and set current example to zero
b.run(0)
j = 0

while j < (int(num_examples)):

    if test_mode:
        if use_testing_set:
            rates = testing['x'][j % 10000, :, :].reshape(
                (n_input)) / 8. * input_intensity
        else:
            rates = training['x'][j % 60000, :, :].reshape(
                (n_input)) / 8. * input_intensity

    else:
        # ensure weights don't grow without bound
        normalize_weights()
def run_test():
    global fig_num, input_intensity, previous_spike_count, rates, assignments, clusters, cluster_assignments, \
       kmeans, kmeans_assignments, simple_clusters, simple_cluster_assignments, index_matrix

    # if do_plot:
    # 	assignments_image, assignments_figure = plot_assignments(assignments)
    # 	fig_num += 1

    # initialize network
    j = 0
    num_retries = 0
    b.run(0)

    # start recording time
    start_time = timeit.default_timer()

    activity = None
    max_fired = None

    confusion_histogram = np.zeros((num_examples, 4))

    print '\n'

    while j < num_examples:
        # get the firing rates of the next input example
        rates = (data['x'][j % data_size, :, :] / 8.0) * input_intensity

        # sets the input firing rates
        input_groups['Xe'].rate = rates.reshape(n_input)

        # run the network for a single example time
        b.run(single_example_time)

        # get count of spikes over the past iteration
        current_spike_count = np.copy(spike_counters['Ae'].count[:]).reshape(
            (conv_features, n_e)) - previous_spike_count
        previous_spike_count = np.copy(spike_counters['Ae'].count[:]).reshape(
            (conv_features, n_e))

        # if the neurons in the network didn't spike more than four times
        if np.sum(current_spike_count) < 5 and num_retries < 3:
            # increase the intensity of input
            input_intensity += 2
            num_retries += 1

            # set all network firing rates to zero
            for name in ['X']:
                input_groups[name + 'e'].rate = 0

            # let the network relax back to equilibrium
            if not reset_state_vars:
                b.run(resting_time)
            else:
                for neuron_group in neuron_groups:
                    neuron_groups[neuron_group].v = v_reset_e
                    neuron_groups[neuron_group].ge = 0
                    neuron_groups[neuron_group].gi = 0

        # otherwise, record results and continue simulation
        else:
            if (j + 1) % 25 == 0:
                print '-', j + 1, '/', num_examples

            num_retries = 0

            # record the current number of spikes
            result_monitor[j % update_interval, :] = current_spike_count

            # get true label of the past input example
            input_numbers[j] = data['y'][j % data_size][0]

            # get the output classifications of the network
            for scheme, outputs in predict_label(
                    assignments,
                    result_monitor[j % update_interval, :]).items():
                output_numbers[scheme][j, :] = outputs

            activity = result_monitor[j % update_interval, :] / np.sum(
                result_monitor[j % update_interval, :])

            rates = (data['x'][j % data_size, :, :] / 8.0) * input_intensity

            if do_plot:
                fig = plt.figure(9, figsize=(8, 8))
                plt.imshow(rates.reshape((28, 28)),
                           interpolation='nearest',
                           vmin=0,
                           vmax=64,
                           cmap='binary')
                plt.title(
                    str(data['y'][j % data_size][0]) + ' : ' + ', '.join([
                        str(int(output_numbers[scheme][j, 0]))
                        for scheme in voting_schemes
                    ]))
                fig = plt.figure(10, figsize=(7, 7))
                plt.xticks(xrange(features_sqrt))
                plt.yticks(xrange(features_sqrt))
                plt.title('Activity heatmap (total spikes = ' +
                          str(np.sum(result_monitor[j % update_interval, :])) +
                          ')')
                plt.imshow(activity.reshape((features_sqrt, features_sqrt)).T,
                           interpolation='nearest',
                           cmap='binary')
                plt.grid(True)

                fig.canvas.draw()

                if sleep:
                    time.sleep(5.0)

            max_fired = np.argmax(result_monitor[j % update_interval, :])
            max_firing = np.max(result_monitor[j % update_interval, :])

            confusion_histogram[j, 0] = input_numbers[j]
            confusion_histogram[j, 1] = max_fired
            confusion_histogram[j, 2] = assignments[max_fired]
            confusion_histogram[j, 3] = max_firing

            # set input firing rates back to zero
            input_groups['Xe'].rate = 0

            # let the network relax back to equilibrium
            if not reset_state_vars:
                b.run(resting_time)
            else:
                for neuron_group in neuron_groups:
                    neuron_groups[neuron_group].v = v_reset_e
                    neuron_groups[neuron_group].ge = 0
                    neuron_groups[neuron_group].gi = 0

            # bookkeeping
            input_intensity = start_input_intensity
            j += 1

    np.save(
        os.path.join(confusion_histogram_dir,
                     '_'.join([ending, str(num_examples)])),
        confusion_histogram)
예제 #26
0
w = 17.4 * np.ones([6400, 6400]) - 17.4 * np.diag(np.ones(6400))
connections['Ai_Ae'].connect(groups['Ai'], groups['Ae'], w)

print('Creating Poisson input group.')
groups['X'] = b.PoissonGroup(784, 0)

print('Creating connection between input and excitatory layer.')
connections['X_Ae'] = b.Connection(groups['X'],
                                   groups['Ae'],
                                   state='ge',
                                   delay=True,
                                   max_delay=10 * b.ms)

w = 0.3 * np.random.rand(784, 6400)
connections['X_Ae'].connect(groups['X'], groups['Ae'], w)

stdp = {}
stdp['X_Ae'] = b.STDP(connections['X_Ae'],
                      eqs=eqs_stdp,
                      pre=eqs_stdp_pre,
                      post=eqs_stdp_post,
                      wmin=0,
                      wmax=1)

groups['X'].rate = 0.1 * np.random.rand(784)

print('Running simulation for 10^6 timesteps.')
start = default_timer()
b.run(1000000 * b.ms)
print('Time: %.4f' % (default_timer() - start))
S=Synapses(input,neurons,
           model=s_eq,
           pre='NMDAi+=w\nAMPAi+=w',post='',clock=simclock) # NMDA synapses
neurons.NMDAo=S.NMDAo
neurons.AMPAo=S.AMPAo
S[:,:]=True
S.w=[0.01,0.01]
S.delay=[0.003,0.005]
input.v=[0.,0.5]

M=StateMonitor(S,'NMDAo',record=True,clock=simclock)
M0 = StateMonitor(S,'NMDAi',record=True,clock=simclock)
M2=StateMonitor(S,'AMPAo',record=True,clock=simclock)
Mn=StateMonitor(neurons,'v',record=True,clock=simclock)

run(100*ms)
subplot(411)
plot(M0.times/ms,M0[0])
plot(M0.times/ms,M0[1])
subplot(412)
plot(M.times/ms,M[0])
plot(M.times/ms,M[1])
subplot(413)
plot(M2.times/ms,M2[0])
plot(M2.times/ms,M2[1])

subplot(414)
plot(Mn.times/ms,Mn[0])
show()