Exemple #1
0
def test_lif(Simulator, plt, rng, dt, allclose):
    """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=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
    logging.info("ME = %f", (sim_rates - math_rates).mean())
    logging.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 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()
Exemple #2
0
def test_lif(Simulator):
    """Test that the dynamic model approximately matches the rates"""
    rng = np.random.RandomState(85243)

    dt = 0.001
    n = 5000
    x = 0.5
    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(nengo.LIF(n),
                             1,
                             max_rates=max_rates,
                             intercepts=intercepts)
        nengo.Connection(ins, ens.neurons, transform=np.ones((n, 1)))
        spike_probe = nengo.Probe(ens.neurons, "output")

    sim = Simulator(m, dt=dt)

    t_final = 1.0
    sim.run(t_final)
    spikes = sim.data[spike_probe].sum(0)

    math_rates = ens.neurons.rates(
        x, *ens.neurons.gain_bias(max_rates, intercepts))
    sim_rates = spikes / t_final
    logger.debug("ME = %f", (sim_rates - math_rates).mean())
    logger.debug("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)
Exemple #3
0
def test_pes_error_clip(plt, seed, Simulator):
    dims = 2
    n_per_dim = 120
    tau = 0.01
    error_scale = 5.  # scale up error signal so it clips
    simtime = 0.3
    model, probes = pes_network(
        n_per_dim, dims, seed, learn_synapse=tau,
        learning_rule_type=nengo.PES(learning_rate=1e-2 / error_scale),
        input_scale=np.array([1., -1.]),
        error_scale=error_scale,
        period=simtime)

    with pytest.warns(UserWarning, match=r'.*PES error.*Clipping.'):
        with Simulator(model) as loihi_sim:
            loihi_sim.run(simtime)

    t = loihi_sim.trange()
    post_tmask = t > simtime - 1.0

    dec_tau = loihi_sim.model.decode_tau
    y = loihi_sim.data[probes['stim']]
    y_dpre = nengo.Lowpass(dec_tau).filt(y)
    y_dpost = nengo.Lowpass(tau).combine(nengo.Lowpass(dec_tau)).filt(y_dpre)
    y_loihi = loihi_sim.data[probes['post']]

    plt.plot(t, y_dpost, 'k', label='target')
    plt.plot(t, y_loihi, 'g', label='loihi')

    # --- assert that we've learned something, but not everything
    error = (rms(y_loihi[post_tmask] - y_dpost[post_tmask])
             / rms(y_dpost[post_tmask]))
    assert error < 0.5
    assert error > 0.05
Exemple #4
0
def test_decoder_solver(solver):
    rng = np.random.RandomState(39408)

    dims = 1
    n_neurons = 100
    n_points = 500

    rates = get_rate_function(n_neurons, dims, rng=rng)
    E = get_encoders(n_neurons, dims, rng=rng)

    train = get_eval_points(n_points, dims, rng=rng)
    Atrain = rates(np.dot(train, E))

    D, _ = solver(Atrain, train, rng=rng)

    test = get_eval_points(n_points, dims, rng=rng, sort=True)
    Atest = rates(np.dot(test, E))
    est = np.dot(Atest, D)
    rel_rmse = rms(est - test) / rms(test)

    with Plotter(nengo.Simulator) as plt:
        plt.plot(test, np.zeros_like(test), 'k--')
        plt.plot(test, test - est)
        plt.title("relative RMSE: %0.2e" % rel_rmse)
        plt.savefig('test_decoders.test_decoder_solver.%s.pdf'
                    % solver.__name__)
        plt.close()

    assert np.allclose(test, est, atol=3e-2, rtol=1e-3)
    assert rel_rmse < 0.02
Exemple #5
0
def test_decoder_solver(Solver, plt, rng, allclose):
    solver = Solver()

    dims = 1
    n_neurons = 100
    n_points = 1000

    rates = get_rate_function(n_neurons, rng=rng)
    E = get_encoders(n_neurons, dims, rng=rng)

    train = get_eval_points(n_points, dims, rng=rng)
    Atrain = rates(np.dot(train, E))

    D, _ = solver(Atrain, train, rng=rng)

    test = get_eval_points(n_points, dims, rng=rng, sort=True)
    Atest = rates(np.dot(test, E))
    est = np.dot(Atest, D)
    rel_rmse = rms(est - test) / rms(test)

    plt.plot(test, np.zeros_like(test), "k--")
    plt.plot(test, test - est)
    plt.title(f"relative RMSE: {rel_rmse:0.2e}")

    atol = (0.1 if isinstance(solver, (LstsqNoise, LstsqDrop,
                                       LstsqMultNoise)) else 1.5e-2)
    assert allclose(test, est, atol=atol, rtol=1e-3)
    assert rel_rmse < 0.02
Exemple #6
0
def error_plot(dt, prestime, t, ystar, learners, tmin=None, tmax=None, ax=None):
    ax = plt.gca() if ax is None else ax
    es = [learner['e'] for learner in learners]

    if tmin is not None or tmax is not None:
        tmin = t[0] if tmin is None else tmin
        tmax = t[-1] if tmax is None else tmax
        tmask = (t >= tmin) & (t <= tmax)
        t = t[tmask]
        es = [e[tmask] for e in es]

    vsynapse = Alpha(0.01, default_dt=dt)

    # esynapse = Alpha(1*prestime, default_dt=dt)
    # esynapse = Alpha(1*prestime, default_dt=dt)
    esynapse = Alpha(5*prestime, default_dt=dt)
    # esynapse = Alpha(20*prestime, default_dt=dt)

    # yrms = rms(ystar, axis=1).mean()
    yrms = rms(vsynapse.filtfilt(ystar), axis=1).mean()
    # yrms = rms(esynapse.filtfilt(ystar), axis=1).mean()
    # print(yrms)
    for e in es:
        # erms = esynapse.filtfilt(rms(e, axis=1) / yrms)
        # erms = rms(esynapse.filtfilt(e), axis=1) / yrms
        # erms = rms(vsynapse.filtfilt(e), axis=1) / yrms
        erms = esynapse.filtfilt(rms(vsynapse.filtfilt(e), axis=1) / yrms)
        plt.plot(t, erms)
Exemple #7
0
def test_decoder_solver(Solver, plt, rng):
    solver = Solver()

    dims = 1
    n_neurons = 100
    n_points = 500

    rates = get_rate_function(n_neurons, dims, rng=rng)
    E = get_encoders(n_neurons, dims, rng=rng)

    train = get_eval_points(n_points, dims, rng=rng)
    Atrain = rates(np.dot(train, E))

    D, _ = solver(Atrain, train, rng=rng)

    test = get_eval_points(n_points, dims, rng=rng, sort=True)
    Atest = rates(np.dot(test, E))
    est = np.dot(Atest, D)
    rel_rmse = rms(est - test) / rms(test)

    plt.plot(test, np.zeros_like(test), 'k--')
    plt.plot(test, test - est)
    plt.title("relative RMSE: %0.2e" % rel_rmse)

    atol = 3.5e-2 if isinstance(solver, LstsqNoise) else 1.5e-2
    assert np.allclose(test, est, atol=atol, rtol=1e-3)
    assert rel_rmse < 0.02
Exemple #8
0
def test_decay_magnitude(offset, plt, logger):
    bits = 12
    decays = np.arange(1, 2**bits, 7)
    ref = []
    emp = []
    est = []

    for decay in decays:
        def empirical_decay_magnitude(decay, x0):
            x = x0 * np.ones(decay.shape, dtype=np.int32)
            y = x
            y_sum = np.zeros(decay.shape, dtype=np.int64)
            for i in range(100000):
                y_sum += y
                if (y <= 0).all():
                    break
                y = decay_int(y, decay, bits=bits, offset=offset)
            else:
                raise RuntimeError("Exceeded max number of iterations")

            return y_sum / x0

        x0 = np.arange(2**21 - 1000, 2**21, step=41, dtype=np.int32)

        m = empirical_decay_magnitude(decay * np.ones_like(x0), x0)
        m0 = m.mean()
        emp.append(m0)

        # reference (naive) method, not accounting for truncation loss
        r = (2**bits - offset - decay) / 2**bits
        Sx1 = (1 - r/x0) / (1 - r)  # sum_i^n x_i/x_0, where x_n = r**n*x_0 = 1
        ref.append(Sx1.mean())

        m = decay_magnitude(decay, x0, bits=bits, offset=offset)
        m2 = m.mean()
        est.append(m2)

    ref = np.array(ref)
    emp = np.array(emp)
    est = np.array(est)
    rms_ref = rms(ref - emp) / rms(emp)
    rms_est = rms(est - emp) / rms(emp)
    logger.info("Ref rel RMSE: %0.3e, decay_magnitude rel RMSE: %0.3e" % (
        rms_ref, rms_est))

    abs_ref = np.abs(ref - emp)
    abs_est = np.abs(est - emp)

    # find places where ref is better than est
    relative_diff = (abs_est - abs_ref) / emp

    ax = plt.subplot(211)
    plt.plot(abs_ref, 'b')
    plt.plot(abs_est, 'g')
    ax.set_yscale('log')

    ax = plt.subplot(212)
    plt.plot(relative_diff.clip(0, None))

    assert np.all(relative_diff < 1e-6)
Exemple #9
0
def test_run(Simulator, seed):
    rng = np.random.RandomState(seed)
    vocab = spa.Vocabulary(16, rng=rng)

    with spa.SPA(seed=seed, vocabs=[vocab]) as model:
        model.bind = spa.Bind(dimensions=16)

        def inputA(t):
            if 0 <= t < 0.1:
                return "A"
            else:
                return "B"

        model.input = spa.Input(bind_A=inputA, bind_B="A")

    bind, vocab = model.get_module_output("bind")

    with model:
        p = nengo.Probe(bind, "output", synapse=0.03)

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

    error = rms(vocab.parse("B*A").v - sim.data[p][-1])
    assert error < 0.1

    error = rms(vocab.parse("A*A").v - sim.data[p][100])
    assert error < 0.1
Exemple #10
0
def test_decoder_solver(Solver, plt, rng):
    if isinstance(Solver, tuple):
        Solver, args, kwargs = Solver
    else:
        args, kwargs = (), {}

    dims = 1
    n_neurons = 100
    n_points = 500

    rates = get_rate_function(n_neurons, dims, rng=rng)
    E = get_encoders(n_neurons, dims, rng=rng)

    train = get_eval_points(n_points, dims, rng=rng)
    Atrain = rates(np.dot(train, E))

    D, _ = Solver(*args, **kwargs)(Atrain, train, rng=rng)

    test = get_eval_points(n_points, dims, rng=rng, sort=True)
    Atest = rates(np.dot(test, E))
    est = np.dot(Atest, D)
    rel_rmse = rms(est - test) / rms(test)

    plt.plot(test, np.zeros_like(test), 'k--')
    plt.plot(test, test - est)
    plt.title("relative RMSE: %0.2e" % rel_rmse)

    atol = 3.5e-2 if Solver is LstsqNoise else 1.5e-2
    assert np.allclose(test, est, atol=atol, rtol=1e-3)
    assert rel_rmse < 0.02
Exemple #11
0
def test_run(Simulator, algebra, seed):
    rng = np.random.RandomState(seed)
    vocab = spa.Vocabulary(16, pointer_gen=rng, algebra=algebra)
    vocab.populate("A; B")

    with spa.Network(seed=seed) as model:
        model.bind = spa.Bind(vocab)

        def inputA(t):
            if 0 <= t < 0.1:
                return "A"
            else:
                return "B"

        model.input = spa.Transcode(inputA, output_vocab=vocab)
        model.input >> model.bind.input_left
        spa.sym.A >> model.bind.input_right

    with model:
        p = nengo.Probe(model.bind.output, synapse=0.03)

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

    error = rms(vocab.parse("(B*A).normalized()").v - sim.data[p][-1])
    assert error < 0.15

    error = rms(vocab.parse("(A*A).normalized()").v - sim.data[p][100])
    assert error < 0.15
Exemple #12
0
def test_lif(Simulator):
    """Test that the dynamic model approximately matches the rates"""
    rng = np.random.RandomState(85243)

    dt = 0.001
    n = 5000
    x = 0.5
    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(
            nengo.LIF(n), 1, max_rates=max_rates, intercepts=intercepts)
        nengo.Connection(ins, ens.neurons, transform=np.ones((n, 1)))
        spike_probe = nengo.Probe(ens.neurons, "output")

    sim = Simulator(m, dt=dt)

    t_final = 1.0
    sim.run(t_final)
    spikes = sim.data[spike_probe].sum(0)

    math_rates = ens.neurons.rates(
        x, *ens.neurons.gain_bias(max_rates, intercepts))
    sim_rates = spikes / t_final
    logger.debug("ME = %f", (sim_rates - math_rates).mean())
    logger.debug("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)
def test_decoder_solver(Solver):
    rng = np.random.RandomState(39408)

    dims = 1
    n_neurons = 100
    n_points = 500

    rates = get_rate_function(n_neurons, dims, rng=rng)
    E = get_encoders(n_neurons, dims, rng=rng)

    train = get_eval_points(n_points, dims, rng=rng)
    Atrain = rates(np.dot(train, E))

    D, _ = Solver()(Atrain, train, rng=rng)

    test = get_eval_points(n_points, dims, rng=rng, sort=True)
    Atest = rates(np.dot(test, E))
    est = np.dot(Atest, D)
    rel_rmse = rms(est - test) / rms(test)

    with Plotter(nengo.Simulator) as plt:
        plt.plot(test, np.zeros_like(test), 'k--')
        plt.plot(test, test - est)
        plt.title("relative RMSE: %0.2e" % rel_rmse)
        plt.savefig('test_decoders.test_decoder_solver.%s.pdf'
                    % Solver.__name__)
        plt.close()

    assert np.allclose(test, est, atol=3e-2, rtol=1e-3)
    assert rel_rmse < 0.02
Exemple #14
0
def test_fastlif(plt):
    """Test that the dynamic model approximately matches the rates."""
    # Based nengo.tests.test_neurons.test_lif
    rng = np.random.RandomState(10)

    dt = 1e-3
    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=FastLIF(),
                             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 nengo.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
    print("ME = %f" % (sim_rates - math_rates).mean())
    print("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()
Exemple #15
0
def test_subsolvers(Solver, seed, rng, tol=1e-2):
    get_rng = lambda: np.random.RandomState(seed)

    A, b = get_system(500, 100, 5, rng=rng)
    x0, _ = Solver(solver=lstsq.Cholesky())(A, b, rng=get_rng())

    subsolvers = [lstsq.Conjgrad, lstsq.BlockConjgrad]
    for subsolver in subsolvers:
        x, info = Solver(solver=subsolver(tol=tol))(A, b, rng=get_rng())
        rel_rmse = rms(x - x0) / rms(x0)
        assert rel_rmse < 4 * tol
Exemple #16
0
def test_subsolvers(Solver, seed, rng, tol=1e-2):
    get_rng = lambda: np.random.RandomState(seed)

    A, b = get_system(500, 100, 5, rng=rng)
    x0, _ = Solver(solver=cholesky)(A, b, rng=get_rng())

    subsolvers = [conjgrad, block_conjgrad]
    for subsolver in subsolvers:
        x, info = Solver(solver=subsolver, tol=tol)(A, b, rng=get_rng())
        rel_rmse = rms(x - x0) / rms(x0)
        assert rel_rmse < 4 * tol
Exemple #17
0
def test_alif(Simulator):
    """Test ALIF and ALIFRate by comparing them to each other"""

    n = 100
    max_rates = 50 * np.ones(n)
    intercepts = np.linspace(-0.99, 0.99, n)
    encoders = np.ones((n, 1))
    nparams = dict(tau_n=1, inc_n=10e-3)
    eparams = dict(n_neurons=n,
                   max_rates=max_rates,
                   intercepts=intercepts,
                   encoders=encoders)

    model = nengo.Network()
    with model:
        u = nengo.Node(output=0.5)
        a = nengo.Ensemble(neuron_type=nengo.AdaptiveLIFRate(**nparams),
                           dimensions=1,
                           **eparams)
        b = nengo.Ensemble(neuron_type=nengo.AdaptiveLIF(**nparams),
                           dimensions=1,
                           **eparams)
        nengo.Connection(u, a, synapse=0)
        nengo.Connection(u, b, synapse=0)
        ap = nengo.Probe(a, "spikes", synapse=0)
        bp = nengo.Probe(b, "spikes", synapse=0)

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

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

    tmask = (t > 0.1) & (t < 1.7)
    rel_rmse = rms(b_rates[tmask] - a_rates[tmask]) / rms(a_rates[tmask])

    with Plotter(Simulator) as plt:
        ax = plt.subplot(311)
        implot(plt, t, intercepts[::-1], a_rates.T, ax=ax)
        ax.set_ylabel('input')
        ax = plt.subplot(312)
        implot(plt, t, intercepts[::-1], b_rates.T, ax=ax)
        ax.set_ylabel('input')
        ax = plt.subplot(313)
        implot(plt, t, intercepts[::-1], (b_rates - a_rates)[tmask].T, ax=ax)
        ax.set_xlabel('time [s]')
        ax.set_ylabel('input')
        plt.savefig('test_neurons.test_alif.pdf')
        plt.close()

    assert rel_rmse < 0.07
Exemple #18
0
def test_subsolvers(solver, tol=1e-2):
    rng = np.random.RandomState(89)
    get_rng = lambda: np.random.RandomState(87)

    A, b = get_system(500, 100, 5, rng=rng)
    x0, _ = solver(A, b, rng=get_rng(), solver=_cholesky)

    subsolvers = [_conjgrad, _block_conjgrad]
    for subsolver in subsolvers:
        x, info = solver(A, b, rng=get_rng(), solver=subsolver, tol=tol)
        rel_rmse = rms(x - x0) / rms(x0)
        assert rel_rmse < 3 * tol
Exemple #19
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
Exemple #20
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))

    model = nengo.Network()
    with model:
        model.config[nengo.Ensemble].max_rates = max_rates
        model.config[nengo.Ensemble].intercepts = intercepts
        model.config[nengo.Ensemble].encoders = encoders
        u = nengo.Node(output=whitenoise(1, 5, seed=8393))
        a = nengo.Ensemble(n, 1, neuron_type=nengo.LIFRate())
        b = nengo.Ensemble(n, 1, neuron_type=nengo.LIF())
        nengo.Connection(u, a, synapse=0)
        nengo.Connection(u, b, synapse=0)
        up = nengo.Probe(u)
        ap = nengo.Probe(a.neurons)
        bp = nengo.Probe(b.neurons)

    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
Exemple #21
0
def test_lif(Simulator):
    """Test that the dynamic model approximately matches the rates"""
    rng = np.random.RandomState(85243)

    dt = 0.001
    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)))
        spike_probe = nengo.Probe(ens.neurons)
        voltage_probe = nengo.Probe(ens.neurons, 'voltage')
        ref_probe = nengo.Probe(ens.neurons, 'refractory_time')

    sim = Simulator(m, dt=dt)

    t_final = 1.0
    sim.run(t_final)

    with Plotter(Simulator) as plt:
        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])
        plt.savefig('test_neurons.test_lif.pdf')
        plt.close()

    # check rates against analytic rates
    math_rates = ens.neuron_type.rates(
        x, *ens.neuron_type.gain_bias(max_rates, intercepts))
    sim_rates = sim.data[spike_probe].sum(0) / t_final
    logger.debug("ME = %f", (sim_rates - math_rates).mean())
    logger.debug("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
def test_alif(Simulator):
    """Test ALIF and ALIFRate by comparing them to each other"""

    n = 100
    max_rates = 50 * np.ones(n)
    intercepts = np.linspace(-0.99, 0.99, n)
    encoders = np.ones((n, 1))
    nparams = dict(tau_n=1, inc_n=10e-3)
    eparams = dict(n_neurons=n, max_rates=max_rates,
                   intercepts=intercepts, encoders=encoders)

    model = nengo.Network()
    with model:
        u = nengo.Node(output=0.5)
        a = nengo.Ensemble(neuron_type=nengo.AdaptiveLIFRate(**nparams),
                           dimensions=1,
                           **eparams)
        b = nengo.Ensemble(neuron_type=nengo.AdaptiveLIF(**nparams),
                           dimensions=1,
                           **eparams)
        nengo.Connection(u, a, synapse=0)
        nengo.Connection(u, b, synapse=0)
        ap = nengo.Probe(a, "spikes", synapse=0)
        bp = nengo.Probe(b, "spikes", synapse=0)

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

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

    tmask = (t > 0.1) & (t < 1.7)
    rel_rmse = rms(b_rates[tmask] - a_rates[tmask]) / rms(a_rates[tmask])

    with Plotter(Simulator) as plt:
        ax = plt.subplot(311)
        implot(plt, t, intercepts[::-1], a_rates.T, ax=ax)
        ax.set_ylabel('input')
        ax = plt.subplot(312)
        implot(plt, t, intercepts[::-1], b_rates.T, ax=ax)
        ax.set_ylabel('input')
        ax = plt.subplot(313)
        implot(plt, t, intercepts[::-1], (b_rates - a_rates)[tmask].T, ax=ax)
        ax.set_xlabel('time [s]')
        ax.set_ylabel('input')
        plt.savefig('test_neurons.test_alif.pdf')
        plt.close()

    assert rel_rmse < 0.07
Exemple #23
0
def test_lif(Simulator, plt, rng, logger):
    """Test that the dynamic model approximately matches the rates"""
    dt = 0.001
    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)))
        spike_probe = nengo.Probe(ens.neurons)
        voltage_probe = nengo.Probe(ens.neurons, 'voltage')
        ref_probe = nengo.Probe(ens.neurons, 'refractory_time')

    sim = Simulator(m, dt=dt)

    t_final = 1.0
    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
Exemple #24
0
def test_alif(Simulator, plt):
    """Test ALIF and ALIFRate by comparing them to each other"""

    n = 100
    max_rates = 50 * np.ones(n)
    intercepts = np.linspace(-0.99, 0.99, n)
    encoders = np.ones((n, 1))
    nparams = dict(tau_n=1, inc_n=10e-3)
    eparams = dict(n_neurons=n,
                   max_rates=max_rates,
                   intercepts=intercepts,
                   encoders=encoders)

    model = nengo.Network()
    with model:
        u = nengo.Node(output=0.5)
        a = nengo.Ensemble(neuron_type=AdaptiveLIFRate(**nparams),
                           dimensions=1,
                           **eparams)
        b = nengo.Ensemble(neuron_type=AdaptiveLIF(**nparams),
                           dimensions=1,
                           **eparams)
        nengo.Connection(u, a, synapse=0)
        nengo.Connection(u, b, synapse=0)
        ap = nengo.Probe(a.neurons)
        bp = nengo.Probe(b.neurons)

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

    t = sim.trange()
    a_rates = sim.data[ap]
    spikes = sim.data[bp]
    b_rates = nengo.Lowpass(0.04).filtfilt(spikes)

    tmask = (t > 0.1) & (t < 1.7)
    rel_rmse = rms(b_rates[tmask] - a_rates[tmask]) / rms(a_rates[tmask])

    ax = plt.subplot(311)
    implot(plt, t, intercepts[::-1], a_rates.T, ax=ax)
    ax.set_ylabel("input")
    ax = plt.subplot(312)
    implot(plt, t, intercepts[::-1], b_rates.T, ax=ax)
    ax.set_ylabel("input")
    ax = plt.subplot(313)
    implot(plt, t, intercepts[::-1], (b_rates - a_rates)[tmask].T, ax=ax)
    ax.set_xlabel("time [s]")
    ax.set_ylabel("input")

    assert rel_rmse < 0.07
Exemple #25
0
def _test_rates(Simulator, rates, plt, seed, name=None):
    if name is None:
        name = rates.__name__

    n = 100
    intercepts = np.linspace(-0.99, 0.99, n)

    model = nengo.Network(seed=seed)
    with model:
        model.config[nengo.Ensemble].max_rates = Choice([50])
        model.config[nengo.Ensemble].encoders = Choice([[1]])
        u = nengo.Node(output=WhiteNoise(2., 5).f(
            rng=np.random.RandomState(seed=seed)))
        a = nengo.Ensemble(n, 1,
                           intercepts=intercepts, neuron_type=nengo.LIFRate())
        b = nengo.Ensemble(n, 1,
                           intercepts=intercepts, neuron_type=nengo.LIF())
        nengo.Connection(u, a, synapse=0)
        nengo.Connection(u, b, synapse=0)
        up = nengo.Probe(u)
        ap = nengo.Probe(a.neurons)
        bp = nengo.Probe(b.neurons)

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

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

    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.saveas = 'utils.test_neurons.test_rates.%s.pdf' % name

    tmask = (t > 0.1) & (t < 1.9)
    relative_rmse = rms(b_rates[tmask] - a_rates[tmask]) / rms(a_rates[tmask])
    return relative_rmse
def _test_rates(Simulator, rates, plt, seed):
    n = 100
    intercepts = np.linspace(-0.99, 0.99, n)

    model = nengo.Network(seed=seed)
    with model:
        model.config[nengo.Ensemble].max_rates = nengo.dists.Choice([50])
        model.config[nengo.Ensemble].encoders = nengo.dists.Choice([[1]])
        u = nengo.Node(output=nengo.processes.WhiteSignal(2, high=5))
        a = nengo.Ensemble(n,
                           1,
                           intercepts=intercepts,
                           neuron_type=nengo.LIFRate())
        b = nengo.Ensemble(n,
                           1,
                           intercepts=intercepts,
                           neuron_type=nengo.LIF())
        nengo.Connection(u, a, synapse=0)
        nengo.Connection(u, b, synapse=0)
        up = nengo.Probe(u)
        ap = nengo.Probe(a.neurons)
        bp = nengo.Probe(b.neurons)

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

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

    if plt is not None:
        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')

    tmask = (t > 0.1) & (t < 1.9)
    relative_rmse = rms(b_rates[tmask] - a_rates[tmask]) / rms(a_rates[tmask])
    return relative_rmse
Exemple #27
0
def test_amplitude(Simulator, seed, rng, plt, allclose, Neuron):
    amp = 0.1
    neuron0 = Neuron()
    neuron = Neuron(amplitude=amp)

    # check static
    x = np.linspace(-5, 30)
    y = amp * neuron0.rates(x, 1.0, 0.0)
    y2 = neuron.rates(x, 1.0, 0.0)
    assert 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).squeeze(axis=0)

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

    error = rms(r - r0) / rms(r0)
    assert (error < 0.02).all()
Exemple #28
0
def test_alif(Simulator, plt):
    """Test ALIF and ALIFRate by comparing them to each other"""

    n = 100
    max_rates = 50 * np.ones(n)
    intercepts = np.linspace(-0.99, 0.99, n)
    encoders = np.ones((n, 1))
    nparams = dict(tau_n=1, inc_n=10e-3)
    eparams = dict(n_neurons=n, max_rates=max_rates,
                   intercepts=intercepts, encoders=encoders)

    model = nengo.Network()
    with model:
        u = nengo.Node(output=0.5)
        a = nengo.Ensemble(neuron_type=AdaptiveLIFRate(**nparams),
                           dimensions=1,
                           **eparams)
        b = nengo.Ensemble(neuron_type=AdaptiveLIF(**nparams),
                           dimensions=1,
                           **eparams)
        nengo.Connection(u, a, synapse=0)
        nengo.Connection(u, b, synapse=0)
        ap = nengo.Probe(a.neurons)
        bp = nengo.Probe(b.neurons)

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

    t = sim.trange()
    a_rates = sim.data[ap]
    spikes = sim.data[bp]
    b_rates = nengo.Lowpass(0.04).filtfilt(spikes)

    tmask = (t > 0.1) & (t < 1.7)
    rel_rmse = rms(b_rates[tmask] - a_rates[tmask]) / rms(a_rates[tmask])

    ax = plt.subplot(311)
    implot(plt, t, intercepts[::-1], a_rates.T, ax=ax)
    ax.set_ylabel('input')
    ax = plt.subplot(312)
    implot(plt, t, intercepts[::-1], b_rates.T, ax=ax)
    ax.set_ylabel('input')
    ax = plt.subplot(313)
    implot(plt, t, intercepts[::-1], (b_rates - a_rates)[tmask].T, ax=ax)
    ax.set_xlabel('time [s]')
    ax.set_ylabel('input')

    assert rel_rmse < 0.07
Exemple #29
0
def solve_HingeLoss(solver, queue, clA, Y, rng=None, E=None):
    import scipy.optimize
    import pyopencl_blas
    pyopencl_blas.setup()

    tstart = time.time()

    assert clA.shape[0] == Y.shape[0]
    m, n = clA.shape
    _, d = Y.shape
    Xshape = (n, d)

    # regularization
    sigma = solver.reg * cl.array.max(clA).get()
    lamb = m * sigma**2

    # --- initialization
    X0 = rng.uniform(-1. / n, 1. / n, size=Xshape)

    # --- solve with L-BFGS
    yinds = Y.argmax(axis=1)

    clX = cl.array.Array(queue, (n, d), dtype=np.float32)
    clyinds = cl.array.to_device(queue, yinds.astype(np.int32))
    clZ = cl.array.Array(queue, (m, d), dtype=np.float32)
    clc = cl.array.Array(queue, (m, ), dtype=np.float32)
    clE = cl.array.Array(queue, (m, d), dtype=np.float32)
    clG = cl.array.Array(queue, (n, d), dtype=np.float32)

    hingeloss_plan = plan_hingeloss(queue, clyinds, clZ, clc, clE)

    def f_df(x):
        clX.set(x.astype(np.float32).reshape(Xshape))
        pyopencl_blas.gemm(queue, clA, clX, clZ)
        hingeloss_plan()

        cost = pyopencl.array.sum(clc).get()
        pyopencl_blas.gemm(queue, clA, clE, clG, transA=True)
        if lamb > 0:
            cost += 0.5 * lamb * pyopencl.array.sum(clX**2).get()
            # cost += 0.5 * lamb * sum_square(clX).get()
            clG[:] += lamb * clX

        G = clG.get().astype(np.float64)
        return cost, G.ravel()

    x0 = X0.ravel()
    x, mincost, info = scipy.optimize.fmin_l_bfgs_b(f_df,
                                                    x0,
                                                    maxfun=solver.n_epochs,
                                                    iprint=solver.verbose)

    tend = time.time()

    A = clA.get()
    X = x.reshape(Xshape)
    return solver.mul_encoders(X, E), {
        'rmses': npext.rms(np.dot(A, X) - Y, axis=1),
        'time': tend - tstart
    }
Exemple #30
0
def objective(args):
    sargs = arg_string(args)
    w_kind = args['w_kind']
    w_scale = args['w_scale']
    eta = args['eta']
    # eta = [args['eta0'], args['eta1'], args['eta2']]

    max_cost = -np.inf
    for _ in range(5):
        f, df = static_f_df(tau_rc=0.05, **args['neuron_type'])

        weights = initial_weights(sizes, kind=w_kind, scale=w_scale, rng=rng)
        # weights = initial_weights(sizes, kind='ortho', rng=rng)
        # lsuv(X, weights, f, target_input=True, target_std=1, verbose=1)
        # lsuv(X[:100], weights, f, target_input=True, target_std=1, verbose=1)

        # --- learners
        network = Network(weights, f=f, df=df, biases=None)
        # network = Network(weights, f=f, df=df, biases=None, noise=1.)
        learner = Learner(network,
                          squared_cost,
                          rms_error,
                          eta=eta,
                          alpha=alpha,
                          momentum=momentum)
        learner.train(1, batch_fn, verbose=0)

        y = learner.network.predict(Xvalid)
        mean_cost = rms(y - Yvalid, axis=1).mean()
        max_cost = max(max_cost, mean_cost)

    print("%s: %0.3e" % (sargs, max_cost))

    return max_cost
Exemple #31
0
def lstsq_L1(A, Y, rng=np.random, E=None, l1=1e-4, l2=1e-6):
    """Least-squares with L1 and L2 regularization (elastic net).

    This method is well suited for creating sparse decoders or weight matrices.
    """
    import sklearn.linear_model

    # TODO: play around with these regularization constants (I just guessed).
    #   Do we need to scale regularization by number of neurons, to get same
    #   level of sparsity? esp. with weights? Currently, setting l1=1e-3 works
    #   well with weights when connecting 1D populations with 100 neurons each.
    a = l1 * A.max()      # L1 regularization
    b = l2 * A.max()**2   # L2 regularization
    alpha = a + b
    l1_ratio = a / (a + b)

    # --- solve least-squares A * X = Y
    if E is not None:
        Y = np.dot(Y, E)

    model = sklearn.linear_model.ElasticNet(
        alpha=alpha, l1_ratio=l1_ratio, fit_intercept=False, max_iter=1000)
    model.fit(A, Y)
    X = model.coef_.T
    X.shape = (A.shape[1], Y.shape[1]) if Y.ndim > 1 else (A.shape[1],)
    infos = {'rmses': npext.rms(Y - np.dot(A, X), axis=0)}
    return X, infos
Exemple #32
0
    def run_trial():
        model = nengo.Network(seed=rng.randint(maxint))
        with model:
            model.config[nengo.Ensemble].n_eval_points = n_eval_points

            stimulus = nengo.Node(
                output=lambda t: stimulus_fn(max(0.0, t - wait_duration)),
                size_out=2)

            product_net = nengo.networks.Product(n_neurons, 1)
            nengo.Connection(stimulus[0], product_net.input_a)
            nengo.Connection(stimulus[1], product_net.input_b)
            probe_test = nengo.Probe(product_net.output)

            ens_direct = nengo.Ensemble(1,
                                        dimensions=2,
                                        neuron_type=nengo.Direct())
            result_direct = nengo.Node(size_in=1)
            nengo.Connection(stimulus, ens_direct)
            nengo.Connection(ens_direct,
                             result_direct,
                             function=lambda x: x[0] * x[1],
                             synapse=None)
            probe_direct = nengo.Probe(result_direct)

        with Simulator(model) as sim:
            sim.run(duration + wait_duration, progress_bar=False)

        selection = sim.trange() > wait_duration
        test = sim.data[probe_test][selection]
        direct = sim.data[probe_direct][selection]
        return rms(test - direct)
 def __call__(self, A, Y, sigma, rng=None):
     Y, m, _, _, matrix_in = format_system(A, Y)
     U, s, V = np.linalg.svd(A, full_matrices=0)
     si = s / (s**2 + m * sigma**2)
     X = np.dot(V.T, si[:, None] * np.dot(U.T, Y))
     info = {'rmses': npext.rms(Y - np.dot(A, X), axis=0)}
     return X if matrix_in else X.flatten(), info
Exemple #34
0
    def make_step(self, size_in, size_out, dt, rng):
        assert size_in[0] == 0
        assert size_out[0] == 1

        rate = 1. / dt

        orig_rate, orig = readwav(self.path)
        new_size = int(orig.size * (rate / orig_rate))
        wave = resample(orig, new_size)
        wave -= wave.mean()

        # Normalize wave to desired rms
        wave_rms = npext.rms(wave)
        wave *= (self.rms / wave_rms)

        if self.at_end == 'loop':

            def step_wavfileloop(t):
                idx = int(t * rate) % wave.size
                return wave[idx]

            return step_wavfileloop

        elif self.at_end == 'stop':

            def step_wavfilestop(t):
                idx = int(t * rate)
                if idx > wave.size:
                    return 0.
                else:
                    return wave[idx]

            return step_wavfilestop
Exemple #35
0
def test_eval_point_decoding(Simulator, nl_nodirect, plt, seed):
    with nengo.Network(seed=seed) as model:
        model.config[nengo.Ensemble].neuron_type = nl_nodirect()
        a = nengo.Ensemble(200, 2)
        b = nengo.Ensemble(100, 1)
        c = nengo.Connection(a, b, function=lambda x: x[0] * x[1])

    sim = Simulator(model)
    eval_points, targets, decoded = eval_point_decoding(c, sim)

    def contour(xy, z):
        xi = np.linspace(-1, 1, 101)
        yi = np.linspace(-1, 1, 101)
        zi = griddata(xy[:, 0], xy[:, 1], z.ravel(), xi, yi, interp="linear")
        plt.contourf(xi, yi, zi, cmap=plt.cm.seismic)
        plt.colorbar()

    plt.figure(figsize=(15, 5))
    plt.subplot(131)
    contour(eval_points, targets)
    plt.title("Target (desired decoding)")
    plt.subplot(132)
    plt.title("Actual decoding")
    contour(eval_points, decoded)
    plt.subplot(133)
    plt.title("Difference between actual and desired")
    contour(eval_points, decoded - targets)

    # Generous error check, just to make sure it's in the right ballpark.
    # Also make sure error is above zero, i.e. y != z
    error = rms(decoded - targets, axis=1).mean()
    assert error < 0.1 and error > 1e-8
Exemple #36
0
def randomized_svd(A, Y, sigma, rng=np.random,
                   n_components=60, n_oversamples=10, **kwargs):
    """Solve the least-squares system using a randomized (partial) SVD.

    Parameters
    ----------
    n_components : int (default is 50)
        The number of SVD components to compute. A small survey of activity
        matrices suggests that the first 50 components capture almost all
        the variance.
    n_oversamples: int (default is 10)
        The number of additional samples on the range of A.
    n_iter : int (default is 0)
        The number of power iterations to perform (can help with noisy data).

    See also
    --------
    ``sklearn.utils.extmath.randomized_svd`` for details about the parameters.
    """
    from sklearn.utils.extmath import randomized_svd as sklearn_randomized_svd

    Y, m, n, _, matrix_in = _format_system(A, Y)
    if min(m, n) <= n_components + n_oversamples:
        # more efficient to do a full SVD
        return svd(A, Y, sigma, rng=rng)

    U, s, V = sklearn_randomized_svd(
        A, n_components, random_state=rng, **kwargs)
    si = s / (s**2 + m * sigma**2)
    X = np.dot(V.T, si[:, None] * np.dot(U.T, Y))
    info = {'rmses': npext.rms(Y - np.dot(A, X), axis=0)}
    return X if matrix_in else X.flatten(), info
Exemple #37
0
def test_eval_points(Simulator, nl_nodirect, plt, seed, rng):
    n = 100
    d = 5
    filter = 0.08

    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(seed=seed)
            with model:
                model.config[nengo.Ensemble].neuron_type = nl_nodirect()
                u = nengo.Node(output=x)
                a = nengo.Ensemble(n * d,
                                   dimensions=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)
            sim.run(10 * filter)

            t = sim.trange()
            xt = nengo.synapses.filtfilt(sim.data[up], filter, dt=sim.dt)
            yt = nengo.synapses.filtfilt(sim.data[ap], filter, dt=sim.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)

    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)
Exemple #38
0
def conjgrad_scipy(A, Y, sigma, tol=1e-4):
    """Solve the least-squares system using Scipy's conjugate gradient."""
    import scipy.sparse.linalg
    Y, m, n, d, matrix_in = _format_system(A, Y)

    damp = m * sigma**2
    calcAA = lambda x: np.dot(A.T, np.dot(A, x)) + damp * x
    G = scipy.sparse.linalg.LinearOperator(
        (n, n), matvec=calcAA, matmat=calcAA, dtype=A.dtype)
    B = np.dot(A.T, Y)

    X = np.zeros((n, d), dtype=B.dtype)
    infos = np.zeros(d, dtype='int')
    itns = np.zeros(d, dtype='int')
    for i in range(d):
        def callback(x):
            itns[i] += 1  # use the callback to count the number of iterations

        X[:, i], infos[i] = scipy.sparse.linalg.cg(
            G, B[:, i], tol=tol, callback=callback)

    info = {'rmses': npext.rms(Y - np.dot(A, X), axis=0),
            'iterations': itns,
            'info': infos}
    return X if matrix_in else X.flatten(), info
