Ejemplo n.º 1
0
def test_basic(Simulator, seed, plt):
    # solve for a standard nengo connection using a feed-forward reservoir

    train_t = 5.0
    test_t = 0.5
    dt = 0.001

    n_neurons = 100
    synapse = 0.01

    process = WhiteSignal(max(train_t, test_t), high=10, rms=0.3)

    def function(x): return x**2

    with Network() as model:
        ens = nengo.Ensemble(n_neurons, 1, seed=seed)  # <- must have seed!
        res = Reservoir(ens, ens.neurons, synapse)

        # Solve for the readout that approximates a function of the *filtered*
        # stimulus. We include a lowpass here because the final RMSE will be
        # with respect to the lowpass stimulus, which is also consistent
        # with what the NEF is doing. But in a general recurrent reservoir
        # this filter could hypothetically be anything.
        res.train(
            lambda x: function(Lowpass(synapse).filt(x, dt=dt)),
            train_t, dt, process, seed=seed+1)

        assert res.size_in == 1
        assert res.size_mid == n_neurons
        assert res.size_out == 1

        # Validation
        _, (_, _, check_output) = res.run(
            test_t, dt, process, seed=seed+2)

        stim = nengo.Node(output=process)
        output = nengo.Node(size_in=1)

        nengo.Connection(stim, ens, synapse=None)
        nengo.Connection(ens, output, function=function, synapse=None)

        # note the reservoir output already includes a synapse
        p_res = nengo.Probe(res.output, synapse=None)
        p_normal = nengo.Probe(output, synapse=synapse)
        p_stim = nengo.Probe(stim, synapse=synapse)

    with Simulator(model, dt=dt, seed=seed+2) as sim:
        sim.run(test_t)

    # Since the seed for the two test processes were the same, the validation
    # run should produce the same output as the test simulation.
    assert np.allclose(check_output, sim.data[p_res])

    ideal = function(sim.data[p_stim])

    plt.figure()
    plt.plot(sim.trange(), sim.data[p_res], label="Reservoir")
    plt.plot(sim.trange(), sim.data[p_normal], label="Standard")
    plt.plot(sim.trange(), ideal, label="Ideal")
    plt.legend()

    assert np.allclose(rmse(sim.data[p_res], ideal),
                       rmse(sim.data[p_normal], ideal), atol=1e-2)
Ejemplo n.º 2
0
def test_basic(Simulator, seed, plt):
    # solve for a standard nengo connection using a feed-forward reservoir

    train_t = 5.0
    test_t = 0.5
    dt = 0.001

    n_neurons = 100
    synapse = 0.01

    process = WhiteSignal(max(train_t, test_t), high=10, rms=0.3)

    def function(x):
        return x**2

    with Network() as model:
        ens = nengo.Ensemble(n_neurons, 1, seed=seed)  # <- must have seed!
        res = Reservoir(ens, ens.neurons, synapse)

        # Solve for the readout that approximates a function of the *filtered*
        # stimulus. We include a lowpass here because the final RMSE will be
        # with respect to the lowpass stimulus, which is also consistent
        # with what the NEF is doing. But in a general recurrent reservoir
        # this filter could hypothetically be anything.
        res.train(lambda x: function(Lowpass(synapse).filt(x, dt=dt)),
                  train_t,
                  dt,
                  process,
                  seed=seed + 1)

        assert res.size_in == 1
        assert res.size_mid == n_neurons
        assert res.size_out == 1

        # Validation
        _, (_, _, check_output) = res.run(test_t, dt, process, seed=seed + 2)

        stim = nengo.Node(output=process)
        output = nengo.Node(size_in=1)

        nengo.Connection(stim, ens, synapse=None)
        nengo.Connection(ens, output, function=function, synapse=None)

        # note the reservoir output already includes a synapse
        p_res = nengo.Probe(res.output, synapse=None)
        p_normal = nengo.Probe(output, synapse=synapse)
        p_stim = nengo.Probe(stim, synapse=synapse)

    with Simulator(model, dt=dt, seed=seed + 2) as sim:
        sim.run(test_t)

    # Since the seed for the two test processes were the same, the validation
    # run should produce the same output as the test simulation.
    assert np.allclose(check_output, sim.data[p_res])

    ideal = function(sim.data[p_stim])

    plt.figure()
    plt.plot(sim.trange(), sim.data[p_res], label="Reservoir")
    plt.plot(sim.trange(), sim.data[p_normal], label="Standard")
    plt.plot(sim.trange(), ideal, label="Ideal")
    plt.legend()

    assert np.allclose(rmse(sim.data[p_res], ideal),
                       rmse(sim.data[p_normal], ideal),
                       atol=1e-2)