Ejemplo n.º 1
0
def setup():
    print('Begin %s:setup()' %
          (os.path.splitext(os.path.basename(__file__))[0]))

    sim_ticks = 100  # Total simulation time
    N_CORES = 1  # Number of cores
    N_NEURONS = [1]  # Number of neurons per core (list)
    N_INPUTS = [0]  # 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,
                                 ben_clock=True)

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

    # Sign matrix A
    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] = np.array([10, 0, 0, 0], dtype='int')
    # Threshold
    cfg.core_cfgs[0].Xth[0] = 100
    # Reset value
    cfg.core_cfgs[0].Xreset[0] = np.array([0, 0, 0, 0], 'int')
    # Turn reset on
    cfg.core_cfgs[0].XresetOn[0] = np.array([False, False, False, False],
                                            'bool')

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

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

    # Write Intel FPGA parameters hex files
    #    intel_fpga_writer = nsat.IntelFPGAWriter(cfg, path='.',
    #                                             prefix='test_coupling_leak')
    #    intel_fpga_writer.write()
    #    intel_fpga_writer.write_globals()
    print('End %s:setup()' % (os.path.splitext(os.path.basename(__file__))[0]))
Ejemplo n.º 2
0
def setup():
    sim_ticks = 500  # Total simulation time
    N_CORES = 1  # Number of cores
    N_NEURONS = [51]  # Number of neurons per core
    N_INPUTS = [0]  # Number of inputs per core
    N_STATES = [4]  # Number of states per neuron per 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

    # 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,
                                 plasticity_en=np.array([True], 'bool'),
                                 ben_clock=True)

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

    # Sign matrix A
    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] = np.array([50, 0, 0, 0], dtype='int')
    # Threshold
    cfg.core_cfgs[0].Xth[0] = 100
    # Reset value
    cfg.core_cfgs[0].Xreset[0] = np.array([0, XMAX, XMAX, XMAX], 'int')
    # Turn reset on
    cfg.core_cfgs[0].XresetOn[0] = np.array([True, False, False, False],
                                            'bool')

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

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

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

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

    # Mapping function between learning parameters groups and neurons
    lrnmap = np.zeros((nsat.N_GROUPS, N_STATES[0]), dtype='int')
    lrnmap[1] = 1
    cfg.core_cfgs[0].lrnmap = lrnmap

    # Synaptic strength matrix
    W = np.zeros((N_UNITS, N_UNITS, N_STATES[0]))
    W[1:, 0, 0] = 1
    cfg.core_cfgs[0].W = W

    # Adjacent matrix
    CW = np.zeros((N_UNITS, N_UNITS, N_STATES[0]))
    CW[1:, 0, 0] = 1
    cfg.core_cfgs[0].CW = CW

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

    # Write Intel FPGA parameters hex files
    #    intel_fpga_writer = nsat.IntelFPGAWriter(cfg, path='.',
    #                                             prefix='test_accumulator')
    #    intel_fpga_writer.write()
    #
    #    # Call the C NSAT
    #    print("Running C NSAT!")
    #    nsat.run_c_nsat(nsat.fname)
    #
    #    # Load the results (read binary files)
    #    c_nsat_reader = nsat.C_NSATReader(cfg, nsat.fname)
    #    states = c_nsat_reader.read_c_nsat_states()
    #    time_core0, states_core0 = states[0][0], states[0][1]
    #
    #    # Plot the results
    #    fig = plt.figure(figsize=(10, 10))
    #    for i in range(1, 5):
    #        ax = fig.add_subplot(4, 1, i)
    #        ax.plot(states_core0[:-1, 0, i-1], 'b', lw=3)
    #
    #     import os
    #     plt.savefig('/tmp/%s.png' % (os.path.splitext(os.path.basename(__file__))[0]))
    #     plt.close()
    print('End %s:setup()' % (os.path.splitext(os.path.basename(__file__))[0]))
