Esempio n. 1
0
def test_tuning_curves_1d(Simulator, plt, seed):
    """For 1D ensembles, should be able to do plt.plot(*tuning_curves(...))."""
    model = nengo.Network(seed=seed)
    with model:
        ens_1d = nengo.Ensemble(10, dimensions=1, neuron_type=nengo.LIF())
    with Simulator(model) as sim:
        plt.plot(*tuning_curves(ens_1d, sim))
Esempio n. 2
0
    def test_constant_vector(self):
        """A network that represents a constant 3D vector."""
        N = 30
        vals = [0.6, 0.1, -0.5]

        m = nengo.Model('test_constant_vector', seed=123)
        m.make_node('in', output=vals)
        m.make_ensemble('A', nengo.LIF(N * len(vals)), len(vals))
        m.connect('in', 'A')
        m.probe('in')
        m.probe('A', filter=0.1)

        sim = m.simulator(dt=0.001, sim_class=self.Simulator)
        sim.run(1.0)

        with Plotter(self.Simulator) as plt:
            t = sim.data(m.t)
            plt.plot(t, sim.data('in'), label='Input')
            plt.plot(t, sim.data('A'), label='Neuron approximation, pstc=0.1')
            plt.legend(loc=0, prop={'size': 10})
            plt.savefig('test_ensemble.test_constant_vector.pdf')
            plt.close()

        self.assertTrue(np.allclose(sim.data('in')[-10:], vals,
                                    atol=.1, rtol=.01))
        self.assertTrue(np.allclose(sim.data('A')[-10:], vals,
                                    atol=.1, rtol=.01))
Esempio n. 3
0
    def test_constant_scalar(self):
        """A Network that represents a constant value."""
        N = 30
        val = 0.5

        m = nengo.Model('test_constant_scalar', seed=123)
        m.make_node('in', output=val)
        m.make_ensemble('A', nengo.LIF(N), 1)
        m.connect('in', 'A')
        m.probe('in')
        m.probe('A', filter=0.1)

        sim = m.simulator(dt=0.001, sim_class=self.Simulator)
        sim.run(1.0)

        with Plotter(self.Simulator) as plt:
            t = sim.data(m.t)
            plt.plot(t, sim.data('in'), label='Input')
            plt.plot(t, sim.data('A'), label='Neuron approximation, pstc=0.1')
            plt.legend(loc=0)
            plt.savefig('test_ensemble.test_constant_scalar.pdf')
            plt.close()

        self.assertTrue(np.allclose(sim.data('in').ravel(), val,
                                    atol=.1, rtol=.01))
        self.assertTrue(np.allclose(sim.data('A')[-10:], val,
                                    atol=.1, rtol=.01))
Esempio n. 4
0
    def test_vector(self):
        """A network that represents sin(t), cos(t), arctan(t)."""
        N = 40

        m = nengo.Model('test_vector', seed=123)
        m.make_node('in', output=lambda t: [np.sin(t), np.cos(t), np.arctan(t)])
        m.make_ensemble('A', nengo.LIF(N * 3), 3, radius=2)
        m.connect('in', 'A')
        m.probe('in')
        m.probe('A', filter=0.02)

        sim = m.simulator(dt=0.001, sim_class=self.Simulator)
        sim.run(5)

        with Plotter(self.Simulator) as plt:
            t = sim.data(m.t)
            plt.plot(t, sim.data('in'), label='Input')
            plt.plot(t, sim.data('A'), label='Neuron approximation, pstc=0.02')
            plt.legend(loc='best', prop={'size': 10})
            plt.savefig('test_ensemble.test_vector.pdf')
            plt.close()

        target = np.vstack((np.sin(np.arange(5000) / 1000.),
                            np.cos(np.arange(5000) / 1000.),
                            np.arctan(np.arange(5000) / 1000.))).T
        logger.debug("In RMSE: %f", rmse(target, sim.data('in')))
        self.assertTrue(rmse(target, sim.data('in') < 0.001))
        self.assertTrue(rmse(target, sim.data('A')) < 0.1)
Esempio n. 5
0
    def test_scalar(self):
        """A network that represents sin(t)."""
        N = 30

        m = nengo.Model('test_scalar', seed=123)
        m.make_node('in', output=np.sin)
        m.make_ensemble('A', nengo.LIF(N), 1)
        m.connect('in', 'A')
        m.probe('in')
        m.probe('A', filter=0.02)

        sim = m.simulator(dt=0.001, sim_class=self.Simulator)
        sim.run(5.0)

        with Plotter(self.Simulator) as plt:
            t = sim.data(m.t)
            plt.plot(t, sim.data('in'), label='Input')
            plt.plot(t, sim.data('A'), label='Neuron approximation, pstc=0.02')
            plt.legend(loc=0)
            plt.savefig('test_ensemble.test_scalar.pdf')
            plt.close()

        target = np.sin(np.arange(5000) / 1000.)
        target.shape = (-1, 1)
        logger.debug("[New API] input RMSE: %f", rmse(target, sim.data('in')))
        logger.debug("[New API] A RMSE: %f", rmse(target, sim.data('A')))
        self.assertTrue(rmse(target, sim.data('in')) < 0.001)
        self.assertTrue(rmse(target, sim.data('A')) < 0.1)
