예제 #1
0
def RegularSpikingStimulus(freqs, ticks=1000):
    N_NEURONS = np.shape(freqs)[0]
    SL = pyST.SpikeList(id_list=list(range(N_NEURONS)))
    for i in range(N_NEURONS):
        f = freqs[i]
        if f > 0:
            SL[i] = pyST.STCreate.regular_generator(freqs[i], t_stop=ticks)
    return nsat.exportAER(SL)
예제 #2
0
def PoissonSpikingStimulus(rates, n_inputs=2):
    SL = pyST.SpikeList(id_list=list(range(n_inputs)))
    for i in range(n_inputs):
        # Tracking
        # if i > 0 and i < 20:
        #     t_start = 0
        #     t_stop = 500
        #     rate = 50
        # elif i > 20 and i < 40:
        #     t_start = 500
        #     t_stop = 1000
        #     rate = 50
        # elif i > 40 and i < 60:
        #     t_start = 1000
        #     t_stop = 1500
        #     rate = 50
        # elif i > 60 and i < 80:
        #     t_start = 1500
        #     t_stop = 2000
        #     rate = 50
        # elif i > 80 and i < 100:
        #     t_start = 2000
        #     t_stop = 2500
        #     rate = 50
        # else:
        #     continue
        # SL[i] = pyST.STCreate.poisson_generator(rate,
        #                                         t_start,
        #                                         t_stop)

        t_start = 100
        t_stop = 500
        rate = 1
        if (i > 40 and i < 60):
            t_start = 100
            t_stop = 500
            rate = 35
        if (i < 40 or i > 60):
            t_start = 100
            t_stop = 500
            rate = 10
        SL[i] = pyST.STCreate.poisson_generator(rate, t_start, t_stop)

        # Selection
        # if (i > 20 and i < 40) or (i > 70 and i < 90):
        #     t_start = 100
        #     t_stop = 500
        #     rate = 50
        #     SL[i] = pyST.STCreate.poisson_generator(rate,
        #                                             t_start,
        #                                             t_stop)
    return nsat.exportAER(SL), SL
예제 #3
0
    wgt_table, ptr_table = gen_ptr_wgt_table_from_W_CW(W, CW, [])
    np.set_printoptions(threshold=np.nan)
    cfg.core_cfgs[0].wgt_table = wgt_table
    cfg.core_cfgs[0].ptr_table = ptr_table

    # Generate spike events (external events)
    # rates = np.random.randint(5, 20, (N_INPUTS,), dtype='i')
    # rates = np.hstack([np.random.randint(5, 10, (N_INPUTS[0]//2,), 'int'),
    #                   np.random.randint(10, 20, (N_INPUTS[0]//2,), 'int')])

    # Build external spikes
    freqs = 5
    SL = PeriodicPrePostSpikingStimulus(freqs, -5, sim_ticks)
    import copy
    spk0 = copy.deepcopy(SL)
    ext_evts_data = nsat.exportAER(SL)
    cfg.set_ext_events(ext_evts_data)

    # Write the C NSAT parameters binary files
    c_nsat_writer = nsat.C_NSATWriter(cfg, path='/tmp', prefix='test_stdp')
    c_nsat_writer.write()

    # Write Intel FPGA hex parameters files
    #    intel_fpga_writer = nsat.IntelFPGAWriter(cfg, path='.',
    #                                             prefix='test_stdp')
    #    intel_fpga_writer.write()
    #    intel_fpga_writer.write_globals()

    # Call the C NSAT
    print("Running C NSAT!")
    nsat.run_c_nsat(c_nsat_writer.fname)
예제 #4
0
        cont = f.read()
    size = int(len(cont) / 4)
    return np.array(st.unpack('i' * size, cont))


if __name__ == '__main__':
    cfg = pyNSATlib.ConfigurationNSAT(N_CORES=2,
                                      N_INPUTS=[10, 10],
                                      N_NEURONS=[512, 100],
                                      N_STATES=[4, 2],
                                      bm_rng=True,
                                      ben_clock=True)

    cfg.core_cfgs[0].W[:cfg.core_cfgs[0].n_inputs,
                       cfg.core_cfgs[0].n_inputs:] = 1
    cfg.core_cfgs[0].CW[:cfg.core_cfgs[0].n_inputs,
                        cfg.core_cfgs[0].n_inputs:] = 1
    cfg.core_cfgs[1].W[:cfg.core_cfgs[1].n_inputs,
                       cfg.core_cfgs[1].n_inputs:] = 1
    cfg.core_cfgs[1].CW[:cfg.core_cfgs[1].n_inputs,
                        cfg.core_cfgs[1].n_inputs:] = 1
    cfg.set_L1_connectivity({(0, 1): ((1, 0), (1, 1))})

    SL1 = pyNSATlib.build_SpikeList(evs_time=[1, 2, 3], evs_addr=[5, 6, 7])
    SL2 = pyNSATlib.build_SpikeList(evs_time=[2, 5, 1], evs_addr=[3, 9, 5])
    evs = pyNSATlib.exportAER([SL1, SL2])
    cfg.set_ext_events(evs)

    c_nsat_writer = C_NSATWriter(cfg, path='/tmp/', prefix='test')
    c_nsat_writer.write()
