示例#1
0
def test_multiple_builds(Simulator, seed):
    # this is not a separate test because of nengo issue #1011
    solver = BiasedSolver()
    A, Y = np.ones((1, 1)), np.ones((1, 1))
    assert solver.bias is None
    solver(A, Y)
    assert solver.bias is not None
    with warns(UserWarning):
        solver(A, Y)

    # issue: #99
    with Network(seed=seed) as model:
        stim = nengo.Node(output=0)
        x = nengo.Ensemble(100, 1)
        out = nengo.Node(size_in=1)
        nengo.Connection(stim, x, synapse=None)
        conn = Connection(x, out, synapse=None)
        p = nengo.Probe(out, synapse=0.1)

    assert isinstance(conn.solver, BiasedSolver)

    with Simulator(model):
        pass

    with Simulator(model) as sim:
        sim.run(0.1)

    assert rmse(sim.data[p], 0) < 0.01
def test_none_dt(Simulator, seed, rng):
    dt = 0.0001  # approximates continuous case
    T = 1.0

    sys = Bandpass(8, 5)
    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,
                               input_synapse=synapse,
                               dt=None,
                               neuron_type=nengo.neurons.Direct())
        nengo.Connection(stim, subnet.input, synapse=None)

        assert subnet.output_synapse is None

        p_ideal = nengo.Probe(subnet.input, synapse=sys)
        p_output = nengo.Probe(subnet.output, synapse=None)

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

    assert np.allclose(sim.data[p_output], sim.data[p_ideal], atol=0.1)
def test_output_filter(Simulator, seed, rng):
    dt = 0.001
    T = 1.0

    sys = PadeDelay(0.1, order=3, p=3)
    assert sys.has_passthrough
    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,
                               output_synapse=synapse,
                               dt=dt,
                               neuron_type=nengo.neurons.Direct())
        nengo.Connection(stim, subnet.input, synapse=None)

        assert subnet.input_synapse is None

        p_ideal = nengo.Probe(subnet.input, synapse=sys)
        p_output = nengo.Probe(subnet.output, synapse=None)

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

    assert np.allclose(sim.data[p_output][:-1], sim.data[p_ideal][1:])
示例#4
0
def test_pes_learning_rate(Simulator, plt, seed):
    n = 50
    dt = 0.0005
    T = 1.0
    initial = 0.7
    desired = -0.9
    epsilon = 1e-3  # get to factor epsilon with T seconds

    # Get activity vector and initial decoders
    with Network(seed=seed) as model:
        x = nengo.Ensemble(n,
                           1,
                           seed=seed,
                           neuron_type=nengo.neurons.LIFRate())
        y = nengo.Node(size_in=1)
        conn = nengo.Connection(x, y, synapse=None)

    with Simulator(model, dt=dt) as sim:
        a = get_activities(sim.model, x, [initial])
        d = sim.data[conn].weights
        assert np.any(a > 0)

    # Use util function to calculate learning_rate
    init_error = float(desired - np.dot(d, a))
    learning_rate, gamma = pes_learning_rate(epsilon / abs(init_error), a, T,
                                             dt)

    # Build model with no filtering on any connections
    with model:
        stim = nengo.Node(output=initial)
        ystar = nengo.Node(output=desired)

        conn.learning_rule_type = nengo.PES(pre_tau=1e-15,
                                            learning_rate=learning_rate)

        nengo.Connection(stim, x, synapse=None)
        nengo.Connection(ystar, conn.learning_rule, synapse=0, transform=-1)
        nengo.Connection(y, conn.learning_rule, synapse=0)

        p = nengo.Probe(y, synapse=None)
        decoders = nengo.Probe(conn, 'weights', synapse=None)

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

    # Check that the final error is exactly epsilon
    assert np.allclose(abs(desired - sim.data[p][-1]), epsilon)

    # Check that all of the errors are given exactly by gamma**k
    k = np.arange(len(sim.trange()))
    error = init_error * gamma**k
    assert np.allclose(sim.data[p].flatten(), desired - error)

    # Check that all of the decoders are equal to their analytical solution
    dk = d.T + init_error * a.T[:, None] * (1 - gamma**k) / np.dot(a, a.T)
    assert np.allclose(dk, np.squeeze(sim.data[decoders].T))

    plt.figure()
    plt.plot(sim.trange(), sim.data[p], lw=5, alpha=0.5)
    plt.plot(sim.trange(), desired - error, linestyle='--', lw=5, alpha=0.5)
