def test_all_negative_activities(allclose, plt, seed, Simulator, Solver): class CheckActivitiesSolver(Solver): def __call__(self, A, Y, rng=np.random): assert np.all(A < 0) return super().__call__(A, Y, rng=rng) val = -0.5 with nengo.Network(seed=seed) as net: input = nengo.Node(output=val, label="input") ens = nengo.Ensemble( 30, 1, neuron_type=nengo.Tanh(), encoders=Choice([[1]]), intercepts=Uniform(0, 0.95), eval_points=Uniform(-1, -0.1), ) nengo.Connection(input, ens) in_p = nengo.Probe(input, "output") ens_p = nengo.Probe( ens, "decoded_output", synapse=0.05, solver=CheckActivitiesSolver() ) with Simulator(net) as sim: sim.run(0.3) t = sim.trange() plt.plot(t, sim.data[in_p], label="Input") plt.plot(t, sim.data[ens_p], label="Neuron approximation, pstc=0.05") plt.xlim(right=t[-1]) plt.legend(loc=0) assert allclose(sim.data[in_p], val, atol=0.1, rtol=0.01) assert allclose(sim.data[ens_p][-10:], val, atol=0.1, rtol=0.01)
def test_random_spiking(Simulator, inference_only, seed): with nengo.Network() as net: config.configure_settings(inference_only=inference_only) inp = nengo.Node([1]) ens0 = nengo.Ensemble(100, 1, neuron_type=nengo.Tanh(), seed=seed) ens1 = nengo.Ensemble( 100, 1, neuron_type=nengo.StochasticSpiking(nengo.Tanh()), seed=seed, ) ens2 = nengo.Ensemble( 100, 1, neuron_type=nengo.PoissonSpiking(nengo.Tanh()), seed=seed, ) nengo.Connection(inp, ens0, synapse=None) nengo.Connection(inp, ens1, synapse=None) nengo.Connection(inp, ens2, synapse=None) p0 = nengo.Probe(ens0.neurons) p1 = nengo.Probe(ens1.neurons) p2 = nengo.Probe(ens2.neurons) with pytest.warns(None) as recwarns: with Simulator(net, seed=seed) as sim: sim.run_steps(10000) assert not any("native TensorFlow implementation" in str(w.message) for w in recwarns) assert np.allclose(sim.data[p0][0], np.mean(sim.data[p1], axis=0), atol=1, rtol=2e-1) assert np.allclose(sim.data[p0], np.mean(sim.data[p2], axis=0), atol=1, rtol=1e-1)
def test_simneuronsmerger_warning(rng): nt = nengo.PoissonSpiking(nengo.Tanh()) op1 = SimNeurons( nt, J=Signal(shape=(1,)), output=Signal(shape=(1,)), state={"rng": rng} ) op2 = SimNeurons( nt, J=Signal(shape=(1,)), output=Signal(shape=(1,)), state={"rng": rng} ) assert SimNeuronsMerger.is_mergeable(op1, op2) with pytest.warns(UserWarning, match="Extra state has been modified"): SimNeuronsMerger.merge([op1, op2])