예제 #5
0
def setup():
    print('Begin %s:setup()' %
          (os.path.splitext(os.path.basename(__file__))[0]))

    pyST.STCreate.seed(100)
    N_CORES = 1  # Number of cores
    N_NEURONS = [17]  # Number of neurons per core
    N_INPUTS = [16]  # Number of inputes per core
    N_STATES = [8]  # Number of states per core
    N_UNITS = N_INPUTS[0] + N_NEURONS[0]  # Total number of units

    # Constants
    XMAX = nsat.XMAX
    XMIN = nsat.XMIN
    OFF = -16
    MAX = nsat.MAX
    MIN = nsat.XMIN

    # 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,
                                 ben_clock=True)

    # Transition matrix group 0
    cfg.core_cfgs[0].A[0] = [[-3, OFF, OFF, OFF, OFF, OFF, OFF, OFF],
                             [2, -5, OFF, OFF, OFF, OFF, OFF, OFF],
                             [OFF, OFF, -7, -5, OFF, OFF, OFF, OFF],
                             [OFF, OFF, OFF, 0, OFF, OFF, OFF, OFF],
                             [OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF],
                             [OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF],
                             [OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF],
                             [OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF]]

    # Sign matrix group 0
    cfg.core_cfgs[0].sA[0] = [[-1, 1, 1, 1, 1, 1, 1, 1],
                              [1, -1, 1, 1, 1, 1, 1, 1],
                              [1, 1, -1, -1, 1, 1, 1, 1],
                              [1, 1, 1, -1, 1, 1, 1, 1],
                              [1, 1, 1, 1, 1, 1, 1,
                               1], [1, 1, 1, 1, 1, 1, 1, 1],
                              [1, 1, 1, 1, 1, 1, 1, 1],
                              [1, 1, 1, 1, 1, 1, 1, 1]]

    # Transition matrix group 1
    cfg.core_cfgs[0].A[1] = [[0, OFF, OFF, OFF, OFF, OFF, OFF, OFF],
                             [0, -10, OFF, OFF, OFF, OFF, OFF, OFF],
                             [0, OFF, -8, OFF, OFF, OFF, OFF, OFF],
                             [OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF],
                             [OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF],
                             [OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF],
                             [OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF],
                             [OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF]]

    # Sign matrix group 1
    cfg.core_cfgs[0].sA[1] = [[-1, 1, 1, 1, 1, 1, 1, 1],
                              [+1, -1, 1, 1, 1, 1, 1, 1],
                              [-1, 1, -1, 1, 1, 1, 1, 1],
                              [1, 1, 1, 1, 1, 1, 1,
                               1], [1, 1, 1, 1, 1, 1, 1, 1],
                              [1, 1, 1, 1, 1, 1, 1,
                               1], [1, 1, 1, 1, 1, 1, 1, 1],
                              [1, 1, 1, 1, 1, 1, 1, 1]]

    # Transition matrix group 2
    cfg.core_cfgs[0].A[2] = [[0, OFF, OFF, OFF, OFF, OFF, OFF, OFF],
                             [-3, OFF, OFF, OFF, OFF, OFF, OFF, OFF],
                             [OFF, -13, -6, OFF, OFF, OFF, OFF, OFF],
                             [OFF, -15, OFF, -8, OFF, OFF, OFF, OFF],
                             [0, OFF, OFF, OFF, OFF, OFF, OFF, OFF],
                             [OFF, OFF, OFF, OFF, -7, -8, OFF, OFF],
                             [OFF, OFF, OFF, OFF, -7, OFF, -3, OFF],
                             [OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF]]

    # Sign matrix group 2
    cfg.core_cfgs[0].sA[2] = [[-1, 1, 1, 1, 1, 1, 1, 1],
                              [1, 1, 1, 1, 1, 1, 1, 1],
                              [1, 1, -1, 1, 1, 1, 1, 1],
                              [1, -1, 1, -1, 1, 1, 1, 1],
                              [1, 1, 1, 1, 1, 1, 1, 1],
                              [1, 1, 1, 1, 1, -1, 1, 1],
                              [1, 1, 1, 1, -1, 1, -1, 1],
                              [1, 1, 1, 1, 1, 1, 1, 1]]

    # Bias
    cfg.core_cfgs[0].b[0] = np.array([-12000, 0, -7, 0, 0, 0, 0, 0], 'int')
    cfg.core_cfgs[0].b[1] = np.array([-40, 0, 0, 0, 0, 0, 0, 0], dtype='int')
    cfg.core_cfgs[0].b[2] = np.array([-40, 0, 0, 0, 0, 0, 0, 0], dtype='int')
    # Threshold
    cfg.core_cfgs[0].t_ref[0] = 0
    # cfg.core_cfgs[0].Xth[0] = 30
    # Spike increment value
    cfg.core_cfgs[0].XspikeIncrVal[1] = np.array([-1000] + [0] * 7, 'int')
    # Additive noise variance
    cfg.core_cfgs[0].sigma[0] = np.array([15000] + [0] * 7, 'int')
    # refractory period
    cfg.core_cfgs[0].t_ref[0] = 0
    # Reset value
    cfg.core_cfgs[0].Xreset[0] = np.array([0] + [XMAX] * 7, 'int')

    # Turn reset on
    cfg.core_cfgs[0].XresetOn[0] = np.array([True] + [False] * 7, 'bool')
    cfg.core_cfgs[0].XresetOn[1] = np.array([False] * 8, 'bool')
    cfg.core_cfgs[0].XresetOn[2] = np.array([False] * 8, 'bool')

    # Mapping function between neurons and NSAT parameters groups
    cfg.core_cfgs[0].nmap = np.zeros((N_NEURONS[0], ), dtype='int')
    cfg.core_cfgs[0].nmap[-2] = 1
    cfg.core_cfgs[0].nmap[-1] = 2

    # Synaptic strength
    from scipy.linalg import toeplitz
    col = np.zeros((N_INPUTS[0] - 1, ))
    col[0] = 1
    row = np.zeros((N_NEURONS[0] - 2, ))
    row[0:3] = np.array([1, 2, 1])
    T = toeplitz(col, row)
    W = np.zeros((N_UNITS, N_UNITS, N_STATES[0]), 'i')
    W[:N_INPUTS[0] - 1, N_INPUTS[0]:-2, 1] = T
    W[N_INPUTS[0]:, N_UNITS - 2, 1] = 100
    W[N_INPUTS[0]:, N_UNITS - 2, 2] = 100
    W[N_INPUTS[0]:, N_UNITS - 1, 2] = 1
    W[N_INPUTS[0]:, N_UNITS - 1, 3] = 1
    W[N_INPUTS[0] - 1, N_UNITS - 1, 5] = 100
    W[N_INPUTS[0] - 1, N_UNITS - 1, 6] = 100

    CW = W.astype('bool')

    # np.set_printoptions(threshold=np.nan)
    wgt_table, ptr_table = gen_ptr_wgt_table_from_W_CW(W, CW, [])
    np.set_printoptions(threshold=np.nan)
    cfg.core_cfgs[0].wgt_table = wgt_table
    cfg.core_cfgs[0].ptr_table = ptr_table

    # Generate external events firing rates from 5 to 50 inc by 5
    freqs = np.random.randint(300, 400, (N_INPUTS[0], ))
    SL = RegularSpikingStimulus(freqs, sim_ticks)
    # SL.raster_plot()
    ext_evts_data = nsat.exportAER(SL)

    # Set external events
    cfg.set_ext_events(ext_evts_data)

    # Write C NSAT parameters binary files
    c_nsat_writer = nsat.C_NSATWriter(cfg, path='/tmp', prefix='test_td')
    c_nsat_writer.write()

    print('End %s:setup()' % (os.path.splitext(os.path.basename(__file__))[0]))
예제 #6
0
def setup():
    global SL
    print('Begin %s:setup()' %
          (os.path.splitext(os.path.basename(__file__))[0]))

    N_CORES = 1  # Number of cores
    N_test = 2  # Number of tests
    Nv = 100  # Visible neurons
    Nh = 100  # Hidden neurons
    N_NEURONS = [Nh]  # Total number of inputes per core
    N_INPUTS = [Nv]  # Total number of inputs per core
    N_STATES = [4]  # Number of states per core
    N_UNITS = N_INPUTS[0] + N_NEURONS[0]  # Total number of units

    # Constants
    XMAX = nsat.XMAX
    XMIN = nsat.XMIN
    OFF = -16
    MAX = nsat.MAX
    MIN = nsat.XMIN

    # 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,
                                 ben_clock=True)

    # Transition matrix
    cfg.core_cfgs[0].A[0] = [[-3, OFF, OFF, OFF], [2, -5, OFF, OFF],
                             [OFF, OFF, -7, -5], [OFF, OFF, OFF, 0]]

    # Sign matrix
    cfg.core_cfgs[0].sA[0] = [[-1, 1, 1, 1], [1, -1, 1, 1], [1, 1, -1, -1],
                              [1, 1, 1, -1]]

    # Bias
    cfg.core_cfgs[0].b[0] = [-12000, 0, -7, 0]
    # Refractory period
    cfg.core_cfgs[0].t_ref[0] = 40
    # Initial conditions
    cfg.core_cfgs[0].Xinit[0] = np.zeros((N_STATES[0], ), 'int')
    # Reset value
    cfg.core_cfgs[0].Xreset[0] = np.array([0, MAX, MAX, MAX], 'int')
    # Turn on reset
    cfg.core_cfgs[0].XresetOn[0] = np.array([True, False, False, False],
                                            'bool')

    # Spike increment value
    cfg.core_cfgs[0].XspikeIncrVal[1] = np.array([-1000, 0, 0, 0], 'int')
    # Additive noise variance
    cfg.core_cfgs[0].sigma[0] = np.array([15000, 0, 0, 0], 'int')

    # Set parameters
    cfg.core_cfgs[0].nmap = np.zeros((N_NEURONS[0], ), dtype='int')

    # Set weight matrix
    extW = np.zeros([N_INPUTS[0], N_NEURONS[0], N_STATES[0]])
    extCW = np.zeros([N_INPUTS[0], N_NEURONS[0], N_STATES[0]])
    extW[:Nv, :Nh, 1] = np.eye(N_INPUTS[0]) * 127
    extCW[:Nv, :Nh, 1] = np.eye(N_INPUTS[0]).astype('bool')

    W = np.zeros([N_UNITS, N_UNITS, N_STATES[0]], 'int')
    W[:N_INPUTS[0], N_INPUTS[0]:] = extW

    # Set adjacent matrix
    CW = np.zeros([N_UNITS, N_UNITS, N_STATES[0]], 'int')
    CW[:N_INPUTS[0], N_INPUTS[0]:] = extCW

    wgt_table, ptr_table = gen_ptr_wgt_table_from_W_CW(W, CW, [])
    np.set_printoptions(threshold=np.nan)
    cfg.core_cfgs[0].wgt_table = wgt_table
    cfg.core_cfgs[0].ptr_table = ptr_table

    # Set external events
    stim = np.linspace(1, 1000, N_NEURONS[0])
    ext_evts_data = nsat.exportAER(
        SimSpikingStimulus(stim, sim_ticks, t_sim=sim_ticks))
    cfg.set_ext_events(ext_evts_data)

    # Write C NSAT parameters binary files
    c_nsat_writer = nsat.C_NSATWriter(cfg,
                                      path='/tmp',
                                      prefix='test_sigmoid_ext')
    c_nsat_writer.write()

    print('End %s:setup()' % (os.path.splitext(os.path.basename(__file__))[0]))
    return c_nsat_writer.fname
