示例#1
0
def test_seed(Simulator, seed):
    with nengo.Network() as model:
        a = nengo.Node(WhiteSignal(0.1, high=100, seed=seed))
        b = nengo.Node(WhiteSignal(0.1, high=100, seed=seed + 1))
        c = nengo.Node(WhiteSignal(0.1, high=100))
        d = nengo.Node(WhiteNoise(seed=seed))
        e = nengo.Node(WhiteNoise())
        ap = nengo.Probe(a)
        bp = nengo.Probe(b)
        cp = nengo.Probe(c)
        dp = nengo.Probe(d)
        ep = nengo.Probe(e)

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

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

    tols = dict(atol=1e-7, rtol=1e-4)
    assert np.allclose(sim1.data[ap], sim2.data[ap], **tols)
    assert np.allclose(sim1.data[bp], sim2.data[bp], **tols)
    assert not np.allclose(sim1.data[cp], sim2.data[cp], **tols)
    assert not np.allclose(sim1.data[ap], sim1.data[bp], **tols)
    assert np.allclose(sim1.data[dp], sim2.data[dp], **tols)
    assert not np.allclose(sim1.data[ep], sim2.data[ep], **tols)
示例#2
0
def test_whitesignal_high_errors(Simulator, dt, high, seed):
    """Check for errors if ``high`` is not between 1/period and nyquist frequency."""

    with pytest.raises(ValidationError, match="Make ``high >= 1. / period``"):
        process = WhiteSignal(period=10 * dt, high=9 * dt)

    process = WhiteSignal(1.0, high=high)
    with nengo.Network() as model:
        nengo.Node(process, size_out=1)

    with pytest.raises(ValidationError,
                       match="High must not exceed the Nyquist"):
        Simulator(model, dt=dt, seed=seed)
示例#3
0
def test_whitesignal_continuity(rng, plt):
    """Test that WhiteSignal is continuous over multiple periods."""
    rms = 0.5
    high = 10
    dt = 0.001
    t = 1
    process = WhiteSignal(t, high=high, rms=rms)
    x = process.run(4 * t, d=1, dt=dt, rng=rng)

    plt.plot(process.ntrange(len(x), dt=dt), x)

    # tolerances approximated from derivatives of sine wave of highest freq
    safety_factor = 2.
    a, f = np.sqrt(2) * rms, (2 * np.pi * high) * dt
    assert abs(np.diff(x, axis=0)).max() <= safety_factor * a * f
    assert abs(np.diff(x, n=2, axis=0)).max() <= safety_factor**2 * a * f**2
示例#4
0
def test_reset(Simulator, nl_nodirect, seed, rng):
    """Make sure resetting actually resets."""
    m = nengo.Network(seed=seed)
    with m:
        m.config[nengo.Ensemble].neuron_type = nl_nodirect()
        u = nengo.Node(WhiteSignal(0.3, high=10), size_out=2)
        ens = nengo.Ensemble(60, dimensions=2)
        square = nengo.Ensemble(60, dimensions=2)
        nengo.Connection(u, ens)
        nengo.Connection(ens,
                         square,
                         function=lambda x: x**2,
                         solver=LstsqL2nz(weights=True))
        square_p = nengo.Probe(square, synapse=0.01)

    with Simulator(m, seed=seed + 1) as sim:
        sim.run(0.1)
        sim.run(0.2)
        t1 = sim.trange()
        square1 = np.array(sim.data[square_p])

        sim.reset()
        sim.run(0.3)

    assert np.allclose(sim.trange(), t1)
    assert np.allclose(sim.data[square_p], square1)