Exemple #39
0
def test_regularization(Simulator, nl_nodirect, plt):

    # TODO: multiple trials per parameter set, with different seeds

    Solvers = [LstsqL2, LstsqL2nz]
    neurons = np.array([10, 20, 50, 100])
    regs = np.linspace(0.01, 0.3, 16)
    filters = np.linspace(0, 0.03, 11)

    buf = 0.2  # buffer for initial transients
    dt = 1e-3
    tfinal = 3 + buf

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

    model = nengo.Network('test_regularization')
    with model:
        model.config[nengo.Ensemble].neuron_type = nl_nodirect()
        u = nengo.Node(output=input_function)
        up = nengo.Probe(u)

        probes = np.zeros(
            (len(Solvers), len(neurons), len(regs), len(filters)),
            dtype='object')

        for j, n_neurons in enumerate(neurons):
            a = nengo.Ensemble(n_neurons, dimensions=1)
            nengo.Connection(u, a)

            for i, Solver in enumerate(Solvers):
                for k, reg in enumerate(regs):
                    for l, synapse in enumerate(filters):
                        probes[i, j, k, l] = nengo.Probe(
                            a, solver=Solver(reg=reg), synapse=synapse)

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

    ref = sim.data[up]
    rmse_buf = lambda a, b: rms(a[t > buf] - b[t > buf])
    rmses = np.zeros(probes.shape)
    for i, probe in enumerate(probes.flat):
        rmses.flat[i] = rmse_buf(sim.data[probe], ref)
    rmses = rmses - rmses[:, :, [0], :]

    plt.figure(figsize=(8, 12))
    X, Y = np.meshgrid(filters, regs)

    for i, Solver in enumerate(Solvers):
        for j, n_neurons in enumerate(neurons):
            plt.subplot(len(neurons), len(Solvers), len(Solvers)*j + i + 1)
            Z = rmses[i, j, :, :]
            plt.contourf(X, Y, Z, levels=np.linspace(Z.min(), Z.max(), 21))
            plt.xlabel('filter')
            plt.ylabel('reg')
            plt.title("%s (N=%d)" % (Solver.__name__, n_neurons))

    plt.tight_layout()