Esempio n. 6
0
def run(sim_type, dim, DperE, cleanup_n):
    sim_class = {0: nengo.Simulator, 1: SimOCL}[sim_type]
    input_vector = hrr.HRR(dim).v
    cleanup_encoders = np.array([input_vector] * cleanup_n)

    num_ensembles = int(dim / DperE)
    dim = num_ensembles * DperE
    NperE = NperD * DperE

    print "Building..."
    start = time.time()
    model = nengo.Model("PES timing", seed=seed)

    def input_func(x):
        return input_vector

    inn = nengo.Node(output=input_func)

    # Build ensembles
    cleanup = nengo.Ensemble(label='cleanup',
                             neurons=nengo.LIF(cleanup_n),
                             dimensions=dim,
                             encoders=cleanup_encoders)

    nengo.Connection(inn, cleanup)

    ensembles = \
            build.build_cleanup_pes(cleanup, inn, DperE, NperD, num_ensembles, learning_rate)
    output_ensembles = ensembles[0]
    error_ensembles = ensembles[1]

    # Build probes
    output_probes = [
        nengo.Probe(o, 'decoded_output', filter=0.1) for o in output_ensembles
    ]
    error_probes = [
        nengo.Probe(e, 'decoded_output', filter=0.1) for e in error_ensembles
    ]

    input_probe = nengo.Probe(inn, 'output')

    end = time.time()
    print "Build Time: ", end - start

    # Run model
    print "Simulating..."
    start = time.time()
    sim = sim_class(model, dt=0.001)
    sim.run(sim_length)
    end = time.time()
    print "Sim Time: ", end - start

    end = time.time()
    print "Plot Time: ", end - start

    overall_end = time.time()
    print "Total time: ", overall_end - overall_start

    plt.show()
Esempio n. 7
0
def forward(x, weights, biases):
    lif = nengo.LIF()
    for w, b in zip(weights, biases):
        x = np.dot(x, w)
        x += b
        if w is not weights[-1]:
            x = lif.rates(x, 1, 1) / 63.04
    return x
Esempio n. 8
0
    def test_seeding(self):
        """Test that setting the model seed fixes everything"""

        ### TODO: this really just checks random parameters in ensembles.
        ###   Are there other objects with random parameters that should be
        ###   tested? (Perhaps initial weights of learned connections)

        m = nengo.Model('test_seeding')
        m.make_node('in', output=1)
        m.make_ensemble('A', nengo.LIF(40), 1)
        m.make_ensemble('B', nengo.LIF(20), 1)
        m.connect('in', 'A')
        m.connect('A', 'B', function=lambda x: x**2)
        m.probe('in')
        m.probe('A', filter=0.01)
        m.probe('B', filter=0.01)

        m.seed = 872
        m1 = m.simulator(dt=0.001).model
        m2 = m.simulator(dt=0.001).model
        m.seed = 873
        m3 = m.simulator(dt=0.001).model

        def compare_objs(obj1, obj2, attrs, equal=True):
            for attr in attrs:
                check = (np.all(getattr(obj1, attr) == getattr(obj2, attr))
                         if equal else np.any(
                             getattr(obj1, attr) != getattr(obj2, attr)))
                if not check:
                    print getattr(obj1, attr)
                    print getattr(obj2, attr)
                self.assertTrue(check)

        ens_attrs = ('encoders', 'max_rates', 'intercepts')
        A = [mi.get('A') for mi in [m1, m2, m3]]
        B = [mi.get('B') for mi in [m1, m2, m3]]
        compare_objs(A[0], A[1], ens_attrs)
        compare_objs(B[0], B[1], ens_attrs)
        compare_objs(A[0], A[2], ens_attrs, equal=False)
        compare_objs(B[0], B[2], ens_attrs, equal=False)

        neur_attrs = ('gain', 'bias')
        compare_objs(A[0].neurons, A[1].neurons, neur_attrs)
        compare_objs(B[0].neurons, B[1].neurons, neur_attrs)
        compare_objs(A[0].neurons, A[2].neurons, neur_attrs, equal=False)
        compare_objs(B[0].neurons, B[2].neurons, neur_attrs, equal=False)
