예제 #1
0
def construct_layer(cell_params_lif, pop_list_in, mode, k_size, w_layer):
    max_F = 140. #150.
    max_curr = 1. #1.
    syn = 5.*0.96
    in_num = len(pop_list_in) #populations number in previous layer
    in_size = int(np.sqrt(pop_list_in[0].size)) #in_size*in_size = neuron_num per pop in the previous layer
    pop_layer = []
    if mode > 0: #convoluational layer
        out_num = mode #populations number in current layer
        #print in_num, out_num
        out_size = in_size - k_size + 1
        for j in range(out_num):
            pop_layer.append(p.Population(out_size*out_size, p.IF_curr_exp, cell_params_lif))
            for i in range(in_num):
                conv_pops(pop_list_in[i], pop_layer[j], w_layer[i][j])# * 1000./max_F * max_curr / syn)
    elif mode == 0: #pooling layer
        out_num = in_num #populations number in current layer
        #print in_num, out_num
        out_size = in_size/k_size
        for j in range(out_num):
            pop_layer.append(p.Population(out_size*out_size, p.IF_curr_exp, cell_params_lif))
            pool_pops(pop_list_in[j], pop_layer[j], w_layer[0][0])# * 1000./max_F * max_curr / syn)
    elif mode == -1: #top layer
        out_size = k_size
        #print out_size
        pop_layer.append(p.Population(out_size, p.IF_curr_exp, cell_params_lif))
        out_pops(pop_list_in, pop_layer[0], w_layer)# * 1000./max_F * max_curr / syn)
    return pop_layer
예제 #2
0
def run_test(w_list, cell_para, spike_source_data):
    pop_list = []
    p.setup(timestep=1.0, min_delay=1.0, max_delay=3.0)
    #input poisson layer
    input_size = w_list[0].shape[0]
    pop_in = p.Population(input_size, p.SpikeSourceArray, {'spike_times': []})
    for j in range(input_size):
        pop_in[j].spike_times = spike_source_data[j]
    pop_list.append(pop_in)

    for w in w_list:
        pos_w = np.copy(w)
        pos_w[pos_w < 0] = 0
        neg_w = np.copy(w)
        neg_w[neg_w > 0] = 0

        output_size = w.shape[1]
        pop_out = p.Population(output_size, p.IF_curr_exp, cell_para)
        p.Projection(pop_in,
                     pop_out,
                     p.AllToAllConnector(weights=pos_w),
                     target='excitatory')
        p.Projection(pop_in,
                     pop_out,
                     p.AllToAllConnector(weights=neg_w),
                     target='inhibitory')
        pop_list.append(pop_out)
        pop_in = pop_out

    pop_out.record()
    run_time = np.ceil(np.max(spike_source_data)[0] / 1000.) * 1000
    p.run(run_time)
    spikes = pop_out.getSpikes(compatible_output=True)
    return spikes
예제 #3
0
def test_nest_dense_normalisation():
    p1 = pynn.Population(12, pynn.IF_cond_exp(**v.DEFAULT_NEURON_PARAMETERS))
    p2 = pynn.Population(10, pynn.IF_cond_exp(**v.DEFAULT_NEURON_PARAMETERS))
    l = v.Dense(p1, p2, v.ReLU(), weights=1)
    m = v.Model(l)
    out = m.predict(np.ones(12) * 12, 50)
    assert np.allclose(np.ones(10), out, atol=0.1)
예제 #4
0
def test_nest_dense_create():
    p1 = pynn.Population(12, pynn.IF_cond_exp())
    p2 = pynn.Population(10, pynn.IF_cond_exp())
    d = v.Dense(p1, p2, v.ReLU())
    expected_weights = np.ones((12, 10))
    actual_weights = d.projection.get('weight', format='array')
    assert not np.allclose(actual_weights, expected_weights) # Should be normal distributed
    assert abs(actual_weights.sum()) <= 24
예제 #5
0
def test_nest_dense_shape():
    p1 = pynn.Population(12, pynn.SpikeSourcePoisson(rate = 10))
    p2 = pynn.Population(10, pynn.IF_cond_exp())
    d = v.Dense(p1, p2, v.ReLU(), weights = 1)
    pynn.run(1000)
    d.store_spikes()
    assert d.input.shape == (12,)
    assert d.output.shape[0] == 10
