def test_configure_weight_solver(Simulator, seed, plt, allclose): """Ensures that connections that don't use the weight solver ignore it""" n1, n2 = 100, 101 function = lambda x: x ** 2 with nengo.Network(seed=seed) as net: net.config[nengo.Connection].solver = nengo.solvers.LstsqL2(weights=True) u = nengo.Node(lambda t: np.sin(8 * t)) a = nengo.Ensemble(n1, 1) b = nengo.Ensemble(n2, 1) v = nengo.Node(size_in=1) up = nengo.Probe(u, synapse=nengo.Alpha(0.01)) vp = nengo.Probe(v, synapse=nengo.Alpha(0.01)) nengo.Connection(u, a) ens_conn = nengo.Connection(a, b, function=function) nengo.Connection(b, v) with nengo.Simulator(net) as sim: sim.run(1.0) t = sim.trange() x = sim.data[up] y = function(x) z = sim.data[vp] assert sim.data[ens_conn].weights.shape == (n2, n1) assert signals_allclose( t, y, z, buf=0.01, delay=0.015, atol=0.05, rtol=0.05, plt=plt, allclose=allclose )
def test_input_synapses(Simulator, allclose, plt): synapse = 0.1 with nengo.Network() as net: stim = nengo.Node(lambda t: 1 if t % 0.5 < 0.25 else 0) ens = nengo.Ensemble(n_neurons=1, dimensions=1, encoders=[[1]], intercepts=[0], max_rates=[50]) nengo.Connection(stim, ens, synapse=synapse) p_stim = nengo.Probe(stim) p_neurons = nengo.Probe(ens.neurons) with Simulator(net) as sim: sim.run(0.5) with nengo.Simulator(net) as ref: ref.run(0.5) t = sim.trange() ref_filt = nengo.Alpha(0.03).filtfilt(ref.data[p_neurons]) sim_filt = nengo.Alpha(0.03).filtfilt(sim.data[p_neurons]) plt.plot(t, ref_filt, label="nengo") plt.plot(t, sim_filt, label="nengo_loihi") plt.legend(loc="best") plt.twinx() plt.plot(t, sim.data[p_stim], c="k") # Only looking at t < 0.4 as there are weird effects at the end assert allclose(ref_filt[t < 0.4], sim_filt[t < 0.4], atol=1.5)
def test_node_neurons(decode_neurons, tolerance, Simulator, seed, plt): sim_time = 0.2 stim_fn = lambda t: 0.9 * np.sin(2 * np.pi * t / sim_time) out_synapse = nengo.Alpha(0.03) stim_synapse = out_synapse.combine(nengo.Alpha(0.005)) with nengo.Network(seed=seed) as model: stim = nengo.Node(stim_fn) a = nengo.Ensemble(n_neurons=100, dimensions=1) nengo.Connection(stim, a) p_stim = nengo.Probe(stim, synapse=stim_synapse) p_a = nengo.Probe(a, synapse=out_synapse) build_model = Model() build_model.node_neurons = decode_neurons with Simulator(model, model=build_model) as sim: sim.run(sim_time) t = sim.trange() target = sim.data[p_stim] error = np.abs(sim.data[p_a] - target).mean() plt.plot(t, target) plt.plot(t, sim.data[p_a]) plt.ylim([-1.1, 1.1]) plt.title("error = %0.2e" % error) assert error < tolerance
def test_function_points(Simulator, seed, rng, plt, allclose): x = rng.uniform(-1, 1, size=(1000, 1)) y = -x with nengo.Network(seed=seed) as model: u = nengo.Node(nengo.processes.WhiteSignal(1.0, high=9, rms=0.3)) a = nengo.Ensemble(100, 1) v = nengo.Node(size_in=1) nengo.Connection(u, a, synapse=None) nengo.Connection(a, v, eval_points=x, function=y) up = nengo.Probe(u, synapse=nengo.Alpha(0.01)) vp = nengo.Probe(v, synapse=nengo.Alpha(0.01)) with Simulator(model, seed=seed) as sim: sim.run(1.0) assert signals_allclose( sim.trange(), -sim.data[up], sim.data[vp], buf=0.01, delay=0.005, atol=5e-2, rtol=3e-2, plt=plt, allclose=allclose, )
def test_add_inputs(decode_neurons, tolerance, Simulator, seed, plt): sim_time = 2.0 pres_time = sim_time / 4 eval_time = sim_time / 8 stim_values = [[0.5, 0.5], [0.5, -0.9], [-0.7, -0.3], [-0.3, 1.0]] stim_times = np.arange(0, sim_time, pres_time) stim_fn_a = nengo.processes.Piecewise( {t: stim_values[i][0] for i, t in enumerate(stim_times)}) stim_fn_b = nengo.processes.Piecewise( {t: stim_values[i][1] for i, t in enumerate(stim_times)}) with nengo.Network(seed=seed) as model: stim_a = nengo.Node(stim_fn_a) stim_b = nengo.Node(stim_fn_b) a = nengo.Ensemble(n_neurons=100, dimensions=1) b = nengo.Ensemble(n_neurons=100, dimensions=1) nengo.Connection(stim_a, a) nengo.Connection(stim_b, b) c = nengo.Ensemble(n_neurons=100, dimensions=1) nengo.Connection(a, c) nengo.Connection(b, c) out_synapse = nengo.Alpha(0.03) stim_synapse = out_synapse.combine(nengo.Alpha(0.005)).combine( nengo.Alpha(0.005)) p_stim_a = nengo.Probe(stim_a, synapse=stim_synapse) p_stim_b = nengo.Probe(stim_b, synapse=stim_synapse) p_c = nengo.Probe(c, synapse=out_synapse) build_model = Model() build_model.decode_neurons = decode_neurons with Simulator(model, model=build_model) as sim: sim.run(sim_time) t = sim.trange() tmask = np.zeros(t.shape, dtype=bool) for pres_t in np.arange(0, sim_time, pres_time): t0 = pres_t + pres_time - eval_time t1 = pres_t + pres_time tmask |= (t >= t0) & (t <= t1) target = sim.data[p_stim_a] + sim.data[p_stim_b] error = np.abs(sim.data[p_c][tmask] - target[tmask]).mean() plt.plot(t, target) plt.plot(t, sim.data[p_c]) plt.ylim([-1.1, 1.1]) plt.title("error = %0.2e" % error) assert error < tolerance
def piecewise_net(n_pres, pres_time, seed): values = np.linspace(-1, 1, n_pres) with nengo.Network(seed=seed) as net: add_params(net) inp = nengo.Node(nengo.processes.PresentInput(values, pres_time), size_out=1) ens = nengo.Ensemble(100, 1) nengo.Connection(inp, ens) net.probe = nengo.Probe(ens, synapse=nengo.Alpha(0.01)) node = nengo.Node(size_in=1) nengo.Connection(ens, node, synapse=nengo.Alpha(0.01)) net.node_probe = nengo.Probe(node) return net, values
def test_pickle_model(Simulator, seed, allclose): t_run = 0.5 simseed = seed + 1 with nengo.Network(seed=seed) as network: u = nengo.Node(nengo.processes.WhiteSignal(t_run, 5)) a = nengo.Ensemble(100, 1) b = nengo.Ensemble(100, 1) nengo.Connection(u, a, synapse=None) nengo.Connection(a, b, function=np.square) up = nengo.Probe(u, synapse=0.01) ap = nengo.Probe(a, synapse=0.01) bp = nengo.Probe(b, synapse=nengo.Alpha(0.01)) with Simulator(network, seed=simseed) as sim: sim.run(t_run) t0, u0, a0, b0 = sim.trange(), sim.data[up], sim.data[ap], sim.data[bp] pkls = pickle.dumps(dict(model=sim.model, up=up, ap=ap, bp=bp)) # reload model del network, sim, up, ap, bp pkl = pickle.loads(pkls) up, ap, bp = pkl["up"], pkl["ap"], pkl["bp"] with Simulator(None, model=pkl["model"], seed=simseed) as sim: sim.run(t_run) t1, u1, a1, b1 = sim.trange(), sim.data[up], sim.data[ap], sim.data[bp] tols = dict(atol=1e-5) assert allclose(t1, t0, **tols) assert allclose(u1, u0, **tols) assert allclose(a1, a0, **tols) assert allclose(b1, b0, **tols)
def test_unfiltered(Simulator, seed, rng): dt = 0.001 T = 1.0 sys = nengo.Alpha(0.1) synapse = 0.01 with Network(seed=seed) as model: stim = nengo.Node( output=nengo.processes.WhiteSignal(T, high=10, seed=seed)) subnet = LinearNetwork(sys, 1, synapse=synapse, dt=dt, neuron_type=nengo.neurons.Direct()) nengo.Connection(stim, subnet.input, synapse=None) assert subnet.input_synapse is None assert subnet.output_synapse is None p_ideal = nengo.Probe(subnet.input, synapse=sys) p_output = nengo.Probe(subnet.output, synapse=synapse) with Simulator(model, dt=dt) as sim: sim.run(T) assert np.allclose(sim.data[p_output], sim.data[p_ideal])
def test_conv_non_lowpass(Simulator): k = 10 d = 5 with nengo.Network() as model: a = nengo.Ensemble(n_neurons=k**2, dimensions=k) x = nengo.Ensemble(n_neurons=d, dimensions=d, gain=np.ones(d), bias=np.ones(d)) conv = nengo.Convolution(n_filters=d, input_shape=(k, k, 1), strides=(1, 1), kernel_size=(k, k)) assert conv.size_in == k**2 assert conv.size_out == d nengo.Connection(a.neurons, x.neurons, transform=conv, synapse=nengo.Alpha(0.005)) with pytest.raises(NotImplementedError, match="non-Lowpass synapses"): with Simulator(model): pass
def test_conv_onchip(Simulator, plt): """Tests a fully on-chip conv connection. """ from nengo._vendor.npconv2d.conv2d import conv2d kernel = np.array([[-1, 2, -1], [-1, 2, -1], [-1, 2, -1]], dtype=float) kernel /= kernel.max() image = np.array([[1, 2, 1, 2, 0], [2, 3, 2, 1, 1], [1, 2, 1, 2, 3], [2, 3, 2, 1, 1], [1, 2, 1, 2, 0]], dtype=float) image /= image.max() input_scale = 119. bias = input_scale * image.ravel() neuron_type = nengo.SpikingRectifiedLinear() y_ref = LoihiSpikingRectifiedLinear().rates(image.ravel(), input_scale, 0) y_ref = conv2d(y_ref.reshape(1, 5, 5, 1), kernel.reshape(3, 3, 1, 1), pad='VALID') y_ref = LoihiSpikingRectifiedLinear().rates(y_ref.ravel(), 1., 0.).reshape(3, 3) with nengo.Network() as net: a = nengo.Ensemble(bias.size, 1, neuron_type=neuron_type, gain=nengo.dists.Choice([0]), bias=bias) transform = nengo_transforms.Convolution(n_filters=1, input_shape=(5, 5, 1), init=kernel.reshape( 3, 3, 1, 1)) b = nengo.Ensemble(transform.output_shape.size, 1, neuron_type=neuron_type, gain=nengo.dists.Choice([1]), bias=nengo.dists.Choice([0])) nengo.Connection(a.neurons, b.neurons, transform=transform) bp = nengo.Probe(b.neurons, synapse=nengo.Alpha(0.02)) with Simulator(net) as sim: sim.run(0.3) y_ref = y_ref / input_scale y = sim.data[bp][-1].reshape(3, -1) / input_scale plt.subplot(121) plt.imshow(y_ref) plt.colorbar() plt.subplot(122) plt.imshow(y) plt.colorbar() assert np.allclose(y, y_ref, atol=0.02, rtol=0.1)
def test_neuron_probes(precompute, probe_target, Simulator, seed, plt, allclose): simtime = 0.3 with nengo.Network(seed=seed) as model: stim = nengo.Node(lambda t: [np.sin(t * 2 * np.pi / simtime)]) a = nengo.Ensemble( 1, 1, neuron_type=nengo.LIF(min_voltage=-1), encoders=nengo.dists.Choice([[1]]), max_rates=nengo.dists.Choice([100]), intercepts=nengo.dists.Choice([0.0]), ) nengo.Connection(stim, a, synapse=None) p_stim = nengo.Probe(stim, synapse=0.005) p_neurons = nengo.Probe(a.neurons, probe_target) probe_synapse = nengo.Alpha(0.01) p_stim_f = nengo.Probe(stim, synapse=probe_synapse.combine( nengo.Lowpass(0.005))) p_neurons_f = nengo.Probe(a.neurons, probe_target, synapse=probe_synapse) with Simulator(model, precompute=precompute) as sim: sim.run(simtime) scale = float(sim.data[p_neurons].max()) t = sim.trange() x = sim.data[p_stim] xf = sim.data[p_stim_f] y = sim.data[p_neurons] / scale yf = sim.data[p_neurons_f] / scale plt.plot(t, x, label="stim") plt.plot(t, xf, label="stim filt") plt.plot(t, y, label="loihi") plt.plot(t, yf, label="loihi filt") plt.legend() if probe_target == "input": # shape of current input should roughly match stimulus assert allclose(y, x, atol=0.4, rtol=0) # noisy, so rough match assert allclose(yf, xf, atol=0.05, rtol=0) # tight match elif probe_target == "voltage": # check for voltage fluctuations (spiking) when stimulus is positive, # and negative voltage when stimulus is most negative spos = (t > 0.1 * simtime) & (t < 0.4 * simtime) assert allclose(yf[spos], 0.5, atol=0.1, rtol=0.1) assert y[spos].std() > 0.25 sneg = (t > 0.7 * simtime) & (t < 0.9 * simtime) assert np.all(y[sneg] < 0)
def test_dt(dt, pre_on_chip, Simulator, seed, plt, allclose): function = lambda x: -x simtime = 0.2 probe_synapse = nengo.Alpha(0.01) conn_synapse = nengo.Lowpass(0.005) stim_synapse = probe_synapse # stim synapse accounts for delays in connections/probes, so we can compare if pre_on_chip: stim_synapse = stim_synapse.combine(conn_synapse) ens_params = dict( intercepts=nengo.dists.Uniform(-0.9, 0.9), max_rates=nengo.dists.Uniform(100, 120), ) with nengo.Network(seed=seed) as model: nengo_loihi.add_params(model) stim = nengo.Node(lambda t: -(np.sin(2 * np.pi * t / simtime))) stim_p = nengo.Probe(stim, synapse=stim_synapse) pre = nengo.Ensemble(100, 1, **ens_params) model.config[pre].on_chip = pre_on_chip pre_p = nengo.Probe(pre, synapse=probe_synapse) post = nengo.Ensemble(101, 1, **ens_params) post_p = nengo.Probe(post, synapse=probe_synapse) nengo.Connection(stim, pre, synapse=None) nengo.Connection( pre, post, function=function, synapse=conn_synapse, solver=nengo.solvers.LstsqL2(weights=True), ) with Simulator(model, dt=dt) as sim: assert sim.model.decode_tau == conn_synapse.tau sim.run(simtime) x = sim.data[stim_p] y = function(x) if pre_on_chip: y = conn_synapse.filt(y, dt=dt) else: y = conn_synapse.combine(conn_synapse).filt(y, dt=dt) plt.plot(sim.trange(), x, "k--") plt.plot(sim.trange(), y, "k--") plt.plot(sim.trange(), sim.data[pre_p]) plt.plot(sim.trange(), sim.data[post_p]) assert allclose(sim.data[pre_p], x, rtol=0.1, atol=0.1) assert allclose(sim.data[post_p], y, rtol=0.1, atol=0.1)
def test_overridden_configs(): # Using default synapses, all that are not None should be # Lowpass with tau of 0.002 (AMPA) or 0.008 (GABA) bg = nengo.networks.BasalGanglia(dimensions=2) for connection in bg.all_connections: if connection.synapse is None: continue assert isinstance(connection.synapse, nengo.Lowpass) assert connection.synapse.tau in (0.002, 0.008) # These configs do not override the synapse on the connection. # The synapses should be the same as before, and the config should # be unchanged after instantiation, even though inside the class # it is changed. ampa_config = nengo.Config(nengo.Connection) ampa_config[nengo.Connection].solver = nengo.solvers.LstsqL2nz() assert "synapse" not in ampa_config[nengo.Connection] gaba_config = nengo.Config(nengo.Connection) gaba_config[nengo.Connection].solver = nengo.solvers.LstsqL2nz() assert "synapse" not in gaba_config[nengo.Connection] bg = nengo.networks.BasalGanglia( dimensions=2, ampa_config=ampa_config, gaba_config=gaba_config) for connection in bg.all_connections: if connection.synapse is None: continue # Same synapses assert isinstance(connection.synapse, nengo.Lowpass) assert connection.synapse.tau in (0.002, 0.008) # But solver should be different assert isinstance(connection.solver, nengo.solvers.LstsqL2nz) # Ensure default synapse has been removed from both objects assert "synapse" not in ampa_config[nengo.Connection] assert "synapse" not in gaba_config[nengo.Connection] # This config overrides the synapse on AMPA and GABA connections config_with_synapse = nengo.Config(nengo.Connection) config_with_synapse[nengo.Connection].synapse = nengo.Alpha(0.1) bg = nengo.networks.BasalGanglia( dimensions=2, ampa_config=config_with_synapse, gaba_config=config_with_synapse, ) for connection in bg.all_connections: if connection.synapse is None: continue # Synapse changes assert isinstance(connection.synapse, nengo.Alpha) assert connection.synapse.tau == 0.1 # Solver should be the same assert not isinstance(connection.solver, nengo.solvers.LstsqL1) # Ensure synapse is the same as before passing to BasalGanglia synapse = config_with_synapse[nengo.Connection].synapse assert isinstance(synapse, nengo.Alpha) assert synapse.tau == 0.1
def test_pes_pre_synapse_type_error(Simulator): with nengo.Network() as model: pre = nengo.Ensemble(10, 1) post = nengo.Node(size_in=1) rule_type = nengo.PES(pre_synapse=nengo.Alpha(0.005)) conn = nengo.Connection(pre, post, learning_rule_type=rule_type) nengo.Connection(post, conn.learning_rule) with pytest.raises(ValidationError): with Simulator(model): pass
def test_pickle_sim(Simulator, seed, allclose, optimize, dt, progress_bar): trun0 = 0.5 trun1 = 0.5 simseed = seed + 1 progress = TerminalProgressBar() if progress_bar else progress_bar with nengo.Network(seed=seed) as network: u = nengo.Node(nengo.processes.WhiteSignal(trun0 + trun1, high=5)) a = nengo.Ensemble(100, 1) b = nengo.Ensemble(100, 1) nengo.Connection(u, a, synapse=None) nengo.Connection(a, b, function=np.square) up = nengo.Probe(u, synapse=0.01) ap = nengo.Probe(a, synapse=0.01) bp = nengo.Probe(b, synapse=nengo.Alpha(0.01)) with Simulator(network, seed=simseed, dt=dt, optimize=optimize, progress_bar=progress) as sim: sim.run(trun0) pkls = pickle.dumps(dict(sim=sim, up=up, ap=ap, bp=bp)) sim.run(trun1) t0, u0, a0, b0 = sim.trange(), sim.data[up], sim.data[ap], sim.data[bp] # reload model del network, sim, up, ap, bp pkl = pickle.loads(pkls) up, ap, bp = pkl["up"], pkl["ap"], pkl["bp"] with pkl["sim"] as sim: assert sim.seed == simseed assert sim.dt == dt assert sim.optimize == optimize if progress_bar: assert isinstance(sim.progress_bar, TerminalProgressBar) else: assert sim.progress_bar is progress_bar sim.run(trun1) t1, u1, a1, b1 = sim.trange(), sim.data[up], sim.data[ap], sim.data[bp] tols = dict(atol=1e-5) assert allclose(t1, t0, **tols) assert allclose(u1, u0, **tols) assert allclose(a1, a0, **tols) assert allclose(b1, b0, **tols) # check that closed status is preserved pkl = pickle.loads(pickle.dumps(dict(sim=sim))) assert pkl["sim"].closed
def test_sys_conversions(): sys = Alpha(0.1) tf = sys2tf(sys) ss = sys2ss(sys) zpk = sys2zpk(sys) assert sys_equal(sys2ss(tf), ss) assert sys_equal(sys2ss(ss), ss) # unchanged assert sys_equal(sys2tf(tf), tf) # unchanged assert sys_equal(sys2tf(ss), tf) assert sys_equal(sys2zpk(zpk), zpk) # sanity check assert sys_equal(sys2zpk(tf), zpk) # sanity check assert sys_equal(sys2zpk(ss), zpk) assert sys_equal(sys2tf(zpk), tf) assert sys_equal(sys2ss(zpk), ss) # should also work with nengo's synapse types assert sys_equal(sys2zpk(nengo.Alpha(0.1)), zpk) assert sys_equal(sys2tf(nengo.Alpha(0.1)), tf) assert sys_equal(sys2ss(nengo.Alpha(0.1)), ss) # system can also be just a scalar assert sys_equal(sys2tf(2.0), (1, 0.5)) assert np.allclose(sys2ss(5)[3], 5) assert sys_equal(sys2zpk(5), 5) with pytest.raises(ValueError): sys2ss(np.zeros(5)) with pytest.raises(ValueError): sys2zpk(np.zeros(5)) with pytest.raises(ValueError): sys2tf(np.zeros(5)) with pytest.raises(ValueError): # _ss2tf(...): passthrough must be single element sys2tf(([], [], [], [1, 2]))
def test_noise_copies_ok(Simulator, NonDirectNeuronType, seed, plt, allclose): """Make sure the same noise process works in multiple ensembles. We test this both with the default system and without. """ process = FilteredNoise(synapse=nengo.Alpha(1.0), dist=Choice([[0.5]])) with nengo.Network(seed=seed) as model: if ( NonDirectNeuronType.spiking or RegularSpiking in NonDirectNeuronType.__bases__ ): neuron_type = NonDirectNeuronType(initial_state={"voltage": Choice([0])}) else: neuron_type = NonDirectNeuronType() model.config[nengo.Ensemble].neuron_type = neuron_type model.config[nengo.Ensemble].encoders = Choice([[1]]) model.config[nengo.Ensemble].gain = Choice([5]) model.config[nengo.Ensemble].bias = Choice([2]) model.config[nengo.Ensemble].noise = process const = nengo.Node(output=1) a = nengo.Ensemble(1, 1, noise=process) b = nengo.Ensemble(1, 1, noise=process) c = nengo.Ensemble(1, 1) # defaults to noise=process nengo.Connection(const, a) nengo.Connection(const, b) nengo.Connection(const, c) ap = nengo.Probe(a.neurons, synapse=0.01) bp = nengo.Probe(b.neurons, synapse=0.01) cp = nengo.Probe(c.neurons, synapse=0.01) with Simulator(model) as sim: sim.run(0.06) t = sim.trange() plt.subplot(2, 1, 1) plt.plot(t, sim.data[ap], lw=3) plt.plot(t, sim.data[bp], lw=2) plt.plot(t, sim.data[cp]) plt.subplot(2, 1, 2) plt.plot(*nengo.utils.ensemble.tuning_curves(a, sim), lw=3) plt.plot(*nengo.utils.ensemble.tuning_curves(b, sim), lw=2) plt.plot(*nengo.utils.ensemble.tuning_curves(c, sim)) assert allclose(sim.data[ap], sim.data[bp]) assert allclose(sim.data[bp], sim.data[cp])
def test_pes_pre_synapse_type_error(Simulator): def make_network(rule_type): with nengo.Network() as net: pre = nengo.Ensemble(10, 1) post = nengo.Node(size_in=1) conn = nengo.Connection(pre, post, learning_rule_type=rule_type) nengo.Connection(post, conn.learning_rule) return net net = make_network(nengo.PES(pre_synapse=nengo.Alpha(0.005))) with pytest.raises(ValidationError, match="pre-synapses for learning"): with Simulator(net): pass net = make_network(nengo.PES(pre_synapse=nengo.Lowpass(0.0015))) with pytest.warns(UserWarning, match="pre_synapse.tau.*integer multiple"): with Simulator(net): pass
def test_minibatch(Simulator, seed): with nengo.Network(seed=seed) as net: inp = [ nengo.Node(output=[0.5]), nengo.Node(output=np.sin), nengo.Node(output=nengo.processes.WhiteSignal(5, 0.5, seed=seed)) ] ens = [ nengo.Ensemble(10, 1, neuron_type=nengo.AdaptiveLIF()), nengo.Ensemble(10, 1, neuron_type=nengo.LIFRate()), nengo.Ensemble(10, 2, noise=nengo.processes.WhiteNoise(seed=seed)) ] nengo.Connection(inp[0], ens[0]) nengo.Connection(inp[1], ens[1], synapse=None) nengo.Connection(inp[2], ens[2], synapse=nengo.Alpha(0.1), transform=[[1], [1]]) conn = nengo.Connection(ens[0], ens[1], learning_rule_type=nengo.PES()) nengo.Connection(inp[0], conn.learning_rule) ps = [nengo.Probe(e) for e in ens] with Simulator(net, minibatch_size=None) as sim: probe_data = [[] for _ in ps] for i in range(5): sim.run_steps(100) for j, p in enumerate(ps): probe_data[j] += [sim.data[p]] sim.reset() probe_data = [np.stack(x, axis=0) for x in probe_data] with Simulator(net, minibatch_size=5) as sim: sim.run_steps(100) assert np.allclose(sim.data[ps[0]], probe_data[0], atol=1e-6) assert np.allclose(sim.data[ps[1]], probe_data[1], atol=1e-6) assert np.allclose(sim.data[ps[2]], probe_data[2], atol=1e-6)
def test_check_gradients_error(Simulator): # check_gradients detects nans in gradient with nengo.Network() as net: x = nengo.Node([0]) y = tensor_layer(x, lambda x: 1 / x) nengo.Probe(y) with Simulator(net) as sim: with pytest.raises(SimulationError): sim.check_gradients() # check_gradients detects errors in gradient (in this case caused by the # fact that nengo.Alpha doesn't have a TensorFlow implementation) with nengo.Network() as net: x = nengo.Node([0]) nengo.Probe(x, synapse=nengo.Alpha(0.1)) with Simulator(net) as sim: with pytest.raises(SimulationError): sim.check_gradients()
def test_canonical(): sys = ([1], [1], [1], [0]) assert sys_equal(canonical(sys), sys) sys = ([[1, 0], [1, 0]], [[1], [0]], [[1, 1]], [0]) assert sys_equal(canonical(sys), sys) sys = ([[1, 0], [0, 1]], [[0], [1]], [[1, 1]], [0]) assert sys_equal(canonical(sys), sys) sys = ([[1, 0], [0, 1]], [[1], [0]], [[1, 1]], [0]) assert sys_equal(canonical(sys), sys) sys = ([[1, 0], [0, 0]], [[1], [0]], [[1, 1]], [0]) assert sys_equal(canonical(sys), sys) sys = ([[1, 0], [0, 0]], [[0], [1]], [[1, 1]], [0]) assert sys_equal(canonical(sys), sys) sys = ([[1, 0, 1], [0, 1, 1], [1, 0, 0]], [[0], [1], [-1]], [[1, 1, 1]], [0]) assert sys_equal(canonical(sys), sys) sys = nengo.Alpha(0.1) csys = canonical(sys, controllable=True) osys = canonical(sys, controllable=False) assert ss_equal(csys, LinearSystem(sys).controllable) assert ss_equal(osys, LinearSystem(sys).observable) assert sys_equal(csys, osys) assert not ss_equal(csys, osys) # different state-space realizations A, B, C, D = csys.ss assert sys_equal(csys, sys) assert ss_equal(csys, ([[-20, -100], [1, 0]], [[1], [0]], [[0, 100]], [[0]])) assert sys_equal(osys, sys) assert ss_equal(osys, ([[-20, 1], [-100, 0]], [[0], [100]], [[1, 0]], [[0]]))
def test_dt(dt, pre_on_chip, Simulator, seed, plt, allclose): function = lambda x: x**2 probe_synapse = nengo.Alpha(0.01) simtime = 0.2 ens_params = dict( intercepts=nengo.dists.Uniform(-0.9, 0.9), max_rates=nengo.dists.Uniform(100, 120), ) with nengo.Network(seed=seed) as model: nengo_loihi.add_params(model) stim = nengo.Node(lambda t: -(np.sin(2 * np.pi * t / simtime))) stim_p = nengo.Probe(stim, synapse=probe_synapse) pre = nengo.Ensemble(100, 1, **ens_params) model.config[pre].on_chip = pre_on_chip pre_p = nengo.Probe(pre, synapse=probe_synapse) post = nengo.Ensemble(101, 1, **ens_params) post_p = nengo.Probe(post, synapse=probe_synapse) nengo.Connection(stim, pre) nengo.Connection(pre, post, function=function, solver=nengo.solvers.LstsqL2(weights=True)) with Simulator(model, dt=dt) as sim: sim.run(simtime) x = sim.data[stim_p] y = function(x) plt.plot(sim.trange(), x, "k--") plt.plot(sim.trange(), y, "k--") plt.plot(sim.trange(), sim.data[pre_p]) plt.plot(sim.trange(), sim.data[post_p]) assert allclose(sim.data[pre_p], x, rtol=0.1, atol=0.1) assert allclose(sim.data[post_p], y, rtol=0.1, atol=0.1)
def test_noise_copies_ok(Simulator, nl_nodirect, seed, plt): """Make sure the same noise process works in multiple ensembles. We test this both with the default system and without. """ process = StochasticProcess(Choice([0.5]), synapse=nengo.Alpha(1.)) with nengo.Network(seed=seed) as model: inp, gain, bias = 1, 5, 2 model.config[nengo.Ensemble].neuron_type = nl_nodirect() model.config[nengo.Ensemble].encoders = Choice([[1]]) model.config[nengo.Ensemble].gain = Choice([gain]) model.config[nengo.Ensemble].bias = Choice([bias]) model.config[nengo.Ensemble].noise = process const = nengo.Node(output=inp) a = nengo.Ensemble(1, 1, noise=process) b = nengo.Ensemble(1, 1, noise=process) c = nengo.Ensemble(1, 1) # defaults to noise=process nengo.Connection(const, a) nengo.Connection(const, b) nengo.Connection(const, c) ap = nengo.Probe(a.neurons, synapse=0.01) bp = nengo.Probe(b.neurons, synapse=0.01) cp = nengo.Probe(c.neurons, synapse=0.01) sim = Simulator(model) sim.run(0.06) t = sim.trange() plt.subplot(2, 1, 1) plt.plot(t, sim.data[ap], lw=3) plt.plot(t, sim.data[bp], lw=2) plt.plot(t, sim.data[cp]) plt.subplot(2, 1, 2) plt.plot(*nengo.utils.ensemble.tuning_curves(a, sim), lw=3) plt.plot(*nengo.utils.ensemble.tuning_curves(b, sim), lw=2) plt.plot(*nengo.utils.ensemble.tuning_curves(c, sim)) assert np.allclose(sim.data[ap], sim.data[bp]) assert np.allclose(sim.data[bp], sim.data[cp])
def build_network(params, inputs, dimensions, input_encoders=None, direct_input=True, presentation_time=1., pause_time=0.1, coords=None, n_outputs=None, n_exc=None, n_inh=None, n_inh_decoder=None, seed=0, t_learn_exc=None, t_learn_inh=None, sample_weights_every=10.): local_random = np.random.RandomState(seed) if coords is None: coords = {} srf_output_coords = coords.get('srf_output', None) srf_exc_coords = coords.get('srf_exc', None) srf_inh_coords = coords.get('srf_inh', None) decoder_coords = coords.get('decoder', None) decoder_inh_coords = coords.get('decoder_inh', None) if type(inputs) == np.ndarray: n_inputs = np.product(inputs.shape[1:]) else: n_inputs = len(inputs) if srf_output_coords is not None: n_outputs = srf_output_coords.shape[0] if srf_exc_coords is not None: n_exc = srf_exc_coords.shape[0] if srf_inh_coords is not None: n_inh = srf_inh_coords.shape[0] if decoder_inh_coords is not None: n_inh_decoder = decoder_inh_coords.shape[0] if n_outputs is None: raise RuntimeError( "n_outputs is not provided and srf_output coordinates are not provided" ) if n_exc is None: raise RuntimeError( "n_exc is not provided and srf_exc coordinates are not provided") if n_inh is None: raise RuntimeError( "n_exc is not provided and srf_inh coordinates are not provided") if n_inh_decoder is None: n_inh_decoder = n_inh if srf_exc_coords is None: srf_exc_coords = np.asarray(range(n_exc)).reshape((n_exc, 1)) / n_exc if srf_output_coords is None: srf_output_coords = np.asarray(range(n_outputs)).reshape( (n_outputs, 1)) / n_outputs if srf_inh_coords is None: srf_inh_coords = np.asarray(range(n_inh)).reshape((n_inh, 1)) / n_inh if decoder_coords is None: decoder_coords = np.asarray(range(n_exc)).reshape((n_exc, 1)) / n_exc if decoder_inh_coords is None: decoder_inh_coords = np.asarray(range(n_inh_decoder)).reshape( (n_inh_decoder, 1)) / n_inh_decoder autoencoder_network = nengo.Network( label="Learning with spatial receptive fields", seed=seed) exc_input_process = None if type(inputs) == np.ndarray: exc_input_process = PresentInputWithPause(inputs, presentation_time, pause_time) else: exc_input_process = inputs with autoencoder_network as model: learning_rate_E = params.get('learning_rate_E', 1e-5) learning_rate_EE = params.get('learning_rate_EE', 1e-5) learning_rate_I = params.get('learning_rate_I', 1e-4) learning_rate_E_func = ( lambda t: learning_rate_E if t <= t_learn_exc else 0.0 ) if t_learn_exc is not None and learning_rate_E is not None else None learning_rate_EE_func = ( lambda t: learning_rate_EE if t <= t_learn_exc else 0.0 ) if t_learn_exc is not None and learning_rate_EE is not None else None learning_rate_I_func = ( lambda t: learning_rate_I if t <= t_learn_inh else 0.0 ) if t_learn_inh is not None and learning_rate_E is not None else None srf_network = PRF(dimensions=dimensions, exc_input_process=exc_input_process, connect_exc_inh_input=True, connect_out_out=True if ('w_initial_EE' in params) and (params['w_initial_EE'] is not None) else False, connect_inh_inh=True, n_excitatory=n_exc, n_inhibitory=n_inh, n_outputs=n_outputs, output_coordinates=srf_output_coords, exc_coordinates=srf_exc_coords, inh_coordinates=srf_inh_coords, w_initial_E=params['w_initial_E'], w_initial_I=params['w_initial_I'], w_initial_EI=params['w_initial_EI'], w_initial_EE=params.get('w_initial_EE', None), w_EI_Ext=params['w_EI_Ext'], p_E=params['p_E_srf'], p_I=params['p_I_srf'], p_EE=params['p_EE'], p_EI_Ext=params['p_EI_Ext'], p_EI=params['p_EI'], tau_E=params['tau_E'], tau_I=params['tau_I'], tau_input=params['tau_input'], learning_rate_I=learning_rate_I, learning_rate_E=learning_rate_E, learning_rate_EE=learning_rate_EE, learning_rate_E_func=learning_rate_E_func, learning_rate_EE_func=learning_rate_EE_func, learning_rate_I_func=learning_rate_I_func, sigma_scale_E=params['sigma_scale_E'], sigma_scale_EI=params['sigma_scale_EI'], sigma_scale_EI_Ext=params['sigma_scale_EI_Ext'], sigma_scale_EE=params['sigma_scale_EE'], sigma_scale_I=params['sigma_scale_I'], isp_target_rate=params['isp_target_rate'], direct_input=direct_input, label="Spatial receptive field network", seed=seed) if input_encoders is not None: srf_network.exc.encoders = input_encoders decoder = nengo.Ensemble(n_exc, dimensions=dimensions, neuron_type=nengo.LIF(), radius=1, intercepts=nengo.dists.Choice([0.1]), max_rates=nengo.dists.Choice([40])) decoder_inh = nengo.Ensemble(n_inh_decoder, dimensions=dimensions, neuron_type=nengo.LIF(tau_rc=0.005), radius=1, intercepts=nengo.dists.Choice([0.1]), max_rates=nengo.dists.Choice([100])) tau_E = params['tau_E'] w_DEC_E = params['w_DEC_E'] p_DEC = params['p_DEC'] model.weights_initial_DEC_E = local_random.uniform( size=n_outputs * n_exc).reshape((n_exc, n_outputs)) * w_DEC_E for i in range(n_exc): dist = cdist(decoder_coords[i, :].reshape((1, -1)), srf_network.output_coordinates).flatten() sigma = 0.1 prob = distance_probs(dist, sigma) sources = np.asarray(local_random.choice(n_outputs, round(p_DEC * n_outputs), replace=False, p=prob), dtype=np.int32) model.weights_initial_DEC_E[ i, np.logical_not(np.in1d(range(n_outputs), sources))] = 0. learning_rate_DEC_E = params['learning_rate_D_Exc'] model.conn_DEC_E = nengo.Connection( srf_network.output.neurons, decoder.neurons, transform=model.weights_initial_DEC_E, synapse=nengo.Alpha(tau_E), learning_rule_type=HSP(directed=True)) model.node_learning_rate_DEC_E = nengo.Node( lambda t: learning_rate_DEC_E) model.conn_learning_rate_DEC_E = nengo.Connection( model.node_learning_rate_DEC_E, model.conn_DEC_E.learning_rule) w_DEC_I_ff = params['w_DEC_I'] weights_initial_DEC_I_ff = local_random.uniform( size=n_inh * n_outputs).reshape((n_inh, n_outputs)) * w_DEC_I_ff for i in range(n_inh_decoder): dist = cdist(decoder_inh_coords[i, :].reshape((1, -1)), srf_network.output_coordinates).flatten() sigma = 1.0 prob = distance_probs(dist, sigma) sources = np.asarray(local_random.choice(n_outputs, round(p_DEC * n_outputs), replace=False, p=prob), dtype=np.int32) weights_initial_DEC_I_ff[ i, np.logical_not(np.in1d(range(n_outputs), sources))] = 0. conn_DEC_I_ff = nengo.Connection(srf_network.output.neurons, decoder_inh.neurons, transform=weights_initial_DEC_I_ff, synapse=nengo.Alpha(tau_E)) w_DEC_I_fb = params['w_initial_I_DEC_fb'] weights_initial_DEC_I_fb = local_random.uniform( size=n_inh_decoder * n_exc).reshape( (n_exc, n_inh_decoder)) * w_DEC_I_fb for i in range(n_exc): dist = cdist(decoder_coords[i, :].reshape((1, -1)), decoder_inh_coords).flatten() sigma = 1.0 prob = distance_probs(dist, sigma) sources = np.asarray(local_random.choice(n_inh_decoder, round(0.2 * n_inh_decoder), replace=False, p=prob), dtype=np.int32) weights_initial_DEC_I_fb[ i, np.logical_not(np.in1d(range(n_inh_decoder), sources))] = 0. conn_DEC_I_fb = nengo.Connection( decoder_inh.neurons, decoder.neurons, transform=weights_initial_DEC_I_fb, synapse=nengo.Lowpass(params['tau_I']), learning_rule_type=CDISP(learning_rate=params['learning_rate_D'])) w_SRF_I_bp = params['w_initial_I'] weights_initial_SRF_I_bp = local_random.uniform( size=n_inh_decoder * n_outputs).reshape( (n_outputs, n_inh_decoder)) * w_SRF_I_bp for i in range(n_outputs): dist = cdist(srf_network.output_coordinates[i, :].reshape((1, -1)), decoder_inh_coords).flatten() sigma = 0.1 prob = distance_probs(dist, sigma) sources = np.asarray(local_random.choice(n_inh_decoder, round(0.05 * n_inh_decoder), replace=False, p=prob), dtype=np.int32) weights_initial_SRF_I_bp[ i, np.logical_not(np.in1d(range(n_inh_decoder), sources))] = 0. conn_SRF_I_bp = nengo.Connection(decoder_inh.neurons, srf_network.output.neurons, transform=weights_initial_SRF_I_bp, synapse=nengo.Alpha(params['tau_I'])) coincidence_detection = nengo.Node( size_in=2 * n_exc, size_out=n_exc, output=lambda t, x: np.subtract(x[:n_exc], x[n_exc:])) nengo.Connection(coincidence_detection, conn_DEC_I_fb.learning_rule) nengo.Connection(srf_network.exc.neurons, coincidence_detection[n_exc:]) nengo.Connection(decoder.neurons, coincidence_detection[:n_exc]) p_srf_rec_weights = None with srf_network: p_srf_output_spikes = nengo.Probe(srf_network.output.neurons, 'output', synapse=None) p_srf_exc_spikes = nengo.Probe(srf_network.exc.neurons, 'output') p_srf_inh_spikes = nengo.Probe(srf_network.inh.neurons, 'output') p_srf_inh_weights = nengo.Probe(srf_network.conn_I, 'weights', sample_every=sample_weights_every) p_srf_exc_weights = nengo.Probe(srf_network.conn_E, 'weights', sample_every=sample_weights_every) if srf_network.conn_EE is not None: p_srf_rec_weights = nengo.Probe( srf_network.conn_EE, 'weights', sample_every=sample_weights_every) p_decoder_spikes = nengo.Probe(decoder.neurons, synapse=None) p_decoder_inh_spikes = nengo.Probe(decoder_inh.neurons, synapse=None) p_decoder_weights = nengo.Probe(model.conn_DEC_E, 'weights', sample_every=sample_weights_every) model.srf_network = srf_network model.decoder_ens = decoder model.decoder_inh_ens = decoder_inh return { 'network': autoencoder_network, 'neuron_probes': { 'srf_output_spikes': p_srf_output_spikes, 'srf_exc_spikes': p_srf_exc_spikes, 'srf_inh_spikes': p_srf_inh_spikes, 'decoder_spikes': p_decoder_spikes, 'decoder_inh_spikes': p_decoder_inh_spikes, }, 'weight_probes': { 'srf_exc_weights': p_srf_exc_weights, 'srf_rec_weights': p_srf_rec_weights, 'decoder_weights': p_decoder_weights, 'srf_inh_weights': p_srf_inh_weights, # 'srf_exc_weights': p_srf_exc_weights, } }
# the output node provides the 10-dimensional classification out = nengo.Node(size_in=10) for _ in range(n_parallel): # build parallel copies of the network layer, conv = conv_layer(inp, 1, input_shape, kernel_size=(1, 1), init=np.ones(init_ones)) # first layer is off-chip to translate the images into spikes net.config[layer.ensemble].on_chip = False layer, conv = conv_layer(layer, 1, conv.output_shape, strides=(2, 2)) nengo.Connection(layer, out, transform=nengo_dl.dists.Glorot()) out_p = nengo.Probe(out) out_p_filt = nengo.Probe(out, synapse=nengo.Alpha(0.01)) #train_data = {inp: train_data[0][:, None, :], # out_p: train_data[1][:, None, :]} # for the test data evaluation we'll be running the network over time # using spiking neurons, so we need to repeat the input/target data # for a number of timesteps (based on the presentation_time) test_data = { inp: np.tile(test_data[0][:1000, None, :], (1, int(presentation_time / dt), 1)), out_p_filt: np.tile(test_data[1][:1000, None, :], (1, int(presentation_time / dt), 1)) }
def test_conv_preslice(Simulator, plt): from nengo._vendor.npconv2d.conv2d import conv2d kernel = np.array([[-1, 2, -1], [-1, 2, -1], [-1, 2, -1]], dtype=float) kernel /= kernel.max() image = np.array([[1, 2, 1, 2, 0], [2, 3, 2, 1, 1], [1, 2, 1, 2, 3], [2, 3, 2, 1, 1], [1, 2, 1, 2, 0]], dtype=float) image /= image.max() image2 = np.column_stack([c * x for c in image.T for x in (1, -1)]) input_gain = 149. neuron_type = nengo.SpikingRectifiedLinear() y_ref = LoihiSpikingRectifiedLinear().rates(image.ravel(), input_gain, 0) y_ref = conv2d(y_ref.reshape(1, 5, 5, 1), kernel.reshape(3, 3, 1, 1), pad='VALID') y_ref = LoihiSpikingRectifiedLinear().rates(y_ref.ravel(), 1., 0.).reshape(3, 3) with nengo.Network() as net: u = nengo.Node(image2.ravel()) a = nengo.Ensemble(50, 1, neuron_type=neuron_type, gain=nengo.dists.Choice([input_gain]), bias=nengo.dists.Choice([0])) transform = nengo_transforms.Convolution(n_filters=1, input_shape=(5, 5, 1), init=kernel.reshape( 3, 3, 1, 1)) b = nengo.Ensemble(transform.output_shape.size, 1, neuron_type=neuron_type, gain=nengo.dists.Choice([1]), bias=nengo.dists.Choice([0])) nengo.Connection(u, a.neurons, synapse=None) nengo.Connection(a.neurons[::2], b.neurons, transform=transform) bp = nengo.Probe(b.neurons, synapse=nengo.Alpha(0.02)) hw_opts = dict(snip_max_spikes_per_step=100) with Simulator(net, hardware_options=hw_opts) as sim: sim.run(0.3) y_ref = y_ref / input_gain y = sim.data[bp][-1].reshape(3, -1) / input_gain plt.subplot(121) plt.imshow(y_ref) plt.colorbar() plt.subplot(122) plt.imshow(y) plt.colorbar() assert np.allclose(y, y_ref, atol=0.02, rtol=0.1)
net.config[layer_1].on_chip = False nengo.Connection(inp, layer_1.neurons, transform=nengo_dl.dists.Glorot()) p1 = nengo.Probe(layer_1.neurons) layer_2 = nengo.Ensemble(n_neurons=32, dimensions=1, neuron_type=neuron_type, label="Layer 2") nengo.Connection(layer_1.neurons, layer_2.neurons, transform=nengo_dl.dists.Glorot()) p2 = nengo.Probe(layer_2.neurons) layer_3 = nengo.Ensemble(n_neurons=16, dimensions=1, neuron_type=neuron_type, label="Layer 3") nengo.Connection(layer_2.neurons, layer_3.neurons, transform=nengo_dl.dists.Glorot()) p3 = nengo.Probe(layer_3.neurons) nengo.Connection(layer_3.neurons, out, transform=nengo_dl.dists.Glorot()) out_p = nengo.Probe(out, label="out_p") out_p_filt = nengo.Probe(out, synapse=nengo.Alpha(0.01), label="out_p_filt") #set train data train_data = train_data.reshape(-1, 1, nVariables) valid_data = valid_data.reshape(-1, 1, nVariables) train_data_out = train_data_out.reshape(-1, 1, 1) train_data_out_categorical = train_data_out_categorical.reshape(-1, 1, 2) valid_data_out = valid_data_out.reshape(-1, 1, 1) train_data = {inp: train_data, out_p: train_data_out} #train_data = {inp: train_data, out_p: train_data_out_categorical} # for the test data evaluation we'll be running the network over time # using spiking neurons, so we need to repeat the input/target data # for a number of timesteps (based on the presentation_time) test_data = {
def generate(y_des, speed=1, alpha=1000.0, direct_mode=False): """ Pre-motor cortex model, implements a dynamical movement primitive that generates the provided trajectory. input: None output: [trajectory[0], trajectory[1]] """ beta = alpha / 4.0 # generate the forcing function forces, _, goals = forcing_functions.generate(y_des=y_des, rhythmic=False, alpha=alpha, beta=beta) # create alpha synapse, which has point attractor dynamics tau = np.sqrt(1.0 / (alpha * beta)) alpha_synapse = nengo.Alpha(tau) net = nengo.Network('PMC') if direct_mode: net.config[nengo.Ensemble].neuron_type = nengo.Direct() with net: net.output = nengo.Node(size_in=2) # create a start / stop movement signal time_func = lambda t: min(max((t * speed) % 4.5 - 2.5, -1), 1) def goal_func(t): t = time_func(t) if t <= -1: return goals[0] return goals[1] net.goal = nengo.Node(output=goal_func, label='goal') # -------------------- Ramp --------------------------------- ramp_node = nengo.Node(output=time_func, label='ramp') net.ramp = nengo.Ensemble(n_neurons=500, dimensions=1, label='ramp ens') nengo.Connection(ramp_node, net.ramp) # ------------------- Forcing Functions --------------------- def relay_func(t, x): t = time_func(t) if t <= -1: return [0, 0] return x # the relay prevents forces from being sent when resetting relay = nengo.Node(output=relay_func, size_in=2, label='relay gate') domain = np.linspace(-1, 1, len(forces[0])) x_func = interpolate.interp1d(domain, forces[0]) y_func = interpolate.interp1d(domain, forces[1]) nengo.Connection(net.ramp, relay[0], transform=1.0 / alpha / beta, function=x_func, synapse=alpha_synapse) nengo.Connection(net.ramp, relay[1], transform=1.0 / alpha / beta, function=y_func, synapse=alpha_synapse) nengo.Connection(relay, net.output) nengo.Connection(net.goal[0], net.output[0], synapse=alpha_synapse) nengo.Connection(net.goal[1], net.output[1], synapse=alpha_synapse) return net
def test_conv_preslice(on_chip, Simulator, plt): conv2d = pytest.importorskip("nengo._vendor.npconv2d.conv2d") kernel = np.array([[-1, 2, -1], [-1, 2, -1], [-1, 2, -1]], dtype=float) kernel /= kernel.max() image = np.array( [ [1, 2, 1, 2, 0], [2, 3, 2, 1, 1], [1, 2, 1, 2, 3], [2, 3, 2, 1, 1], [1, 2, 1, 2, 0], ], dtype=float, ) image /= image.max() image2 = np.column_stack([c * x for c in image.T for x in (1, -1)]) input_gain = 149.0 neuron_type = nengo.SpikingRectifiedLinear() loihi_neuron = LoihiSpikingRectifiedLinear() layer0_neuron = loihi_neuron if on_chip else neuron_type y_ref = layer0_neuron.rates(image.ravel(), input_gain, 0) y_ref = conv2d.conv2d(y_ref.reshape((1, 5, 5, 1)), kernel.reshape((3, 3, 1, 1)), pad="VALID") y_ref = loihi_neuron.rates(y_ref.ravel(), 1.0, 0.0).reshape((3, 3)) with nengo.Network() as net: nengo_loihi.add_params(net) u = nengo.Node(image2.ravel()) a = nengo.Ensemble( 50, 1, neuron_type=neuron_type, gain=nengo.dists.Choice([input_gain]), bias=nengo.dists.Choice([0]), ) net.config[a].on_chip = on_chip transform = nengo_transforms.Convolution(n_filters=1, input_shape=(5, 5, 1), init=kernel.reshape( (3, 3, 1, 1))) b = nengo.Ensemble( transform.output_shape.size, 1, neuron_type=neuron_type, gain=nengo.dists.Choice([1]), bias=nengo.dists.Choice([0]), ) nengo.Connection(u, a.neurons, synapse=None) nengo.Connection(a.neurons[::2], b.neurons, transform=transform) bp = nengo.Probe(b.neurons, synapse=nengo.Alpha(0.02)) with Simulator(net) as sim: assert sim.precompute is True sim.run(0.3) y_ref = y_ref / input_gain y = sim.data[bp][-1].reshape((3, -1)) / input_gain plt.subplot(121) plt.imshow(y_ref) plt.colorbar() plt.subplot(122) plt.imshow(y) plt.colorbar() assert np.allclose(y, y_ref, atol=0.02, rtol=0.1)
input_shape, kernel_size=(1, 1), init=np.ones((1, 1, 1, 1))) # first layer is off-chip to translate the images into spikes net.config[layer.ensemble].on_chip = False #layer, conv = conv_layer(layer, 2, conv.output_shape, strides=(1, 1)) layer, conv = conv_layer(layer, nClass, conv.output_shape, strides=(1, 1)) nengo.Connection(layer, out, transform=nengo_dl.dists.Glorot()) out_p = nengo.Probe(out, label="out_p") out_p_filt = nengo.Probe(out, synapse=nengo.Alpha(0.01), label="out_p_filt") #set train data train_data = train_data.reshape(-1, 1, nVariables) valid_data = valid_data.reshape(-1, 1, nVariables) train_data_out = train_data_out.reshape(-1, 1, 1) valid_data_out = valid_data_out.reshape(-1, 1, 1) train_data = {inp: train_data, out_p: train_data_out} # for the test data evaluation we'll be running the network over time # using spiking neurons, so we need to repeat the input/target data # for a number of timesteps (based on the presentation_time) test_data = { inp: np.tile(valid_data, (1, int(presentation_time / dt), 1)),