示例#5
0
def create_model():
    dimensions = 4
    model = nengo.Network()
    with model:
        num_neurons = dimensions * 30
        inp = nengo.Node(WhiteSignal(num_neurons, high=5),
                         size_out=dimensions,
                         label="white_noise")
        pre = nengo.Ensemble(num_neurons, dimensions=dimensions, label="pre")
        nengo.Connection(inp, pre)
        post = nengo.Ensemble(num_neurons, dimensions=dimensions, label="post")
        conn = nengo.Connection(
            pre, post, function=lambda x: np.random.random(dimensions))
        inp_p = nengo.Probe(inp, label="inp_p")
        pre_p = nengo.Probe(pre, synapse=0.01, label="pre_p")
        post_p = nengo.Probe(post, synapse=0.01, label="post_p")

        error = nengo.Ensemble(num_neurons,
                               dimensions=dimensions,
                               label="error")
        error_p = nengo.Probe(error, synapse=0.03, label="error_p")

        # Error = actual - target = post - pre
        nengo.Connection(post, error)
        nengo.Connection(pre, error, transform=-1)

        # Add the learning rule to the connection
        conn.learning_rule_type = nengo.PES()

        # Connect the error into the learning rule
        nengo.Connection(error, conn.learning_rule)
    return model, list(), dict()
示例#6
0
def test_whitesignal_high_dt(Simulator, high, dt, seed, plt):
    t = 1.
    rms = 0.5
    d = 500
    process = WhiteSignal(t, high, rms=rms)
    with nengo.Network() as model:
        u = nengo.Node(process, size_out=d)
        up = nengo.Probe(u)

    with Simulator(model, seed=seed, dt=dt) as sim:
        sim.run(t)
    values = sim.data[up]
    freq, val_psd = psd(values, dt=dt)

    trange = sim.trange()
    plt.subplot(2, 1, 1)
    plt.title("First two D of white noise process, high=%d Hz" % high)
    plt.plot(trange, values[:, :2])
    plt.xlim(right=trange[-1])
    plt.subplot(2, 1, 2)
    plt.title("Power spectrum")
    plt.plot(freq, val_psd, drawstyle='steps')
    plt.xlim(right=high * 2.0)

    assert np.allclose(np.std(values, axis=1), rms, rtol=0.15)
    assert np.all(val_psd[npext.rfftfreq(len(trange), dt) > high] < rms * 0.5)
示例#7
0
def test_whitesignal_continuity(rng, plt):
    """Test that WhiteSignal is continuous over multiple periods."""
    rms = 0.5
    high = 10
    dt = 0.001
    t = 1
    process = WhiteSignal(t, high=high, rms=rms)
    x = process.run(4 * t, d=1, dt=dt, rng=rng)

    plt.plot(process.ntrange(len(x), dt=dt), x)

    # tolerances approximated from derivatives of sine wave of highest freq
    safety_factor = 2.
    a, f = np.sqrt(2) * rms, (2 * np.pi * high) * dt
    assert abs(np.diff(x, axis=0)).max() <= safety_factor * a * f
    assert abs(np.diff(x, n=2, axis=0)).max() <= safety_factor**2 * a * f**2
示例#8
0
def run_synapse(Simulator,
                seed,
                synapse,
                dt=1e-3,
                runtime=0.2,
                high=100,
                n_neurons=None):
    model = nengo.Network(seed=seed)
    with model:
        u = nengo.Node(output=WhiteSignal(runtime, high=high))

        if n_neurons is not None:
            a = nengo.Ensemble(n_neurons, 1)
            nengo.Connection(u, a, synapse=None)
            target = a
        else:
            target = u

        ref = nengo.Probe(target)
        filtered = nengo.Probe(target, synapse=synapse)

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

    return sim.trange(), sim.data[ref], sim.data[filtered]
示例#9
0
def test_whitesignal_nyquist(Simulator, dt, high, seed):
    # check that high cannot exceed nyquist frequency
    process = WhiteSignal(1.0, high=high)
    with nengo.Network() as model:
        nengo.Node(process, size_out=1)

    with pytest.raises(ValidationError):
        Simulator(model, dt=dt, seed=seed)
示例#10
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])
示例#11
0
def test_window_function():
    process = WhiteSignal(1.0, 10)
    with pytest.raises(ValidationError):
        RollingWindow(theta=1.0, n_neurons=1, process=process,
                      eval_points=cube)

    rw = RollingWindow(theta=1.0, n_neurons=1, process=None)
    with pytest.raises(ValidationError):
        rw.add_output(function=lambda a, b: a)
