2010/rob10/SpiNNaker: robodrive.py

File robodrive.py, 7.2 KB (added by supplemango, 7 years ago)
Line 
1"""
2Robodrive pyNN script.
3
4the first 2 layers are 2 16x16 matrixes (one for polarity 0 and one for 1)
5The third layer is a 4x4 "neural" subsampling of the input. Its sensitivity can be tuned by changing the parameter sampler_weight. The more/more frequent the cells in the subsampling neuron "receptive field" fire, the more likely will the subsampling neuron fire, inhibiting the rest of the subsampling neurons (WTA)
6The subsampler winning units project to the output layer, so to keep the main stimulus in the center of the visual field. The outputs are also in competition, so to achieve better stability.
7
8Francesco Galluppi, SpiNNaker Group, University of Manchester - Telluride Workshop 2010
9
10"""
11####
12def subSamplerConnector2D(size_in, size_out, weights, delays):
13    """
14    size_in = size of the input population (2D = size_in x size_in)   
15    size_out = size of the sampled population
16    weights = averaging weight value (each connection will have this value) must be float
17    delays = averaging delay value (each connection will have this value)
18    """
19    out = []
20    for i in range(size_in):
21         for j in range(size_in):
22             out.append(((i, j),(i/size_out, j/size_out), weights, delays))
23    return out
24
25#### START FROM HERE
26from pyNN.spiNNaker import *            # Imports the pyNN.spiNNaker module
27from pyNN.random import NumpyRNG, RandomDistribution
28kernelseed  = 43210987  # seed for random generator(s) used during simulation
29rng = NumpyRNG(kernelseed)      # Numpy Random Number Generator
30
31runtime = 100000
32
33# Simulation Setup
34# runScripts = 0 will only generate the txt files in the data/txt_files directory. runScripts = 1 will run everything from pynn
35setup(timestep=1.0, min_delay = 1.0, max_delay = 11.0, runScripts=1)               
36
37
38# Cell parameters
39tau_m    = 32.0    # (ms)
40cm       = 1
41v_rest   = -65.  # (mV)
42v_thresh = -50.   # (mV)
43v_reset  = -70.   # (mV)
44t_refrac = 1.     # (ms) (clamped at v_reset)
45tau_syn = 1
46
47# Network parameters
48SIZE_IN = 16
49SIZE_SUB = 4
50SIZE_OUT = 6
51
52# Connection parameters for the subsampler
53sampler_weight  = 1             # Tune accordingly to input rate
54sampler_delay   = 1.0
55
56# weight for the WTA mechanism
57wta_weight = -4
58
59# weight to the outputs
60output_weight = 20.0
61output_delay = 1.0
62
63# Relative index of the output neuron in their population
64OUT_FWD = 0     
65OUT_RIGHT = 2   
66OUT_LEFT = 3   
67
68
69
70# cell_params will be passed to the constructor of the Population Object
71# change this section if you want to use Izhikevich neurons - do it at your own risk!
72cell_params = {
73    'tau_m'      : tau_m,    'cm'         : cm,       'v_init'     : v_reset,   
74    'v_rest'     : v_rest,   'v_reset'    : v_reset,  'v_thresh'   : v_thresh,
75    'tau_syn_E'  : tau_syn,  'tau_syn_I'  : tau_syn,  'tau_refrac' : t_refrac
76    }
77
78cell_type = IF_curr_exp
79
80####### POPULATIONS: neuron ids are assigned linearly
81input0 = Population((SIZE_IN, SIZE_IN),         
82                   cell_type,   # Neuron Type
83                   cell_params,   # Neuron Parameters
84                   label="Input_Polarity_0") # Label
85
86input1 = Population((SIZE_IN, SIZE_IN),         
87                   cell_type,   # Neuron Type
88                   cell_params,   # Neuron Parameters
89                   label="Input_Polarity_1") # Label
90
91outputs = Population(SIZE_OUT,         # size 512-517
92                   cell_type,   # Neuron Type
93                   cell_params,   # Neuron Parameters
94                   label="Output") # Label
95
96
97subsampler = Population((SIZE_SUB, SIZE_SUB),         
98                   cell_type,   # Neuron Type
99                   cell_params,   # Neuron Parameters
100                   label="Subsampler") # Label
101
102# Randomize initial state of the input layers
103uniformDistr = RandomDistribution('uniform', [v_reset-5,v_thresh-10], rng)
104input0.randomInit(uniformDistr)
105input1.randomInit(uniformDistr)
106
107
108# The filter between the inputs and the sampled population is set up here
109# Is done by creating a list with the function on top of this script
110subsampler_filter = subSamplerConnector2D(SIZE_IN, SIZE_SUB, sampler_weight, sampler_delay)
111
112###### PROJECTIONS
113#input0->subsampler excitatory connection takes the parameters from the list built
114input0_subsampler = Projection(input0,              # Source Population
115                    subsampler,                   # Destination Population
116                    FromListConnector(subsampler_filter) # Connection function
117                    )
118#input1->subsampler
119input1_subsampler = Projection(input1,              # Source Population
120                    subsampler,                   # Destination Population
121                    FromListConnector(subsampler_filter) # Connection function
122                    )
123
124# WTA between cells in subsampler
125wta_inhibition = Projection(subsampler,              # Source Population
126                    subsampler,                   # Destination Population
127                    AllToAllConnector(allow_self_connections=False),
128                    target='inhibitory'
129                    )
130
131wta_inhibition.setWeights(wta_weight)
132wta_inhibition.setDelays(5.0)
133
134# WTA between outputs
135wta_inhibition2 = Projection(outputs,              # Source Population
136                    outputs,                   # Destination Population
137                    AllToAllConnector(allow_self_connections=False),
138                    target='inhibitory'
139                    )
140
141wta_inhibition2.setWeights(wta_weight*2)
142wta_inhibition2.setDelays(5.0)
143
144### SUBSAMPLE->OUTPUT MAP (4x4)
145### You can build this yourself, accordingly to your needs
146### I've organized it this way (L = Left, R = right, F = FORWARD). It tries to keep the main stimulus in the center of the visual field
147### L F F R    (0,0) (0,1) etc...
148### L F F R    (1,0)....
149### L F F R
150### L F F R
151output_map = []
152
153# Go left
154output_map.append(((0,0), ((OUT_LEFT,)), output_weight, output_delay))
155output_map.append(((1,0), ((OUT_LEFT,)), output_weight, output_delay))
156output_map.append(((2,0), ((OUT_LEFT,)), output_weight, output_delay))
157output_map.append(((3,0), ((OUT_LEFT,)), output_weight, output_delay))
158
159
160# Go Right
161output_map.append(((0,3), ((OUT_RIGHT,)), output_weight, output_delay))
162output_map.append(((1,3), ((OUT_RIGHT,)), output_weight, output_delay))
163output_map.append(((2,3), ((OUT_RIGHT,)), output_weight, output_delay))
164output_map.append(((3,3), ((OUT_RIGHT,)), output_weight, output_delay))
165
166# Go Forward
167output_map.append(((0,1), ((OUT_FWD,)), output_weight, output_delay))
168output_map.append(((0,2), ((OUT_FWD,)), output_weight, output_delay))
169output_map.append(((1,1), ((OUT_FWD,)), output_weight, output_delay))
170output_map.append(((1,2), ((OUT_FWD,)), output_weight, output_delay))
171output_map.append(((2,1), ((OUT_FWD,)), output_weight, output_delay))
172output_map.append(((2,2), ((OUT_FWD,)), output_weight, output_delay))
173output_map.append(((3,2), ((OUT_FWD,)), output_weight, output_delay))
174output_map.append(((3,1), ((OUT_FWD,)), output_weight, output_delay))
175
176subsampler_outputs = Projection(subsampler,              # Source Population
177                    outputs,                   # Destination Population
178                    FromListConnector(output_map) # Connection function
179                    )
180
181# This will generate the outputs.txt file used to send packets on the ethernet
182outputs.record('spikes', record_from="eth")   
183
184
185
186run(runtime)              # Simulation time