Esempio n. 9
0
def forward(x, weights, biases):
    lif = nengo.LIF()
    layers = []
    for w, b in zip(weights, biases):
        x = np.dot(x, w) + b
        x = lif.rates(x, 1, 1) / 63.04
        layers.append(x)
    return x, layers
Esempio n. 10
0
def test_amplitude(Simulator, seed, rng, plt):
    amp = 0.1
    neuron0 = nengo.LIF()
    neuron = nengo.LIF(amplitude=amp)

    # check static
    x = np.linspace(-5, 30)
    y = amp * neuron0.rates(x, 1., 0.)
    y2 = neuron.rates(x, 1., 0.)
    assert np.allclose(y, y2, atol=1e-5)

    # check dynamic
    n = 100
    x = 1.0
    encoders = np.ones((n, 1))
    max_rates = rng.uniform(low=20, high=200, size=n)
    intercepts = rng.uniform(low=-1, high=0.8, size=n)

    with nengo.Network(seed=seed) as model:
        ins = nengo.Node(x)
        ens = nengo.Ensemble(n,
                             dimensions=1,
                             neuron_type=neuron,
                             encoders=encoders,
                             max_rates=max_rates,
                             intercepts=intercepts)
        nengo.Connection(ins, ens, synapse=None)
        spike_probe = nengo.Probe(ens.neurons)

    dt = 0.001
    t_final = 0.5
    with Simulator(model, dt=dt) as sim:
        sim.run(t_final)

    spikes = sim.data[spike_probe]
    r = spikes.sum(axis=0) * (dt / t_final)

    gain, bias = neuron0.gain_bias(max_rates, intercepts)
    r0 = amp * neuron0.rates(x, gain, bias)

    i = np.argsort(r0)
    plt.plot(r0[i])
    plt.plot(r[i])

    error = rms(r - r0) / rms(r0)
    assert (error < 0.02).all()
Esempio n. 11
0
def performance_samples(device):  # pragma: no cover
    """
    Run a brief sample of the benchmarks to check overall performance.

    This is mainly used to quickly check that there haven't been any unexpected
    performance regressions.
    """

    # TODO: automatically run some basic performance tests during CI

    default_kwargs = {
        "n_steps": 1000,
        "device": device,
        "unroll_simulation": 25,
        "progress_bar": False,
        "do_profile": False
    }

    print("cconv + relu")
    net = cconv(128, 64, nengo.RectifiedLinear())
    run_profile(net, minibatch_size=64, **default_kwargs)

    print("cconv + lif")
    net = cconv(128, 64, nengo.LIF())
    run_profile(net, minibatch_size=64, **default_kwargs)

    print("integrator training + relu")
    net = integrator(128, 32, nengo.RectifiedLinear())
    run_profile(net, minibatch_size=64, train=True, **default_kwargs)

    print("integrator training + lif")
    net = integrator(128, 32, nengo.LIF())
    run_profile(net, minibatch_size=64, train=True, **default_kwargs)

    print("random")
    net = random_network(128,
                         64,
                         nengo.RectifiedLinear(),
                         n_ensembles=50,
                         connections_per_ensemble=5,
                         seed=0)
    run_profile(net, **default_kwargs)

    print("spaun")
    net = spaun(1)
    run_profile(net, **default_kwargs)
Esempio n. 12
0
    def test_product(self):
        def product(x):
            return x[0] * x[1]

        m = nengo.Model('test_product', seed=124)

        N = 80
        m.make_node('sin', output=np.sin)
        m.make_node('-0.5', output=-.5)
        factors = m.make_ensemble(
            'factors', nengo.LIF(2 * N), dimensions=2, radius=1.5)
        factors.encoders = np.tile([[1, 1],[-1, 1],[1, -1],[-1, -1]],
                                   (factors.n_neurons / 4, 1))
        m.make_ensemble('product', nengo.LIF(N), dimensions=1)
        m.connect('sin', 'factors', transform=[[1], [0]])
        m.connect('-0.5', 'factors', transform=[[0], [1]])
        conn = m.connect('factors', 'product', function=product, filter=0.01)

        m.probe('sin', sample_every=.01)
        # m.probe(conn, sample_every=.01)  # FIXME
        m.probe('factors', sample_every=.01, filter=.01)
        m.probe('product', sample_every=.01, filter=.01)

        sim = m.simulator(dt=0.001, sim_class=self.Simulator)
        sim.run(6)

        with Plotter(self.Simulator) as plt:
            plt.subplot(211)
            plt.plot(sim.data('factors'))
            plt.plot(np.sin(np.arange(0, 6, .01)))
            plt.plot(sim.data('sin'))
            plt.subplot(212)
            plt.plot(sim.data('product'))
            #plt.plot(sim.data(conn))
            plt.plot(-.5 * np.sin(np.arange(0, 6, .01)))
            plt.savefig('test_ensemble.test_prod.pdf')
            plt.close()

        self.assertTrue(rmse(sim.data('factors')[:, 0],
                             np.sin(np.arange(0, 6, .01))) < 0.1)
        self.assertTrue(rmse(sim.data('factors')[20:, 1], -0.5) < 0.1)

        def match(a, b):
            self.assertTrue(rmse(a, b) < 0.1)

        match(sim.data('product')[:, 0], -0.5 * np.sin(np.arange(0, 6, .01)))