Exemple #40
0
def test_sine_waves(Simulator, plt, seed):
    radius = 2
    dim = 5
    product = nengo.networks.Product(200, dim, radius, seed=seed)

    func_a = lambda t: np.sqrt(radius) * np.sin(
        np.arange(1, dim + 1) * 2 * np.pi * t)
    func_b = lambda t: np.sqrt(radius) * np.sin(
        np.arange(dim, 0, -1) * 2 * np.pi * t)
    with product:
        input_a = nengo.Node(func_a)
        input_b = nengo.Node(func_b)
        nengo.Connection(input_a, product.input_a)
        nengo.Connection(input_b, product.input_b)
        p = nengo.Probe(product.output, synapse=0.005)

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

    t = sim.trange()
    ideal = np.asarray([func_a(tt)
                        for tt in t]) * np.asarray([func_b(tt) for tt in t])
    delay = 0.013
    offset = np.where(t >= delay)[0]

    for i in range(dim):
        plt.subplot(dim + 1, 1, i + 1)
        plt.plot(t + delay, ideal[:, i])
        plt.plot(t, sim.data[p][:, i])
        plt.xlim(right=t[-1])
        plt.yticks((-2, 0, 2))

    assert rms(ideal[:len(offset), :] - sim.data[p][offset, :]) < 0.2