Ejemplo n.º 3
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
Ejemplo n.º 4
0
def setup():
    print('Begin %s:setup()' %
          (os.path.splitext(os.path.basename(__file__))[0]))

    sim_ticks = 100  # Simulation time
    N_CORES = 1  # Number of cores
    N_NEURONS = [8]  # Number of neurons
    N_INPUTS = [0]  # Number of inputs
    N_STATES = [4]  # Number of states
    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 per group
    cfg.core_cfgs[0].A[0, 0, 0] = -1
    cfg.core_cfgs[0].A[1, 0, 0] = -2
    cfg.core_cfgs[0].A[2, 0, 0] = -3
    cfg.core_cfgs[0].A[3, 0, 0] = -4
    cfg.core_cfgs[0].A[4, 0, 0] = -5
    cfg.core_cfgs[0].A[5, 0, 0] = -12
    cfg.core_cfgs[0].A[6, 0, 0] = -7
    cfg.core_cfgs[0].A[7, 0, 0] = -3

    # Sign matrix per group
    cfg.core_cfgs[0].sA[0, 0, 0] = -1
    cfg.core_cfgs[0].sA[1, 0, 0] = -1
    cfg.core_cfgs[0].sA[2, 0, 0] = -1
    cfg.core_cfgs[0].sA[3, 0, 0] = -1
    cfg.core_cfgs[0].sA[4, 0, 0] = -1
    cfg.core_cfgs[0].sA[5, 0, 0] = -1
    cfg.core_cfgs[0].sA[6, 0, 0] = -1
    cfg.core_cfgs[0].sA[7, 0, 0] = -1

    # Bias matrix per group
    cfg.core_cfgs[0].b[0, 0] = 50
    cfg.core_cfgs[0].b[1, 0] = 80
    cfg.core_cfgs[0].b[2, 0] = 50
    cfg.core_cfgs[0].b[3, 0] = 20
    cfg.core_cfgs[0].b[4, 0] = 50
    cfg.core_cfgs[0].b[5, 0] = 60
    cfg.core_cfgs[0].b[6, 0] = 50
    cfg.core_cfgs[0].b[7, 0] = 5

    # Threshold
    cfg.core_cfgs[0].Xth = np.ones((nsat.N_GROUPS, )) * 100
    # Reset value
    cfg.core_cfgs[0].Xreset[0] = np.array([0, XMAX, XMAX, XMAX], 'int')
    # Turn reset on
    cfg.core_cfgs[0].XresetOn[0] = np.array([True, False, False, False],
                                            'bool')

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

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

    # Write Intel FPGA parameters hex files
    #    intel_fpga_writer = nsat.IntelFPGAWriter(cfg, path='.',
    #                                             prefix='test_parameters_groups')
    #    intel_fpga_writer.write()
    #    intel_fpga_writer.write_globals()
    print('End %s:setup()' % (os.path.splitext(os.path.basename(__file__))[0]))
    return c_nsat_writer.fname
