Example #1
0
def test_loihi_rates_other_type(neuron_type, allclose):
    """Test using a neuron type that has no Loihi-specific implementation"""
    x = np.linspace(-7, 10)
    gain, bias = 0.2, 0.4
    dt = 0.002
    ref_rates = nengo_rates(neuron_type, x, gain, bias)
    rates = loihi_rates(neuron_type, x, gain, bias, dt)
    assert ref_rates.shape == rates.shape
    assert allclose(rates, ref_rates)
def test_node_to_neurons(dt, precompute, allclose, Simulator, plt):
    tfinal = 0.4

    x = np.array([0.7, 0.3])
    A = np.array([[1, 1], [1, -1], [1, -0.5]])
    y = np.dot(A, x)

    gain = [3] * len(y)
    bias = [0] * len(y)

    neuron_type = nengo.LIF()
    z = nengo_rates(neuron_type, y[np.newaxis, :], gain, bias).squeeze(axis=0)

    with nengo.Network() as model:
        u = nengo.Node(x, label="u")
        a = nengo.Ensemble(len(y),
                           1,
                           label="a",
                           neuron_type=neuron_type,
                           gain=gain,
                           bias=bias)
        ap = nengo.Probe(a.neurons)
        nengo.Connection(u, a.neurons, transform=A)

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

    tsum = tfinal / 2
    t = sim.trange()
    rates = (sim.data[ap][t > t[-1] - tsum] > 0).sum(axis=0) / tsum

    bar_width = 0.35
    plt.bar(np.arange(len(z)), z, bar_width, color="k", label="z")
    plt.bar(np.arange(len(z)) + bar_width, rates, bar_width, label="rates")
    plt.legend(loc="best")

    assert rates.shape == z.shape
    assert allclose(rates, z, atol=3, rtol=0.1)
Example #3
0
 def rates(tau_ref, gain=gain, bias=bias):
     lif = nengo.LIF(tau_ref=tau_ref)
     return nengo_rates(lif, 0.0, gain, bias).squeeze(axis=0)
Example #4
0
def rate_nengo_dl_net(neuron_type,
                      discretize=True,
                      dt=0.001,
                      nx=256,
                      gain=1.,
                      bias=0.):
    """Create a network for determining rate curves with Nengo DL.

    Arguments
    ---------
    neuron_type : NeuronType
        The neuron type used in the network's ensemble.
    discretize : bool, optional (Default: True)
        Whether the tau_ref and tau_rc values should be discretized
        before generating rate curves.
    dt : float, optional (Default: 0.001)
        Simulator timestep.
    nx : int, optional (Default: 256)
        Number of x points in the rate curve.
    gain : float, optional (Default: 1.)
        Gain of all neurons.
    bias : float, optional (Default: 0.)
        Bias of all neurons.
    """
    net = nengo.Network()
    net.dt = dt
    net.bias = bias
    net.gain = gain
    lif_kw = dict(amplitude=neuron_type.amplitude)
    if isinstance(neuron_type, LoihiLIF):
        net.x = np.linspace(-1, 30, nx)

        net.sigma = 0.02
        lif_kw['tau_rc'] = neuron_type.tau_rc
        lif_kw['tau_ref'] = neuron_type.tau_ref

        if discretize:
            lif_kw['tau_ref'] = discretize_tau_ref(lif_kw['tau_ref'], dt)
            lif_kw['tau_rc'] = discretize_tau_rc(lif_kw['tau_rc'], dt)
        lif_kw['tau_ref'] += 0.5 * dt

    elif isinstance(neuron_type, LoihiSpikingRectifiedLinear):
        net.x = np.linspace(-1, 999, nx)

        net.tau_ref1 = 0.5 * dt
        net.j = neuron_type.current(net.x[:, np.newaxis], gain,
                                    bias).squeeze(axis=1) - 1

    with net:
        if isinstance(neuron_type, LoihiLIF) and discretize:
            nengo_dl.configure_settings(lif_smoothing=net.sigma)

        net.stim = nengo.Node(np.zeros(nx))
        net.ens = nengo.Ensemble(nx,
                                 1,
                                 neuron_type=neuron_type,
                                 gain=nengo.dists.Choice([gain]),
                                 bias=nengo.dists.Choice([bias]))
        nengo.Connection(net.stim, net.ens.neurons, synapse=None)
        net.probe = nengo.Probe(net.ens.neurons)

    rates = dict(
        ref=loihi_rates(neuron_type, net.x, gain, bias, dt=dt).squeeze(axis=1))
    # rates['med'] is an approximation of the smoothed Loihi tuning curve
    if isinstance(neuron_type, LoihiLIF):
        rates['med'] = nengo_rates(nengo.LIF(**lif_kw), net.x, gain,
                                   bias).squeeze(axis=1)
    elif isinstance(neuron_type, LoihiSpikingRectifiedLinear):
        rates['med'] = np.zeros_like(net.j)
        rates['med'][net.j > 0] = (neuron_type.amplitude /
                                   (net.tau_ref1 + 1. / net.j[net.j > 0]))

    return net, rates, lif_kw