Exemple #41
0
def lstsq_drop(A, Y, rng, E=None, noise_amp=0.1, drop=0.25, solver=lstsq_L2nz):
    """Find sparser decoders/weights by dropping small values.

    This solver first solves for coefficients (decoders/weights) with
    L2 regularization, drops those nearest to zero, and retrains remaining.
    """
    Y, m, n, d, matrix_in = _format_system(A, Y)

    # solve for coefficients using standard solver
    X, info0 = solver(A, Y, rng=rng, noise_amp=noise_amp)
    X = np.dot(X, E) if E is not None else X

    # drop weights close to zero, based on `drop` ratio
    Xabs = np.sort(np.abs(X.flat))
    threshold = Xabs[int(np.round(drop * Xabs.size))]
    X[np.abs(X) < threshold] = 0

    # retrain nonzero weights
    Y = np.dot(Y, E) if E is not None else Y
    for i in range(X.shape[1]):
        nonzero = X[:, i] != 0
        if nonzero.sum() > 0:
            X[nonzero, i], info1 = solver(
                A[:, nonzero], Y[:, i], rng=rng, noise_amp=0.1 * noise_amp)

    info = {'rmses': npext.rms(Y - np.dot(A, X), axis=0),
            'info0': info0, 'info1': info1}
    return X if matrix_in else X.flatten(), info