Ejemplo n.º 5
0
    # Adjacent matrix
    CW = np.zeros(W.shape, dtype='bool')
    CW[0, 1, 7] = 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

    # Set external events (spikes)
    freqs = [15]
    ext_evts_data = RegularSpikingStimulus(freqs, ticks=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_eight_states_neuron')
    c_nsat_writer.write()

    # Write Intel FPGA parameters hex files
#    intel_fpga_writer = nsat.IntelFPGAWriter(cfg, path='.',
#                                             prefix='test_eight_states_neuron')
#    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)

    # Load the results (read binary files)
    c_nsat_reader = nsat.C_NSATReader(cfg, c_nsat_writer.fname)
    states = c_nsat_reader.read_c_nsat_states()
Ejemplo n.º 6
0
def setup():
    print('Begin %s:setup()' %
          (os.path.splitext(os.path.basename(__file__))[0]))
    np.random.seed(30)  # Numpy RNG seed
    sim_ticks = 5000  # Simulation ticks
    N_CORES = 1  # Number of cores
    N_NEURONS = [10]  # Number of neurons
    N_INPUTS = [10]  # Number of inputs
    N_STATES = [4]  # Number of states
    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] = [[-1, OFF, OFF, OFF], [OFF, OFF, OFF, OFF],
                             [OFF, OFF, OFF, 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]]

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

    # Synaptic strengths gain
    cfg.core_cfgs[0].Wgain[0] = 1

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

    # Synaptic strengths matrix
    W = np.zeros([N_UNITS, N_UNITS, 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

    # Adjacent matrix
    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[0].wgt_table = wgt_table
    cfg.core_cfgs[0].ptr_table = ptr_table

    # Generate external events
    freqs = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
    ext_evts_data = RegularSpikingStimulus(freqs, sim_ticks)

    # Set external events
    cfg.set_ext_events(ext_evts_data)

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

    # Write Intel FPGA hex parameters files
    #    intel_fpga_writer = nsat.IntelFPGAWriter(cfg, path='.',
    #                                             prefix='test_ext_evts_wgain')
    #    intel_fpga_writer.write()
    #    intel_fpga_writer.write_globals()
    print('End %s:setup()' % (os.path.splitext(os.path.basename(__file__))[0]))
    return c_nsat_writer.fname
Ejemplo n.º 7
0
def setup():
    print('Begin %s:setup()' %
          (os.path.splitext(os.path.basename(__file__))[0]))

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

    # Basic 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)

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

    # Sign matrix sA
    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] = np.array([50, 0, 0, 0], dtype='int')
    # Threshold
    cfg.core_cfgs[0].Xth[0] = 100
    # Reset value
    cfg.core_cfgs[0].Xreset[0] = np.array([0, XMAX, XMAX, XMAX], 'int')
    # Turn reset on
    cfg.core_cfgs[0].XresetOn[0] = np.array([True, False, False, False],
                                            'bool')

    # Tested value
    cfg.core_cfgs[0].Xinit[0] = np.zeros((N_NEURONS[0], N_STATES[0]), 'int')
    if len(sys.argv) != 2:
        print('Missing argument! Default value is used [xinit=0]!')
        cfg.core_cfgs[0].Xinit[0, 0] = 0
        cfg.core_cfgs[0].Xinit[0, 1] = 0
        cfg.core_cfgs[0].Xinit[0, 2] = 0
        cfg.core_cfgs[0].Xinit[0, 3] = 0
    else:
        tmp = int(sys.argv[1])
        print(tmp)
        cfg.core_cfgs[0].Xinit[0, 0] = tmp
        cfg.core_cfgs[0].Xinit[0, 1] = 0
        cfg.core_cfgs[0].Xinit[0, 2] = 0
        cfg.core_cfgs[0].Xinit[0, 3] = 0

    # Write C NSAT parameters files
    cfg.core_cfgs[0].nmap = np.zeros((N_NEURONS[0], ), dtype='int')

    c_nsat_writer = nsat.C_NSATWriter(cfg,
                                      path='/tmp',
                                      prefix='test_initial_cond')
    c_nsat_writer.write()

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

    print('End %s:setup()' % (os.path.splitext(os.path.basename(__file__))[0]))
    return c_nsat_writer.fname
Ejemplo n.º 8
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]))
Ejemplo n.º 9
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
Ejemplo n.º 10
0
    lrnmap[1] = 1
    cfg.core_cfgs[0].lrnmap = lrnmap

    # Synaptic strength matrix
    W = np.zeros((N_UNITS, N_UNITS, N_STATES[0]))
    W[1:, 0, 0] = 1
    cfg.core_cfgs[0].W = W

    # Adjacent matrix
    CW = np.zeros((N_UNITS, N_UNITS, N_STATES[0]))
    CW[1:, 0, 0] = 1
    cfg.core_cfgs[0].CW = CW

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

    # Write Intel FPGA parameters hex files
#    intel_fpga_writer = nsat.IntelFPGAWriter(cfg, path='.',
#                                             prefix='test_accumulator')
#    intel_fpga_writer.write()
#
#    # Call the C NSAT
#    print("Running C NSAT!")
#    nsat.run_c_nsat(c_nsat_writer.fname)
#
#    # Load the results (read binary files)
#    c_nsat_reader = nsat.C_NSATReader(cfg, c_nsat_writer.fname)
#    states = c_nsat_reader.read_c_nsat_states()
Ejemplo n.º 11
0
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
Ejemplo n.º 12
0
    d.XspikeIncrVal[0, :4] = [0, 0, 1024, 0]
    d.modstate[0] = 3

    # Synaptic weights and adjacent matrix
    wgt_table, ptr_table = gen_ptr_wgt_table_from_W_CW(W, CW, [])
    np.set_printoptions(threshold=np.nan)
    d.wgt_table = wgt_table
    d.ptr_table = ptr_table

    ###########################################################################
    cfg.set_ext_events(ext_evts_data)

    print("Generating parameters files for training")
    # Write C NSAT parameters binary files
    c_nsat_writer = nsat.C_NSATWriter(cfg,
                                      path='/tmp',
                                      prefix='vmem_coincidence')
    c_nsat_writer.write()

    cfg.core_cfgs[0].latex_print_parameters(1)

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

    c_nsat_reader = nsat.C_NSATReader(cfg, c_nsat_writer.fname)
    states = c_nsat_reader.read_c_nsat_states()
    states_core0 = states[0][1]
    np.save('states_vmem_', states_core0)

    fW = c_nsat_reader.read_c_nsat_synaptic_weights()