Esempio n. 13
0
def test_lif(Simulator, plt, rng, logger, dt):
    """Test that the dynamic model approximately matches the rates."""
    n = 5000
    x = 0.5
    encoders = np.ones((n, 1))
    max_rates = rng.uniform(low=10, high=200, size=n)
    intercepts = rng.uniform(low=-1, high=1, size=n)

    m = nengo.Network()
    with m:
        ins = nengo.Node(x)
        ens = nengo.Ensemble(n,
                             dimensions=1,
                             neuron_type=nengo.LIF(),
                             encoders=encoders,
                             max_rates=max_rates,
                             intercepts=intercepts)
        nengo.Connection(ins,
                         ens.neurons,
                         transform=np.ones((n, 1)),
                         synapse=None)
        spike_probe = nengo.Probe(ens.neurons)
        voltage_probe = nengo.Probe(ens.neurons, 'voltage')
        ref_probe = nengo.Probe(ens.neurons, 'refractory_time')

    t_final = 1.0
    with Simulator(m, dt=dt) as sim:
        sim.run(t_final)

    i = 3
    plt.subplot(311)
    plt.plot(sim.trange(), sim.data[spike_probe][:, :i])
    plt.subplot(312)
    plt.plot(sim.trange(), sim.data[voltage_probe][:, :i])
    plt.subplot(313)
    plt.plot(sim.trange(), sim.data[ref_probe][:, :i])
    plt.ylim([-dt, ens.neuron_type.tau_ref + dt])

    # check rates against analytic rates
    math_rates = ens.neuron_type.rates(
        x, *ens.neuron_type.gain_bias(max_rates, intercepts))
    spikes = sim.data[spike_probe]
    sim_rates = (spikes > 0).sum(0) / t_final
    logger.info("ME = %f", (sim_rates - math_rates).mean())
    logger.info("RMSE = %f",
                rms(sim_rates - math_rates) / (rms(math_rates) + 1e-20))
    assert np.sum(math_rates > 0) > 0.5 * n, (
        "At least 50% of neurons must fire")
    assert np.allclose(sim_rates, math_rates, atol=1, rtol=0.02)

    # if voltage and ref time are non-constant, the probe is doing something
    assert np.abs(np.diff(sim.data[voltage_probe])).sum() > 1
    assert np.abs(np.diff(sim.data[ref_probe])).sum() > 1

    # compute spike counts after each timestep
    actual_counts = (spikes > 0).cumsum(axis=0)
    expected_counts = np.outer(sim.trange(), math_rates)
    assert (abs(actual_counts - expected_counts) < 1).all()
Esempio n. 14
0
def test_lif_step(ctx, upsample):
    """Test the lif nonlinearity, comparing one step with the Numpy version."""
    rng = np.random

    dt = 1e-3
    n_neurons = [12345, 23456, 34567]
    J = RA([rng.normal(scale=1.2, size=n) for n in n_neurons])
    V = RA([rng.uniform(low=0, high=1, size=n) for n in n_neurons])
    W = RA([rng.uniform(low=-5 * dt, high=5 * dt, size=n) for n in n_neurons])
    OS = RA([np.zeros(n) for n in n_neurons])

    ref = 2e-3
    taus = rng.uniform(low=15e-3, high=80e-3, size=len(n_neurons))
    amp = 1.

    queue = cl.CommandQueue(ctx)
    clJ = CLRA(queue, J)
    clV = CLRA(queue, V)
    clW = CLRA(queue, W)
    clOS = CLRA(queue, OS)
    clTaus = CLRA(queue, RA([t * np.ones(n) for t, n in zip(taus, n_neurons)]))

    # simulate host
    nls = [nengo.LIF(tau_ref=ref, tau_rc=taus[i])
           for i, n in enumerate(n_neurons)]
    for i, nl in enumerate(nls):
        if upsample <= 1:
            nl.step_math(dt, J[i], OS[i], V[i], W[i])
        else:
            s = np.zeros_like(OS[i])
            for j in range(upsample):
                nl.step_math(dt / upsample, J[i], s, V[i], W[i])
                OS[i] = (1./dt) * ((OS[i] > 0) | (s > 0))

    # simulate device
    plan = plan_lif(
        queue, dt, clJ, clV, clW, clOS, ref, clTaus, amp, upsample=upsample)
    plan()

    if 1:
        a, b = V, clV
        for i in range(len(a)):
            nc, _ = not_close(a[i], b[i]).nonzero()
            if len(nc) > 0:
                j = nc[0]
                print("i", i, "j", j)
                print("J", J[i][j], clJ[i][j])
                print("V", V[i][j], clV[i][j])
                print("W", W[i][j], clW[i][j])
                print("...", len(nc) - 1, "more")

    n_spikes = np.sum([np.sum(os) for os in OS])
    if n_spikes < 1.0:
        logger.warn("LIF spiking mechanism was not tested!")
    assert ra.allclose(J, clJ.to_host())
    assert ra.allclose(V, clV.to_host())
    assert ra.allclose(W, clW.to_host())
    assert ra.allclose(OS, clOS.to_host())