def test_direct_window(legendre, Simulator, seed, plt):
    theta = 1.0
    T = theta

    assert np.allclose(t_default, np.linspace(0, 1, 1000))

    with Network() as model:
        stim = nengo.Node(output=lambda t: t)
        rw = RollingWindow(theta, n_neurons=1, dimensions=12,
                           neuron_type=nengo.Direct(), process=None,
                           legendre=legendre)
        assert rw.theta == theta
        assert rw.dt == 0.001
        assert rw.process is None
        assert rw.synapse == nengo.Lowpass(0.1)
        assert rw.input_synapse == nengo.Lowpass(0.1)

        nengo.Connection(stim, rw.input, synapse=None)
        output = rw.add_output(function=lambda w: np.sum(w**3)**2)
        p_output = nengo.Probe(output, synapse=None)

    with Simulator(model) as sim:
        sim.run(T)

    actual = sim.data[p_output].squeeze()
    t = sim.trange()
    ideal = shift(np.cumsum(t**3)**2)

    plt.figure()
    plt.plot(t, actual, label="Output")
    plt.plot(t, ideal, label="Ideal", linestyle='--')
    plt.legend()

    assert nrmse(actual, ideal) < 0.005
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_hetero_neurons(Simulator, rng, seed):
    n_neurons = 100
    dt = 0.001
    T = 0.1
    dims_in = 2

    taus = nengo.dists.Uniform(0.001, 0.1).sample(n_neurons, rng=rng)
    synapses = [Lowpass(tau) for tau in taus]
    encoders = sphere.sample(n_neurons, dims_in, rng=rng)

    hs = HeteroSynapse(synapses, dt)

    def embed_encoders(x):
        # Reshapes the vectors to be the same dimensionality as the
        # encoders, and then takes the dot product row by row.
        # See http://stackoverflow.com/questions/26168363/ for a more
        # efficient solution.
        return np.sum(encoders * hs.from_vector(x), axis=1)

    with Network(seed=seed) as model:
        # Input stimulus
        stim = nengo.Node(size_in=dims_in)
        for i in range(dims_in):
            nengo.Connection(nengo.Node(
                output=nengo.processes.WhiteSignal(T, high=10, seed=seed)),
                             stim[i],
                             synapse=None)

        # HeteroSynapse node
        syn = nengo.Node(size_in=dims_in, output=hs)

        # For comparing results
        x = [
            nengo.Ensemble(n_neurons, dims_in, seed=0, encoders=encoders)
            for _ in range(2)
        ]  # expected, actual

        # Expected
        for i, synapse in enumerate(synapses):
            t = np.zeros_like(encoders)
            t[i, :] = encoders[i, :]
            nengo.Connection(stim, x[0].neurons, transform=t, synapse=synapse)

        # Actual
        nengo.Connection(stim, syn, synapse=None)
        nengo.Connection(syn,
                         x[1].neurons,
                         function=embed_encoders,
                         synapse=None)

        # Probes
        p_exp = nengo.Probe(x[0].neurons, synapse=None)
        p_act = nengo.Probe(x[1].neurons, synapse=None)

    # Check correctness
    with Simulator(model, dt=dt) as sim:
        sim.run(T)

    assert np.allclose(sim.data[p_act], sim.data[p_exp])
示例#8
0
def delayed_synapse():
    a = 0.1  # desired delay
    b = 0.01  # synapse delay
    tau = 0.01  # recurrent tau
    hz = 15  # input frequency
    t = 1.0  # simulation time
    dt = 0.00001  # simulation timestep
    order = 6  # order of pade approximation
    tau_probe = 0.02

    dexp_synapse = DoubleExp(tau, tau / 5)

    sys_lambert = lambert_delay(a, b, tau, order - 1, order)
    synapse = (cont2discrete(Lowpass(tau), dt=dt) *
               DiscreteDelay(int(b / dt)))

    n_neurons = 2000
    neuron_type = PerfectLIF()

    A, B, C, D = sys_lambert.observable.transform(5*np.eye(order)).ss

    sys_normal = PadeDelay(a, order)
    assert len(sys_normal) == order

    with Network(seed=0) as model:
        stim = Node(output=WhiteSignal(t, high=hz, y0=0))

        x = EnsembleArray(n_neurons / order, len(A), neuron_type=neuron_type)
        output = Node(size_in=1)

        Connection(x.output, x.input, transform=A, synapse=synapse)
        Connection(stim, x.input, transform=B, synapse=synapse)
        Connection(x.output, output, transform=C, synapse=None)
        Connection(stim, output, transform=D, synapse=None)

        lowpass_delay = LinearNetwork(
            sys_normal, n_neurons_per_ensemble=n_neurons / order,
            synapse=tau, input_synapse=tau,
            dt=None, neuron_type=neuron_type, radii=1.0)
        Connection(stim, lowpass_delay.input, synapse=None)

        dexp_delay = LinearNetwork(
            sys_normal, n_neurons_per_ensemble=n_neurons / order,
            synapse=dexp_synapse, input_synapse=dexp_synapse,
            dt=None, neuron_type=neuron_type, radii=1.0)
        Connection(stim, dexp_delay.input, synapse=None)

        p_stim = Probe(stim, synapse=tau_probe)
        p_output_delayed = Probe(output, synapse=tau_probe)
        p_output_lowpass = Probe(lowpass_delay.output, synapse=tau_probe)
        p_output_dexp = Probe(dexp_delay.output, synapse=tau_probe)

    with Simulator(model, dt=dt, seed=0) as sim:
        sim.run(t)

    return (a, dt, sim.trange(), sim.data[p_stim],
            sim.data[p_output_delayed], sim.data[p_output_lowpass],
            sim.data[p_output_dexp])
