Exemplo n.º 1
0
def test_rms():
    t = np.linspace(0, 1, 1000)

    for rms_desired in [0, 0.5, 1, 100]:
        f = whitenoise(1, 100, rms=rms_desired)
        rms = np.sqrt(np.mean([f(tt) ** 2 for tt in t]))
        assert np.allclose(rms, rms_desired, atol=.1, rtol=.01)
Exemplo n.º 2
0
def test_rms():
    t = np.linspace(0, 1, 1000)

    for rms_desired in [0, 0.5, 1, 100]:
        f = whitenoise(1, 100, rms=rms_desired)
        rms = np.sqrt(np.mean([f(tt)**2 for tt in t]))
        assert np.allclose(rms, rms_desired, atol=.1, rtol=.01)
Exemplo n.º 3
0
def test_array():
    rms_desired = 0.5
    f = whitenoise(1, 5, rms=rms_desired, dimensions=5)

    t = np.linspace(0, 1, 1000)
    data = np.array([f(tt) for tt in t])
    rms = np.sqrt(np.mean(data**2, axis=0))
    assert np.allclose(rms, rms_desired, atol=.1, rtol=.01)
Exemplo n.º 4
0
def test_array():
    rms_desired = 0.5
    f = whitenoise(1, 5, rms=rms_desired, dimensions=5)

    t = np.linspace(0, 1, 1000)
    data = np.array([f(tt) for tt in t])
    rms = np.sqrt(np.mean(data**2, axis=0))
    assert np.allclose(rms, rms_desired, atol=.1, rtol=.01)
Exemplo n.º 5
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
Exemplo n.º 6
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
Exemplo n.º 7
0
def test_reset(Simulator, nl_nodirect):
    """Make sure resetting actually resets.

    A learning network on weights is used as the example network as the
    ultimate stress test; lots of weird stuff happens during learning, but
    if we're able to reset back to initial connection weights and everything
    then we're probably doing resetting right.
    """
    noise = whitenoise(0.1, 5, dimensions=2, seed=328)
    m = nengo.Network(seed=3902)
    with m:
        m.config[nengo.Ensemble].neuron_type = nl_nodirect()
        u = nengo.Node(output=noise)
        ens = nengo.Ensemble(200, dimensions=2)
        error = nengo.Ensemble(200, dimensions=2)
        square = nengo.Ensemble(200, dimensions=2)

        nengo.Connection(u, ens)
        nengo.Connection(u, error)
        nengo.Connection(square, error, transform=-1)
        err_conn = nengo.Connection(error, square, modulatory=True)
        nengo.Connection(ens,
                         square,
                         learning_rule=[nengo.PES(err_conn),
                                        nengo.BCM()],
                         solver=LstsqL2nz(weights=True))

        square_p = nengo.Probe(square, synapse=0.1)
        err_p = nengo.Probe(error, synapse=0.1)

    sim = Simulator(m)
    sim.run(0.2)
    sim.run(0.6)

    first_t = sim.trange()
    first_square_p = np.array(sim.data[square_p], copy=True)
    first_err_p = np.array(sim.data[err_p], copy=True)

    sim.reset()
    sim.run(0.8)

    assert np.all(sim.trange() == first_t)
    assert np.all(sim.data[square_p] == first_square_p)
    assert np.all(sim.data[err_p] == first_err_p)
Exemplo n.º 8
0
def run_synapse(Simulator, synapse, dt=1e-3, runtime=1., n_neurons=None):
    model = nengo.Network(seed=2984)
    with model:
        u = nengo.Node(output=whitenoise(0.1, 5, seed=328))

        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)

    sim = Simulator(model, dt=dt)
    sim.run(runtime)

    return sim.trange(), sim.data[ref], sim.data[filtered]
Exemplo n.º 9
0
def run_synapse(Simulator, synapse, dt=1e-3, runtime=1., n_neurons=None):
    model = nengo.Network(seed=2984)
    with model:
        u = nengo.Node(output=whitenoise(0.1, 5, seed=328))

        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, synapse=None)
        filtered = nengo.Probe(target, synapse=synapse)

    sim = nengo.Simulator(model, dt=dt)
    sim.run(runtime)

    return sim.trange(), sim.data[ref], sim.data[filtered]