Esempio n. 15
0
def test_config():
    @nengo.config.configures(nengo.Ensemble)
    class TestConfigEnsemble(nengo.config.ConfigItem):
        something = nengo.config.Parameter(None)
        other = nengo.config.Parameter(0)

    @nengo.config.configures(nengo.Connection)
    class TestConfigConnection(nengo.config.ConfigItem):
        something_else = nengo.config.Parameter(None)

    class TestConfig(nengo.config.Config):
        config_items = [TestConfigEnsemble, TestConfigConnection]

    model = nengo.Network()
    with model:
        a = nengo.Ensemble(nengo.LIF(50), 1)
        b = nengo.Ensemble(nengo.LIF(90), 1)
        a2b = nengo.Connection(a, b, synapse=0.01)

    config = TestConfig()

    assert config[a].something is None
    assert config[b].something is None
    assert config[a].other == 0
    assert config[b].other == 0
    assert config[a2b].something_else is None

    config[a].something = 'hello'
    assert config[a].something == 'hello'
    config[a].something = 'world'
    assert config[a].something == 'world'

    with pytest.raises(AttributeError):
        config[a].something_else
        config[a2b].something
    with pytest.raises(AttributeError):
        config[a].something_else = 1
        config[a2b].something = 1

    with pytest.raises(KeyError):
        config['a'].something
    with pytest.raises(KeyError):
        config[None].something
    with pytest.raises(KeyError):
        config[model].something
Esempio n. 16
0
def test_invalid_rates(Simulator):
    with nengo.Network() as model:
        nengo.Ensemble(1,
                       1,
                       max_rates=[200],
                       neuron_type=nengo.LIF(tau_ref=0.01))

    with pytest.raises(ValueError):
        Simulator(model)
Esempio n. 17
0
def test_withself():
    model = nengo.Network(label='test_withself')
    with model:
        n1 = nengo.Node(output=0.5)
        assert n1 in model.nodes
        e1 = nengo.Ensemble(nengo.LIF(10), 1)
        assert e1 in model.ensembles
        c1 = nengo.Connection(n1, e1)
        assert c1 in model.connections
        ea1 = nengo.networks.EnsembleArray(nengo.LIF(10), 2)
        assert ea1 in model.networks
        assert len(ea1.ensembles) == 2
        n2 = ea1.add_output("out", None)
        assert n2 in ea1.nodes
        with ea1:
            e2 = nengo.Ensemble(nengo.LIF(10), 1)
            assert e2 in ea1.ensembles
    assert len(nengo.Network.context) == 0
Esempio n. 18
0
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)
Esempio n. 19
0
def test_scalar_spiking(Simulator, seed):
    _test_RLS_network(Simulator,
                      seed,
                      dims=1,
                      lrate=1e-5,
                      neuron_type=nengo.LIF(),
                      tau=0.01,
                      T_train=1,
                      T_test=0.5,
                      tols=[0.03, 0.04, 0.04, 0.3])
Esempio n. 20
0
def test_perfect_lif_invariance(Simulator, seed):
    # as long as the simulation time is divisible by dt, the same number of
    # spikes should be observed
    t = 1.0
    errors = []
    for dt in (0.0001, 0.0005, 0.001, 0.002):
        assert np.allclose(int(t / dt), t / dt)
        error = _test_lif(Simulator, seed, nengo.LIF(), 0, dt, t=t)
        errors.append(error)
    assert np.allclose(errors, errors[0])
