Example #1
0
def test_solvers(Simulator, nl_nodirect):

    N = 100
    decoder_solvers = [
        Lstsq(), LstsqNoise(),
        LstsqL2(), LstsqL2nz(),
        LstsqL1()
    ]
    weight_solvers = [LstsqL1(weights=True), LstsqDrop(weights=True)]

    dt = 1e-3
    tfinal = 4

    def input_function(t):
        return np.interp(t, [1, 3], [-1, 1], left=-1, right=1)

    model = nengo.Network('test_solvers', seed=290)
    with model:
        u = nengo.Node(output=input_function)
        a = nengo.Ensemble(nl_nodirect(N), dimensions=1)
        nengo.Connection(u, a)
        ap = nengo.Probe(a)

        probes = []
        names = []
        for solver in decoder_solvers + weight_solvers:
            b = nengo.Ensemble(nl_nodirect(N), dimensions=1, seed=99)
            nengo.Connection(a, b, solver=solver)
            probes.append(nengo.Probe(b))
            names.append(
                "%s(%s)" %
                (solver.__class__.__name__, 'w' if solver.weights else 'd'))

    sim = Simulator(model, dt=dt)
    sim.run(tfinal)
    t = sim.trange()

    # ref = sim.data[up]
    ref = filtfilt(sim.data[ap], 20)
    outputs = np.array([sim.data[probe] for probe in probes])
    outputs_f = filtfilt(outputs, 20, axis=1)

    close = allclose(t,
                     ref,
                     outputs_f,
                     plotter=Plotter(Simulator, nl_nodirect),
                     filename='test_decoders.test_solvers.pdf',
                     labels=names,
                     atol=0.05,
                     rtol=0,
                     buf=100,
                     delay=7)
    for name, c in zip(names, close):
        assert c, "Solver '%s' does not meet tolerances" % name
Example #2
0
def test_weights(Simulator, nl):
    name = 'test_weights'
    n1, n2 = 100, 50

    def func(t):
        return np.array([np.sin(4 * t), np.cos(12 * t)])

    transform = np.array([[0.6, -0.4]])

    m = nengo.Network(label=name, seed=3902)
    with m:
        m.config[nengo.Ensemble].neuron_type = nl()
        u = nengo.Node(output=func)
        a = nengo.Ensemble(n1, dimensions=2, radius=1.5)
        b = nengo.Ensemble(n2, dimensions=1)
        bp = nengo.Probe(b)

        nengo.Connection(u, a)
        nengo.Connection(a, b, transform=transform,
                         solver=LstsqL2(weights=True))

    sim = Simulator(m)
    sim.run(2.)

    t = sim.trange()
    x = func(t).T
    y = np.dot(x, transform.T)
    z = filtfilt(sim.data[bp], 10, axis=0)
    assert allclose(t, y.flatten(), z.flatten(),
                    plotter=Plotter(Simulator, nl),
                    filename='test_connection.' + name + '.pdf',
                    atol=0.1, rtol=0, buf=100, delay=10)
Example #3
0
def test_weights(Simulator, nl):
    name = 'test_weights'
    n1, n2 = 100, 50

    def func(t):
        return np.array([np.sin(4 * t), np.cos(12 * t)])

    transform = np.array([[0.6, -0.4]])

    m = nengo.Model(name, seed=3902)
    u = nengo.Node(output=func)
    a = nengo.Ensemble(nl(n1), dimensions=2, radius=1.5)
    b = nengo.Ensemble(nl(n2), dimensions=1)
    bp = nengo.Probe(b)

    nengo.Connection(u, a)
    nengo.Connection(a, b, transform=transform,
                     weight_solver=nengo.decoders.lstsq_L2nz)

    sim = Simulator(m)
    sim.run(2.)

    t = sim.trange()
    x = func(t).T
    y = np.dot(x, transform.T)
    z = filtfilt(sim.data[bp], 10, axis=0)
    assert allclose(t, y.flatten(), z.flatten(),
                    plotter=Plotter(Simulator, nl),
                    filename='test_connection.' + name + '.pdf',
                    atol=0.1, rtol=0, buf=100, delay=10)