Exemplo n.º 10
0
def test_reset(Simulator, nl_nodirect):
    """Make sure resetting actually resets.

    A learning network on weights is used as the example network as the
    ultimate stress test; lots of weird stuff happens during learning, but
    if we're able to reset back to initial connection weights and everything
    then we're probably doing resetting right.
    """
    noise = whitenoise(0.1, 5, dimensions=2, seed=328)
    m = nengo.Network(seed=3902)
    with m:
        m.config[nengo.Ensemble].neuron_type = nl_nodirect()
        u = nengo.Node(output=noise)
        ens = nengo.Ensemble(200, dimensions=2)
        error = nengo.Ensemble(200, dimensions=2)
        square = nengo.Ensemble(200, dimensions=2)

        nengo.Connection(u, ens)
        nengo.Connection(u, error)
        nengo.Connection(square, error, transform=-1)
        err_conn = nengo.Connection(error, square, modulatory=True)
        nengo.Connection(ens, square,
                         learning_rule=[nengo.PES(err_conn), nengo.BCM()],
                         solver=LstsqL2nz(weights=True))

        square_p = nengo.Probe(square, synapse=0.1)
        err_p = nengo.Probe(error, synapse=0.1)

    sim = Simulator(m)
    sim.run(0.2)
    sim.run(0.6)

    first_t = sim.trange()
    first_square_p = np.array(sim.data[square_p], copy=True)
    first_err_p = np.array(sim.data[err_p], copy=True)

    sim.reset()
    sim.run(0.8)

    assert np.all(sim.trange() == first_t)
    assert np.all(sim.data[square_p] == first_square_p)
    assert np.all(sim.data[err_p] == first_err_p)
Exemplo n.º 11
0
def test_unsupervised(Simulator, nl_nodirect, learning_rule_type,
                      seed, rng, plt):
    n = 200

    m = nengo.Network(seed=seed)
    with m:
        m.config[nengo.Ensemble].neuron_type = nl_nodirect()
        u = nengo.Node(whitenoise(0.1, 5, dimensions=2, seed=seed+1))
        a = nengo.Ensemble(n, dimensions=2)
        u_learned = nengo.Ensemble(n, dimensions=2)

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

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

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

    sim = Simulator(m)
    sim.run(0.5)
    t = sim.trange()

    name = learning_rule_type.__class__.__name__
    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(t, sim.data[trans_p][..., 4])
    plt.xlabel("Time (s)")
    plt.ylabel("Transform weight")
    plt.saveas = 'test_learning_rules.test_unsupervised_%s.pdf' % name

    assert not np.all(sim.data[trans_p][0] == sim.data[trans_p][-1])
import numpy as np
import nengo
import nengo_spinnaker
import matplotlib.pyplot as plt
from nengo.utils.functions import whitenoise

model = nengo.Network()
config = nengo_spinnaker.Config()

with model:
    # -- input and pre popluation
    inp = nengo.Node(whitenoise(0.1, 5, dimensions=2))
    config[inp].f_of_t = True
    
    pre = nengo.Ensemble(120, dimensions=2)
    nengo.Connection(inp, pre)
    
    # -- error population
    prod_node = nengo.Node(lambda t, x: x[0] * x[1], size_in=2)  # We'll give it the actual product
    config[prod_node].f_of_t = True
    
    nengo.Connection(inp, prod_node, synapse=None)
    error = nengo.Ensemble(60, dimensions=1)
    nengo.Connection(prod_node, error)
    
    # -- inhibit error after 40 seconds
    inhib = nengo.Node(lambda t: 2.0 if t > 40.0 else 0.0)
    config[inhib].f_of_t = True
    
    nengo.Connection(inhib, error.neurons, transform=[[-1]] * error.n_neurons)
Exemplo n.º 13
0
import numpy as np
import nengo
from nengo.utils.functions import whitenoise
model = nengo.Model("Delayed connection")
input = nengo.Node(whitenoise(1, 5, seed=60))
A = nengo.Ensemble(nengo.LIF(40), dimensions=1)
nengo.Connection(input, A)

class Delay(object):
    def __init__(self, dimensions, timesteps=50):
        self.history = np.zeros((timesteps, dimensions))
    def step(self, t, x):
        self.history = np.roll(self.history, -1)
        self.history[-1] = x
        return self.history[0]

dt = 0.001
delay = Delay(1, timesteps=int(0.2 / 0.001))
delaynode = nengo.Node(delay.step, size_in=1)
nengo.Connection(A, delaynode)

B = nengo.Ensemble(nengo.LIF(40), dimensions=1)
nengo.Connection(delaynode, B)
import numpy as np
import nengo
import nengo_spinnaker

spinnaker = True
dimensions = 1
learning_rates = [1.0, 0.5, 0.25]
post_colours = ['r', 'g', 'y']

from nengo.utils.functions import whitenoise
model = nengo.Network()
with model:
    config = nengo_spinnaker.Config()
        
    inp = nengo.Node(whitenoise(0.1, 5, dimensions=dimensions), label = "inp")
    config[inp].f_of_t = True
    
    pre = nengo.Ensemble(60, dimensions=dimensions, label = "pre")
    nengo.Connection(inp, pre)
    
    posts = [nengo.Ensemble(60, dimensions=dimensions, label = "post%u" % i) for i, l in enumerate(learning_rates)]
    conns = [nengo.Connection(pre, p, function=lambda x: np.random.random(dimensions)) for p in posts]
    errors = [nengo.Ensemble(60, dimensions=dimensions, label = "error%u" % i) for i, l in enumerate(learning_rates)]
    error_probes = [nengo.Probe(e, synapse=0.03) for e in errors]
    
    
    for e, p, c, l in zip(errors, posts, conns, learning_rates):
        # Error = pre - post
        nengo.Connection(pre, e)
        nengo.Connection(p, e, transform=-1)