示例#12
0
def test_izhikevich(Simulator, plt, seed, rng):
    """Smoke test for using Izhikevich neurons.

    Tests that the 6 parameter sets listed in the original paper can be
    simulated in Nengo (but doesn't test any properties of them).
    """
    with nengo.Network() as m:
        u = nengo.Node(output=WhiteSignal(0.6, high=10), size_out=1)

        # Seed the ensembles (not network) so we get the same sort of neurons
        ens_args = {'n_neurons': 4, 'dimensions': 1, 'seed': seed}
        rs = nengo.Ensemble(neuron_type=nengo.Izhikevich(), **ens_args)
        ib = nengo.Ensemble(neuron_type=nengo.Izhikevich(reset_voltage=-55,
                                                         reset_recovery=4),
                            **ens_args)
        ch = nengo.Ensemble(neuron_type=nengo.Izhikevich(reset_voltage=-50,
                                                         reset_recovery=2),
                            **ens_args)
        fs = nengo.Ensemble(neuron_type=nengo.Izhikevich(tau_recovery=0.1),
                            **ens_args)
        lts = nengo.Ensemble(neuron_type=nengo.Izhikevich(coupling=0.25),
                             **ens_args)
        rz = nengo.Ensemble(neuron_type=nengo.Izhikevich(tau_recovery=0.1,
                                                         coupling=0.26),
                            **ens_args)

        ensembles = (rs, ib, ch, fs, lts, rz)
        out = {}
        spikes = {}
        for ens in ensembles:
            nengo.Connection(u, ens)
            out[ens] = nengo.Probe(ens, synapse=0.05)
            spikes[ens] = nengo.Probe(ens.neurons)
        up = nengo.Probe(u)

    with Simulator(m, seed=seed + 1) as sim:
        sim.run(0.6)
    t = sim.trange()

    def plot(ens, title, ix):
        ax = plt.subplot(len(ensembles), 1, ix)
        plt.title(title)
        plt.plot(t, sim.data[out[ens]], c='k', lw=1.5)
        plt.plot(t, sim.data[up], c='k', ls=':')
        ax = ax.twinx()
        ax.set_yticks(())
        rasterplot(t, sim.data[spikes[ens]], ax=ax)

    plt.figure(figsize=(10, 12))
    plot(rs, "Regular spiking", 1)
    plot(ib, "Intrinsically bursting", 2)
    plot(ch, "Chattering", 3)
    plot(fs, "Fast spiking", 4)
    plot(lts, "Low-threshold spiking", 5)
    plot(rz, "Resonator", 6)
示例#13
0
def test_invalid_sample():
    process = WhiteSignal(1.0, high=10)
    sys = PadeDelay(0.1, order=4)

    dist = EvalPoints(sys, process)
    with pytest.raises(ValidationError):
        dist.sample(100, len(sys))  # needs to equal sys.size_out

    dist = Encoders(sys, process)
    with pytest.raises(ValidationError):
        dist.sample(100, len(sys))  # needs to equal sys.size_out
示例#14
0
def test_whitesignal_rms(rms, rng, plt):
    d = 500
    t = 1
    dt = 0.001

    process = WhiteSignal(t, rms=rms)
    values = process.run(t, d=d, dt=dt, rng=rng)
    freq, val_psd = psd(values)

    trange = process.trange(t, dt=dt)
    plt.subplot(2, 1, 1)
    plt.title("First two D of white noise process, rms=%.1f" % rms)
    plt.plot(trange, values[:, :2])
    plt.xlim(right=trange[-1])
    plt.subplot(2, 1, 2)
    plt.title("Power spectrum")
    plt.plot(freq, val_psd, drawstyle='steps')

    assert np.allclose(np.std(values), rms, rtol=0.02)
    assert np.allclose(val_psd[1:-1], rms, rtol=0.35)