예제 #7
0
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]))
예제 #8
0
        10000, with_labels = False)

np.random.seed(100)

###########################################################################
print("###################### Train Stimulus Creation ##################################")
sim_ticks = N_train*t_sample_train
idx = range(len(data_train))
np.random.shuffle(idx)
idx = idx[:N_train]
data_train = np.concatenate([data_train[idx,:] for _ in range(n_mult)])

stim = np.zeros([N_train*n_mult, N_INPUTS])
stim[:,:data_train.shape[1]] = data_train
SL_train = SimSpikingStimulus(inp_fact*stim, t_sample_train, t_sim = sim_ticks)
ext_evts_data = nsat.exportAER(SL_train)
#
#print("###################### Test Stimulus Creation ##################################")
stim_test = np.zeros([N_test, N_INPUTS])
stim_test[:N_test,:data_classify.shape[1]] = data_classify[:N_test,:]
sim_ticks_test = len(stim_test)*t_sample_test
SL_test = SimSpikingStimulus(inp_fact*stim_test, t_sample_test, t_sim = sim_ticks_test)
ext_evts_data_test = nsat.exportAER(SL_test)
#print("################################################################################")

print("################## Setting Weight CONF #########################################")
Wvh = np.random.uniform(low=-initv, high=initv,size=[Nv, Nh]).astype('int')
Whp = np.random.uniform(low=-inith, high=inith,size=[Nh, Np]).astype('int')

#Create matrix whose rows sum to 0
Wgg1 = np.zeros([Ng2, Nh], dtype='int')
예제 #9
0
def setup():
    print('Begin %s:setup()' %
          (os.path.splitext(os.path.basename(__file__))[0]))

    N_CORES = 1  # Number of cores
    N_NEURONS = [4]  # Number of neurons per core (list)
    N_INPUTS = [4]  # Number of inputs per core (list)
    N_STATES = [4]  # Number of states per core (list)
    N_UNITS = N_INPUTS[0] + N_NEURONS[0]  # Total inputs

    # Constants
    XMAX = nsat.XMAX
    XMIN = nsat.XMIN
    OFF = -16
    MAX = nsat.MAX
    MIN = nsat.XMIN

    # 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,
                                 tstdpmax=[64],
                                 monitor_states=True,
                                 monitor_spikes=True,
                                 monitor_weights=True,
                                 monitor_weights_final=True,
                                 plasticity_en=[True],
                                 ben_clock=True)

    # Transition matrix
    cfg.core_cfgs[0].A[0] = [[-1, OFF, OFF, OFF], [0, -1, OFF, OFF],
                             [OFF, OFF, 0, OFF], [OFF, OFF, OFF, OFF]]
    cfg.core_cfgs[0].A[1] = cfg.core_cfgs[0].A[0].copy()

    # Sign matrix
    cfg.core_cfgs[0].sA[0] = [[-1, 1, 1, 1], [1, -1, 1, 1], [1, 1, -1, 1],
                              [1, 1, 1, 1]]
    cfg.core_cfgs[0].sA[1] = cfg.core_cfgs[0].sA[0].copy()

    # Refractory period
    cfg.core_cfgs[0].t_ref[0] = 0
    cfg.core_cfgs[0].t_ref[1] = 2
    # Threshold
    cfg.core_cfgs[0].Xth[0] = 100
    cfg.core_cfgs[0].Xth[1] = 100
    # Bias
    cfg.core_cfgs[0].b[0] = [30, 0, 5, 0]
    cfg.core_cfgs[0].b[1] = [30, 0, 5, 0]
    # Initial conditions
    cfg.core_cfgs[0].Xinit[0] = np.array([0, 0, 0, 0], 'int')
    cfg.core_cfgs[0].Xinit[1] = np.array([0, 0, 0, 0], 'int')
    # Reset value
    cfg.core_cfgs[0].Xreset[0] = [0, MAX, MAX, MAX]
    cfg.core_cfgs[0].Xreset[1] = [0, MAX, MAX, MAX]
    # Turn reset on
    cfg.core_cfgs[0].XresetOn[0] = [True, False, False, False]
    cfg.core_cfgs[0].XresetOn[1] = [True, False, False, False]

    # Enable plasticity at states
    cfg.core_cfgs[0].plastic[0] = True
    cfg.core_cfgs[0].plastic[1] = False
    # Enable STDP
    cfg.core_cfgs[0].stdp_en[0] = True
    cfg.core_cfgs[0].stdp_en[1] = False
    # Global modulator state
    cfg.core_cfgs[0].modstate[0] = 2
    cfg.core_cfgs[0].modstate[1] = 2

    # Parameters for the STDP kernel function
    rNLRN_GROUPS = list(range(8))
    cfg.core_cfgs[0].tstdp = np.array([64 for _ in rNLRN_GROUPS], 'int')
    cfg.core_cfgs[0].tca = np.array([[16, 36] for _ in rNLRN_GROUPS], 'int')
    cfg.core_cfgs[0].hica = np.array([[1, 0, -1] for _ in rNLRN_GROUPS], 'int')
    cfg.core_cfgs[0].sica = np.array([[1, 1, 1] for _ in rNLRN_GROUPS], 'int')
    cfg.core_cfgs[0].tac = np.array([[-16, -36] for _ in rNLRN_GROUPS], 'int')
    cfg.core_cfgs[0].hiac = np.array([[1, 0, -1] for _ in rNLRN_GROUPS], 'int')
    cfg.core_cfgs[0].siac = np.array([[-1, -1, -1] for _ in rNLRN_GROUPS],
                                     'int')

    # Set parameters mapping
    cfg.core_cfgs[0].nmap = np.zeros((N_NEURONS[0], ), dtype='int')
    cfg.core_cfgs[0].nmap[0] = 0
    cfg.core_cfgs[0].nmap[1] = 1
    cfg.core_cfgs[0].nmap[2] = 1
    cfg.core_cfgs[0].nmap[3] = 1

    # Learning parameters groups mapping
    lrnmap = np.zeros((nsat.N_GROUPS, N_STATES[0]), dtype='int')
    lrnmap[0, :] = 1
    lrnmap[0, 1] = 0
    lrnmap[1, 2] = 0
    lrnmap[1, 0] = 0
    # lrnmap[:] = 0
    cfg.core_cfgs[0].lrnmap = lrnmap

    # Synaptic weights
    W = np.zeros([N_UNITS, N_UNITS, N_STATES[0]], 'int')
    W[0, 4, 1] = 50
    W[1, 5, 1] = 50
    W[2, 6, 1] = 50
    W[3, 7, 1] = 50

    W[4, 5, 1] = 5
    W[4, 6, 1] = 5
    W[4, 7, 1] = 5

    # Adjacent matrix
    CW = W.astype('bool')

    wgt_table, ptr_table = gen_ptr_wgt_table_from_W_CW(W, CW, [])
    np.set_printoptions(threshold=np.nan)
    cfg.core_cfgs[0].wgt_table = wgt_table
    cfg.core_cfgs[0].ptr_table = ptr_table

    # Generate spike events (external events)
    # spk_train = np.array([0, 1, 2, 50, 60, 70,  100, 110, 120])
    SL = SimSpikingStimulus(t_sim=sim_ticks)
    ext_evts_data = nsat.exportAER(SL)
    cfg.set_ext_events(ext_evts_data)

    # Write the C NSAT parameters binary files
    c_nsat_writer = nsat.C_NSATWriter(cfg, path='/tmp', prefix='test_stdp')
    c_nsat_writer.write()

    print('End %s:setup()' % (os.path.splitext(os.path.basename(__file__))[0]))
    return c_nsat_writer.fname
