def SimSpikingStimulus(stim, time = 1000, t_sim = None, with_labels = True): ''' Times must be sorted. ex: times = [0, 1, 2] ; scale = [1,0] *poisson*: integer, output is a poisson process with mean data/poisson, scaled by *poisson*. ''' from pyNCSre import pyST n = np.shape(stim)[1] nc = 10 stim[stim<=0] = 1e-5 SL = pyST.SpikeList(id_list = range(n)) SLd = pyST.SpikeList(id_list = range(n-nc)) SLc = pyST.SpikeList(id_list = range(n-nc,n)) for i in range(n-nc): SLd[i] = pyST.STCreate.inh_poisson_generator(stim[:,i], range(0,len(stim)*time,time), t_stop=t_sim, refractory = 4.) if with_labels: for t in range(0,len(stim)): SLt= pyST.SpikeList(id_list = range(n-nc,n)) for i in range(n-nc,n): if stim[t,i]>1e-2: SLt[i] = pyST.STCreate.regular_generator(stim[t,i], jitter=True, t_start=t*time, t_stop=(t+1)*time) if len(SLt.raw_data())>0: SLc = pyST.merge_spikelists(SLc, SLt) if len(SLc.raw_data())>0: SL = pyST.merge_spikelists(SLd,SLc) else: SL = SLd return SL
def setup(): global SL print('Begin %s:setup()' % (os.path.splitext(os.path.basename(__file__))[0])) np.random.seed(100) # Numpy RNG seed pyST.STCreate.seed(130) # PyNCS RNG seed N_CORES = 1 # Number of cores N_NEURONS = [100] # Number of neurons per core N_INPUTS = [101] # Number of inputs per core N_STATES = [4] # Number of states pare core N_UNITS = N_INPUTS[0] + N_NEURONS[0] # Total units # Constants XMAX = nsat.XMAX XMIN = nsat.XMIN OFF = -16 MAX = nsat.MAX MIN = nsat.XMIN NLRN_GROUPS = 8 N_GROUPS = 8 # Main class instance cfg = nsat.ConfigurationNSAT(sim_ticks=sim_ticks, N_CORES=N_CORES, N_INPUTS=N_INPUTS, N_NEURONS=N_NEURONS, N_STATES=N_STATES, monitor_states=True, monitor_spikes=True, monitor_weights=True, w_check=False, plasticity_en=np.array([True], 'bool'), ben_clock=True) cfg.core_cfgs[core].sigma[0] = [0, 0, 10, 0] # Transition matrix group 0 cfg.core_cfgs[core].A[0] = [[-5, OFF, OFF, OFF], [3, -5, OFF, OFF], [OFF, OFF, -6, OFF], [OFF, OFF, OFF, OFF]] # Transition matrix group 1 cfg.core_cfgs[core].A[1] = [[OFF, OFF, OFF, OFF], [OFF, OFF, OFF, OFF], [OFF, OFF, OFF, OFF], [OFF, OFF, OFF, OFF]] # Sign matrix group 0 cfg.core_cfgs[core].sA[0] = [[-1, 1, 1, 1], [1, -1, 1, 1], [1, 1, -1, 1], [1, 1, 1, -1]] # Threshold cfg.core_cfgs[core].Xth[0] = 25000 # Refractory period cfg.core_cfgs[core].t_ref[0] = 40 # Bias cfg.core_cfgs[core].b[0] = [0, 0, 0, 0] # Initial conditions cfg.core_cfgs[core].Xinit = np.array([[0, 0, 0, 0] for _ in range(N_NEURONS[0])]) # Reset value cfg.core_cfgs[core].Xreset[0] = [0, MAX, MAX, MAX] # Turn reset on cfg.core_cfgs[core].XresetOn[0] = [True, False, False, False] # Turn plasticity per state on cfg.core_cfgs[core].plastic[0] = True # Turn stdp per state on cfg.core_cfgs[core].stdp_en[0] = True cfg.core_cfgs[core].is_stdp_exp_on[0] = True # Global modulator state cfg.core_cfgs[core].modstate[0] = 2 # Parameters groups mapping function cfg.core_cfgs[core].nmap = np.zeros((N_NEURONS[0], ), dtype='int') lrnmap = 1 + np.zeros((N_GROUPS, N_STATES[0]), dtype='int') lrnmap[0, 1] = 0 cfg.core_cfgs[core].lrnmap = lrnmap # Synaptic weights W = np.zeros([N_UNITS, N_UNITS, N_STATES[core]], 'int') W[0:100, N_INPUTS[core]:, 1] = np.eye(N_NEURONS[core]) * 100 W[100, N_INPUTS[core]:, 2] = 100 # Adjacent matrix CW = np.zeros(W.shape, dtype='int') CW[0:100, N_INPUTS[core]:, 1] = np.eye(N_NEURONS[core]) CW[100, N_INPUTS[core]:, 2] = 1 wgt_table, ptr_table = gen_ptr_wgt_table_from_W_CW(W, CW, []) np.set_printoptions(threshold=np.nan) cfg.core_cfgs[core].wgt_table = wgt_table cfg.core_cfgs[core].ptr_table = ptr_table # Learning STDP parameters cfg.core_cfgs[core].tca = [[24, 48] for _ in range(NLRN_GROUPS)] cfg.core_cfgs[core].hica = [[-3, -5, -6] for _ in range(NLRN_GROUPS)] cfg.core_cfgs[core].sica = [[1, 1, 1] for _ in range(NLRN_GROUPS)] cfg.core_cfgs[core].slca = [[16, 16, 16] for _ in range(NLRN_GROUPS)] cfg.core_cfgs[core].tac = [[-32, -64] for _ in range(NLRN_GROUPS)] cfg.core_cfgs[core].hiac = [[-6, -8, -9] for _ in range(NLRN_GROUPS)] cfg.core_cfgs[core].siac = [[-1, -1, -1] for _ in range(NLRN_GROUPS)] cfg.core_cfgs[core].slac = [[16, 16, 16] for _ in range(NLRN_GROUPS)] # Prepare external stimulus (spikes events) stim = [50] * N_NEURONS[core] SL = SimSpikingStimulus(stim, t_sim=sim_ticks) SLr = pyST.SpikeList(id_list=[100]) SLr[100] = pyST.STCreate.inh_poisson_generator(rate=np.array( [0., 100., 0.]), t=np.array([0, 400, 600]), t_stop=sim_ticks) SL = pyST.merge_spikelists(SL, SLr) ext_evts_data = nsat.exportAER(SL) cfg.set_ext_events(ext_evts_data) # Write C NSAT parameters binary file c_nsat_writer = nsat.C_NSATWriter(cfg, path='/tmp', prefix='test_rSTDP') c_nsat_writer.write() print('End %s:setup()' % (os.path.splitext(os.path.basename(__file__))[0]))
cfg.core_cfgs[core].hica = [[-3, -5, -6] for _ in range(NLRN_GROUPS)] cfg.core_cfgs[core].sica = [[1, 1, 1] for _ in range(NLRN_GROUPS)] cfg.core_cfgs[core].slca = [[16, 16, 16] for _ in range(NLRN_GROUPS)] cfg.core_cfgs[core].tac = [[-32, -64] for _ in range(NLRN_GROUPS)] cfg.core_cfgs[core].hiac = [[-6, -8, -9] for _ in range(NLRN_GROUPS)] cfg.core_cfgs[core].siac = [[-1, -1, -1] for _ in range(NLRN_GROUPS)] cfg.core_cfgs[core].slac = [[16, 16, 16] for _ in range(NLRN_GROUPS)] # Prepare external stimulus (spikes events) stim = [50]*N_NEURONS[core] SL = SimSpikingStimulus(stim, t_sim=sim_ticks) SLr = pyST.SpikeList(id_list=[100]) SLr[100] = pyST.STCreate.inh_poisson_generator(rate=np.array([0., 100., 0.]), t=np.array([0, 400, 600]), t_stop=sim_ticks) SL = pyST.merge_spikelists(SL, SLr) ext_evts_data = nsat.exportAER(SL) cfg.set_ext_events(ext_evts_data) # Write C NSAT parameters binary file c_nsat_writer = nsat.C_NSATWriter(cfg, path='/tmp', prefix='test_rSTDP') c_nsat_writer.write() # Write Intel FPGA parameters hex files # intel_fpga_writer = nsat.IntelFPGAWriter(cfg, path='.', # prefix='test_rSTDP') # intel_fpga_writer.write() # intel_fpga_writer.write_globals() # Call the C NSAT print("Running C NSAT!")