示例#9
0
def test_mapping(Simulator, plt, seed):
    sys = Alpha(0.1)
    syn = Lowpass(0.01)
    gsyn = 2*syn  # scaled lowpass
    isyn = 2/s  # scaled integrator
    dt = 0.001

    ss = ss2sim(sys, syn, None)  # normal lowpass, continuous
    dss = ss2sim(sys, syn, dt)  # normal lowpass, discrete
    gss = ss2sim(sys, gsyn, None)  # scaled lowpass, continuous
    gdss = ss2sim(sys, gsyn, dt)  # scaled lowpass, discrete
    iss = ss2sim(sys, isyn, None)  # scaled integrator, continuous
    idss = ss2sim(sys, isyn, dt)  # scaled integrator, discrete
    assert ss.analog and gss.analog and iss.analog
    assert not (dss.analog or gdss.analog or idss.analog)

    with Network(seed=seed) as model:
        stim = nengo.Node(output=lambda t: np.sin(20*np.pi*t))

        probes = []
        for mapped, synapse in ((ss, syn), (dss, syn), (gss, gsyn),
                                (gdss, gsyn), (iss, isyn), (idss, isyn)):
            A, B, C, D = mapped.ss
            x = nengo.Node(size_in=2)
            y = nengo.Node(size_in=1)

            nengo.Connection(stim, x, transform=B, synapse=synapse)
            nengo.Connection(x, x, transform=A, synapse=synapse)
            nengo.Connection(x, y, transform=C, synapse=None)
            nengo.Connection(stim, y, transform=D, synapse=None)

            probes.append(nengo.Probe(y))

        p_stim = nengo.Probe(stim)

    pss, pdss, pgss, pgdss, piss, pidss = probes

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

    expected = shift(sys.filt(sim.data[p_stim], dt))

    plt.plot(sim.trange(), sim.data[pss], label="Continuous", alpha=0.5)
    plt.plot(sim.trange(), sim.data[pdss], label="Discrete", alpha=0.5)
    plt.plot(sim.trange(), sim.data[pgss], label="Gain Cont.", alpha=0.5)
    plt.plot(sim.trange(), sim.data[pgdss], label="Gain Disc.", alpha=0.5)
    plt.plot(sim.trange(), sim.data[piss], label="Integ Cont.", alpha=0.5)
    plt.plot(sim.trange(), sim.data[pidss], label="Integ Disc.", alpha=0.5)
    plt.plot(sim.trange(), expected, label="Expected", linestyle='--')
    plt.legend()

    assert np.allclose(sim.data[pss], expected, atol=0.01)
    assert np.allclose(sim.data[pdss], expected)
    assert np.allclose(sim.data[pgss], expected, atol=0.01)
    assert np.allclose(sim.data[pgdss], expected)
    assert np.allclose(sim.data[piss], expected, atol=0.01)
    assert np.allclose(sim.data[pidss], expected)
示例#10
0
def test_biased_solver_weights(Simulator):
    solver = BiasedSolver(nengo.solvers.LstsqL2(weights=True))
    assert solver.weights

    with Network() as model:
        x = nengo.Ensemble(100, 1)
        nengo.Connection(x, x, solver=solver)

    Simulator(model)
示例#11
0
def test_network():
    with Network():
        x = nengo.Ensemble(100, 1)
        assert isinstance(x.eval_points, ScatteredHypersphere)
        assert isinstance(x.encoders, ScatteredHypersphere)

    with BaseNetwork():
        x = nengo.Ensemble(100, 1)
        assert not isinstance(x.eval_points, ScatteredHypersphere)
        assert not isinstance(x.encoders, ScatteredHypersphere)
示例#12
0
def test_context():
    with Network():
        # make some arbitrary object for the reservoirs. it's okay that it's
        # not within the same network, because we won't simulate it
        a = nengo.Node(size_in=1)

    with Network() as outer:
        with Network() as inner:
            assert Reservoir(a, a).network is inner
            assert Reservoir(a, a, network=inner).network is inner
            assert Reservoir(a, a, network=outer).network is outer
        assert Reservoir(a, a).network is outer
        assert Reservoir(a, a, network=inner).network is inner
        assert Reservoir(a, a, network=outer).network is outer

    with pytest.raises(NetworkContextError):
        Reservoir(a, a)
    assert Reservoir(a, a, network=inner).network is inner
    assert Reservoir(a, a, network=outer).network is outer