예제 #10
0
def erbp_mlp_2L_multicore(data_train, data_classify, targets_classify,
                          nepochs=10):
    exp_name = '/tmp/mnist_mlp_2L'
    exp_name_test = '/tmp/mnist_mlp_2L_test/'

    inputsize = 28
    Nchannel = 1
    Nxy = inputsize*inputsize
    Nv = Nxy*Nchannel
    Nl = 10
    Nh = 100

    t_sample_test = 3000
    t_sample_train = 1500
    N_train = 500
    N_test = 100
    test_every = 1
    inp_fact = 25

    sim_ticks = N_train*t_sample_train
    sim_ticks_test = N_test*t_sample_test

    np.random.seed(100)

    wpg = 96
    wgp = 37

    net_graph = LogicalGraphSetup()

    pop_data = net_graph.create_population(Population(name='data',
                                                      n=Nv,
                                                      core=-1,
                                                      is_external=True))
    pop_lab = net_graph.create_population(Population(name='lab',
                                                     n=Nl,
                                                     core=-1,
                                                     is_external=True))
    pop_hid1 = net_graph.create_population(Population(name='hid1',
                                                      n=Nh,
                                                      core=0,
                                                      neuron_cfg=erf_ntype))
    pop_hid2 = net_graph.create_population(Population(name='hid2',
                                                      n=Nh,
                                                      core=1,
                                                      neuron_cfg=erf_ntype))
    pop_out = net_graph.create_population(Population(name='out',
                                                     n=Nl,
                                                     core=0,
                                                     neuron_cfg=output_ntype))
    pop_err_pos = net_graph.create_population(Population(name='err_pos',
                                                         n=Nl,
                                                         core=0,
                                                         neuron_cfg=error_ntype))
    pop_err_neg = net_graph.create_population(Population(name='err_neg',
                                                         n=Nl,
                                                         core=0,
                                                         neuron_cfg=error_ntype))

    net_graph.create_connection(pop_data, pop_hid1, 0,
                                connect_random_uniform(low=-16, high=16))
    net_graph.create_connection(pop_hid1, pop_hid2, 0,
                                connect_random_uniform(low=-16, high=16))
    net_graph.create_connection(pop_hid2, pop_out,  0,
                                connect_random_uniform(low=-4, high=4))

    net_graph.create_connection(pop_out, pop_err_pos, 0, connect_one2one(-wpg))
    net_graph.create_connection(pop_out, pop_err_neg, 0, connect_one2one(wpg))

    net_graph.create_connection(pop_lab, pop_err_pos, 0, connect_one2one(wpg))
    net_graph.create_connection(pop_lab, pop_err_neg, 0, connect_one2one(-wpg))

    p, w = connect_shuffle(3000)(pop_err_pos, pop_hid1)

    net_graph.create_connection(pop_err_pos, pop_hid1, 1, [p,  w])
    net_graph.create_connection(pop_err_neg, pop_hid1, 1, [p, -w])

    p, w = connect_shuffle(3000)(pop_err_pos, pop_hid2)

    net_graph.create_connection(pop_err_pos, pop_hid2, 1, [p,  w])
    net_graph.create_connection(pop_err_neg, pop_hid2, 1, [p, -w])

    net_graph.create_connection(pop_err_pos, pop_out, 1, connect_one2one(wgp))
    net_graph.create_connection(pop_err_neg, pop_out, 1, connect_one2one(-wgp))

    setup = net_graph.generate_multicore_setup(NSATSetup)

    spk_rec_mon = [[] for i in range(setup.ncores)]
    spk_rec_mon[pop_out.core] = pop_out.addr

    cfg_train = setup.create_configuration_nsat(sim_ticks=sim_ticks,
                                                w_check=False,
                                                spk_rec_mon=spk_rec_mon,
                                                monitor_spikes=True,
                                                gated_learning=True,
                                                plasticity_en=True)

    spk_rec_mon = [[] for i in range(setup.ncores)]
    spk_rec_mon[pop_out.core] = pop_out.addr

    cfg_test = cfg_train.copy()
    cfg_test.sim_ticks = sim_ticks_test
    cfg_test.plasticity_en[:] = False
    cfg_test.spk_rec_mon = spk_rec_mon
    cfg_test.monitor_spikes = True

    SL_train = create_spike_train(data_train[:N_train],
                                  t_sample_train,
                                  scaling=inp_fact,
                                  with_labels=True)
    ext_evts_data_train = nsat.exportAER(SL_train)

    SL_test = create_spike_train(data_classify[:N_test],
                                 t_sample_test,
                                 scaling=inp_fact,
                                 with_labels=False)
    ext_evts_data_test = nsat.exportAER(SL_test)

    cfg_test.set_ext_events(ext_evts_data_test)
    cfg_train.set_ext_events(ext_evts_data_train)

    c_nsat_writer_train = nsat.C_NSATWriter(cfg_train,
                                            path=exp_name,
                                            prefix='')
    c_nsat_writer_train.write()

    c_nsat_writer_test = nsat.C_NSATWriter(cfg_test,
                                           path=exp_name_test,
                                           prefix='')
    c_nsat_writer_test.write()

    fname_train = c_nsat_writer_train.fname
    fname_test = c_nsat_writer_test.fname

    c_nsat_reader_test = nsat.C_NSATReader(cfg_test, fname_test)

    pip, tt = [], []
    tft, t0t = 0, 0
    for i in range(nepochs):
        t0 = time.time()
        nsat.run_c_nsat(fname_train)
        tf = time.time()

        for j in range(setup.ncores):
            # train->test
            shutil.copy(exp_name+'/_shared_mem_core_{0}.dat'.format(
                j), exp_name_test+'/_wgt_table_core_{0}.dat'.format(j))
            # train->train
            shutil.copy(exp_name+'/_shared_mem_core_{0}.dat'.format(
                j), exp_name+'/_wgt_table_core_{0}.dat'.format(j))
        if test_every > 0:
            if i % test_every == test_every-1:
                t0t = time.time()
                nsat.run_c_nsat(fname_test)
                tft = time.time()
                acc, slout = test_accuracy(c_nsat_reader_test,
                                           targets=targets_classify[:N_test],
                                           pop=pop_out,
                                           sim_ticks=sim_ticks_test,
                                           duration=t_sample_test)
                pip.append(acc)
        t_total = (tf - t0) + (tft - t0t)
        tt.append(t_total)
    return pip, tt