예제 #6
0
def test_replicate_assert_size():
    p1 = pynn.Population(6, pynn.IF_cond_exp())
    p2 = pynn.Population(6, pynn.IF_cond_exp())
    p3 = pynn.Population(5, pynn.IF_cond_exp())
    with pytest.raises(ValueError):
        v.Replicate(p1, (p2, p3))
    with pytest.raises(ValueError):
        v.Replicate(p1, (p3, p2))
예제 #7
0
def test_nest_model_predict_active():
    p1 = pynn.Population(2, pynn.IF_cond_exp())
    p2 = pynn.Population(2, pynn.IF_cond_exp())
    l = v.Dense(p1, p2, v.ReLU(), decoder=v.spike_count, weights=1)
    m = v.Model(l)
    out = m.predict(np.array([1, 1]), 1000)
    assert len(out) == 2
    assert abs(out[0] - out[1]) <= 10  # Must be approx same spikes
예제 #8
0
def test_nest_model_linear_scaling():
    p1 = pynn.Population(2, pynn.IF_cond_exp(**v.DEFAULT_NEURON_PARAMETERS))
    p2 = pynn.Population(2, pynn.IF_cond_exp(**v.DEFAULT_NEURON_PARAMETERS))
    l1 = v.Dense(p1, p2, v.ReLU(), decoder=v.spike_count, weights=1)
    m = v.Model(l1)
    xs = np.array([12, 12])
    out = m.predict(xs, 50)
    assert np.allclose([38, 38], out)
예제 #9
0
def test_nest_model_predict_inactive():
    p1 = pynn.Population(2, pynn.IF_cond_exp())
    p2 = pynn.Population(2, pynn.IF_cond_exp())
    l = v.Dense(p1, p2, v.ReLU(), decoder=v.spike_count)
    m = v.Model(l)
    out = m.predict(np.array([0, 0]), 10)
    assert len(out) == 2
    assert out.sum() < 10
예제 #10
0
def test_nest_projection_gaussian():
    p1 = pynn.Population(2, pynn.IF_cond_exp())
    p2 = pynn.Population(2, pynn.IF_cond_exp())
    c = pynn.Projection(p1, p2,
                        pynn.AllToAllConnector(allow_self_connections=False))
    c.set(weight=pynn.random.RandomDistribution('normal', mu=0.5, sigma=0.1))
    weights = c.get('weight', format='array')
    assert len(weights[weights == 0]) < 1
예제 #11
0
def test_replicate_can_replicate():
    p1 = pynn.Population(6, pynn.IF_cond_exp(i_offset=10))
    p2 = pynn.Population(6, pynn.IF_cond_exp())
    p3 = pynn.Population(6, pynn.IF_cond_exp())
    l = v.Replicate(p1, (p2, p3), v.ReLU(), weights=(1, 1))
    pynn.run(1000)
    l.store_spikes()
    expected = np.ones((2, 6))
    assert np.allclose(expected, l.get_output())
예제 #12
0
 def setUp(self):
     sim.setup()
     self.p1 = sim.Population(7, sim.IF_cond_exp())
     self.p2 = sim.Population(4, sim.IF_cond_exp())
     self.p3 = sim.Population(5, sim.IF_curr_alpha())
     self.syn_rnd = sim.StaticSynapse(weight=0.123, delay=0.5)
     self.syn_a2a = sim.StaticSynapse(weight=0.456, delay=0.4)
     self.random_connect = sim.FixedNumberPostConnector(n=2)
     self.all2all = sim.AllToAllConnector()
예제 #13
0
def test_nest_dense_chain():
    p1 = pynn.Population(12, pynn.SpikeSourcePoisson(rate = 100))
    p2 = pynn.Population(10, pynn.IF_cond_exp())
    p3 = pynn.Population(2, pynn.IF_cond_exp())
    p3.record('spikes')
    d1 = v.Dense(p1, p2, v.ReLU())
    d2 = v.Dense(p2, p3, v.ReLU())
    pynn.run(1000)
    assert len(p3.get_data().segments[-1].spiketrains) > 0
예제 #14
0
def test_nest_dense_reduced_weight_fire():
    p1 = pynn.Population(1, pynn.IF_cond_exp(i_offset=10))
    p2 = pynn.Population(2, pynn.IF_cond_exp())
    d = v.Dense(p1, p2, v.ReLU(), weights = np.array([[1, 0]]))
    pynn.run(1000)
    spiketrains1 = p1.get_data().segments[-1].spiketrains
    spiketrains2 = p2.get_data().segments[-1].spiketrains
    assert spiketrains1[0].size > 0
    assert spiketrains2[0].size > 0
    assert spiketrains2[1].size == 0
