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_transforms.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])
def test_sparse_ens_ens(Simulator, seed, plt, allclose): transform = nengo_transforms.Sparse(shape=(2, 3), indices=[[0, 2], [1, 0]], init=[-0.8, 0.6]) with nengo.Network(seed=seed) as net: u = nengo.Node( lambda t: [np.sin(2 * np.pi * t), t, -np.sin(2 * np.pi * t)]) a = nengo.Ensemble(200, 3) b = nengo.Ensemble(200, 2) nengo.Connection(u, a, synapse=None) nengo.Connection(a, b, transform=transform) up = nengo.Probe(u, synapse=nengo.synapses.Alpha(0.01)) bp = nengo.Probe(b, synapse=nengo.synapses.Alpha(0.01)) with pytest.warns(UserWarning, match="Converting Sparse transform"): with Simulator(net) as sim: sim.run(0.4) matrix = transform.init.toarray() plt.plot(sim.trange(), sim.data[up].dot(matrix.T), "--") plt.plot(sim.trange(), sim.data[bp]) assert allclose(sim.data[bp][:, 0], -0.8 * sim.data[up][:, 2], atol=0.2) assert allclose(sim.data[bp][:, 1], 0.6 * sim.data[up][:, 0], atol=0.2)
def test_sparse_chip_to_chip_error(Simulator): with nengo.Network() as net: pre = nengo.Ensemble(100, 4) post = nengo.Ensemble(100, 2) nengo.Connection(pre, post, transform=nengo_transforms.Sparse( shape=(2, 4), indices=[[0, 0], [1, 1]], init=[-1, -1])) with pytest.raises(BuildError, match="on chip to chip"): with Simulator(net): pass
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_transforms.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
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_transforms.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
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": if nengo_transforms is None: pytest.skip("Sparse matrices require nengo transforms") shape = (nb, nb) data = factor * np.ones(nb) rowi = coli = np.arange(nb) if sparse == "nengo": transform = nengo_transforms.Sparse(shape, indices=np.array( (rowi, coli)).T, init=data) elif sparse == "scipy": transform = nengo_transforms.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)