示例#15
0
def test_whitesignal_rms(rms, rng, plt):
    d = 500
    t = 1
    dt = 0.001

    process = WhiteSignal(t, rms=rms)
    values = process.run(t, d=d, dt=dt, rng=rng)
    freq, val_psd = psd(values)

    trange = process.trange(t, dt=dt)
    plt.subplot(2, 1, 1)
    plt.title("First two D of white noise process, rms=%.1f" % rms)
    plt.plot(trange, values[:, :2])
    plt.xlim(right=trange[-1])
    plt.subplot(2, 1, 2)
    plt.title("Power spectrum")
    plt.plot(freq, val_psd, drawstyle='steps')

    assert np.allclose(np.std(values), rms, rtol=0.02)
    assert np.allclose(val_psd[1:-1], rms, rtol=0.35)
示例#16
0
def test_processes():
    assert (
        repr(WhiteSignal(0.2, 10, rms=0.3))
        == "WhiteSignal(period=0.2, high=10, rms=0.3)"
    )

    check_init_args(WhiteNoise, ["dist", "scale"])
    check_repr(WhiteNoise(scale=False))
    assert repr(WhiteNoise()) == "WhiteNoise()"
    assert repr(WhiteNoise(scale=False)) == "WhiteNoise(scale=False)"

    check_init_args(FilteredNoise, ["synapse", "dist", "scale"])
    check_repr(FilteredNoise(scale=False))
    assert repr(FilteredNoise()) == "FilteredNoise()"
    assert repr(FilteredNoise(scale=False)) == "FilteredNoise(scale=False)"

    check_init_args(BrownNoise, ["dist"])
    check_repr(BrownNoise())
    assert repr(BrownNoise()) == "BrownNoise()"

    check_init_args(PresentInput, ["inputs", "presentation_time"])
    check_repr(PresentInput(inputs=np.array([1.2, 3.4]), presentation_time=5))
    assert (
        repr(PresentInput((1.2, 3.4), 5))
        == "PresentInput(inputs=array([1.2, 3.4]), presentation_time=5)"
    )

    check_init_args(WhiteSignal, ["period", "high", "rms", "y0"])
    check_repr(WhiteSignal(period=1.2, high=3.4, rms=5.6, y0=7.8))
    assert repr(WhiteSignal(1, 2)) == "WhiteSignal(period=1, high=2)"
    assert (
        repr(WhiteSignal(1.2, 3.4, 5.6, 7.8))
        == "WhiteSignal(period=1.2, high=3.4, rms=5.6, y0=7.8)"
    )

    check_init_args(Piecewise, ["data", "interpolation"])
    check_repr(Piecewise(data={1: 0.1, 2: 0.2, 3: 0.3}))
    assert (
        repr(Piecewise({1: 0.1, 2: 0.2, 3: 0.3}))
        == "Piecewise(data={1: array([0.1]), 2: array([0.2]), 3: array([0.3])})"
    )
示例#17
0
class SignalGenerator(Process):
    def __init__(self, duration, high=5.):
        self._whitenoise = WhiteSignal(duration, high=high)

    def make_step(self, size_in, size_out, dt, rng=np.random):
        return functools.partial(
            self.sample,
            sample_whitenoise=self._whitenoise.make_step(size_in, size_out, dt, rng))

    @staticmethod
    def sample(t, sample_whitenoise):
        sampled = sample_whitenoise(t)
        return sampled / np.linalg.norm(sampled)
示例#18
0
def test_whitesignal_high(high, rng, plt):
    rms = 0.5
    d = 500
    t = 1
    dt = 0.001

    process = WhiteSignal(t, high, rms=rms)
    values = process.run(t, d=d, dt=dt, rng=rng)
    freq, val_psd = psd(values)

    trange = process.trange(t, dt=dt)
    plt.subplot(2, 1, 1)
    plt.title("First two D of white noise process, high=%d Hz" % high)
    plt.plot(trange, values[:, :2])
    plt.xlim(right=trange[-1])
    plt.subplot(2, 1, 2)
    plt.title("Power spectrum")
    plt.plot(freq, val_psd, drawstyle='steps')
    plt.xlim(right=high * 2.0)

    assert np.allclose(np.std(values, axis=1), rms, rtol=0.15)
    assert np.all(val_psd[npext.rfftfreq(t, dt) > high] < rms * 0.5)