Esempio n. 21
0
def test_rls_scalar_spiking(Simulator, seed, plt):
    _test_rls_network(
        Simulator,
        seed,
        plt,
        neuron_type=nengo.LIF(),
        lrate=0.05,
        tau=0.01,
        tols=[0.04, 0.05, 0.04, 0.3],
    )
Esempio n. 22
0
def test_neurontypeparam_probeable():
    """NeuronTypeParam can update a probeable list."""
    class Test(object):
        ntp = EnsembleNeuronTypeParam(default=None, optional=True)
        probeable = ['output']

    inst = Test()
    assert inst.probeable == ['output']
    inst.ntp = nengo.LIF()
    assert Counter(inst.probeable) == Counter(inst.ntp.probeable + ['output'])
    # The first element is important,  as it's the default
    assert inst.probeable[0] == 'output'
    # Setting it again should result in the same list
    inst.ntp = nengo.LIF()
    assert Counter(inst.probeable) == Counter(inst.ntp.probeable + ['output'])
    assert inst.probeable[0] == 'output'
    # Unsetting it should clear the list appropriately
    inst.ntp = None
    assert inst.probeable == ['output']
Esempio n. 23
0
def test_neurontypeparam():
    """NeuronTypeParam must be a neuron type."""
    class Test(object):
        ntp = NeuronTypeParam('ntp', default=None)

    inst = Test()
    inst.ntp = nengo.LIF()
    assert isinstance(inst.ntp, nengo.LIF)
    with pytest.raises(ValueError):
        inst.ntp = 'a'
Esempio n. 24
0
def test_loihi_rates(dt, neuron_type, Simulator, plt, allclose):
    n = 256
    x = np.linspace(-0.1, 1, n)

    encoders = np.ones((n, 1))
    max_rates = 400 * np.ones(n)
    intercepts = 0 * np.ones(n)
    gain, bias = neuron_type.gain_bias(max_rates, intercepts)
    j = x * gain + bias

    with nengo.Network() as model:
        a = nengo.Ensemble(n,
                           1,
                           neuron_type=neuron_type,
                           encoders=encoders,
                           gain=gain,
                           bias=j)
        ap = nengo.Probe(a.neurons)

    with Simulator(model, dt=dt) as sim:
        sim.run(1.0)

    est_rates = sim.data[ap].mean(axis=0)
    ref_rates = loihi_rates(neuron_type, x[np.newaxis, :], gain, bias,
                            dt=dt).squeeze(axis=0)

    ref_rates2 = None
    if isinstance(neuron_type, nengo.RegularSpiking):
        if isinstance(neuron_type.base_type, nengo.LIFRate):
            neuron_type2 = nengo.LIF(
                tau_rc=neuron_type.base_type.tau_rc,
                tau_ref=neuron_type.base_type.tau_ref,
                amplitude=neuron_type.amplitude,
            )
        elif isinstance(neuron_type.base_type, nengo.RectifiedLinear):
            neuron_type2 = nengo.SpikingRectifiedLinear(
                amplitude=neuron_type.amplitude, )

        ref_rates2 = loihi_rates(neuron_type2,
                                 x[np.newaxis, :],
                                 gain,
                                 bias,
                                 dt=dt).squeeze(axis=0)

    plt.plot(x, ref_rates, "k", label="predicted")
    if ref_rates2 is not None:
        plt.plot(x, ref_rates2, "b", label="predicted-base")
    plt.plot(x, est_rates, "g", label="measured")
    plt.legend(loc="best")

    assert ref_rates.shape == est_rates.shape
    assert allclose(est_rates, ref_rates, atol=1, rtol=0, xtol=1)
    if ref_rates2 is not None:
        assert allclose(ref_rates2, ref_rates)
Esempio n. 25
0
def test_bad_weight_exponent_error(Simulator):
    with nengo.Network() as net:
        a = nengo.Ensemble(5, 1)
        b = nengo.Ensemble(5, 1, neuron_type=nengo.LIF(tau_rc=5.))
        nengo.Connection(a.neurons,
                         b.neurons,
                         transform=1e-8 * np.ones((5, 5)),
                         synapse=5.)

    with pytest.raises(BuildError, match="[Cc]ould not find.*weight exp"):
        with Simulator(net):
            pass
