Skip to content

Commit

Permalink
Fix connections to single neurons
Browse files Browse the repository at this point in the history
Multiplying an `n x 1` sparse matrix by a 1-vector was yielding
a vector rather than a matrix. Special-case this to ensure we get
a matrix back.

Fixes #274.
  • Loading branch information
hunse authored and drasmuss committed Apr 15, 2020
1 parent 944c0e2 commit 023e3ae
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ Release history
`#275 <https://github.com/nengo/nengo-loihi/pull/275>`__)
- Fixed bug when probing sliced objects.
(`#284 <https://github.com/nengo/nengo-loihi/pull/284>`__)
- Fixed bug when connecting to a single neuron ensemble with a single scalar
weight. (`#287 <https://github.com/nengo/nengo-loihi/pull/287>`__)

0.10.0 (November 25, 2019)
==========================
Expand Down
3 changes: 3 additions & 0 deletions nengo_loihi/builder/sparse_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def scale_matrix(matrix, scale):
assert scale.size == matrix.shape[1]
diag = scipy.sparse.dia_matrix((scale, 0), shape=(scale.size, scale.size))
return matrix.dot(diag)
elif scale.size == 1:
# avoid bug where a sparse n x 1 matrix times a 1-vector gives a vector
return matrix * scale.item()
else:
return matrix * scale

Expand Down
19 changes: 19 additions & 0 deletions nengo_loihi/tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,3 +785,22 @@ def test_sparse_transforms_empty_neurons(Simulator):
# 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_single_neuron_connection(Simulator, allclose):
"""Addresses https://github.com/nengo/nengo-loihi/issues/274"""

max_rate = 50.0
simtime = 0.4

with nengo.Network() as net:
inp = nengo.Node([1.0])
ens = nengo.Ensemble(1, 1, max_rates=[max_rate], intercepts=[-1.0])
nengo.Connection(inp, ens.neurons, synapse=None)
probe = nengo.Probe(ens.neurons)

with Simulator(net) as sim:
sim.run(simtime)

exp_rate = max_rate * simtime
assert allclose((sim.data[probe] > 0).sum(), exp_rate, rtol=0.1)

0 comments on commit 023e3ae

Please sign in to comment.