Exemple #42
0
def test_high_dimensional_integrator(plt):
    d = 32
    dt = 1e-3

    def f(t, hz):
        return np.cos(2 * np.pi * hz * (t - dt)) * (2 * np.pi * hz)

    input_functions = [
        lambda t, hz=hz: f(t, hz) for hz in np.linspace(1, 2, d)
    ]
    u = stimuli(input_functions)
    x = u.integrate()

    y = np.asarray(x.run(1, dt=dt))
    y_hat = np.asarray(x.decode().filter(5e-3).run(1, dt=dt))
    assert y.shape == y_hat.shape

    t = (1 + np.arange(y.shape[1])) * dt
    y_check = np.cumsum(np.asarray([f(t)
                                    for f in input_functions]), axis=1) * dt
    assert np.allclose(y.squeeze(axis=2)[:, 1:], y_check[:, :-1])
    assert rms(y - y_hat) < 0.1

    for y_i, y_hat_i in zip(y, y_hat):
        plt.plot(y_i.squeeze(axis=1), linestyle="--")
        plt.plot(y_hat_i.squeeze(axis=1), alpha=0.7)
    plt.xlabel("Time-step")
Exemple #43
0
    def make_step(self, size_in, size_out, dt, rng):
        assert size_in[0] == 0
        assert size_out[0] == 1

        rate = 1. / dt

        orig_rate, orig = readwav(self.path)
        new_size = int(orig.size * (rate / orig_rate))
        wave = resample(orig, new_size)
        wave -= wave.mean()

        # Normalize wave to desired rms
        wave_rms = npext.rms(wave)
        wave *= (self.rms / wave_rms)

        if self.at_end == 'loop':

            def step_wavfileloop(t):
                idx = int(t * rate) % wave.size
                return wave[idx]
            return step_wavfileloop

        elif self.at_end == 'stop':

            def step_wavfilestop(t):
                idx = int(t * rate)
                if idx > wave.size:
                    return 0.
                else:
                    return wave[idx]
            return step_wavfilestop