Ejemplo n.º 13
0
        # Adjacent matrix
        CW = np.zeros(W.shape, dtype='int')
        CW[0, 0, 1] = 1
        CW[0, 0, 3] = 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

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

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

    # Write Intel FPGA parameters hex files
    #    # intel_fpga_writer = nsat.IntelFPGAWriter(cfg, path='.',
    #    #                                          prefix='test_mn_neuron')
    #    # intel_fpga_writer.write()
    #    # intel_fpga_writer.write_globals()
    cfg.core_cfgs[0].latex_print_parameters(1)

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

    # Load the results (read binary files)
Ejemplo n.º 14
0
                                            'bool')

    # Tested value
    if len(sys.argv) != 2:
        print('Missing argument! Default value is used[XspikeIncrVal=0]!')
        cfg.core_cfgs[0].XspikeIncrVal[0] = np.array([0, 0, 0, 0], 'int')
    else:
        cfg.core_cfgs[0].XspikeIncrVal[0] = np.array(
            [int(sys.argv[1]), 0, 0, 0], 'int')

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

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

    # Write Intel FPGA parameters hex files
    #    # intel_fpga_writer = nsat.IntelFPGAWriter(cfg, path='.',
    #    #                                         prefix='test_spike_increment')
    #    # 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)

    # Load the results (read binary files)
    c_nsat_reader = nsat.C_NSATReader(cfg, c_nsat_writer.fname)