Example #4
0
def test_compare_solvers(Simulator, nl_nodirect):

    N = 70
    decoder_solvers = [
        Lstsq(), LstsqNoise(), LstsqL2(), LstsqL2nz(), LstsqL1()]
    weight_solvers = [LstsqL1(weights=True), LstsqDrop(weights=True)]

    dt = 1e-3
    tfinal = 4

    def input_function(t):
        return np.interp(t, [1, 3], [-1, 1], left=-1, right=1)

    model = nengo.Network('test_solvers', seed=290)
    with model:
        model.config[nengo.Ensemble].neuron_type = nl_nodirect()
        u = nengo.Node(output=input_function)
        a = nengo.Ensemble(N, dimensions=1)
        nengo.Connection(u, a)
        ap = nengo.Probe(a)

        probes = []
        names = []
        for solver in decoder_solvers + weight_solvers:
            b = nengo.Ensemble(N, dimensions=1, seed=99)
            nengo.Connection(a, b, solver=solver)
            probes.append(nengo.Probe(b))
            names.append("%s(%s)" % (
                solver.__class__.__name__, 'w' if solver.weights else 'd'))

    sim = Simulator(model, dt=dt)
    sim.run(tfinal)
    t = sim.trange()

    # ref = sim.data[up]
    ref = filtfilt(sim.data[ap], 20)
    outputs = np.array([sim.data[probe][:, 0] for probe in probes])
    outputs_f = filtfilt(outputs, 20, axis=1)

    close = allclose(t, ref, outputs_f,
                     plotter=Plotter(Simulator, nl_nodirect),
                     filename='test_solvers.test_solvers.pdf',
                     labels=names,
                     atol=0.05, rtol=0, buf=100, delay=7)
    for name, c in zip(names, close):
        assert c, "Solver '%s' does not meet tolerances" % name
Example #5
0
def test_solvers(Simulator, nl_nodirect):

    N = 100
    decoder_solvers = [lstsq, lstsq_noise, lstsq_L2, lstsq_L2nz, lstsq_L1]
    weight_solvers = [lstsq_L1, lstsq_drop]

    dt = 1e-3
    tfinal = 4

    def input_function(t):
        return np.interp(t, [1, 3], [-1, 1], left=-1, right=1)

    model = nengo.Network('test_solvers', seed=290)
    with model:
        u = nengo.Node(output=input_function)
        # up = nengo.Probe(u)

        a = nengo.Ensemble(nl_nodirect(N), dimensions=1)
        ap = nengo.Probe(a)
        nengo.Connection(u, a)

        probes = []
        names = []
        for solver, arg in ([(s, 'decoder_solver') for s in decoder_solvers] +
                            [(s, 'weight_solver') for s in weight_solvers]):
            b = nengo.Ensemble(nl_nodirect(N), dimensions=1, seed=99)
            nengo.Connection(a, b, **{arg: solver})
            probes.append(nengo.Probe(b))
            names.append(solver.__name__ + (" (%s)" % arg[0]))

    sim = nengo.Simulator(model, dt=dt)
    sim.run(tfinal)
    t = sim.trange()

    # ref = sim.data[up]
    ref = filtfilt(sim.data[ap], 20)
    outputs = np.array([sim.data[probe] for probe in probes])
    outputs_f = filtfilt(outputs, 20, axis=1)

    close = allclose(t, ref, outputs_f,
                     plotter=Plotter(Simulator, nl_nodirect),
                     filename='test_decoders.test_solvers.pdf',
                     labels=names,
                     atol=0.05, rtol=0, buf=100, delay=7)
    for name, c in zip(names, close):
        assert c, "Solver '%s' does not meet tolerances" % name
Example #6
0
def test_filtfilt():
    dt = 1e-3
    tend = 3.
    t = dt * np.arange(tend / dt)
    nt = len(t)

    tau = 0.03 / dt

    u = np.random.normal(size=nt)
    x = filt(u, tau)
    x = filt(x[::-1], tau, x0=x[-1])[::-1]
    y = filtfilt(u, tau)

    with Plotter(nengo.Simulator) as plt:
        plt.plot(t, x)
        plt.plot(t, y, '--')
        plt.savefig('utils.test_filtering.test_filtfilt.pdf')
        plt.close()

    assert np.allclose(x, y)
Example #7
0
def test_weights(Simulator, nl):
    name = 'test_weights'
    n1, n2 = 100, 50

    def func(t):
        return np.array([np.sin(4 * t), np.cos(12 * t)])

    transform = np.array([[0.6, -0.4]])

    m = nengo.Network(label=name, seed=3902)
    with m:
        m.config[nengo.Ensemble].neuron_type = nl()
        u = nengo.Node(output=func)
        a = nengo.Ensemble(n1, dimensions=2, radius=1.5)
        b = nengo.Ensemble(n2, dimensions=1)
        bp = nengo.Probe(b)

        nengo.Connection(u, a)
        nengo.Connection(a,
                         b,
                         transform=transform,
                         solver=LstsqL2(weights=True))

    sim = Simulator(m)
    sim.run(2.)

    t = sim.trange()
    x = func(t).T
    y = np.dot(x, transform.T)
    z = filtfilt(sim.data[bp], 10, axis=0)
    assert allclose(t,
                    y.flatten(),
                    z.flatten(),
                    plotter=Plotter(Simulator, nl),
                    filename='test_connection.' + name + '.pdf',
                    atol=0.1,
                    rtol=0,
                    buf=100,
                    delay=10)