示例#13
0
def test_linear_network(Simulator, plt, seed, rng, neuron_type, atol, atol_x):
    n_neurons = 500
    dt = 0.001
    T = 1.0

    sys = Lowpass(0.1)
    scale_input = 2.0

    synapse = 0.02
    tau_probe = 0.005

    with Network(seed=seed) as model:
        stim = nengo.Node(
            output=nengo.processes.WhiteSignal(T, high=10, seed=seed))
        subnet = LinearNetwork(sys,
                               n_neurons_per_ensemble=n_neurons,
                               synapse=synapse,
                               input_synapse=synapse,
                               dt=dt,
                               neuron_type=neuron_type)
        nengo.Connection(stim,
                         subnet.input,
                         synapse=None,
                         transform=scale_input)

        assert subnet.synapse == subnet.input_synapse
        assert subnet.output_synapse is None

        p_input = nengo.Probe(subnet.input, synapse=tau_probe)
        p_x = nengo.Probe(subnet.state.output, synapse=tau_probe)
        p_output = nengo.Probe(subnet.output, synapse=tau_probe)

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

    ideal_output = shift(sys.filt(sim.data[p_input]))
    ideal_x = shift(subnet.realization.X.filt(sim.data[p_input]))

    plt.plot(sim.trange(), sim.data[p_input], label="Input", alpha=0.5)
    plt.plot(sim.trange(), sim.data[p_output], label="Actual y", alpha=0.5)
    plt.plot(sim.trange(),
             ideal_output,
             label="Expected y",
             alpha=0.5,
             linestyle='--')
    plt.plot(sim.trange(), sim.data[p_x], label="Actual x", alpha=0.5)
    plt.plot(sim.trange(),
             ideal_x,
             label="Expected x",
             alpha=0.5,
             linestyle='--')
    plt.legend()

    assert nrmse(sim.data[p_output], ideal_output) < atol
    assert nrmse(sim.data[p_x].squeeze(), ideal_x.squeeze()) < atol_x
示例#14
0
def time_cells(order):
    seed = 0
    n_neurons = 300
    theta = 4.784
    tau = 0.1
    radius = 0.3
    realizer = Balanced


    # The following was patched from nengolib commit
    # 7e204e0c305e34a4f63d0a6fbba7197862bbcf22, prior to
    # aee92b8fc45749f07f663fe696745cf0a33bfa17, so that
    # the generated PDF is consistent with the version that the
    # overlay was added to.
    def PadeDelay(c, q):
        j = np.arange(1, q+1, dtype=np.float64)
        u = (q + j - 1) * (q - j + 1) / (c * j)

        A = np.zeros((q, q))
        B = np.zeros((q, 1))
        C = np.zeros((1, q))
        D = np.zeros((1,))

        A[0, :] = B[0, 0] = -u[0]
        A[1:, :-1][np.diag_indices(q-1)] = u[1:]
        C[0, :] = - j / float(q) * (-1) ** (q - j)
        return LinearSystem((A, B, C, D), analog=True)

    F = PadeDelay(theta, order)
    synapse = Alpha(tau)

    pulse_s = 0
    pulse_w = 1.0
    pulse_h = 1.5

    T = 6.0
    dt = 0.001
    pulse = np.zeros(int(T/dt))
    pulse[int(pulse_s/dt):int((pulse_s + pulse_w)/dt)] = pulse_h

    with Network(seed=seed) as model:
        u = Node(output=PresentInput(pulse, dt))

        delay = LinearNetwork(
            F, n_neurons_per_ensemble=n_neurons / len(F), synapse=synapse,
            input_synapse=None, radii=radius, dt=dt, realizer=realizer())
        Connection(u, delay.input, synapse=None)

        p_x = Probe(delay.state.input, synapse=None)
        p_a = Probe(delay.state.add_neuron_output(), synapse=None)

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

    return sim.trange(), sim.data[p_x], sim.data[p_a]
示例#15
0
def _test_temporal_solver(plt, Simulator, seed, neuron_type, tau, f, solver):
    dt = 0.002

    # we are cheating a bit here because we'll use the same training data as
    # test data. this makes the unit testing a bit simpler since it's more
    # obvious what will happen when comparing temporal to default
    t = np.arange(0, 0.2, dt)
    stim = np.sin(2 * np.pi * 10 * t)
    function = (f(stim) if tau is None else nengo.Lowpass(tau).filt(f(stim),
                                                                    dt=dt))

    with Network(seed=seed) as model:
        u = nengo.Node(output=nengo.processes.PresentInput(stim, dt))
        x = nengo.Ensemble(100, 1, neuron_type=neuron_type)
        output_ideal = nengo.Node(size_in=1)

        post = dict(n_neurons=500,
                    dimensions=1,
                    neuron_type=nengo.LIFRate(),
                    seed=seed + 1)
        output_temporal = nengo.Ensemble(**post)
        output_default = nengo.Ensemble(**post)

        nengo.Connection(u, output_ideal, synapse=tau, function=f)
        nengo.Connection(u, x, synapse=None)
        nengo.Connection(x,
                         output_temporal,
                         synapse=tau,
                         eval_points=stim[:, None],
                         function=function[:, None],
                         solver=Temporal(synapse=tau, solver=solver))
        nengo.Connection(x,
                         output_default,
                         synapse=tau,
                         eval_points=stim[:, None],
                         function=f,
                         solver=solver)

        p_ideal = nengo.Probe(output_ideal, synapse=None)
        p_temporal = nengo.Probe(output_temporal, synapse=None)
        p_default = nengo.Probe(output_default, synapse=None)

    with Simulator(model, dt) as sim:
        sim.run(t[-1])

    plt.plot(sim.trange(),
             sim.data[p_ideal] - sim.data[p_default],
             label="Default")
    plt.plot(sim.trange(),
             sim.data[p_ideal] - sim.data[p_temporal],
             label="Temporal")
    plt.legend()

    return (nrmse(sim.data[p_default], target=sim.data[p_ideal]) /
            nrmse(sim.data[p_temporal], target=sim.data[p_ideal]))