Ejemplo n.º 15
0
def setup():
    print('Begin %s:setup()' %
          (os.path.splitext(os.path.basename(__file__))[0]))

    sim_ticks = 100  # Simulation time
    N_CORES = 2  # Number of cores
    N_NEURONS = [1, 1]  # Number of neurons per core
    N_INPUTS = [5, 5]  # Number of inputes per core
    N_STATES = [2, 2]  # Number of states per core

    # Constants
    XMAX = nsat.XMAX
    OFF = -16

    # 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)

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

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

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

        W = np.zeros((6, 6, 2), dtype='i')
        W[0, 5, 0] = 10
        W[1, 5, 0] = 10
        W[2, 5, 0] = 0
        W[3, 5, 0] = 0
        W[3, 5, 0] = 0

        CW = np.zeros(W.shape, 'i')
        CW[0, 5, 0] = 1
        CW[1, 5, 0] = 1
        CW[2, 5, 0] = 1
        CW[3, 5, 0] = 1
        CW[3, 5, 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

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

    cfg.ext_evts = True

    # Write C NSAT parameters binary files
    c_nsat_writer = nsat.C_NSATWriter(cfg, path='/tmp', prefix='test_davis')
    c_nsat_writer.write()
    build_davis_file("/tmp/test_davis_davis_events", num_ticks=sim_ticks)

    print('End %s:setup()' % (os.path.splitext(os.path.basename(__file__))[0]))
    return c_nsat_writer.fname
Ejemplo n.º 16
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
Ejemplo n.º 17
0
def setup():
    print('Begin %s:setup()' %
          (os.path.splitext(os.path.basename(__file__))[0]))

    N_CORES = 1  # Number of cores
    N_NEURONS = [2]  # Number of neurons per core
    N_INPUTS = [1]  # Number of inputs per core
    N_STATES = [4]  # Number of states per 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

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

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

    # Transition matrix group 1
    cfg.core_cfgs[0].A[1] = [[-2, OFF, OFF, OFF], [0, -5, OFF, OFF],
                             [OFF, OFF, 0, 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] = [[-1, 1, 1, 1], [1, -1, 1, 1], [1, 1, -1, 1],
                              [1, 1, 1, -1]]

    # Threshold
    cfg.core_cfgs[0].Xth[0] = 100
    cfg.core_cfgs[0].Xth[1] = 175
    # Refractory period
    cfg.core_cfgs[0].t_ref[1] = 0
    cfg.core_cfgs[0].t_ref[0] = 120
    # Bias
    cfg.core_cfgs[0].b[0] = [5, 0, 0, 0]
    cfg.core_cfgs[0].b[1] = [5, 0, 5, 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]
    cfg.core_cfgs[0].Xreset[1] = [0, MAX, MAX, MAX]
    # Turn on reset
    cfg.core_cfgs[0].XresetOn[0] = [True, False, False, False]
    cfg.core_cfgs[0].XresetOn[1] = [True, False, False, False]

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

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

    # Set learning parameters mapping function
    lrnmap = np.zeros((nsat.N_GROUPS, N_STATES[0]), dtype='int')
    lrnmap[nmap[0], 1] = 0
    lrnmap[nmap[1], 1] = 1
    cfg.core_cfgs[0].lrnmap = lrnmap

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

    # Adjacent matrix
    CW = np.zeros(W.shape, dtype='int')
    CW[0, 1, 0] = True
    CW[1, 2, 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

    # Build external events (spikes)
    freqs = [5]
    ext_evts_data = RegularSpikingStimulus(freqs, 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_two_neurons_stdp')
    c_nsat_writer.write()

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

    print('End %s:setup()' % (os.path.splitext(os.path.basename(__file__))[0]))
Ejemplo n.º 18
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]))
Ejemplo n.º 19
0
def setup():
    print('Begin %s:setup()' %
          (os.path.splitext(os.path.basename(__file__))[0]))

    sim_ticks = 100  # Simulation time
    N_CORES = 1  # Number of cores
    N_NEURONS = [1]  # Number of neurons per core
    N_INPUTS = [0]  # Number of inputes 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

    # 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] = [[-1, OFF, OFF, OFF], [OFF, OFF, OFF, OFF],
                             [OFF, OFF, OFF, 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]]

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

    # Tested value
    if len(sys.argv) != 2:
        print('Missing argument! Default value is used[Xreset=0]!')
        cfg.core_cfgs[0].Xreset[0] = np.array([0, MAX, MAX, MAX], 'int')
    else:
        cfg.core_cfgs[0].Xreset[0] = np.array([sys.argv[1], MAX, MAX, MAX],
                                              'int')

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

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

    print('End %s:setup()' % (os.path.splitext(os.path.basename(__file__))[0]))
    return c_nsat_writer.fname
Ejemplo n.º 20
0
cfg_test.core_cfgs[0] = copy.copy(cfg_train.core_cfgs[0])
d = cfg_test.core_cfgs[0]
d.sigma[:] = 0

# FIXME It doesn't read the weights after training
d.wgt_table = core0_cfg.wgt_table.copy()
d.ptr_table = core0_cfg.ptr_table.copy()

cfg_test.set_ext_events(ext_evts_data_test)

#fname_test = cfg_test.write_all_cnsat_params(prefix + hex_dir_name_test)
#fname_test.syn_weights = fname.syn_weights

print("################## Writing Parameters Files ##################")
c_nsat_writer_train = nsat.C_NSATWriter(cfg_train, path='/tmp/erbp_mnist_train1/', prefix='')
c_nsat_writer_train.write()

c_nsat_writer_test = nsat.C_NSATWriter(cfg_test, path='/tmp/erbp_mnist_test1/', prefix='')
c_nsat_writer_test.write()

fname_train = c_nsat_writer_train.fname
fname_test = c_nsat_writer_test.fname

c_nsat_reader_train = nsat.C_NSATReader(cfg_train, fname_train)
c_nsat_reader_test = nsat.C_NSATReader(cfg_test, fname_test)

print("############# Running simulation #####################")
pip = []
for i in range(nepochs):
    nsat.run_c_nsat(fname_train)
Ejemplo n.º 21
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 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()

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

    # Load the results (read binary files)
    c_nsat_reader = nsat.C_NSATReader(cfg, c_nsat_writer.fname)
    states = c_nsat_reader.read_c_nsat_states()