Exemple #44
0
def cholesky(A, y, sigma, transpose=None):
    """Solve the least-squares system using the Cholesky decomposition."""
    m, n = A.shape
    if transpose is None:
        # transpose if matrix is fat, but not if we have sigmas for each neuron
        transpose = m < n and sigma.size == 1

    if transpose:
        # substitution: x = A'*xbar, G*xbar = b where G = A*A' + lambda*I
        G = np.dot(A, A.T)
        b = y
    else:
        # multiplication by A': G*x = A'*b where G = A'*A + lambda*I
        G = np.dot(A.T, A)
        b = np.dot(A.T, y)

    # add L2 regularization term 'lambda' = m * sigma**2
    np.fill_diagonal(G, G.diagonal() + m * sigma**2)

    try:
        import scipy.linalg
        factor = scipy.linalg.cho_factor(G, overwrite_a=True)
        x = scipy.linalg.cho_solve(factor, b)
    except ImportError:
        L = np.linalg.cholesky(G)
        L = np.linalg.inv(L.T)
        x = np.dot(L, np.dot(L.T, b))

    x = np.dot(A.T, x) if transpose else x
    info = {'rmses': npext.rms(y - np.dot(A, x), axis=0)}
    return x, info
Exemple #45
0
 def __call__(self, A, Y, rng=None, E=None):
     Y = self.mul_encoders(Y, E)
     X, residuals2, rank, s = np.linalg.lstsq(A, Y, rcond=self.rcond)
     return X, {'rmses': npext.rms(Y - np.dot(A, X), axis=0),
                'residuals': np.sqrt(residuals2),
                'rank': rank,
                'singular_values': s}