示例#16
0
def discrete_example(seed, dt):
    n_neurons = 1000
    theta = 0.1
    freq = 50
    q = 27
    radii = 1.0
    sys = PadeDelay(theta, q)

    T = 5000*(dt+0.001)
    rms = 1.0
    signal = WhiteSignal(T, high=freq, rms=rms, y0=0)

    tau = 0.1
    tau_probe = 0.02
    reg = 0.1

    # Determine radii using direct mode
    with LinearNetwork(
            sys, n_neurons_per_ensemble=1, input_synapse=tau, synapse=tau,
            dt=dt, neuron_type=Direct(),
            realizer=Balanced()) as model:
        Connection(Node(output=signal), model.input, synapse=None)
        p_x = Probe(model.state.input, synapse=None)

    with Simulator(model, dt=dt, seed=seed+1) as sim:
        sim.run(T)

    radii *= np.max(abs(sim.data[p_x]), axis=0)
    logging.info("Radii: %s", radii)

    with Network(seed=seed) as model:
        u = Node(output=signal)

        kwargs = dict(
            n_neurons_per_ensemble=n_neurons / len(sys),
            input_synapse=tau, synapse=tau, radii=radii,
            solver=LstsqL2(reg=reg), realizer=Balanced())
        delay_disc = LinearNetwork(sys, dt=dt, **kwargs)
        delay_cont = LinearNetwork(sys, dt=None, **kwargs)
        Connection(u, delay_disc.input, synapse=None)
        Connection(u, delay_cont.input, synapse=None)

        p_u = Probe(u, synapse=tau_probe)
        p_y_disc = Probe(delay_disc.output, synapse=tau_probe)
        p_y_cont = Probe(delay_cont.output, synapse=tau_probe)

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

    return (theta, dt, sim.trange(), sim.data[p_u],
            sim.data[p_y_disc], sim.data[p_y_cont])
示例#17
0
def _test_lif(Simulator, seed, neuron_type, u, dt, n=500, t=2.0):
    with Network(seed=seed) as model:
        stim = nengo.Node(u)
        x = nengo.Ensemble(n, 1, neuron_type=neuron_type)
        nengo.Connection(stim, x, synapse=None)
        p = nengo.Probe(x.neurons)

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

    expected = get_activities(sim.model, x, [u]) * t
    actual = (sim.data[p] > 0).sum(axis=0)

    return rmse(actual, expected, axis=0)
示例#18
0
def _test_normalization(Simulator,
                        sys,
                        rng,
                        realizer,
                        l1_lower,
                        lower,
                        radius=5.0,
                        dt=0.0001,
                        T=1.0,
                        eps=1e-5):
    response = sys.X.impulse(int(T / dt), dt=dt)
    assert np.allclose(response[-10:], 0)
    l1_norms = radius * np.sum(abs(response * dt), axis=0)

    with Network() as model:
        stim = nengo.Node(output=lambda t: rng.choice([-radius, radius])
                          if t < T / 2 else radius)
        tau = 0.02
        subnet = LinearNetwork(sys,
                               n_neurons_per_ensemble=1,
                               synapse=tau,
                               dt=dt,
                               input_synapse=tau,
                               radii=radius,
                               realizer=realizer,
                               neuron_type=nengo.neurons.Direct())
        nengo.Connection(stim, subnet.input, synapse=None)
        p = nengo.Probe(subnet.state.output, synapse=None)

        assert np.allclose(inv(subnet.realizer_result.T),
                           subnet.realizer_result.Tinv)

    trans = subnet.realizer_result.T
    assert trans.shape == (len(sys), len(sys))
    est_worst_x = np.diagonal(trans)
    assert np.allclose(np.diag(est_worst_x), trans)  # make sure diagonal
    assert est_worst_x.shape == (len(sys), )

    assert ((l1_lower * est_worst_x <= l1_norms) | (est_worst_x <= eps)).all()
    assert (l1_norms <= est_worst_x + eps).all()

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

    # lower bound includes both approximation error and the gap between
    # random {-1, 1} flip-flop inputs and the true worst-case input
    worst_x = np.max(abs(sim.data[p]), axis=0)
    assert (lower <= worst_x + eps).all()
    assert (worst_x <= 1 + eps).all()
示例#19
0
def test_sim_new_synapse(Simulator):
    # Create a new synapse object and simulate it
    synapse = Lowpass(0.1) - Lowpass(0.01)
    with Network() as model:
        stim = nengo.Node(output=np.sin)
        x = nengo.Node(size_in=1)
        nengo.Connection(stim, x, synapse=synapse)
        p_stim = nengo.Probe(stim, synapse=None)
        p_x = nengo.Probe(x, synapse=None)

    with Simulator(model) as sim:
        sim.run(0.1)

    assert np.allclose(shift(synapse.filt(sim.data[p_stim])),
                       sim.data[p_x])