예제 #11
0
def setup():
    global SL
    print('Begin %s:setup()' %
          (os.path.splitext(os.path.basename(__file__))[0]))

    N_CORES = 1  # Number of cores
    N_NEURONS = [1]  # Number of neurons per core (list)
    N_INPUTS = [1000]  # Number of inputs per core (list)
    N_STATES = [4]  # Number of states per core (list)
    N_UNITS = N_INPUTS[0] + N_NEURONS[0]  # Total inputs

    # Constants
    XMAX = nsat.XMAX
    XMIN = nsat.XMIN
    OFF = -16
    MAX = nsat.MAX
    MIN = nsat.XMIN

    # 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,
                                 w_check=False,
                                 tstdpmax=[100],
                                 monitor_weights_final=True,
                                 plasticity_en=[True],
                                 ben_clock=True)

    # Transition matrix
    cfg.core_cfgs[0].A[0] = [[-1, OFF, OFF, OFF], [0, -2, OFF, OFF],
                             [OFF, OFF, 0, OFF], [OFF, OFF, OFF, OFF]]

    # Sign matrix
    cfg.core_cfgs[0].sA[0] = [[-1, 1, 1, 1], [1, -1, 1, 1], [1, 1, -1, 1],
                              [1, 1, 1, -1]]

    # Refractory period
    cfg.core_cfgs[0].t_ref[0] = 20
    # Threshold
    cfg.core_cfgs[0].Xth[0] = 25
    # Bias
    cfg.core_cfgs[0].b[0] = [0, 0, 1, 0]
    # Initial conditions
    cfg.core_cfgs[0].Xinit[0] = np.array([0, 0, 0, 0], 'int')
    # Reset value
    cfg.core_cfgs[0].Xreset[0] = [0, MAX, MAX, MAX]
    # Turn reset on
    cfg.core_cfgs[0].XresetOn[0] = [True, False, False, False]

    # Enable plasticity at states
    cfg.core_cfgs[0].plastic[0] = True
    # Enable STDP
    cfg.core_cfgs[0].stdp_en[0] = True
    # Global modulator state
    cfg.core_cfgs[0].modstate[0] = 2

    # Parameters for the STDP kernel function
    # cfg.core_cfgs[0].tstdp = np.array([32 for _ in rNLRN_GROUPS], 'int')
    # cfg.core_cfgs[0].tca = np.array([[6, 15] for _ in rNLRN_GROUPS], 'int')
    # cfg.core_cfgs[0].hica = np.array([[2, 1, 0] for _ in rNLRN_GROUPS], 'int')
    # cfg.core_cfgs[0].sica = np.array([[1, 1, 1] for _ in rNLRN_GROUPS], 'int')
    # cfg.core_cfgs[0].tac = np.array([[6, 15] for _ in rNLRN_GROUPS], 'int')
    # cfg.core_cfgs[0].hiac = np.array([[-16, -16, 16] for _ in rNLRN_GROUPS],
    #                                  'int')
    # cfg.core_cfgs[0].siac = np.array([[-1, -1, -1] for _ in rNLRN_GROUPS],
    #                                  'int')

    # Set parameters mapping
    cfg.core_cfgs[0].nmap = np.zeros((N_NEURONS[0], ), dtype='int')

    # Synaptic weights
    W = np.zeros([N_UNITS, N_UNITS, N_STATES[0]], 'int')
    W[:N_INPUTS[0], N_INPUTS[0], 1] = 50

    # Adjacent matrix
    CW = np.zeros(W.shape, dtype='int')
    CW[:N_INPUTS[0], N_INPUTS[0], 1] = True

    wgt_table, ptr_table = gen_ptr_wgt_table_from_W_CW(W, CW, [])
    np.set_printoptions(threshold=np.nan)
    cfg.core_cfgs[0].wgt_table = wgt_table
    cfg.core_cfgs[0].ptr_table = ptr_table

    # Generate spike events (external events)
    # rates = np.random.randint(5, 20, (N_INPUTS,), dtype='i')
    rates = np.hstack([
        np.random.randint(5, 10, (N_INPUTS[0] // 2, ), 'int'),
        np.random.randint(10, 20, (N_INPUTS[0] // 2, ), 'int')
    ])
    SL = SimSpikingStimulus(rates, t_sim=sim_ticks)
    ext_evts_data = nsat.exportAER(SL)
    cfg.set_ext_events(ext_evts_data)

    # Write the C NSAT parameters binary files
    c_nsat_writer = nsat.C_NSATWriter(cfg, path='/tmp', prefix='test_stdp')
    c_nsat_writer.write()

    print('End %s:setup()' % (os.path.splitext(os.path.basename(__file__))[0]))
    return c_nsat_writer.fname
예제 #12
0
파일: test_wta.py 프로젝트: nmi-lab/NSAT
def setup():
    global SL, t_start, t_stop
    print('Begin %s:setup()' %
          (os.path.splitext(os.path.basename(__file__))[0]))

    N_CORES = 1  # Number of cores
    N_NEURONS = [4]  # Number of neurons per core
    N_INPUTS = [3]  # Number of inputs per core
    N_STATES = [4]  # Number of states per core
    N_UNITS = N_INPUTS[0] + N_NEURONS[0]  # Total number of units

    # Constants
    XMAX = nsat.XMAX
    XMIN = nsat.XMIN
    OFF = -16
    MAX = nsat.MAX
    MIN = nsat.XMIN

    # Configuration 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,
                                 ben_clock=True)

    # Transition matrix group 0
    cfg.core_cfgs[0].A[0] = [[-7, OFF, OFF, OFF], [0, -5, OFF, OFF],
                             [OFF, OFF, OFF, OFF], [OFF, OFF, OFF, OFF]]

    # Transition matrix group 1
    cfg.core_cfgs[0].A[1] = [[-7, OFF, OFF, OFF], [0, -7, OFF, OFF],
                             [OFF, OFF, OFF, OFF], [OFF, OFF, OFF, OFF]]

    # Sign matrix group 0
    cfg.core_cfgs[0].sA[0] = [[-1, 1, 1, 1], [+1, -1, 1, 1], [1, 1, 1, 1],
                              [1, 1, 1, 1]]

    # Sign matrix group 1
    cfg.core_cfgs[0].sA[1] = cfg.core_cfgs[0].sA[0].copy()

    # Refractory period group 0
    cfg.core_cfgs[0].t_ref[0] = 40
    # Refractory period group 1
    cfg.core_cfgs[0].t_ref[1] = 30
    # Bias group 0
    cfg.core_cfgs[0].b[0] = [0, 0, 0, 0]
    # Bias group 1
    cfg.core_cfgs[0].b[1] = [0, 0, 0, 0]
    # Threshold group 0
    cfg.core_cfgs[0].Xth[0] = 100
    # Threshold group 1
    cfg.core_cfgs[0].Xth[1] = 80
    # Initial conditions
    cfg.core_cfgs[0].Xinit[0] = np.array([0, 0, 0, 0], 'int')
    # Reset value group 0
    cfg.core_cfgs[0].Xreset[0] = [0, MAX, MAX, MAX]
    # Reset value group 1
    cfg.core_cfgs[0].Xreset[1] = cfg.core_cfgs[0].Xreset[0].copy()
    # Turn reset on group 0
    cfg.core_cfgs[0].XresetOn[0] = np.array([True, False, False, False],
                                            'bool')
    # Turn reset on group 1
    cfg.core_cfgs[0].XresetOn[1] = np.array([True, False, False, False],
                                            'bool')

    # Global modulator state group 0
    cfg.core_cfgs[0].modstate[0] = 3
    # Global modulator state group 0
    cfg.core_cfgs[0].modstate[1] = 3

    # Parameters groups mapping function
    nmap = np.array([0, 0, 0, 1], dtype='int')
    cfg.core_cfgs[0].nmap = nmap

    # Synaptic weights and adjacent matrix
    W, CW = build_synaptic_w(N_INPUTS[0], N_NEURONS[0], N_STATES[0])

    wgt_table, ptr_table = gen_ptr_wgt_table_from_W_CW(W, CW, [])
    np.set_printoptions(threshold=np.nan)
    cfg.core_cfgs[0].wgt_table = wgt_table
    cfg.core_cfgs[0].ptr_table = ptr_table

    # Set external events
    rates = [60, 30, 30]
    SL = SimSpikingStimulus(rates, t_start, t_stop)
    ext_evts_data = nsat.exportAER(SL)
    cfg.set_ext_events(ext_evts_data)

    # Write C NSAT parameters binary files
    c_nsat_writer = nsat.C_NSATWriter(cfg, path='/tmp', prefix='test_wta')
    c_nsat_writer.write()

    print('End %s:setup()' % (os.path.splitext(os.path.basename(__file__))[0]))
    return c_nsat_writer.fname
예제 #13
0
    rN_STATES = range(N_STATES)
    rN_GROUPS = range(N_GROUPS)
    rN_LRNGROUPS = range(N_LRNGROUPS)

    #MAX = 2**15-1
    #MIN =-2**15
    #OFF = -16
    OFF = nsat.OFF
    MAX = nsat.MAX
    MIN = nsat.MIN

    ###################### Stimulus Creation ##################################
    SL_train, tp = SimSpikingStimulus(N_INPUTS, **pdict)
    SL_train.save('ext_evts')
    ext_evts_data = nsat.exportAER(SL_train)
    ###########################################################################

    # Configuration 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_final=True,
                                 plasticity_en=[True],
                                 ben_clock=True)
    d = cfg.core_cfgs[0]
예제 #14
0
    W = np.zeros([N_UNITS, N_UNITS, N_STATES[0]], 'int')
    W[:N_INPUTS[0], N_INPUTS[0]:] = extW

    # Set adjacent matrix
    CW = np.zeros([N_UNITS, N_UNITS, N_STATES[0]], 'int')
    CW[:N_INPUTS[0], N_INPUTS[0]:] = extCW

    wgt_table, ptr_table = gen_ptr_wgt_table_from_W_CW(W, CW, [])
    np.set_printoptions(threshold=np.nan)
    cfg.core_cfgs[0].wgt_table = wgt_table
    cfg.core_cfgs[0].ptr_table = ptr_table

    # Set external events
    stim = np.linspace(1, 1000, N_NEURONS[0])
    ext_evts_data = nsat.exportAER(
        SimSpikingStimulus(stim, sim_ticks, t_sim=sim_ticks))
    cfg.set_ext_events(ext_evts_data)

    # Write C NSAT parameters binary files
    # c_nsat_writer = nsat.C_NSATWriter(cfg, path='/tmp',
    #                                   prefix='test_sigmoid_ext')
    c_nsat_writer = nsat.C_NSATWriterMultithread(cfg,
                                                 path='/tmp',
                                                 prefix='test_sigmoid_ext')
    c_nsat_writer.write()

    # Call the C NSAT
    print("Running C NSAT!")
    nsat.run_c_nsat(c_nsat_writer.fname)

    print("Plotting data")
예제 #15
0
def setup():
    print('Begin %s:setup()' %
          (os.path.splitext(os.path.basename(__file__))[0]))
    np.random.seed(30)  # Numpy random number generator seed
    sim_ticks = 5000  # Total simulation time
    N_CORES = 2  # Number of cores
    N_NEURONS = [10, 10]  # Number of neurons per core (list)
    N_INPUTS = [10, 10]  # Number of inputs per core (list)
    N_STATES = [4, 4]  # Number of states per core (list)
    N_UNITS = [sum(i) for i in zip(N_INPUTS, N_NEURONS)]

    # Constants
    XMAX = nsat.XMAX
    XMIN = nsat.XMIN
    OFF = -16
    MAX = nsat.MAX
    MIN = nsat.XMIN

    # 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,
                                 ben_clock=True)

    # Loop over the cores and set the parameters
    for i in range(N_CORES):
        # Transition matrix A
        cfg.core_cfgs[i].A[0] = [[-1, OFF, OFF, OFF], [OFF, OFF, OFF, OFF],
                                 [OFF, OFF, OFF, OFF], [OFF, OFF, OFF, OFF]]

        # Sign matrix sA
        cfg.core_cfgs[i].sA[0] = [[-1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1],
                                  [1, 1, 1, 1]]

        # Bias
        cfg.core_cfgs[i].b[0] = np.array([30, 0, 0, 0], dtype='int')
        # Threshold
        cfg.core_cfgs[i].Xth[0] = 100
        # Reset value
        cfg.core_cfgs[i].Xreset[0] = np.array([0, XMAX, XMAX, XMAX], 'int')
        # Turn reset on per state
        cfg.core_cfgs[i].XresetOn[0] = np.array([True, False, False, False],
                                                'bool')

    for i in range(N_CORES):
        # Mapping NSAT parameters (per core)
        cfg.core_cfgs[i].nmap = np.zeros((N_NEURONS[0], ), dtype='int')

        if i == 0:
            # Assigning synaptic strength to each core
            W = np.zeros([N_UNITS[i], N_UNITS[i], 4], 'int')
            W[0, 10, 0] = 50
            W[1, 11, 0] = 50
            W[2, 12, 0] = 50
            W[3, 13, 0] = 50
            W[4, 14, 0] = 50
            W[5, 15, 0] = 50
            W[6, 16, 0] = 50
            W[7, 17, 0] = 50
            W[8, 18, 0] = 50
            W[9, 19, 0] = 50

            # Assigning adjacent matrix to each core
            CW = np.zeros(W.shape, dtype='int')
            CW[0, 10, 0] = 1
            CW[1, 11, 0] = 1
            CW[2, 12, 0] = 1
            CW[3, 13, 0] = 1
            CW[4, 14, 0] = 1
            CW[5, 15, 0] = 1
            CW[6, 16, 0] = 1
            CW[7, 17, 0] = 1
            CW[8, 18, 0] = 1
            CW[9, 19, 0] = 1

            wgt_table, ptr_table = gen_ptr_wgt_table_from_W_CW(W, CW, [])
            np.set_printoptions(threshold=np.nan)
            cfg.core_cfgs[i].wgt_table = wgt_table
            cfg.core_cfgs[i].ptr_table = ptr_table
        if i == 1:
            W = np.zeros([N_UNITS[i], N_UNITS[i], 4], 'int')
            CW = np.zeros(W.shape, dtype='int')
            W[1, 11, 0] = 50
            W[5, 15, 0] = 50
            CW[1, 11, 0] = 1
            CW[5, 15, 0] = 1
            wgt_table, ptr_table = gen_ptr_wgt_table_from_W_CW(W, CW, [])
            np.set_printoptions(threshold=np.nan)
            cfg.core_cfgs[i].wgt_table = wgt_table
            cfg.core_cfgs[i].ptr_table = ptr_table

    # Connect core 0 neuron 9 with core 1 neurons 1 and 5
    cfg.set_L1_connectivity({(0, 9): ((1, 1), (1, 5))})

    # Generate external events firing rates from 5 to 50 inc by 5
    freqs = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
    SL = []
    for i in range(N_CORES - 1):
        SL.append(RegularSpikingStimulus(freqs, sim_ticks))
    ext_evts_data = nsat.exportAER(SL)

    # Set external events
    cfg.set_ext_events(ext_evts_data)

    # Generate all the CNSAT parameters files
    c_nsat_writer = nsat.C_NSATWriter(cfg,
                                      path='/tmp',
                                      prefix='test_external_evts')
    c_nsat_writer.write()

    print('End %s:setup()' % (os.path.splitext(os.path.basename(__file__))[0]))
예제 #16
0
def setup():
    print('Begin %s:setup()' %
          (os.path.splitext(os.path.basename(__file__))[0]))

    np.random.seed(30)
    sim_ticks = 5000
    N_CORES = 2
    N_NEURONS = [100, 100]
    N_INPUTS = [100, 100]
    N_STATES = [4, 4]
    N_UNITS = [sum(i) for i in zip(N_INPUTS, N_NEURONS)]

    XMAX = nsat.XMAX
    XMIN = nsat.XMIN
    OFF = -16
    MAX = nsat.MAX
    MIN = nsat.XMIN

    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,
                                 ben_clock=True)

    for i in range(N_CORES):
        cfg.core_cfgs[i].A[0] = [[-1, OFF, OFF, OFF], [OFF, OFF, OFF, OFF],
                                 [OFF, OFF, OFF, OFF], [OFF, OFF, OFF, OFF]]

        cfg.core_cfgs[i].sA[0] = [[-1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1],
                                  [1, 1, 1, 1]]

        # if i == 1:
        #     cfg.core_cfgs[i].A[0] = [[-2,  OFF, OFF, OFF],
        #                              [OFF, OFF, OFF, OFF],
        #                              [OFF, OFF, OFF, OFF],
        #                              [OFF, OFF, OFF, OFF]]

        cfg.core_cfgs[i].b[0] = np.array([30, 0, 0, 0], dtype='int')
        cfg.core_cfgs[i].Xth[0] = 100
        cfg.core_cfgs[i].Xthup[0] = np.array([XMAX, XMAX, XMAX, XMAX], 'int')
        cfg.core_cfgs[i].Xthlo[0] = np.ones(4, 'int') * XMIN
        cfg.core_cfgs[i].Xreset[0] = np.array([0, XMAX, XMAX, XMAX], 'int')
        cfg.core_cfgs[i].XresetOn[0] = np.array([True, False, False, False],
                                                'bool')

    for i in range(N_CORES):
        # Mapping NSAT parameters (per core)
        cfg.core_cfgs[i].nmap = np.zeros((N_NEURONS[0], ), dtype='int')

        # Assigning synaptic strength to each core
        W = np.zeros([N_UNITS[i], N_UNITS[i], 4], 'int')
        tmp = np.zeros((N_INPUTS[i], N_NEURONS[i]))
        np.fill_diagonal(tmp, 50)
        W[:N_INPUTS[i], N_INPUTS[i]:, 0] = tmp

        # Assigning adjacent matrix to each core
        CW = np.zeros(W.shape, dtype='bool')
        CW[:N_INPUTS[i], N_INPUTS[i]:, 0] = tmp.astype('bool')

        wgt_table, ptr_table = gen_ptr_wgt_table_from_W_CW(W, CW, [])
        cfg.core_cfgs[i].wgt_table = wgt_table
        cfg.core_cfgs[i].ptr_table = ptr_table

    cfg.set_L1_connectivity(({
        (0, 102): ((1, 1), (1, 50), (1, 73)),
        (0, 103): ((1, 15), (1, 95), (1, 7)),
        (0, 105): ((1, 89), (1, 65), (1, 56)),
        (0, 143): ((1, 21), (1, 45), (1, 33)),
        (0, 113): ((1, 5), (1, 50), (1, 7)),
        (0, 133): ((1, 41), (1, 5), (1, 77)),
        (0, 123): ((1, 19), (1, 75), (1, 57)),
        (0, 104): ((1, 3), )
    }))

    # Generate external events
    # freqs = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
    freqs = np.arange(5, 1000, 10)
    SL = []
    for i in range(1):
        SL.append(RegularSpikingStimulus(freqs, sim_ticks))
    ext_evts_data = nsat.exportAER(SL)

    # Set external events
    cfg.set_ext_events(ext_evts_data)

    # Generate all the CNSAT parameters files
    c_nsat_writer = nsat.C_NSATWriter(cfg, path='/tmp', prefix='test_routing')
    c_nsat_writer.write()
    c_nsat_writer.write_L1connectivity()

    print('End %s:setup()' % (os.path.splitext(os.path.basename(__file__))[0]))