示例#19
0
def test_whitesignal_high(high, rng, plt):
    rms = 0.5
    d = 500
    t = 1
    dt = 0.001

    process = WhiteSignal(t, high, rms=rms)
    values = process.run(t, d=d, dt=dt, rng=rng)
    freq, val_psd = psd(values)

    trange = process.trange(t, dt=dt)
    plt.subplot(2, 1, 1)
    plt.title("First two D of white noise process, high=%d Hz" % high)
    plt.plot(trange, values[:, :2])
    plt.xlim(right=trange[-1])
    plt.subplot(2, 1, 2)
    plt.title("Power spectrum")
    plt.plot(freq, val_psd, drawstyle='steps')
    plt.xlim(right=high * 2.0)

    assert np.allclose(np.std(values, axis=1), rms, rtol=0.15)
    assert np.all(val_psd[npext.rfftfreq(t, dt) > high] < rms * 0.5)
示例#20
0
def test_whitesignal_y0(Simulator, seed, y0, d):
    t = .1
    process = WhiteSignal(t, high=500, y0=y0)
    with nengo.Network() as model:
        u = nengo.Node(process, size_out=d)
        up = nengo.Probe(u)

    with Simulator(model, seed=seed) as sim:
        sim.run(t)
    values = sim.data[up]
    error = np.min(abs(y0 - values), axis=0)

    assert ((y0 - error <= values[0, :]) & (values[0, :] <= y0 + error)).all()
示例#21
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])
示例#22
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
示例#23
0
def test_unsupervised(Simulator, rule_type, solver, seed, rng, plt, allclose):
    n = 200

    m = nengo.Network(seed=seed)
    with m:
        u = nengo.Node(WhiteSignal(0.5, high=10), size_out=2)
        a = nengo.Ensemble(n, dimensions=2)
        b = nengo.Ensemble(n + 1, dimensions=2)
        nengo.Connection(u, a)

        if solver:
            conn = nengo.Connection(a,
                                    b,
                                    solver=nengo.solvers.LstsqL2(weights=True))
        else:
            initial_weights = rng.uniform(high=1e-3,
                                          size=(b.n_neurons, a.n_neurons))
            conn = nengo.Connection(a.neurons,
                                    b.neurons,
                                    transform=initial_weights)
        conn.learning_rule_type = rule_type

        inp_p = nengo.Probe(u)
        weights_p = nengo.Probe(conn, "weights", sample_every=0.01)

        ap = nengo.Probe(a, synapse=0.03)
        up = nengo.Probe(b, synapse=0.03)

    with Simulator(m, seed=seed + 1) as sim:
        sim.run(0.5)
    t = sim.trange()

    plt.subplot(2, 1, 1)
    plt.plot(t, sim.data[inp_p], label="Input")
    plt.plot(t, sim.data[ap], label="Pre")
    plt.plot(t, sim.data[up], label="Post")
    plt.legend(loc="best", fontsize="x-small")
    plt.subplot(2, 1, 2)
    best_ix = best_weights(sim.data[weights_p])
    plt.plot(sim.trange(sample_every=0.01), sim.data[weights_p][..., best_ix])
    plt.xlabel("Time (s)")
    plt.ylabel("Weights")

    assert not allclose(sim.data[weights_p][0],
                        sim.data[weights_p][-1],
                        record_rmse=False,
                        print_fail=0)
示例#24
0
def test_pes_synapse(Simulator, seed, pre_synapse, allclose):
    rule = PES(pre_synapse=pre_synapse)

    with nengo.Network(seed=seed) as model:
        stim = nengo.Node(output=WhiteSignal(0.5, high=10))
        x = nengo.Ensemble(100, 1)

        nengo.Connection(stim, x, synapse=None)
        conn = nengo.Connection(x, x, learning_rule_type=rule)

        p_neurons = nengo.Probe(x.neurons, synapse=pre_synapse)
        p_pes = nengo.Probe(conn.learning_rule, "activities")

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

    assert allclose(sim.data[p_neurons][1:, :], sim.data[p_pes][:-1, :])