示例#20
0
def test_hetero_vector(Simulator, rng, seed):
    n_neurons = 20
    dt = 0.0005
    T = 0.1
    dims_in = 2
    synapses = [Alpha(0.1), Lowpass(0.005)]
    assert dims_in == len(synapses)

    encoders = sphere.sample(n_neurons, dims_in, rng=rng)

    with Network(seed=seed) as model:
        # Input stimulus
        stim = nengo.Node(size_in=dims_in)
        for i in range(dims_in):
            nengo.Connection(nengo.Node(
                output=nengo.processes.WhiteSignal(T, high=10, seed=seed)),
                             stim[i],
                             synapse=None)

        # HeteroSynapse Nodes
        syn_elemwise = nengo.Node(size_in=dims_in,
                                  output=HeteroSynapse(synapses,
                                                       dt,
                                                       elementwise=True))

        # For comparing results
        x = [
            nengo.Ensemble(n_neurons, dims_in, seed=0, encoders=encoders)
            for _ in range(2)
        ]  # expected, actual

        # Expected
        for j, synapse in enumerate(synapses):
            nengo.Connection(stim[j], x[0][j], synapse=synapse)

        # Actual
        nengo.Connection(stim, syn_elemwise, synapse=None)
        nengo.Connection(syn_elemwise, x[1], synapse=None)

        # Probes
        p_exp = nengo.Probe(x[0], synapse=None)
        p_act_elemwise = nengo.Probe(x[1], synapse=None)

    # Check correctness
    with Simulator(model, dt=dt) as sim:
        sim.run(T)

    assert np.allclose(sim.data[p_act_elemwise], sim.data[p_exp])
示例#21
0
def test_biased_solver(Simulator, seed, d):
    solver = BiasedSolver()
    function = solver.bias_function(d)

    assert solver.bias is None
    assert np.allclose(function(np.ones(d)), np.zeros(d))

    with Network(seed=seed) as model:
        x = nengo.Ensemble(100, d)
        conn = nengo.Connection(x, x, solver=solver)

    with Simulator(model) as sim:
        bias = sim.data[conn].solver_info['bias']

    assert np.allclose(solver.bias, bias)
    assert np.allclose(function(np.ones(d)), bias)
示例#22
0
def test_discrete_synapse(Simulator):
    # Test that discrete synapses are simulated properly
    delay_steps = 50

    with Network() as model:
        stim = nengo.Node(output=np.sin)
        output = nengo.Node(size_in=1)
        nengo.Connection(stim, output, synapse=z**-delay_steps)
        p_stim = nengo.Probe(stim, synapse=None)
        p_output = nengo.Probe(output, synapse=None)

    with Simulator(model) as sim:
        sim.run(1.0)

    assert np.allclose(sim.data[p_output][delay_steps:],
                       sim.data[p_stim][:-delay_steps])
示例#23
0
def test_tanh(Simulator, seed):
    T = 0.1
    with Network(seed=seed) as model:
        stim = nengo.Node(
            output=nengo.processes.WhiteSignal(T, high=10, seed=seed))
        x = nengo.Ensemble(2, 1, neuron_type=Tanh())
        nengo.Connection(stim,
                         x.neurons,
                         transform=np.ones((2, 1)),
                         synapse=None)
        p_stim = nengo.Probe(stim, synapse=None)
        p_x = nengo.Probe(x.neurons, synapse=None)

    with Simulator(model) as sim:
        sim.run(T)

    assert np.allclose(sim.data[x].gain, 1)
    assert np.allclose(sim.data[x].bias, 0)
    assert np.allclose(sim.data[p_x], np.tanh(sim.data[p_stim]))
示例#24
0
def test_echo_state(Simulator, plt, seed, rng, include_bias):
    test_t = 1.0
    train_t = 5.0
    dt = 0.001

    n_neurons = 1000
    dimensions = 2
    process = WhiteSignal(train_t, high=10)

    with Network(seed=seed) as model:
        stim = nengo.Node(output=process, size_out=dimensions)
        esn = EchoState(n_neurons,
                        dimensions,
                        include_bias=include_bias,
                        rng=rng)
        nengo.Connection(stim, esn.input, synapse=None)

        p = nengo.Probe(esn.output, synapse=None)
        p_stim = nengo.Probe(stim, synapse=None)

    # train the reservoir to compute a highpass filter
    def function(x):
        return Highpass(0.01).filt(x, dt=dt)

    esn.train(function, test_t, dt, process, seed=seed)

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

    ideal = function(sim.data[p_stim])

    plt.figure()
    plt.plot(sim.trange(), sim.data[p_stim], label="Input")
    plt.plot(sim.trange(), sim.data[p], label="Output")
    plt.plot(sim.trange(), ideal, label="Ideal")
    plt.legend()

    if include_bias:
        assert rmse(sim.data[p], ideal) <= 0.5 * rms(ideal)
    else:
        assert rmse(sim.data[p], ideal) <= 0.7 * rms(ideal)