Exemple #46
0
def cholesky(A, y, sigma, transpose=None):
    """Solve the least-squares system using the Cholesky decomposition."""
    m, n = A.shape
    if transpose is None:
        # transpose if matrix is fat, but not if we have sigmas for each neuron
        transpose = m < n and sigma.size == 1

    if transpose:
        # substitution: x = A'*xbar, G*xbar = b where G = A*A' + lambda*I
        G = np.dot(A, A.T)
        b = y
    else:
        # multiplication by A': G*x = A'*b where G = A'*A + lambda*I
        G = np.dot(A.T, A)
        b = np.dot(A.T, y)

    # add L2 regularization term 'lambda' = m * sigma**2
    np.fill_diagonal(G, G.diagonal() + m * sigma**2)

    try:
        import scipy.linalg
        factor = scipy.linalg.cho_factor(G, overwrite_a=True)
        x = scipy.linalg.cho_solve(factor, b)
    except ImportError:
        L = np.linalg.cholesky(G)
        L = np.linalg.inv(L.T)
        x = np.dot(L, np.dot(L.T, b))

    x = np.dot(A.T, x) if transpose else x
    info = {'rmses': npext.rms(y - np.dot(A, x), axis=0)}
    return x, info
Exemple #47
0
    def __call__(self, A, Y, rng=None, E=None):
        Y, m, n, d, matrix_in = _format_system(A, Y)

        # solve for coefficients using standard solver
        X, info0 = self.solver1(A, Y, rng=rng)
        X = self.mul_encoders(X, E)

        # drop weights close to zero, based on `drop` ratio
        Xabs = np.sort(np.abs(X.flat))
        threshold = Xabs[int(np.round(self.drop * Xabs.size))]
        X[np.abs(X) < threshold] = 0

        # retrain nonzero weights
        Y = self.mul_encoders(Y, E)
        for i in range(X.shape[1]):
            nonzero = X[:, i] != 0
            if nonzero.sum() > 0:
                X[nonzero, i], info1 = self.solver2(A[:, nonzero],
                                                    Y[:, i],
                                                    rng=rng)

        info = {
            'rmses': npext.rms(Y - np.dot(A, X), axis=0),
            'info0': info0,
            'info1': info1
        }
        return X if matrix_in else X.flatten(), info