예제 #15
0
def test_replicate_create():
    p1 = pynn.Population(6, pynn.IF_cond_exp())
    p2 = pynn.Population(6, pynn.IF_cond_exp())
    p3 = pynn.Population(6, pynn.IF_cond_exp())
    l = v.Replicate(p1, (p2, p3), v.ReLU(), weights=(1, 1))
    pynn.run(1000)
    l.store_spikes()
    assert l.layer1.input.shape == (6, 0)
    assert l.layer2.input.shape == (6, 0)
    assert l.get_output().shape == (2, 6)
예제 #16
0
def test_nest_create_input_populations():
    p1 = pynn.Population(2, pynn.IF_cond_exp())
    p2 = pynn.Population(2, pynn.IF_cond_exp())
    l = v.Dense(p1, p2, v.ReLU())
    m = v.Model(l)
    assert len(m.input_populations) == 2
    inp = np.array([1, 0.2])
    m.set_input(inp)
    assert m.input_populations[0].get('i_offset') == 1
    assert m.input_populations[1].get('i_offset') == 0.2
예제 #17
0
def test_ticket244():
    nest = pyNN.nest
    nest.setup(threads=4)
    p1 = nest.Population(4, nest.IF_curr_exp())
    p1.record('spikes')
    poisson_generator = nest.Population(3, nest.SpikeSourcePoisson(rate=1000.0))
    conn = nest.OneToOneConnector()
    syn = nest.StaticSynapse(weight=1.0)
    nest.Projection(poisson_generator, p1.sample(3), conn, syn, receptor_type="excitatory")
    nest.run(15)
    p1.get_data()
예제 #18
0
def test_nest_model_spike_normalisation():
    p1 = pynn.Population(2, pynn.IF_cond_exp(**v.DEFAULT_NEURON_PARAMETERS))
    p2 = pynn.Population(4, pynn.IF_cond_exp(**v.DEFAULT_NEURON_PARAMETERS))
    p3 = pynn.Population(4, pynn.IF_cond_exp(**v.DEFAULT_NEURON_PARAMETERS))
    l1 = v.Dense(p1, p2, decoder=v.spike_rate(50), weights=1)
    l2 = v.Dense(p2, p3, decoder=v.spike_rate(50), weights=1)
    m = v.Model(l1, l2)
    m.predict([12, 12], 50)
    for rate in np.concatenate((l1.get_output(), l2.get_output())):
        assert rate > 0
        assert rate < 1
예제 #19
0
def test_ticket240():
    nest = pyNN.nest
    nest.setup(threads=4)
    parameters = {'Tau_m': 17.0}
    p1 = nest.Population(4, nest.IF_curr_exp())
    p2 = nest.Population(5, nest.native_cell_type("ht_neuron")(**parameters))
    conn = nest.AllToAllConnector()
    syn = nest.StaticSynapse(weight=1.0)
    prj = nest.Projection(p1, p2, conn, syn, receptor_type='AMPA')  # This should be a nonstandard receptor type but I don't know of one to use.
    connections = prj.get(('weight',), format='list')
    assert len(connections) > 0
예제 #20
0
def test_nest_dense_backprop():
    p1 = pynn.Population(4, pynn.IF_cond_exp())
    p2 = pynn.Population(2, pynn.IF_cond_exp())
    l = v.Dense(p1, p2, v.UnitActivation(), weights = 1, decoder = lambda x: x)
    old_weights = l.get_weights()
    l.input_cache = np.ones((1, 4)) # Mock spikes
    errors = l.backward(np.array([[0, 1]]), lambda w, g, b, bg: (w - g, b - bg))
    expected_errors = np.zeros((2, 4)) + 4
    assert np.allclose(errors, expected_errors)
    expected_weights = np.tile([1, -3], (4, 1))
    assert np.allclose(l.get_weights(), expected_weights)