示例#25
0
def test_white_signal():
    model = nengo.Network()
    with model:
        inp = nengo.Node(WhiteSignal(60, high=5), size_out=2)
        inp_p = nengo.Probe(inp)

    nengo_spinnaker.add_spinnaker_params(model.config)
    model.config[inp].function_of_time = True

    sim = nengo_spinnaker.Simulator(model)
    with sim:
        sim.run(0.2)

    # Read data
    in_data = sim.data[inp_p]

    # Check that the input value actually changes
    assert np.any(in_data[5] != in_data[6:])
示例#26
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)
示例#27
0
def test_dt_dependence(Simulator, nl_nodirect, plt, seed, rng):
    """Neurons should not wildly change with different dt."""
    with nengo.Network(seed=seed) as m:
        m.config[nengo.Ensemble].neuron_type = nl_nodirect()
        u = nengo.Node(output=WhiteSignal(0.1, 5), size_out=2)
        pre = nengo.Ensemble(60, dimensions=2)
        square = nengo.Ensemble(60, dimensions=2)
        nengo.Connection(u, pre)
        nengo.Connection(pre, square, function=lambda x: x**2)

        activity_p = nengo.Probe(square.neurons,
                                 synapse=.05,
                                 sample_every=0.001)
        out_p = nengo.Probe(square, synapse=.05, sample_every=0.001)

    activity_data = []
    out_data = []
    dts = (0.0001, 0.001)
    colors = ('b', 'g', 'r')
    for c, dt in zip(colors, dts):
        sim = Simulator(m, dt=dt, seed=seed + 1)
        sim.run(0.1)
        t = sim.trange(dt=0.001)
        activity_data.append(sim.data[activity_p])
        out_data.append(sim.data[out_p])
        plt.subplot(2, 1, 1)
        plt.plot(t, sim.data[out_p], c=c)
        plt.subplot(2, 1, 2)
        # Just plot 5 neurons
        plt.plot(t, sim.data[activity_p][..., :5], c=c)

    plt.subplot(2, 1, 1)
    plt.xlim(right=t[-1])
    plt.ylabel("Decoded output")
    plt.subplot(2, 1, 2)
    plt.xlim(right=t[-1])
    plt.ylabel("Neural activity")

    assert rmse(activity_data[0], activity_data[1]) < ((1. / dt) * 0.01)
    assert np.allclose(out_data[0], out_data[1], atol=0.05)
示例#28
0
def test_whitesignal_continuity(Simulator, seed, plt):
    """Test that WhiteSignal is continuous over multiple periods."""
    t = 1.
    high = 10
    rms = 0.5
    process = WhiteSignal(t, high=high, rms=rms)
    with nengo.Network() as model:
        u = nengo.Node(process, size_out=1)
        up = nengo.Probe(u)

    with Simulator(model, seed=seed) as sim:
        sim.run(4 * t)
    dt = sim.dt
    x = sim.data[up]

    plt.plot(sim.trange(), x)

    # tolerances approximated from derivatives of sine wave of highest freq
    safety_factor = 2.
    a, f = np.sqrt(2) * rms, (2 * np.pi * high) * dt
    assert abs(np.diff(x, axis=0)).max() <= safety_factor * a * f
    assert abs(np.diff(x, n=2, axis=0)).max() <= safety_factor**2 * a * f**2
示例#29
0
def test_unsupervised(Simulator, learning_rule_type, seed, rng, plt):
    n = 200

    m = nengo.Network(seed=seed)
    with m:
        u = nengo.Node(WhiteSignal(0.5, high=5), size_out=2)
        a = nengo.Ensemble(n, dimensions=2)
        b = nengo.Ensemble(n, dimensions=2)

        initial_weights = rng.uniform(
            high=1e-3,
            size=(a.n_neurons, b.n_neurons))

        nengo.Connection(u, a)
        conn = nengo.Connection(a.neurons, b.neurons,
                                transform=initial_weights,
                                learning_rule_type=learning_rule_type)
        inp_p = nengo.Probe(u)
        trans_p = nengo.Probe(conn, 'transform', sample_every=0.01)

        ap = nengo.Probe(a, synapse=0.03)
        up = nengo.Probe(b, synapse=0.03)

    sim = Simulator(m, seed=seed+1)
    sim.run(0.5)
    t = sim.trange()

    plt.subplot(2, 1, 1)
    plt.plot(t, sim.data[inp_p], label="Input")
    plt.plot(t, sim.data[ap], label="Pre")
    plt.plot(t, sim.data[up], label="Post")
    plt.legend(loc="best", fontsize="x-small")
    plt.subplot(2, 1, 2)
    plt.plot(sim.trange(dt=0.01), sim.data[trans_p][..., 4])
    plt.xlabel("Time (s)")
    plt.ylabel("Transform weight")

    assert not np.all(sim.data[trans_p][0] == sim.data[trans_p][-1])