Ejemplo n.º 22
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]))
Ejemplo n.º 23
0
    # Reset value
    cfg.core_cfgs[0].Xreset[0] = np.array([0, XMAX, XMAX, XMAX], 'int')
    # Turn on reset
    cfg.core_cfgs[0].XresetOn[0] = np.array([True, False, False, False],
                                            'bool')
    # Tested value
    if len(sys.argv) != 2:
        print('Missing argument! Default value is used[Xthr=100]!')
        cfg.core_cfgs[0].Xth[0] = 100
    else:
        cfg.core_cfgs[0].Xth[0] = int(sys.argv[1])

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

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

    # Write Intel FPGA parameters files
#    # intel_fpga_writer = nsat.IntelFPGAWriter(cfg, path='.',
#    #                                         prefix='test_threshold')
#    # 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)

    # Load the results (read binary files)
    c_nsat_reader = nsat.C_NSATReader(cfg, c_nsat_writer.fname)
    states = c_nsat_reader.read_c_nsat_states()
Ejemplo n.º 24
0
    cfg.core_cfgs[0].b[7, 0] = 5

    # Threshold
    cfg.core_cfgs[0].Xth = np.ones((nsat.N_GROUPS, )) * 100
    # Reset value
    cfg.core_cfgs[0].Xreset[0] = np.array([0, XMAX, XMAX, XMAX], 'int')
    # Turn reset on
    cfg.core_cfgs[0].XresetOn[0] = np.array([True, False, False, False],
                                            'bool')

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

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

    # Write Intel FPGA parameters hex files
#    intel_fpga_writer = nsat.IntelFPGAWriter(cfg, path='.',
#                                             prefix='test_parameters_groups')
#    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)

    # Load the results (read binary files)
    c_nsat_reader = nsat.C_NSATReader(cfg, c_nsat_writer.fname)
    states = c_nsat_reader.read_c_nsat_states()
Ejemplo n.º 25
0
    # 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)

    # Load the results (read binary files)
    c_nsat_reader = nsat.C_NSATReader(cfg, c_nsat_writer.fname)
    #ww = np.array(c_nsat_reader.read_c_nsat_synaptic_weights()[0])
Ejemplo n.º 26
0
def setup():
    print('Begin %s:setup()' %
          (os.path.splitext(os.path.basename(__file__))[0]))
    sim_ticks = 5000  # Simulation time
    N_CORES = 1  # Number of cores
    N_NEURONS = [2]  # Number of neurons per core (list)
    N_INPUTS = [0]  # Number of inputs per core (list)
    N_STATES = [4]  # Number of states per core (list)

    # 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 A
    cfg.core_cfgs[0].A[0] = [[-6, OFF, OFF, OFF], [0, -11, OFF, OFF],
                             [0, OFF, -8, OFF], [OFF, OFF, OFF, OFF]]

    # Sign matrix sA
    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] = np.array([600, 0, 0, 1], dtype='int')
    # Threshold
    cfg.core_cfgs[0].Xth[0] = XMAX
    # Reset value
    cfg.core_cfgs[0].Xreset[0] = np.array([0, XMAX, XMAX, XMAX], 'int')
    # Turn reset on
    cfg.core_cfgs[0].XresetOn[0] = np.array([True, False, False, False],
                                            'bool')
    # Global modulator state (e.g. Dopamine)
    cfg.core_cfgs[0].modstate[0] = 3

    # Synaptic weights
    N_UNITS = N_INPUTS[0] + N_NEURONS[0]  # Total number of units
    W = np.zeros([N_UNITS, N_UNITS, N_STATES[0]], 'int')
    W[0, 0, 1] = 115
    W[0, 1, 2] = 125
    W[1, 1, 1] = 115
    W[1, 0, 2] = 125
    cfg.core_cfgs[0].W = W

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

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

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

    # Write Intel FPGA parameters hex files
    #    intel_fpga_writer = nsat.IntelFPGAWriter(cfg, path='.',
    #                                             prefix='test_adapting')
    #    intel_fpga_writer.write()
    #    intel_fpga_writer.write_globals()
    print('End %s:setup()' % (os.path.splitext(os.path.basename(__file__))[0]))
