Пример #1
0
def test_sparse_validation_errors():
    with pytest.raises(ValidationError, match="Either `init` must be a `scipy.sparse"):
        nengo.Sparse((1, 1))

    nengo.Sparse((3, 3), indices=[(0, 0), (1, 2)], init=[1, 2])
    with pytest.raises(ValidationError, match="Must be a vector.*length as `ind"):
        nengo.Sparse((3, 3), indices=[(0, 0), (1, 2)], init=[1, 2, 3])
Пример #2
0
def test_sparse_transforms_empty_neurons(Simulator):
    """Test that sparse transforms work properly, even if some neurons get no input"""
    n_neurons = 3
    transform = nengo.Sparse(shape=(n_neurons, n_neurons),
                             indices=[(0, 0), (2, 2)],
                             init=[1, 2])

    with nengo.Network() as model:
        x = nengo.Ensemble(
            n_neurons,
            1,
            max_rates=nengo.dists.Choice([200]),
            intercepts=nengo.dists.Choice([-1]),
        )
        y = nengo.Ensemble(
            n_neurons,
            1,
            max_rates=nengo.dists.Choice([100]),
            intercepts=nengo.dists.Choice([0]),
        )
        nengo.Connection(x.neurons, y.neurons, transform=transform)

        probe = nengo.Probe(y.neurons)

    with Simulator(model) as sim:
        # Ensure the model builds and runs correctly as this used to raise a ValueError
        assert sim
        sim.run(0.1)

    # only the first and third neurons should get input, not the second
    spikes = (sim.data[probe] > 0).sum(axis=0)
    assert np.array_equal(spikes > 0, [1, 0, 1])
Пример #3
0
def test_sparse_host_to_chip_error(Simulator):
    with nengo.Network() as net:
        stim = nengo.Node(np.ones(4))
        ens = nengo.Ensemble(100, 2)
        nengo.Connection(
            stim,
            ens,
            transform=nengo.Sparse(shape=(2, 4),
                                   indices=[[0, 0], [1, 1]],
                                   init=[-1, -1]),
        )

    with pytest.raises(BuildError, match="on host to chip connections"):
        with Simulator(net):
            pass
Пример #4
0
def test_sparse_host_to_learning_rule_error(Simulator):
    with nengo.Network() as net:
        err = nengo.Node(np.ones(4))
        pre = nengo.Ensemble(100, 2)
        post = nengo.Ensemble(100, 2)
        conn = nengo.Connection(pre, post, learning_rule_type=nengo.PES())
        nengo.Connection(
            err,
            conn.learning_rule,
            transform=nengo.Sparse(shape=(2, 4),
                                   indices=[[0, 0], [1, 1]],
                                   init=[-1, -1]),
        )

    with pytest.raises(BuildError, match="on host to chip learning rule"):
        with Simulator(net):
            pass
Пример #5
0
def test_neuron_to_neuron(Simulator, factor, do_pre_slice, sparse, seed,
                          allclose, plt):
    # note: we use these weird factor values so that voltages don't line up
    # exactly with the firing threshold.  since loihi neurons fire when
    # voltage > threshold (rather than >=), if the voltages line up
    # exactly then we need an extra spike each time to push `b` over threshold
    dt = 5e-4
    simtime = 0.2

    na = 500  # test big to ensure full weight matrices are not being used

    if do_pre_slice:
        nb = int(np.ceil(na / 2.0))
        pre_slice = slice(None, None, 2)
    else:
        nb = na
        pre_slice = slice(None)

    if sparse != "dense":
        shape = (nb, nb)
        data = factor * np.ones(nb)
        rowi = coli = np.arange(nb)
        if sparse == "nengo":
            transform = nengo.Sparse(shape,
                                     indices=np.array((rowi, coli)).T,
                                     init=data)
        elif sparse == "scipy":
            transform = nengo.Sparse(shape,
                                     init=scipy.sparse.coo_matrix(
                                         (data, (rowi, coli)), shape=shape))
    else:
        transform = factor

    with nengo.Network(seed=seed) as net:

        stim = nengo.Node(lambda t: [np.sin(t * 2 * np.pi / simtime)])
        a = nengo.Ensemble(na, 1)
        nengo.Connection(stim, a)

        b = nengo.Ensemble(
            nb,
            1,
            neuron_type=nengo.SpikingRectifiedLinear(),
            gain=np.ones(nb),
            bias=np.zeros(nb),
        )
        nengo.Connection(a.neurons[pre_slice],
                         b.neurons,
                         synapse=None,
                         transform=transform)

        p_a = nengo.Probe(a.neurons)
        p_b = nengo.Probe(b.neurons)

    with Simulator(net, dt=dt) as sim:
        sim.run(simtime)

    y_ref = np.floor(np.sum(sim.data[p_a][:, pre_slice] > 0, axis=0) * factor)
    y_sim = np.sum(sim.data[p_b] > 0, axis=0)
    plt.plot(y_ref, c="k")
    plt.plot(y_sim)

    assert allclose(y_sim, y_ref, atol=1)