Exemple #48
0
def conjgrad_scipy(A, Y, sigma, tol=1e-4):
    """Solve the least-squares system using Scipy's conjugate gradient."""
    import scipy.sparse.linalg
    Y, m, n, d, matrix_in = _format_system(A, Y)

    damp = m * sigma**2
    calcAA = lambda x: np.dot(A.T, np.dot(A, x)) + damp * x
    G = scipy.sparse.linalg.LinearOperator((n, n),
                                           matvec=calcAA,
                                           matmat=calcAA,
                                           dtype=A.dtype)
    B = np.dot(A.T, Y)

    X = np.zeros((n, d), dtype=B.dtype)
    infos = np.zeros(d, dtype='int')
    itns = np.zeros(d, dtype='int')
    for i in range(d):

        def callback(x):
            itns[i] += 1  # use the callback to count the number of iterations

        X[:, i], infos[i] = scipy.sparse.linalg.cg(G,
                                                   B[:, i],
                                                   tol=tol,
                                                   callback=callback)

    info = {
        'rmses': npext.rms(Y - np.dot(A, X), axis=0),
        'iterations': itns,
        'info': infos
    }
    return X if matrix_in else X.flatten(), info
Exemple #49
0
 def __call__(self, A, Y, sigma, rng=None):
     Y, m, _, _, matrix_in = format_system(A, Y)
     U, s, V = np.linalg.svd(A, full_matrices=0)
     si = s / (s**2 + m * sigma**2)
     X = np.dot(V.T, si[:, None] * np.dot(U.T, Y))
     info = {'rmses': npext.rms(Y - np.dot(A, X), axis=0)}
     return X if matrix_in else X.ravel(), info
Exemple #50
0
def lstsq(A, Y, rng=np.random, E=None, rcond=0.01):
    """Unregularized least-squares."""
    Y = np.dot(Y, E) if E is not None else Y
    X, residuals2, rank, s = np.linalg.lstsq(A, Y, rcond=rcond)
    return X, {'rmses': npext.rms(Y - np.dot(A, X), axis=0),
               'residuals': np.sqrt(residuals2),
               'rank': rank,
               'singular_values': s}
Exemple #51
0
def svd(A, Y, sigma, rng=None):
    """Solve the least-squares system using a full SVD."""
    Y, m, _, _, matrix_in = _format_system(A, Y)
    U, s, V = np.linalg.svd(A, full_matrices=0)
    si = s / (s**2 + m * sigma**2)
    X = np.dot(V.T, si[:, None] * np.dot(U.T, Y))
    info = {'rmses': npext.rms(Y - np.dot(A, X), axis=0)}
    return X if matrix_in else X.flatten(), info
Exemple #52
0
def _test_rates(Simulator, rates, plt, seed):
    n = 100
    intercepts = np.linspace(-0.99, 0.99, n)

    model = nengo.Network(seed=seed)
    with model:
        model.config[nengo.Ensemble].max_rates = Choice([50])
        model.config[nengo.Ensemble].encoders = Choice([[1]])
        u = nengo.Node(output=WhiteSignal(2, high=5))
        a = nengo.Ensemble(n, 1,
                           intercepts=intercepts, neuron_type=nengo.LIFRate())
        b = nengo.Ensemble(n, 1,
                           intercepts=intercepts, neuron_type=nengo.LIF())
        nengo.Connection(u, a, synapse=0)
        nengo.Connection(u, b, synapse=0)
        up = nengo.Probe(u)
        ap = nengo.Probe(a.neurons)
        bp = nengo.Probe(b.neurons)

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

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

    if plt is not None:
        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')

    tmask = (t > 0.1) & (t < 1.9)
    relative_rmse = rms(b_rates[tmask] - a_rates[tmask]) / rms(a_rates[tmask])
    return relative_rmse
Exemple #53
0
def test_amplitude(Simulator, seed, rng, plt, Neuron):
    amp = 0.1
    neuron0 = Neuron()
    neuron = Neuron(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).squeeze(axis=0)

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

    error = rms(r - r0) / rms(r0)
    assert (error < 0.02).all()
Exemple #54
0
def test_nnls(Solver, plt):
    rng = np.random.RandomState(39408)
    A, x = get_system(500, 100, 1, rng=rng, sort=True)
    y = x**2

    d, _ = Solver()(A, y, rng)
    yest = np.dot(A, d)
    rel_rmse = rms(yest - y) / rms(y)

    plt.subplot(211)
    plt.plot(x, y, 'k--')
    plt.plot(x, yest)
    plt.ylim([-0.1, 1.1])
    plt.subplot(212)
    plt.plot(x, np.zeros_like(x), 'k--')
    plt.plot(x, yest - y)
    plt.saveas = 'test_solvers.test_nnls.%s.pdf' % Solver.__name__

    assert np.allclose(yest, y, atol=3e-2, rtol=1e-3)
    assert rel_rmse < 0.02
Exemple #55
0
def test_nnls(Solver, plt, rng):
    pytest.importorskip('scipy')

    A, x = get_system(500, 100, 1, rng=rng, sort=True)
    y = x**2

    d, _ = Solver()(A, y, rng)
    yest = np.dot(A, d)
    rel_rmse = rms(yest - y) / rms(y)

    plt.subplot(211)
    plt.plot(x, y, 'k--')
    plt.plot(x, yest)
    plt.ylim([-0.1, 1.1])
    plt.subplot(212)
    plt.plot(x, np.zeros_like(x), 'k--')
    plt.plot(x, yest - y)

    assert np.allclose(yest, y, atol=3e-2, rtol=1e-3)
    assert rel_rmse < 0.02
Exemple #56
0
 def _get_samples(self, path, labels, rms=0.5):
     data, fs = sf.read("%s.WAV" % self.path)
     assert fs == TIMIT.fs, "fs (%s) != 16000" % fs
     # Normalize data to desired rms (or do this per sample?)
     data_rms = npext.rms(data)
     data *= (rms / data_rms)
     info = self._parse_text(path)
     samples = {lbl: [] for lbl in labels}
     for inf in info:
         if inf['lbl'] in samples:
             samples[inf['lbl']].append(data[inf['start']: inf['end']])
     return samples
Exemple #57
0
 def __call__(self, A, Y, rng=None, E=None):
     Y = self.mul_encoders(Y, E)
     X, residuals2, rank, s = np.linalg.lstsq(A, Y, rcond=self.rcond)
     return (
         X,
         {
             "rmses": npext.rms(Y - np.dot(A, X), axis=0),
             "residuals": np.sqrt(residuals2),
             "rank": rank,
             "singular_values": s,
         },
     )
Exemple #58
0
    def __call__(self, A, Y, rng=None, E=None):
        import scipy.optimize

        Y, m, n, d, matrix_in = _format_system(A, Y)
        Y = self.mul_encoders(Y, E)

        X = np.zeros((n, d))
        residuals = np.zeros(d)
        for i in range(d):
            X[:, i], residuals[i] = scipy.optimize.nnls(A, Y[:, i])

        info = {"rmses": npext.rms(Y - np.dot(A, X), axis=0), "residuals": residuals}
        return X if matrix_in else X.flatten(), info
Exemple #59
0
def test_nnls(solver):
    rng = np.random.RandomState(39408)
    A, x = get_system(500, 100, 1, rng=rng, sort=True)
    y = x**2

    d, _ = solver(A, y, rng)
    yest = np.dot(A, d)
    rel_rmse = rms(yest - y) / rms(y)

    with Plotter(nengo.Simulator) as plt:
        plt.subplot(211)
        plt.plot(x, y, 'k--')
        plt.plot(x, yest)
        plt.ylim([-0.1, 1.1])
        plt.subplot(212)
        plt.plot(x, np.zeros_like(x), 'k--')
        plt.plot(x, yest - y)
        plt.savefig('test_decoders.test_nnls.%s.pdf' % solver.__name__)
        plt.close()

    assert np.allclose(yest, y, atol=3e-2, rtol=1e-3)
    assert rel_rmse < 0.02