예제 #21
0
def test_nest_input_projection():
    p1 = pynn.Population(2, pynn.IF_cond_exp(**v.DEFAULT_NEURON_PARAMETERS))
    p2 = pynn.Population(2, pynn.IF_cond_exp(**v.DEFAULT_NEURON_PARAMETERS))
    l = v.Dense(p1, p2, v.ReLU(), weights=1)
    m = v.Model(l)
    t = v.LinearTranslation()
    assert np.allclose(l.get_weights(), np.ones((2, 2)))
    assert m.input_projection[0].weight == t.weights(1, 1)
    assert m.input_projection[1].weight == t.weights(1, 1)
    m.predict([1, 1], 50)
    spiketrains = l.output
    assert abs(len(spiketrains[0]) - len(spiketrains[1])) <= 20
예제 #22
0
def test_nest_dense_projection():
    p1 = pynn.Population(12, pynn.SpikeSourcePoisson(rate = 10))
    p2 = pynn.Population(10, pynn.IF_cond_exp())
    p2.record('spikes')
    d = v.Dense(p1, p2, v.ReLU(), weights = 1)
    pynn.run(1000)
    spiketrains = p2.get_data().segments[-1].spiketrains
    assert len(spiketrains) == 10
    avg_len = np.array(list(map(len, spiketrains))).mean()
    # Should have equal activation
    for train in spiketrains:
        assert abs(len(train) - avg_len) <= 1
예제 #23
0
def test_nest_dense_restore():
    p1 = pynn.Population(12, pynn.IF_cond_exp())
    p2 = pynn.Population(10, pynn.IF_cond_exp())
    d = v.Dense(p1, p2, v.ReLU(), weights = 2)
    d.set_weights(-1)
    t = v.LinearTranslation()
    assert np.array_equal(d.projection.get('weight', format='array'),
             t.weights(np.ones((12, 10)) * -1, 12))
    d.projection.set(weight = 1) # Simulate reset()
    assert np.array_equal(d.projection.get('weight', format='array'),
            np.ones((12, 10)))
    d.restore_weights()
    assert np.array_equal(d.projection.get('weight', format='array'),
            t.weights(np.ones((12, 10)) * -1, 12))
예제 #24
0
def test_nest_model_backwards():
    p1 = pynn.Population(2, pynn.IF_cond_exp())
    p2 = pynn.Population(3, pynn.IF_cond_exp())
    l1 = v.Dense(p1, p2, v.ReLU(), decoder=v.spike_count_linear, weights=1)
    m = v.Model(l1)
    xs = np.array([12, 12])
    spikes = m.predict(xs, 50)

    def l(w, g, b, bg):
        return w - g, b - bg

    m.backward([0, 1, 1], l)  # no learning rate
    expected_weights = np.array([[1, 0, 0], [1, 0, 0]])
    assert np.allclose(l1.get_weights(), expected_weights, atol=0.2)
예제 #25
0
def run_sim(ncell):

    print "Cells: ", ncell

    setup0 = time.time()

    sim.setup(timestep=0.1)

    hh_cell_type = sim.HH_cond_exp()

    hh = sim.Population(ncell, hh_cell_type)

    pulse = sim.DCSource(amplitude=0.5, start=20.0, stop=80.0)
    pulse.inject_into(hh)

    hh.record('v')

    setup1 = time.time()

    t0 = time.time()

    sim.run(100.0)

    v = hh.get_data()

    sim.end()

    t1 = time.time()

    setup_total = setup1 - setup0
    run_total = t1 - t0
    print "Setup: ", setup_total
    print "Run: ", run_total
    print "Total sim time: ", setup_total + run_total
    return run_total
def create_empty_input_layers_for_scales(target: np.array, scales: [float])\
        -> Dict[float, List[Layer]]:
    """
    Creates empty input layers from the target image shape for every scale in
    the passed list.

    Parameters:
        `target`: The target image from which to compute the gabor filters and
                  create the input layers

        `scales`: A list of the scales for which to create input layers
    
    Returns:
        A dictionary which contains for each scale a list of four input layers,
        one for each orientation
    """
    input_layers = {}
    feature_names = cm.get_gabor_feature_names()
    for size in scales:
        print('Creating input layers for size', size)
        n = round(target.shape[0] * size)
        m = round(target.shape[1] * size)
        input_layers[size] = [Layer(sim.Population(n * m,
                                                   sim.IF_curr_exp(),
                                label=feature_name), (n, m))\
                              for feature_name in feature_names]
    return input_layers