Example #8
0
def test_eval_points(Simulator, nl_nodirect):
    rng = np.random.RandomState(0)
    n = 100
    d = 5
    filter = 0.08
    dt = 1e-3

    eval_points = np.logspace(np.log10(300), np.log10(5000), 11)
    eval_points = np.round(eval_points).astype('int')
    max_points = eval_points.max()
    n_trials = 1

    rmses = np.nan * np.zeros((len(eval_points), n_trials))
    for j in range(n_trials):
        points = rng.normal(size=(max_points, d))
        points *= (rng.uniform(size=max_points)
                   / norm(points, axis=-1))[:, None]

        rng_j = np.random.RandomState(348 + j)
        seed = 903824 + j

        # generate random input in unit hypersphere
        x = rng_j.normal(size=d)
        x *= rng_j.uniform() / norm(x)

        for i, n_points in enumerate(eval_points):
            model = nengo.Network(
                'test_eval_points(%d,%d)' % (i, j), seed=seed)
            with model:
                u = nengo.Node(output=x)
                a = nengo.Ensemble(nl_nodirect(n * d), d,
                                   eval_points=points[:n_points])
                nengo.Connection(u, a, synapse=0)
                up = nengo.Probe(u)
                ap = nengo.Probe(a)

            with Timer() as timer:
                sim = Simulator(model, dt=dt)
            sim.run(10 * filter)

            t = sim.trange()
            xt = filtfilt(sim.data[up], filter / dt)
            yt = filtfilt(sim.data[ap], filter / dt)
            t0 = 5 * filter
            t1 = 7 * filter
            tmask = (t > t0) & (t < t1)

            rmses[i, j] = rms(yt[tmask] - xt[tmask])
            print("done %d (%d) in %0.3f s" % (n_points, j, timer.duration))

    # subtract out mean for each model
    rmses_norm = rmses - rmses.mean(0, keepdims=True)

    with Plotter(Simulator, nl_nodirect) as plt:
        mean = rmses_norm.mean(1)
        low = rmses_norm.min(1)
        high = rmses_norm.max(1)
        plt.semilogx(eval_points, mean, 'k-')
        plt.semilogx(eval_points, high, 'r-')
        plt.semilogx(eval_points, low, 'b-')
        plt.xlim([eval_points[0], eval_points[-1]])
        plt.xticks(eval_points, eval_points)
        plt.savefig('test_decoders.test_eval_points.pdf')
        plt.close()
Example #9
0
def test_eval_points(Simulator, nl_nodirect):
    rng = np.random.RandomState(0)
    n = 100
    d = 5
    filter = 0.08
    dt = 1e-3

    eval_points = np.logspace(np.log10(300), np.log10(5000), 11)
    eval_points = np.round(eval_points).astype('int')
    max_points = eval_points.max()
    n_trials = 1

    rmses = np.nan * np.zeros((len(eval_points), n_trials))
    for j in range(n_trials):
        points = rng.normal(size=(max_points, d))
        points *= (rng.uniform(size=max_points)
                   / norm(points, axis=-1))[:, None]

        rng_j = np.random.RandomState(348 + j)
        seed = 903824 + j

        # generate random input in unit hypersphere
        x = rng_j.normal(size=d)
        x *= rng_j.uniform() / norm(x)

        for i, n_points in enumerate(eval_points):
            model = nengo.Network(
                'test_eval_points(%d,%d)' % (i, j), seed=seed)
            with model:
                u = nengo.Node(output=x)
                a = nengo.Ensemble(nl_nodirect(n * d), d,
                                   eval_points=points[:n_points])
                nengo.Connection(u, a, synapse=0)
                up = nengo.Probe(u)
                ap = nengo.Probe(a)

            with Timer() as timer:
                sim = Simulator(model, dt=dt)
            sim.run(10 * filter)

            t = sim.trange()
            xt = filtfilt(sim.data[up], filter / dt)
            yt = filtfilt(sim.data[ap], filter / dt)
            t0 = 5 * filter
            t1 = 7 * filter
            tmask = (t > t0) & (t < t1)

            rmses[i, j] = rms(yt[tmask] - xt[tmask])
            print("done %d (%d) in %0.3f s" % (n_points, j, timer.duration))

    # subtract out mean for each model
    rmses_norm = rmses - rmses.mean(0, keepdims=True)

    with Plotter(Simulator, nl_nodirect) as plt:
        mean = rmses_norm.mean(1)
        low = rmses_norm.min(1)
        high = rmses_norm.max(1)
        plt.semilogx(eval_points, mean, 'k-')
        plt.semilogx(eval_points, high, 'r-')
        plt.semilogx(eval_points, low, 'b-')
        plt.xlim([eval_points[0], eval_points[-1]])
        plt.xticks(eval_points, eval_points)
        plt.savefig('test_decoders.test_eval_points.pdf')
        plt.close()