Esempio n. 26
0
    def test_constant_scalar(self):
        """A Network that represents a constant value."""
        simulator = self.Simulator
        params = dict(simulator=simulator, seed=123, dt=0.001)
        N = 30
        val = 0.5

        # old api
        net = nef.Network('test_constant_scalar', **params)
        net.make_input('in', value=[val])
        net.make('A', N, 1)
        net.connect('in', 'A')

        in_p = net.make_probe('in', dt_sample=0.001, pstc=0.0)
        a_p = net.make_probe('A', dt_sample=0.001, pstc=0.1)
        net.run(1)

        in_data = in_p.get_data()
        a_data = a_p.get_data()

        with Plotter(simulator) as plt:
            t = net.model.data[net.model.simtime]
            plt.plot(t, in_data, label='Input')
            plt.plot(t, a_data, label='Neuron approximation, pstc=0.1')
            plt.legend(loc=0)
            plt.savefig('test_ensemble.test_constant_scalar-old.pdf')
            plt.close()

        self.assertTrue(np.allclose(in_data.ravel(), val, atol=.05, rtol=.05))
        self.assertTrue(np.allclose(a_data[-10:], val, atol=.05, rtol=.05))

        # New API
        m = nengo.Model('test_constant_scalar', **params)
        m.make_node('in', output=val)
        m.make_ensemble('A', nengo.LIF(N), 1)
        m.connect('in', 'A')

        m.probe('in')
        m.probe('A', filter=0.1)
        m.run(1)

        with Plotter(simulator) as plt:
            t = m.data[m.simtime]
            plt.plot(t, m.data['in'], label='Input')
            plt.plot(t, m.data['A'], label='Neuron approximation, pstc=0.1')
            plt.legend(loc=0)
            plt.savefig('test_ensemble.test_constant_scalar-new.pdf')
            plt.close()

        self.assertTrue(
            np.allclose(m.data['in'].ravel(), val, atol=.05, rtol=.05))
        self.assertTrue(np.allclose(m.data['A'][-10:], val, atol=.05,
                                    rtol=.05))
Esempio n. 27
0
def _test_rates(Simulator, rates, name=None):
    if name is None:
        name = rates.__name__

    n = 100
    max_rates = 50 * np.ones(n)
    # max_rates = 200 * np.ones(n)
    intercepts = np.linspace(-0.99, 0.99, n)
    encoders = np.ones((n, 1))
    nparams = dict(n_neurons=n)
    eparams = dict(
        max_rates=max_rates, intercepts=intercepts, encoders=encoders)

    model = nengo.Network()
    with model:
        u = nengo.Node(output=whitenoise(1, 5, seed=8393))
        a = nengo.Ensemble(nengo.LIFRate(**nparams), 1, **eparams)
        b = nengo.Ensemble(nengo.LIF(**nparams), 1, **eparams)
        nengo.Connection(u, a, synapse=0)
        nengo.Connection(u, b, synapse=0)
        up = nengo.Probe(u)
        ap = nengo.Probe(a.neurons, "output", synapse=None)
        bp = nengo.Probe(b.neurons, "output", synapse=None)

    dt = 1e-3
    sim = Simulator(model, dt=dt)
    sim.run(2.)

    t = sim.trange()
    x = sim.data[up]
    a_rates = sim.data[ap] / dt
    spikes = sim.data[bp]
    b_rates = rates(t, spikes)

    with Plotter(Simulator) as plt:
        ax = plt.subplot(411)
        plt.plot(t, x)
        ax = plt.subplot(412)
        implot(plt, t, intercepts, a_rates.T, ax=ax)
        ax.set_ylabel('intercept')
        ax = plt.subplot(413)
        implot(plt, t, intercepts, b_rates.T, ax=ax)
        ax.set_ylabel('intercept')
        ax = plt.subplot(414)
        implot(plt, t, intercepts, (b_rates - a_rates).T, ax=ax)
        ax.set_xlabel('time [s]')
        ax.set_ylabel('intercept')
        plt.savefig('utils.test_neurons.test_rates.%s.pdf' % name)
        plt.close()

    tmask = (t > 0.1) & (t < 1.9)
    relative_rmse = rms(b_rates[tmask] - a_rates[tmask]) / rms(a_rates[tmask])
    return relative_rmse