示例#30
0
def test_whitesignal_rms(Simulator, rms, seed, plt):
    t = 1.
    d = 500
    process = WhiteSignal(t, high=500, rms=rms)
    with nengo.Network() as model:
        u = nengo.Node(process, size_out=d)
        up = nengo.Probe(u)

    with Simulator(model, seed=seed) as sim:
        sim.run(t)
    values = sim.data[up]
    freq, val_psd = psd(values, dt=sim.dt)

    trange = sim.trange()
    plt.subplot(2, 1, 1)
    plt.title("First two D of white noise process, rms=%.1f" % rms)
    plt.plot(trange, values[:, :2])
    plt.xlim(right=trange[-1])
    plt.subplot(2, 1, 2)
    plt.title("Power spectrum")
    plt.plot(freq, val_psd, drawstyle='steps')

    assert np.allclose(np.std(values), rms, rtol=0.02)
    assert np.allclose(val_psd[1:-1], rms, rtol=0.35)
示例#31
0
def test_sampling_shape():
    process = WhiteSignal(0.1)
    assert process.run_steps(1).shape == (1, 1)
    assert process.run_steps(5, d=1).shape == (5, 1)
    assert process.run_steps(1, d=2). shape == (1, 2)
示例#32
0
 def __init__(self, duration, high=5.):
     self._whitenoise = WhiteSignal(duration, high=high)
rasterplot(sim.trange(), sim.data[A_spikes], ax)
plt.xlim(0, 1)
plt.xlabel("Time (s)")
plt.ylabel("Neuron")

plt.subplot(1, 3, 3)
plt.plot(sim.trange(), sim.data[input_probe], label="Input Signal")
plt.plot(sim.trange(), sim.data[A_probe], label="Decoded estimate")
plt.legend(loc="best")
plt.xlabel("Time (s)")
plt.xlim(0, 1)
# plt.show()

model = nengo.Network(label="NEF Summary")
with model:
    input = nengo.Node(WhiteSignal(1, high=5), size_out=1)
    input_probe = nengo.Probe(input)
    A = nengo.Ensemble(300, dimensions=1, max_rates=Uniform(80, 100))
    nengo.Connection(input, A)
    A_spikes = nengo.Probe(A.neurons)
    A_probe = nengo.Probe(A, synapse=0.01)

with nengo.Simulator(model) as sim:
    sim.run(1)

plt.figure(figsize=(10, 3.5))
plt.subplot(1, 2, 1)
plt.plot(sim.trange(), sim.data[input_probe], label="Input Signal")
plt.plot(sim.trange(), sim.data[A_probe], label="Decoded estimate")
plt.legend(loc="best")
plt.xlabel("Time (s)")
示例#34
0
def test_sampling_shape():
    process = WhiteSignal(0.1, high=500)
    assert process.run_steps(1).shape == (1, 1)
    assert process.run_steps(5, d=1).shape == (5, 1)
    assert process.run_steps(1, d=2).shape == (1, 2)