예제 #17
0
def erbp_convnet_2L(data_train, data_classify, targets_classify, nepochs=10):
    N_FEAT1 = 16
    N_FEAT2 = 32
    stride = 2
    ksize = 5

    exp_name = '/tmp/mnist_convnet_2L'
    exp_name_test = '/tmp/mnist_convnet_2L_test/'

    inputsize = 28
    Nchannel = 1
    Nxy = inputsize*inputsize
    Nv = Nxy*Nchannel
    Nl = 10
    Nconv1 = Nv//stride//stride*N_FEAT1//Nchannel
    Nconv2 = Nconv1//stride//stride*N_FEAT2//N_FEAT1
    Nh = 100

    t_sample_test = 3000
    t_sample_train = 1500
    N_train = 5000
    N_test = 1000
    test_every = 1
    inp_fact = 25

    sim_ticks = N_train*t_sample_train
    sim_ticks_test = N_test*t_sample_test

    np.random.seed(100)

    wpg = 96
    wgp = 37

    erbp_ptype_ = erbp_ptype.copy()
    erbp_ptype_.rr_num_bits = 12
    erbp_ptype_.hiac = [-7, OFF, OFF]

    erf_ntype_ = erf_ntype.copy()
    erf_ntype_.plasticity_type = [erbp_ptype_, nonplastic_ptype]
    erf_ntype_.Wgain[0] = 2

    net_graph = LogicalGraphSetup()

    pop_data = net_graph.create_population(Population(name='pop_data',
                                                      n=Nv,
                                                      core=-1,
                                                      is_external=True))
    pop_lab = net_graph.create_population(Population(name='pop_lab',
                                                     n=Nl,
                                                     core=-1,
                                                     is_external=True))
    pop_conv1 = net_graph.create_population(Population(name='pop_conv1',
                                                       n=Nconv1,
                                                       core=0,
                                                       neuron_cfg=erf_ntype_))
    pop_conv2 = net_graph.create_population(Population(name='pop_conv2',
                                                       n=Nconv2,
                                                       core=1,
                                                       neuron_cfg=erf_ntype_))
    pop_hid = net_graph.create_population(Population(name='pop_hid',
                                                     n=Nh,
                                                     core=1,
                                                     neuron_cfg=erf_ntype_))
    pop_out = net_graph.create_population(Population(name='pop_out',
                                                     n=Nl,
                                                     core=1,
                                                     neuron_cfg=output_ntype))

    net_graph.create_connection(pop_data, pop_conv1, 0,
                                connect_conv2dbank(inputsize,
                                                   Nchannel,
                                                   N_FEAT1,
                                                   stride,
                                                   ksize))
    net_graph.create_connection(pop_conv1, pop_conv2, 0,
                                connect_conv2dbank(inputsize//stride,
                                                   N_FEAT1,
                                                   N_FEAT2,
                                                   stride,
                                                   ksize))
    net_graph.create_connection(pop_conv2, pop_hid, 0,
                                connect_random_uniform(low=-16, high=16))
    net_graph.create_connection(pop_hid, pop_out, 0,
                                connect_random_uniform(low=-4, high=4))

    pop_err_pos = net_graph.create_population(Population(name='pop_err_pos',
                                                         n=Nl,
                                                         core=0,
                                                         neuron_cfg=error_ntype))
    pop_err_neg = net_graph.create_population(Population(name='pop_err_neg',
                                                         n=Nl,
                                                         core=0,
                                                         neuron_cfg=error_ntype))

    net_graph.create_connection(pop_out, pop_err_pos, 0, connect_one2one(-wpg))
    net_graph.create_connection(pop_out, pop_err_neg, 0, connect_one2one(wpg))

    net_graph.create_connection(pop_lab, pop_err_pos, 0, connect_one2one(wpg))
    net_graph.create_connection(pop_lab, pop_err_neg, 0, connect_one2one(-wpg))

    [p, w] = connect_shuffle(2000)(pop_err_pos, pop_conv1)
    net_graph.create_connection(pop_err_pos, pop_conv1, 1, [p, +w])
    net_graph.create_connection(pop_err_neg, pop_conv1, 1, [p, -w])

    [p, w] = connect_shuffle(2000)(pop_err_pos, pop_conv2)
    net_graph.create_connection(pop_err_pos, pop_conv2, 1, [p, +w])
    net_graph.create_connection(pop_err_neg, pop_conv2, 1, [p, -w])

    [p, w] = connect_shuffle(3000)(pop_err_pos, pop_hid)
    net_graph.create_connection(pop_err_pos, pop_hid, 1, [p, +w])
    net_graph.create_connection(pop_err_neg, pop_hid, 1, [p, -w])

    net_graph.create_connection(pop_err_pos, pop_out, 1, connect_one2one(wgp))
    net_graph.create_connection(pop_err_neg, pop_out, 1, connect_one2one(-wgp))

    setup = net_graph.generate_multicore_setup(NSATSetup)

    spk_rec_mon = [[] for i in range(setup.ncores)]

    cfg_train = setup.create_configuration_nsat(sim_ticks=sim_ticks,
                                                w_check=False,
                                                spk_rec_mon=spk_rec_mon,
                                                monitor_spikes=False,
                                                gated_learning=[True, True],
                                                plasticity_en=[True, True])

    spk_rec_mon = [[] for i in range(setup.ncores)]
    spk_rec_mon[pop_out.core] = pop_out.addr

    cfg_test = cfg_train.copy()
    cfg_test.sim_ticks = sim_ticks_test
    cfg_test.plasticity_en[:] = False
    cfg_test.spk_rec_mon = spk_rec_mon
    cfg_test.monitor_spikes = True

    SL_train = create_spike_train(data_train[:N_train],
                                  t_sample_train,
                                  scaling=inp_fact,
                                  with_labels=True)
    ext_evts_data_train = nsat.exportAER(SL_train)

    SL_test = create_spike_train(data_classify[:N_test],
                                 t_sample_test,
                                 scaling=inp_fact,
                                 with_labels=False)
    ext_evts_data_test = nsat.exportAER(SL_test)

    cfg_test.set_ext_events(ext_evts_data_test)
    cfg_train.set_ext_events(ext_evts_data_train)

    c_nsat_writer_train = nsat.C_NSATWriter(cfg_train,
                                            path=exp_name,
                                            prefix='')
    c_nsat_writer_train.write()

    c_nsat_writer_test = nsat.C_NSATWriter(cfg_test,
                                           path=exp_name_test,
                                           prefix='')
    c_nsat_writer_test.write()

    fname_train = c_nsat_writer_train.fname
    fname_test = c_nsat_writer_test.fname
    c_nsat_reader_test = nsat.C_NSATReader(cfg_test, fname_test)

    pip, total_time = [], []
    t0t, tft = 0, 0
    for i in range(nepochs):
        t0 = time.time()
        nsat.run_c_nsat(fname_train)
        tf = time.time()

        for j in range(setup.ncores):
            # train->test
            shutil.copy(exp_name+'/_shared_mem_core_{0}.dat'.format(
                j), exp_name_test+'/_wgt_table_core_{0}.dat'.format(j))
            # train->train
            shutil.copy(exp_name+'/_shared_mem_core_{0}.dat'.format(
                j), exp_name+'/_wgt_table_core_{0}.dat'.format(j))
        if test_every > 0:
            if i % test_every == test_every-1:
                t0t = time.time()
                nsat.run_c_nsat(fname_test)
                tft = time.time()
                acc, slout = test_accuracy(c_nsat_reader_test,
                                           targets=targets_classify[:N_test],
                                           pop=pop_out,
                                           sim_ticks=sim_ticks_test,
                                           duration=t_sample_test)
                pip.append(acc)
        total_time.append(tf - t0 + tft - t0t)
    return pip, total_time