Esempio n. 28
0
    def test_constant_vector(self):
        """A network that represents a constant 3D vector."""
        simulator = self.Simulator
        params = dict(simulator=simulator, seed=123, dt=0.001)
        N = 30
        vals = [0.6, 0.1, -0.5]

        # Old API
        net = nef.Network('test_constant_vector', **params)
        net.make_input('in', value=vals)
        net.make('A', N * len(vals), len(vals))
        net.connect('in', 'A', transform=np.eye(len(vals)))

        in_p = net.make_probe('in', dt_sample=0.001, pstc=0.0)
        a_p = net.make_probe('A', dt_sample=0.001, pstc=0.1)
        net.run(1)

        in_data = in_p.get_data()
        a_data = a_p.get_data()

        with Plotter(simulator) as plt:
            t = net.model.data[net.model.simtime]
            plt.plot(t, in_data, label='Input')
            plt.plot(t, a_data, label='Neuron approximation, pstc=0.1')
            plt.legend(loc=0, prop={'size': 10})
            plt.savefig('test_ensemble.test_constant_vector-old.pdf')
            plt.close()

        self.assertTrue(np.allclose(in_data[-10:], vals, atol=.05, rtol=.05))
        self.assertTrue(np.allclose(a_data[-10:], vals, atol=.05, rtol=.05))

        # New API
        m = nengo.Model('test_constant_vector', **params)
        m.make_node('in', output=vals)
        m.make_ensemble('A', nengo.LIF(N * len(vals)), len(vals))
        m.connect('in', 'A')

        m.probe('in')
        m.probe('A', filter=0.1)
        m.run(1)

        with Plotter(simulator) as plt:
            t = m.data[m.simtime]
            plt.plot(t, m.data['in'], label='Input')
            plt.plot(t, m.data['A'], label='Neuron approximation, pstc=0.1')
            plt.legend(loc=0, prop={'size': 10})
            plt.savefig('test_ensemble.test_constant_vector-new.pdf')
            plt.close()

        self.assertTrue(
            np.allclose(m.data['in'][-10:], vals, atol=.05, rtol=.05))
        self.assertTrue(
            np.allclose(m.data['A'][-10:], vals, atol=.05, rtol=.05))
Esempio n. 29
0
def test_nested_context(Simulator):
    model = nengo.Network()
    with model:
        con1 = nengo.Network()
        con2 = nengo.Network()
        con3 = nengo.Network()

        with con1:
            e1 = nengo.Ensemble(nengo.LIF(1), 1)
            assert e1 in con1.ensembles

            with con2:
                e2 = nengo.Ensemble(nengo.LIF(1), 1)
                assert e2 in con2.ensembles
                assert e2 not in con1.ensembles

                with con3:
                    e3 = nengo.Ensemble(nengo.LIF(1), 1)
                    assert e3 in con3.ensembles
                    assert e3 not in con2.ensembles \
                        and e3 not in con1.ensembles

                e4 = nengo.Ensemble(nengo.LIF(1), 1)
                assert e4 in con2.ensembles
                assert e4 not in con3.ensembles

            e5 = nengo.Ensemble(nengo.LIF(1), 1)
            assert e5 in con1.ensembles

        e6 = nengo.Ensemble(nengo.LIF(1), 1)
        assert e6 not in con1.ensembles
Esempio n. 30
0
def test_conv_input(channels_last, Simulator, plt, allclose):
    input_shape = ImageShape(4, 4, 1, channels_last=channels_last)
    seed = 3  # fix seed to do the same computation for both channel positions
    rng = np.random.RandomState(seed + 1)

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

        a = nengo.Node(rng.uniform(0, 1, size=input_shape.size))

        nc = 2
        kernel = np.array([1., -1.]).reshape((1, 1, 1, nc))
        transform = nengo_loihi.Conv2D.from_kernel(kernel, input_shape)
        b = nengo.Ensemble(transform.output_shape.size,
                           1,
                           neuron_type=nengo.SpikingRectifiedLinear(),
                           max_rates=nengo.dists.Choice([50]),
                           intercepts=nengo.dists.Choice([0]))
        net.config[b].on_chip = False
        nengo.Connection(a, b.neurons, transform=transform)
        output_shape = transform.output_shape

        nf = 4
        kernel = rng.uniform(-0.005, 0.005, size=(nc, 3, 3, nf))
        transform = nengo_loihi.Conv2D.from_kernel(kernel, output_shape)
        c = nengo.Ensemble(transform.output_shape.size,
                           1,
                           neuron_type=nengo.LIF(),
                           max_rates=nengo.dists.Choice([100]),
                           intercepts=nengo.dists.Choice([0]))
        nengo.Connection(b.neurons, c.neurons, transform=transform)
        output_shape = transform.output_shape

        p = nengo.Probe(c.neurons)

    with nengo.Simulator(net, optimize=False) as sim:
        sim.run(1.0)

    with Simulator(net, seed=seed) as sim_loihi:
        sim_loihi.run(1.0)

    p0 = np.sum(sim.data[p] > 0, axis=0).reshape(output_shape.shape())
    p1 = np.sum(sim_loihi.data[p] > 0, axis=0).reshape(output_shape.shape())
    if not output_shape.channels_last:
        p0 = np.transpose(p0, (1, 2, 0))
        p1 = np.transpose(p1, (1, 2, 0))

    plt.plot(p0.ravel(), 'k')
    plt.plot(p1.ravel(), 'b--')

    # loihi spikes are not exactly the same, but should be close-ish
    assert allclose(p0, p1, rtol=0.15, atol=1)