def create_C2_layers(S2_layers: Dict[float, Sequence[Layer]],
                     s2_prototype_cells: int) -> List[sim.Population]:
    """
    Creates the populations of the C2 layer, one for each S2 prototype cell,
    containing only a single cell which max-pools the spikes of all layers of a
    prototype.

    Parameters:
        `S2_layers`: A dictionary containing for each scale a list of S2
                     layers, one for each prototype cell

        `s2_prototype_cells`: The number of S2 prototype cells

    Returns:
        A list of populations of size one, one population for each prototype
        cell
    """
    no_inh_w = 17.15  # synapse weight without S2 inhibitions
    with_inh_w = 4 * no_inh_w  # synapse weight with S2 inhibitions
    C2_populations = [sim.Population(1, sim.IF_curr_exp(),
                                     label=str(prot))\
                        for prot in range(s2_prototype_cells)]
    total_connections = sum(
        map(lambda ll: ll[0].shape[0] * ll[0].shape[1], S2_layers.values()))
    for s2ll in S2_layers.values():
        for prot in range(s2_prototype_cells):

            sim.Projection(
                s2ll[prot].population, C2_populations[prot],
                sim.AllToAllConnector(),
                sim.StaticSynapse(weight=with_inh_w / total_connections))
    return C2_populations
예제 #28
0
파일: test_nest.py 프로젝트: wau/PyNN
def test_record_native_model():
    if not have_nest:
        raise SkipTest
    nest = pyNN.nest
    from pyNN.random import RandomDistribution

    init_logging(logfile=None, debug=True)

    nest.setup()

    parameters = {'tau_m': 17.0}
    n_cells = 10
    p1 = nest.Population(n_cells,
                         nest.native_cell_type("ht_neuron")(**parameters))
    p1.initialize(V_m=-70.0, Theta=-50.0)
    p1.set(theta_eq=-51.5)
    #assert_arrays_equal(p1.get('theta_eq'), -51.5*numpy.ones((10,)))
    assert_equal(p1.get('theta_eq'), -51.5)
    print(p1.get('tau_m'))
    p1.set(tau_m=RandomDistribution('uniform', low=15.0, high=20.0))
    print(p1.get('tau_m'))

    current_source = nest.StepCurrentSource(
        times=[50.0, 110.0, 150.0, 210.0],
        amplitudes=[0.01, 0.02, -0.02, 0.01])
    p1.inject(current_source)

    p2 = nest.Population(
        1,
        nest.native_cell_type("poisson_generator")(rate=200.0))

    print("Setting up recording")
    p2.record('spikes')
    p1.record('V_m')

    connector = nest.AllToAllConnector()
    syn = nest.StaticSynapse(weight=0.001)

    prj_ampa = nest.Projection(p2, p1, connector, syn, receptor_type='AMPA')

    tstop = 250.0
    nest.run(tstop)

    vm = p1.get_data().segments[0].analogsignals[0]
    n_points = int(tstop / nest.get_time_step()) + 1
    assert_equal(vm.shape, (n_points, n_cells))
    assert vm.max() > 0.0  # should have some spikes
예제 #29
0
def test_nest_dense_increased_weight_fire():
    p1 = pynn.Population(1, pynn.SpikeSourcePoisson(rate = 1))
    p2 = pynn.Population(1, pynn.IF_cond_exp())
    p2.record('spikes')
    d = v.Dense(p1, p2, v.ReLU(), weights = 2)
    pynn.run(1000)
    spiketrains = p2.get_data().segments[-1].spiketrains
    count1 = spiketrains[0].size
    pynn.reset()
    p1 = pynn.Population(1, pynn.SpikeSourcePoisson(rate = 1))
    p2 = pynn.Population(1, pynn.IF_cond_exp())
    p2.record('spikes')
    d = v.Dense(p1, p2, v.ReLU(), weights = 2)
    pynn.run(1000)
    spiketrains = p2.get_data().segments[-1].spiketrains
    count2 = spiketrains[0].size
    assert count2 >= count1 * 2 
def create_empty_spike_source_layer_with_shape(shape):
    """
    Creates a spike source layer with the given shape and its firing rates set
    to zero. This is also a helper function of create_spike_source_layer_from().
    """
    spike_source_layer = sim.Population(
        size=shape[0] * shape[1], cellclass=sim.SpikeSourcePoisson(rate=0))
    return Layer(spike_source_layer, shape)