示例#35
0
    #Connecting pre population to post population (communication channel)
    conn = nengo.Connection(pre,
                            post,
                            function=lambda x: np.random.random(1),
                            solver=nengo.solvers.LstsqL2(weights=True))

    #Adding the learning rule to the connection
    conn.learning_rule_type = {
        'my_pes': nengo.PES(learning_rate=1e-3),
        'my_bcm': nengo.BCM()
    }

    #Error connections don't impart current
    error_conn = nengo.Connection(error, conn.learning_rule['my_pes'])

    #Providing input to the model
    input = nengo.Node(WhiteSignal(30, high=10))  # RMS = 0.5 by default
    # Connecting input to the pre ensemble
    nengo.Connection(input, pre, synapse=0.02)

    #function to inhibit the error population after 25 seconds
    def inhib(t):
        return 2.0 if t > 15.0 else 0.0

    #Connecting inhibit population to error population
    inhibit = nengo.Node(inhib)
    nengo.Connection(inhibit,
                     error.neurons,
                     transform=[[-1]] * error.n_neurons,
                     synapse=0.01)
def LearningModel(neurons, dimensions, learning_rule, function_to_learn,
                  convolve, seed):
    global decoded

    with nengo.Network() as model:

        nengo_dl.configure_settings(stateful=False)

        model.inp = nengo.Node(
            # WhiteNoise( dist=Gaussian( 0, 0.05 ), seed=seed ),
            WhiteSignal(sim_time, high=5, seed=seed),
            size_out=dimensions[0])
        model.pre = nengo.Ensemble(neurons[0],
                                   dimensions=dimensions[0],
                                   seed=seed)
        model.post = nengo.Ensemble(neurons[1],
                                    dimensions=dimensions[1],
                                    seed=seed)
        model.ground_truth = nengo.Ensemble(neurons[2],
                                            dimensions=dimensions[2],
                                            seed=seed)

        nengo.Connection(model.inp, model.pre)

        if convolve:
            model.conv = nengo.networks.CircularConvolution(neurons[4],
                                                            dimensions[4],
                                                            seed=seed)
            nengo.Connection(model.inp[:int(dimensions[0] / 2)],
                             model.conv.input_a,
                             synapse=None)
            nengo.Connection(model.inp[int(dimensions[0] / 2):],
                             model.conv.input_b,
                             synapse=None)
            nengo.Connection(model.conv.output,
                             model.ground_truth,
                             synapse=None)
        else:
            nengo.Connection(model.inp,
                             model.ground_truth,
                             function=function_to_learn,
                             synapse=None)

        if learning_rule:
            model.error = nengo.Ensemble(neurons[3],
                                         dimensions=dimensions[3],
                                         seed=seed)

            if isinstance(learning_rule,
                          mPES) or (isinstance(learning_rule, PES)
                                    and not decoded):
                model.conn = nengo.Connection(model.pre.neurons,
                                              model.post.neurons,
                                              transform=np.random.random(
                                                  (model.post.n_neurons,
                                                   model.pre.n_neurons)),
                                              learning_rule_type=learning_rule)
            else:
                model.conn = nengo.Connection(
                    model.pre,
                    model.post,
                    function=lambda x: np.random.random(dimensions[1]),
                    learning_rule_type=learning_rule)
            nengo.Connection(model.error, model.conn.learning_rule)
            nengo.Connection(model.post, model.error)
            nengo.Connection(model.ground_truth, model.error, transform=-1)

            class cyclic_inhibit:
                def __init__(self, cycle_time):
                    self.out_inhibit = 0.0
                    self.cycle_time = cycle_time

                def step(self, t):
                    if t % self.cycle_time == 0:
                        if self.out_inhibit == 0.0:
                            self.out_inhibit = 2.0
                        else:
                            self.out_inhibit = 0.0

                    return self.out_inhibit

            model.inhib = nengo.Node(cyclic_inhibit(learn_block_time).step)
            nengo.Connection(model.inhib,
                             model.error.neurons,
                             transform=[[-1]] * model.error.n_neurons)
        else:
            model.conn = nengo.Connection(model.pre,
                                          model.post,
                                          function=function_to_learn)

        # -- probes
        model.pre_probe = nengo.Probe(model.pre, synapse=0.01)
        model.post_probe = nengo.Probe(model.post, synapse=0.01)
        model.ground_truth_probe = nengo.Probe(model.ground_truth,
                                               synapse=0.01)
        # function_learning_model.error_probe = nengo.Probe( function_learning_model.error, synapse=0.03 )

    return model