示例#25
0
def test_radii(Simulator, seed, plt):
    sys = canonical(PadeDelay(0.2, order=3))
    dt = 0.001
    T = 0.5

    plt.figure()

    # Precompute the exact bounds for an impulse stimulus
    radii = []
    for sub in sys:
        response = sub.impulse(int(T / dt), dt=dt)
        amplitude = np.max(abs(response))
        assert amplitude >= 1e-4  # otherwise numerical issues
        radii.append(amplitude)

        plt.plot(response / amplitude, linestyle='--')

    with Network(seed=seed) as model:
        # Impulse stimulus
        stim = nengo.Node(output=lambda t: 1 / dt if t <= dt else 0)

        # Set explicit radii for controllable realization
        subnet = LinearNetwork(sys,
                               n_neurons_per_ensemble=1,
                               synapse=0.2,
                               input_synapse=0.2,
                               dt=dt,
                               radii=radii,
                               realizer=Identity(),
                               neuron_type=nengo.neurons.Direct())
        nengo.Connection(stim, subnet.input, synapse=None)
        p = nengo.Probe(subnet.state.output, synapse=None)

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

    plt.plot(sim.data[p], lw=5, alpha=0.5)

    assert np.allclose(np.max(abs(sim.data[p]), axis=0), 1, atol=1e-4)
示例#26
0
def test_simulation(sys, Simulator, plt, seed):
    assert isinstance(sys, LinearSystem)
    old_sys = nengo.LinearFilter(sys.num, sys.den)
    assert sys == old_sys

    with Network() as model:
        stim = nengo.Node(output=nengo.processes.WhiteSignal(
            1.0, high=10, seed=seed))
        out_new = nengo.Node(size_in=2)
        out_old = nengo.Node(size_in=2)
        nengo.Connection(stim, out_new, transform=[[1], [-1]], synapse=sys)
        nengo.Connection(stim, out_old, transform=[[1], [-1]], synapse=old_sys)
        p_new = nengo.Probe(out_new)
        p_old = nengo.Probe(out_old)

    with Simulator(model) as sim:
        sim.run(1.0)

    plt.figure()
    plt.plot(sim.trange(), sim.data[p_new])
    plt.plot(sim.trange(), sim.data[p_old])

    assert np.allclose(sim.data[p_new], sim.data[p_old])
示例#27
0
def test_connection(Simulator, seed, d):
    with Network(seed=seed) as model:
        stim = nengo.Node(output=lambda t: np.sin(t*2*np.pi), size_out=d)
        x = nengo.Ensemble(1, d, intercepts=[-1], neuron_type=nengo.LIFRate())
        default = nengo.Node(size_in=d)
        improved = nengo.Node(size_in=d)

        stim_conn = Connection(stim, x, synapse=None)
        default_conn = nengo.Connection(x, default)
        improved_conn = Connection(x, improved)

        p_default = nengo.Probe(default)
        p_improved = nengo.Probe(improved)
        p_stim = nengo.Probe(stim, synapse=0.005)

    assert not isinstance(stim_conn.solver, BiasedSolver)
    assert not isinstance(default_conn.solver, BiasedSolver)
    assert isinstance(improved_conn.solver, BiasedSolver)

    with Simulator(model) as sim:
        sim.run(1.0)

    assert (rmse(sim.data[p_default], sim.data[p_stim]) >
            rmse(sim.data[p_improved], sim.data[p_stim]))
示例#28
0
def test_bad_inputs_outputs():
    with Network():
        only_out = nengo.Node(output=[0])
        no_out = nengo.Node(size_out=0)
        okay = nengo.Ensemble(1, 1)
        bad = nengo.Network()

        with pytest.raises(ValueError):  # must contain at least one input
            Reservoir([], okay)

        with pytest.raises(ValueError):  # must contain at least one output
            Reservoir(okay, [])

        with pytest.raises(ValueError):  # must contain at least one input
            Reservoir(only_out, okay)

        with pytest.raises(ValueError):  # must contain at least one output
            Reservoir(okay, no_out)

        with pytest.raises(TypeError):  # must be connectable object
            Reservoir(bad, okay)

        with pytest.raises(TypeError):  # must be connectable object
            Reservoir(okay, bad)
示例#29
0
def test_ensemble_array():
    with Network():
        ea = nengo.networks.EnsembleArray(100, 2)
        for ens in ea.ea_ensembles:
            assert isinstance(ens.eval_points, ScatteredHypersphere)
            assert isinstance(ens.encoders, ScatteredHypersphere)