Ejemplo n.º 27
0
def setup():
    print('Begin %s:setup()' %
          (os.path.splitext(os.path.basename(__file__))[0]))
    pyST.STCreate.seed(130)  # pyNCSre random number generator
    np.random.seed(30)  # Numpy random number generator

    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 = [8]  # Number of states per core (list)
    N_UNITS = N_INPUTS[0] + N_NEURONS[0]  # Total number of units

    # Number of states (8)
    rN_STATES = list(range(N_STATES[0]))
    # Number of parameters groups
    rN_GROUPS = list(range(nsat.N_GROUPS))

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

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

    # Transition matrix A
    cfg.core_cfgs[0].A[0] = np.array([[-1, OFF, OFF, OFF, OFF, OFF, OFF, OFF],
                                      [-3, -1, OFF, OFF, OFF, OFF, OFF, OFF],
                                      [OFF, -2, -1, OFF, OFF, OFF, OFF, OFF],
                                      [OFF, OFF, -1, -1, OFF, OFF, OFF, OFF],
                                      [OFF, OFF, OFF, -1, -1, OFF, OFF, OFF],
                                      [OFF, OFF, OFF, OFF, -2, -1, OFF, OFF],
                                      [OFF, OFF, OFF, OFF, OFF, -3, -1, OFF],
                                      [OFF, OFF, OFF, OFF, OFF, OFF, 0, -1]],
                                     'int')

    # Sign matrix sA
    cfg.core_cfgs[0].sA[0] = np.array(
        [[-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]], 'int')

    # Bias
    cfg.core_cfgs[0].b[0] = np.array([0, 0, 0, 0, 0, 0, 0, 0], 'int')
    # Initiali conditions
    cfg.core_cfgs[0].Xinit[0] = np.array([[0] * 8
                                          for _ in range(N_NEURONS[0])], 'int')
    # Reset value
    cfg.core_cfgs[0].Xreset[0] = np.array([0, 0, 0, 0, 0, 0, 0, 0], 'int')
    # Turn reset on
    cfg.core_cfgs[0].XresetOn[0] = np.array(
        [True, False, False, False, False, False, False, False], 'bool')
    # Synaptic probability
    cfg.core_cfgs[0].prob = np.ones(8, dtype='int') * 15
    # Threshold
    cfg.core_cfgs[0].Xth[0] = 100

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

    # Sunaptic weights
    W = np.zeros([N_UNITS, N_UNITS, 8], dtype='int')
    W[0, 1, 7] = 50

    # Adjacent matrix
    CW = np.zeros(W.shape, dtype='bool')
    CW[0, 1, 7] = 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

    # Set external events (spikes)
    freqs = [15]
    ext_evts_data = RegularSpikingStimulus(freqs, ticks=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_eight_states_neuron')
    c_nsat_writer.write()

    # Write Intel FPGA parameters hex files
    #    intel_fpga_writer = nsat.IntelFPGAWriter(cfg, path='.',
    #                                             prefix='test_eight_states_neuron')
    #    intel_fpga_writer.write()
    #    intel_fpga_writer.write_globals()
    print('End %s:setup()' % (os.path.splitext(os.path.basename(__file__))[0]))
Ejemplo n.º 28
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
Ejemplo n.º 29
0
def setup():
    print('Begin %s:setup()' %
          (os.path.splitext(os.path.basename(__file__))[0]))

    N_CORES = 1  # Number of cores
    N_NEURONS = [2]  # Number of neurons per core
    N_INPUTS = [0]  # Number of inputs per core
    N_STATES = [4]  # Number of states per 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

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

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

    # Transition matrix (group 0)
    cfg.core_cfgs[0].A[1] = [[-2, OFF, OFF, OFF], [2, -5, 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 0)
    cfg.core_cfgs[0].sA[1] = [[-1, 1, 1, 1], [1, -1, 1, 1], [1, 1, -1, 1],
                              [1, 1, 1, -1]]

    # Refractory period group 0
    cfg.core_cfgs[0].t_ref[0] = 0
    # Refractory period group 1
    cfg.core_cfgs[0].t_ref[1] = 20

    # Threshold group 0
    cfg.core_cfgs[0].Xth[0] = 100
    # Threshold group 1
    cfg.core_cfgs[0].Xth[1] = 2500
    # Bias group 0
    cfg.core_cfgs[0].b[0] = np.array([5, 0, 0, 0], 'int')
    # Bias group 1
    cfg.core_cfgs[0].b[1] = np.array([5, 0, 5, 0], 'int')
    # Initial conditions group 0
    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] = [0, MAX, MAX, MAX]
    # 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')

    # Enable plastic states for group 1
    cfg.core_cfgs[0].plastic[1] = True

    # Turn on STDP per state for group 0
    cfg.core_cfgs[0].stdp_en[0] = True
    # Turn on STDP per state for group 1
    cfg.core_cfgs[0].stdp_en[1] = True

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

    # K = 40
    # cfg.core_cfgs[0].tstdp[0] = K
    # cfg.core_cfgs[0].tca[0] = np.array([K, K])
    # cfg.core_cfgs[0].hica[0] = np.array([0, 0, 0])
    # cfg.core_cfgs[0].sica[0] = np.array([1, 1, 1])
    # cfg.core_cfgs[0].tac[0] = np.array([-K, -K])
    # cfg.core_cfgs[0].hiac[0] = np.array([0, 0, 0])
    # cfg.core_cfgs[0].siac[0] = np.array([1, 1, 1])

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

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

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

    # Adjacent matrix
    CW = np.zeros(W.shape, dtype='int')
    CW[0, 1, 1] = 1
    # CW[1, 0, 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

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

    # Write Intel FPGA hex parameters files
    #    intel_fpga_writer = nsat.IntelFPGAWriter(cfg, path='.',
    #                                             prefix='test_iSTDP')
    #    intel_fpga_writer.write()
    #    intel_fpga_writer.write_globals()
    print('End %s:setup()' % (os.path.splitext(os.path.basename(__file__))[0]))
    return c_nsat_writer.fname
Ejemplo n.º 30
0
def setup():
    print('Begin %s:setup()' %
          (os.path.splitext(os.path.basename(__file__))[0]))

    sim_ticks = 100  # Simulation time
    N_CORES = 1  # Number of cores
    N_NEURONS = [2]  # Number of neurons per core (list)
    N_INPUTS = [0]  # Number of input units per core (list)
    N_STATES = [4]  # Number of states per core (list)
    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

    # Instance of main class
    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)

    # Define transition matrices
    cfg.core_cfgs[0].A[0] = [[-1, OFF, OFF, OFF], [OFF, OFF, OFF, OFF],
                             [OFF, OFF, OFF, OFF], [OFF, OFF, OFF, OFF]]
    cfg.core_cfgs[0].A[1] = [[-1, OFF, OFF, OFF], [OFF, OFF, OFF, OFF],
                             [OFF, OFF, OFF, OFF], [OFF, OFF, OFF, OFF]]

    # Sign matrices
    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] = [[-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([50, 0, 0, 0], dtype='int')
    cfg.core_cfgs[0].b[1] = np.array([0, 0, 0, 0], dtype='int')
    # Thresholds
    cfg.core_cfgs[0].Xth[0] = 100
    cfg.core_cfgs[0].Xth[1] = 100
    # Set reset value
    cfg.core_cfgs[0].Xreset[0] = np.array([0, XMAX, XMAX, XMAX], 'int')
    cfg.core_cfgs[0].Xreset[1] = np.array([0, MAX, MAX, MAX], 'int')
    # Turn reset on
    cfg.core_cfgs[0].XresetOn[0] = np.array([True, False, False, False],
                                            'bool')
    cfg.core_cfgs[0].XresetOn[1] = np.array([True, False, False, False],
                                            'bool')
    # Synaptic weights
    W = np.zeros([N_UNITS, N_UNITS, N_STATES[0]], 'int')
    W[0, 1, 0] = 50

    # Adjacent matrix
    CW = np.zeros(W.shape, dtype='int')
    CW[0, 1, 0] = 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

    # Tested value
    if len(sys.argv) != 2:
        print('Missing argument! Default value is used[prob=0]!')
        cfg.core_cfgs[0].prob_syn[1] = np.array([0, 0, 0, 0], dtype='int')
    else:
        p = int(sys.argv[1])
        cfg.core_cfgs[0].prob_syn[1] = np.array([p, p, p, p], 'int')

    # NSAT parameters and neurons mapping function
    cfg.core_cfgs[0].nmap = np.array([0, 1], dtype='int')

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

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