예제 #18
0
def setup():
    global SL
    print('Begin %s:setup()' % (os.path.splitext(os.path.basename(__file__))[0]))

    N_CORES = 1             # Number of cores
    N_NEURONS = [1]         # Number of neurons per core (list)
    N_INPUTS = [1]          # Number of inputs per core (list)
    N_STATES = [4]          # Number of states per core (list)
    N_UNITS = N_INPUTS[0] + N_NEURONS[0]        # Total number of units

    # Constants
    XMAX = nsat.XMAX
    XMIN = nsat.XMIN
    OFF = -16
    MAX = nsat.MAX
    MIN = nsat.XMIN

    # 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_weights=True,
                                 monitor_spikes=True,
                                 plasticity_en=[True],
                                 ben_clock=True)

    # Transition matrix A (parameters group 0)
    cfg.core_cfgs[0].A[0] = [[-5, OFF, OFF, OFF],
                             [2, -5,  OFF, OFF],
                             [OFF, OFF, -7, OFF],
                             [OFF, OFF, OFF, OFF]]

    # Transition matrix A (parameters group 1)
    cfg.core_cfgs[0].A[1] = [[OFF, OFF, OFF, OFF],
                             [OFF, OFF, OFF, OFF],
                             [OFF, OFF, OFF, OFF],
                             [OFF, OFF, OFF, OFF]]

    # Sign matrix A (parameters group 0)
    cfg.core_cfgs[0].sA[0] = [[-1, 1, 1, 1],
                              [1, -1, 1, 1],
                              [1, 1, -1, 1],
                              [1, 1, 1, -1]]

    # Bias
    cfg.core_cfgs[0].b[0] = [0,  0, 0, 0]
    # Threshold
    cfg.core_cfgs[0].Xth[0] = 25000
    # Initial conditions
    cfg.core_cfgs[0].Xinit[0] = np.array([[0, 0, 0, 0] for _
                                         in range(N_NEURONS[0])],
                                         'int')
    # Reset value
    cfg.core_cfgs[0].Xreset[0] = np.array([0, MAX, MAX, MAX], 'int')
    # Turn reset on
    cfg.core_cfgs[0].XresetOn[0] = np.array([True, False, False, False],
                                            'bool')

    # STDP kernel height of acausal part
    cfg.core_cfgs[0].hiac = [[-1, 4, 0] for _ in range(8)]

    cfg.core_cfgs[0].plastic[0] = True          # Plastic states group 0
    cfg.core_cfgs[0].stdp_en[0] = False         # STDP enabled for group 0
    cfg.core_cfgs[0].sigma[0] = [0, 0, 5, 0]    # Additive noise
    cfg.core_cfgs[0].modstate[0] = 2            # Global modulator state
    cfg.core_cfgs[0].t_ref[0] = 40              # Refractory period

    # Mapping between neurons and parameter groups
    cfg.core_cfgs[0].nmap = np.zeros((N_NEURONS[0],), dtype='int')

    # Synaptic weights
    W = np.zeros([N_UNITS, N_UNITS, N_STATES[0]], 'int')
    W[0, 1, 1] = 100

    # Adjacent matrix
    CW = np.zeros(W.shape, dtype='int')
    CW[0, 1, 1] = 1

    wgt_table, ptr_table = gen_ptr_wgt_table_from_W_CW(W, CW, [])
    np.set_printoptions(threshold=np.nan)
    cfg.core_cfgs[0].wgt_table = wgt_table
    cfg.core_cfgs[0].ptr_table = ptr_table

    # Generate external event spikes (firing rate 50Hz)
    stim = [50]
    SL = SimSpikingStimulus(stim, t_sim=sim_ticks)
    ext_evts_data = nsat.exportAER(SL)

    # Set external events
    cfg.set_ext_events(ext_evts_data)

    # Write the C NSAT parameters binary files
    c_nsat_writer = nsat.C_NSATWriter(cfg, path='/tmp', prefix='test_modstate')
    c_nsat_writer.write()

    # Write Intel FPGA parameters hex files
#    intel_fpga_writer = nsat.IntelFPGAWriter(cfg, path='.',
#                                             prefix='test_modstate')
#    intel_fpga_writer.write()
#    intel_fpga_writer.write_globals()
    print('End %s:setup()' % (os.path.splitext(os.path.basename(__file__))[0]))