示例#30
0
    def __init__(self, n_neurons, dimensions, recurrent_synapse=0.005,
                 readout_synapse=None, radii=1.0, gain=1.25, rng=None,
                 neuron_type=Tanh(), include_bias=True, ens_seed=None,
                 label=None, seed=None, add_to_container=None, **ens_kwargs):
        """Initializes the Echo State Network.

        Parameters
        ----------
        n_neurons : int
            The number of neurons to use in the reservoir.
        dimensions : int
            The dimensionality of the input signal.
        recurrent_synapse : nengo.synapses.Synapse (Default: ``0.005``)
            Synapse used to filter the recurrent connection.
        readout_synapse : nengo.synapses.Synapse (Default: ``None``)
            Optional synapse to filter all of the outputs before solving
            for the linear readout. This is included in the connection to the
            ``output`` Node created within the network.
        radii : scalar or array_like, optional (Default: ``1``)
            The radius of each dimension of the input signal, used to normalize
            the incoming connection weights.
        gain : scalar, optional (Default: ``1.25``)
            A scalar gain on the recurrent connection weight matrix.
        rng : ``numpy.random.RandomState``, optional (Default: ``None``)
            Random state used to initialize all weights.
        neuron_type : ``nengo.neurons.NeuronType`` optional \
                      (Default: ``Tanh()``)
            Neuron model to use within the reservoir.
        include_bias : ``bool`` (Default: ``True``)
            Whether to include a bias current to the neural nonlinearity.
            This should be ``False`` if the neuron model already has a bias,
            e.g., ``LIF`` or ``LIFRate``.
        ens_seed : int, optional (Default: ``None``)
            Seed passed to the ensemble of neurons.
        """

        Network.__init__(self, label, seed, add_to_container)

        self.n_neurons = n_neurons
        self.dimensions = dimensions
        self.recurrent_synapse = recurrent_synapse
        self.radii = radii  # TODO: make array or scalar parameter?
        self.gain = gain
        self.rng = np.random if rng is None else rng
        self.neuron_type = neuron_type
        self.include_bias = include_bias

        self.W_in = (
            self.rng.rand(self.n_neurons, self.dimensions) - 0.5) / self.radii
        if self.include_bias:
            self.W_bias = self.rng.rand(self.n_neurons, 1) - 0.5
        else:
            self.W_bias = np.zeros((self.n_neurons, 1))
        self.W = self.rng.rand(self.n_neurons, self.n_neurons) - 0.5
        self.W *= self.gain / max(abs(eig(self.W)[0]))

        with self:
            self.ensemble = nengo.Ensemble(
                self.n_neurons, 1, neuron_type=self.neuron_type, seed=ens_seed,
                **ens_kwargs)
            self.input = nengo.Node(size_in=self.dimensions)

            pool = self.ensemble.neurons
            nengo.Connection(
                self.input, pool, transform=self.W_in, synapse=None)
            nengo.Connection(  # note the bias will be active during training
                nengo.Node(output=1, label="bias"), pool,
                transform=self.W_bias, synapse=None)
            nengo.Connection(
                self.ensemble.neurons, pool, transform=self.W,
                synapse=self.recurrent_synapse)

        Reservoir.__init__(
           self, self.input, pool, readout_synapse=readout_synapse,
           network=self)
示例#31
0
def test_alpha_whitesignal(Simulator, seed, rng, plt):
    # Pick test LinearSystem
    sys = Alpha(0.1)
    state = sys.X
    assert state.shape == (2, 1)

    # Pick test process
    n_steps = 1000
    dt = 0.01
    process = WhiteSignal(5.0, high=10, default_dt=dt, seed=seed)

    # Sample evaluation points
    dist = EvalPoints(state, process, n_steps=n_steps)
    assert dist.n_steps == n_steps
    assert dist.dt == dt  # taken from process
    assert isinstance(repr(dist), str)

    n_eval_points = 500
    eval_points = dist.sample(n_eval_points, 2, rng=rng)
    assert eval_points.shape == (n_eval_points, 2)

    # Sample encoders
    encoders = Encoders(state, process).sample(n_eval_points, 2, rng=rng)
    assert encoders.shape == (n_eval_points, 2)

    plt.figure()
    # plt.scatter(*encoders.T, label="Encoders")
    plt.scatter(*eval_points.T, s=2, marker='*', label="Eval Points")

    # Check that most evaluation points fall within radii
    x_m, zero, x_p = sorted(np.unique(encoders[:, 0]))
    assert np.allclose(-x_m, x_p)
    assert np.allclose(zero, 0)
    sl = (1 / x_m < eval_points[:, 0]) & (eval_points[:, 0] < 1 / x_p)
    assert np.count_nonzero(sl) / float(n_eval_points) >= 0.99

    y_m, zero, y_p = sorted(np.unique(encoders[:, 1]))
    assert np.allclose(zero, 0)
    assert np.allclose(-y_m, y_p)
    sl = (1 / y_m < eval_points[:, 1]) & (eval_points[:, 1] < 1 / y_p)
    assert np.count_nonzero(sl) / float(n_eval_points) >= 0.99

    # Simulate same process / system in nengo network
    with Network() as model:
        output = nengo.Node(output=process)
        probes = [nengo.Probe(output, synapse=sub) for sub in sys]

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

    plt.scatter(sim.data[probes[0]],
                sim.data[probes[1]],
                s=1,
                alpha=0.5,
                label="Simulated")
    plt.legend()

    # Check that each eval_point is a subset / sample from the ideal
    ideal = np.asarray(
        [sim.data[probes[0]].squeeze(), sim.data[probes[1]].squeeze()]).T
    assert ideal.shape == (n_steps, 2)

    for pt in eval_points:
        dists = np.linalg.norm(ideal - pt[None, :], axis=1)
        assert dists.shape == (n_steps, )
        assert np.allclose(np.min(dists), 0)