Esempio n. 1
0
def test_nengo_dl_noise(neuron_type, seed, plt):
    install_dl_builders()

    net, rates, lif_kw = rate_nengo_dl_net(neuron_type)
    n_noise = 1000  # number of noise samples per x point

    with nengo_dl.Simulator(net, dt=net.dt, minibatch_size=n_noise,
                            seed=seed) as sim:
        input_data = {net.stim: np.tile(net.x[None, None, :], (n_noise, 1, 1))}
        sim.step(input_feeds=input_data,
                 extra_feeds={sim.tensor_graph.signals.training: True})
        y = sim.data[net.probe][:, 0, :]

    ymean = y.mean(axis=0)
    y25 = np.percentile(y, 25, axis=0)
    y75 = np.percentile(y, 75, axis=0)
    dy25 = y25 - rates["ref"]
    dy75 = y75 - rates["ref"]

    # exponential models roughly fitted to 25/75th percentiles
    x1mask = net.x > 1.5
    x1 = net.x[x1mask]
    if isinstance(neuron_type.nengo_dl_noise, AlphaRCNoise):
        exp_model = 0.7 + 2.8 * np.exp(-0.22 * (x1 - 1))
        atol = 0.12 * exp_model.max()
    elif isinstance(neuron_type.nengo_dl_noise, LowpassRCNoise):
        exp_model = 1.5 + 2.2 * np.exp(-0.22 * (x1 - 1))
        atol = 0.2 * exp_model.max()

    rtol = 0.2
    mu_atol = 0.6  # depends on n_noise and variance of noise

    # --- plots
    plt.subplot(211)
    plt.plot(net.x, rates["med"], '--', label='LIF(tau_ref += 0.5*dt)')
    plt.plot(net.x, ymean, label='nengo_dl')
    plt.plot(net.x, y25, ':', label='25th')
    plt.plot(net.x, y75, ':', label='75th')
    plt.plot(net.x, rates["ref"], 'k--', label='LoihiLIF')
    plt.legend()

    plt.subplot(212)
    plt.plot(net.x, ymean - rates["ref"], 'b', label='mean')
    plt.plot(net.x, mu_atol * np.ones_like(net.x), 'b:')
    plt.plot(net.x, -mu_atol * np.ones_like(net.x), 'b:')
    plt.plot(net.x, y25 - rates["ref"], ':', label='25th')
    plt.plot(net.x, y75 - rates["ref"], ':', label='75th')
    plt.plot(x1, exp_model, 'k--')
    plt.plot(x1, exp_model * (1 + rtol) + atol, 'k:')
    plt.plot(x1, exp_model * (1 - rtol) - atol, 'k:')
    plt.plot(x1, -exp_model, 'k--')
    plt.legend()

    assert ymean.shape == rates["ref"].shape
    assert np.allclose(ymean, rates["ref"], atol=mu_atol)
    assert np.allclose(dy25[x1mask], -exp_model, atol=atol, rtol=rtol)
    assert np.allclose(dy75[x1mask], exp_model, atol=atol, rtol=rtol)
Esempio n. 2
0
def test_nengo_dl_neurons(neuron_type, inference_only, Simulator, plt,
                          allclose):
    install_dl_builders()

    dt = 0.0007

    n = 256
    encoders = np.ones((n, 1))
    gain = np.zeros(n)
    if isinstance(neuron_type, nengo.SpikingRectifiedLinear):
        bias = np.linspace(0, 1001, n)
    else:
        bias = np.linspace(0, 30, n)

    with nengo.Network() as model:
        nengo_dl.configure_settings(inference_only=inference_only)

        a = nengo.Ensemble(n,
                           1,
                           neuron_type=neuron_type,
                           encoders=encoders,
                           gain=gain,
                           bias=bias)
        ap = nengo.Probe(a.neurons)

    t_final = 1.0
    with nengo_dl.Simulator(model, dt=dt) as dl_sim:
        dl_sim.run(t_final)

    with Simulator(model, dt=dt) as loihi_sim:
        loihi_sim.run(t_final)

    rates_dlsim = (dl_sim.data[ap] > 0).sum(axis=0) / t_final
    rates_loihisim = (loihi_sim.data[ap] > 0).sum(axis=0) / t_final

    zeros = np.zeros((1, gain.size))
    rates_ref = neuron_type.rates(zeros, gain, bias, dt=dt).squeeze(axis=0)
    plt.plot(bias, rates_loihisim, 'r', label='loihi sim')
    plt.plot(bias, rates_dlsim, 'b-.', label='dl sim')
    plt.plot(bias, rates_ref, 'k--', label='rates_ref')
    plt.legend(loc='best')

    atol = 1. / t_final  # the fundamental unit for our rates
    assert rates_ref.shape == rates_dlsim.shape == rates_loihisim.shape
    assert allclose(rates_dlsim, rates_ref, atol=atol, rtol=0, xtol=1)
    assert allclose(rates_loihisim, rates_ref, atol=atol, rtol=0, xtol=1)
Esempio n. 3
0
def test_nengo_dl_neuron_grads(neuron_type, plt):
    from nengo_extras.neurons import SoftLIFRate
    import tensorflow as tf
    from tensorflow.python.ops import gradient_checker
    install_dl_builders()

    net, rates, lif_kw = rate_nengo_dl_net(neuron_type)
    with nengo_dl.Simulator(net, dt=net.dt) as sim:
        sim.run_steps(1,
                      input_feeds={net.stim: net.x[None, None, :]},
                      extra_feeds={sim.tensor_graph.signals.training: True})
        y = sim.data[net.probe][0]

    # --- compute spiking rates
    n_spike_steps = 1000
    x_spikes = net.x + np.zeros((1, n_spike_steps, 1), dtype=net.x.dtype)
    with nengo_dl.Simulator(net, dt=net.dt) as sim:
        sim.run_steps(n_spike_steps,
                      input_feeds={net.stim: x_spikes},
                      extra_feeds={sim.tensor_graph.signals.training: False})
        y_spikes = sim.data[net.probe]
        y_spikerate = y_spikes.mean(axis=0)

    # --- compute derivatives
    if isinstance(neuron_type, LoihiLIF):
        dy_ref = SoftLIFRate(sigma=net.sigma,
                             **lif_kw).derivative(net.x, net.gain, net.bias)
    else:
        # use the derivative of rates['med'] (the smoothed Loihi tuning curve)
        dy_ref = np.zeros_like(net.j)
        dy_ref[net.j > 0] = (neuron_type.amplitude /
                             (net.j[net.j > 0] * net.tau_ref1 + 1)**2)

    with nengo_dl.Simulator(net, dt=net.dt) as sim:
        n_steps = sim.unroll
        assert n_steps == 1

        inp = sim.tensor_graph.input_ph[net.stim]
        inp_shape = inp.get_shape().as_list()
        inp_shape = [n_steps if s is None else s for s in inp_shape]
        inp_data = np.zeros(inp_shape) + net.x[None, :, None]

        out = sim.tensor_graph.probe_arrays[net.probe] + 0
        out_shape = out.get_shape().as_list()
        out_shape = [n_steps if s is None else s for s in out_shape]

        data = {
            n: np.zeros((sim.minibatch_size, n_steps, n.size_out))
            for n in sim.tensor_graph.invariant_inputs
        }
        data.update({
            p: np.zeros((sim.minibatch_size, n_steps, p.size_in))
            for p in sim.tensor_graph.target_phs
        })
        feed = sim._fill_feed(n_steps, data, training=True)

        with tf.variable_scope(tf.get_variable_scope()) as scope:
            dx, dy = gradient_checker._compute_dx_and_dy(inp, out, out_shape)
            sim.sess.run(
                tf.variables_initializer(
                    scope.get_collection("gradient_vars")))

        with sim.sess.as_default():
            analytic = gradient_checker._compute_theoretical_jacobian(
                inp,
                inp_shape,
                inp_data,
                dy,
                out_shape,
                dx,
                extra_feed_dict=feed)

        dy = np.array(np.diag(analytic))

    dx = net.x[1] - net.x[0]
    dy_est = np.diff(nengo.synapses.Alpha(10).filtfilt(rates["ref"],
                                                       dt=1)) / dx
    x1 = 0.5 * (net.x[:-1] + net.x[1:])

    # --- plots
    plt.subplot(211)
    plt.plot(net.x, rates["med"], '--', label='LIF(tau_ref += 0.5*dt)')
    plt.plot(net.x, y, label='nengo_dl')
    plt.plot(net.x, y_spikerate, label='nengo_dl spikes')
    plt.plot(net.x, rates["ref"], 'k--', label='LoihiLIF')
    plt.legend(loc=4)

    plt.subplot(212)
    plt.plot(x1, dy_est, '--', label='diff(smoothed_y)')
    plt.plot(net.x, dy, label='nengo_dl')
    plt.plot(net.x, dy_ref, 'k--', label='diff(SoftLIF)')
    plt.legend(loc=1)

    np.fill_diagonal(analytic, 0)
    assert np.all(analytic == 0)

    assert np.allclose(y, rates["ref"], atol=1e-3, rtol=1e-5)
    assert np.allclose(dy, dy_ref, atol=1e-3, rtol=1e-5)
    assert np.allclose(y_spikerate, rates["ref"], atol=1, rtol=1e-2)
Esempio n. 4
0
def test_conv_connection(channels, channels_last, Simulator, seed, rng, plt,
                         allclose):
    if channels_last:
        plt.saveas = None
        pytest.xfail("Blocked by CxBase cannot be > 256 bug")

    # load data
    with open(os.path.join(test_dir, 'mnist10.pkl'), 'rb') as f:
        test10 = pickle.load(f)

    test_x = test10[0][0].reshape(28, 28)
    test_x = 1.999 * test_x - 0.999  # range (-1, 1)
    input_shape = nengo_transforms.ChannelShape(
        (test_x.shape + (channels, )) if channels_last else
        ((channels, ) + test_x.shape),
        channels_last=channels_last)

    filters = Gabor(freq=Uniform(0.5, 1)).generate(8, (7, 7), rng=rng)
    filters = filters[None, :, :, :]  # single channel
    filters = np.transpose(filters, (2, 3, 0, 1))
    strides = (2, 2)
    tau_rc = 0.02
    tau_ref = 0.002
    tau_s = 0.005
    dt = 0.001

    neuron_type = LoihiLIF(tau_rc=tau_rc, tau_ref=tau_ref)

    pres_time = 0.1

    with nengo.Network(seed=seed) as model:
        nengo_loihi.add_params(model)

        u = nengo.Node(test_x.ravel(), label="u")

        a = nengo.Ensemble(input_shape.size,
                           1,
                           neuron_type=LoihiSpikingRectifiedLinear(),
                           max_rates=nengo.dists.Choice([40 / channels]),
                           intercepts=nengo.dists.Choice([0]),
                           label='a')
        model.config[a].on_chip = False

        if channels == 1:
            nengo.Connection(u, a.neurons, transform=1, synapse=None)
        elif channels == 2:
            # encode image into spikes using two channels (on/off)
            if input_shape.channels_last:
                nengo.Connection(u, a.neurons[0::2], transform=1, synapse=None)
                nengo.Connection(u,
                                 a.neurons[1::2],
                                 transform=-1,
                                 synapse=None)
            else:
                k = input_shape.spatial_shape[0] * input_shape.spatial_shape[1]
                nengo.Connection(u, a.neurons[:k], transform=1, synapse=None)
                nengo.Connection(u, a.neurons[k:], transform=-1, synapse=None)

            filters = np.concatenate([filters, -filters], axis=2)
        else:
            raise ValueError("Test not configured for more than two channels")

        conv2d_transform = nengo_transforms.Convolution(
            8,
            input_shape,
            strides=strides,
            kernel_size=(7, 7),
            channels_last=channels_last,
            init=filters)

        output_shape = conv2d_transform.output_shape

        gain, bias = neuron_type.gain_bias(max_rates=100, intercepts=0)
        gain = gain * 0.01  # account for `a` max_rates
        b = nengo.Ensemble(output_shape.size,
                           1,
                           neuron_type=neuron_type,
                           gain=nengo.dists.Choice([gain[0]]),
                           bias=nengo.dists.Choice([bias[0]]),
                           label='b')
        nengo.Connection(a.neurons,
                         b.neurons,
                         synapse=tau_s,
                         transform=conv2d_transform)

        bp = nengo.Probe(b.neurons)

    with nengo.Simulator(model, dt=dt, optimize=False) as sim:
        sim.run(pres_time)
    ref_out = sim.data[bp].mean(axis=0).reshape(output_shape.shape)

    # Currently, non-gpu TensorFlow does not support channels first in conv
    use_nengo_dl = HAS_DL and channels_last
    ndl_out = np.zeros_like(ref_out)
    if use_nengo_dl:
        with nengo_dl.Simulator(model, dt=dt) as sim_dl:
            sim_dl.run(pres_time)
        ndl_out = sim_dl.data[bp].mean(axis=0).reshape(output_shape.shape)

    with nengo_loihi.Simulator(model, dt=dt, target='simreal') as sim_real:
        sim_real.run(pres_time)
    real_out = sim_real.data[bp].mean(axis=0).reshape(output_shape.shape)

    with Simulator(model, dt=dt) as sim_loihi:
        if "loihi" in sim_loihi.sims:
            sim_loihi.sims["loihi"].snip_max_spikes_per_step = 800
        sim_loihi.run(pres_time)
    sim_out = sim_loihi.data[bp].mean(axis=0).reshape(output_shape.shape)

    if not output_shape.channels_last:
        ref_out = np.transpose(ref_out, (1, 2, 0))
        ndl_out = np.transpose(ndl_out, (1, 2, 0))
        real_out = np.transpose(real_out, (1, 2, 0))
        sim_out = np.transpose(sim_out, (1, 2, 0))

    out_max = max(ref_out.max(), sim_out.max())

    # --- plot results
    rows = 2
    cols = 3

    ax = plt.subplot(rows, cols, 1)
    imshow(test_x, vmin=0, vmax=1, ax=ax)

    ax = plt.subplot(rows, cols, 2)
    tile(np.transpose(filters[0], (2, 0, 1)), cols=8, ax=ax)

    ax = plt.subplot(rows, cols, 3)
    plt.hist(ref_out.ravel(), bins=31)
    plt.hist(sim_out.ravel(), bins=31)

    ax = plt.subplot(rows, cols, 4)
    tile(np.transpose(ref_out, (2, 0, 1)), vmin=0, vmax=out_max, cols=8, ax=ax)

    ax = plt.subplot(rows, cols, 5)
    tile(np.transpose(ndl_out, (2, 0, 1)), vmin=0, vmax=out_max, cols=8, ax=ax)

    ax = plt.subplot(rows, cols, 6)
    tile(np.transpose(sim_out, (2, 0, 1)), vmin=0, vmax=out_max, cols=8, ax=ax)

    if use_nengo_dl:
        assert allclose(ndl_out, ref_out, atol=1e-5, rtol=1e-5)
    assert allclose(real_out, ref_out, atol=1, rtol=1e-3)
    assert allclose(sim_out, ref_out, atol=10, rtol=1e-3)
Esempio n. 5
0
def test_nengo_dl_noise(neuron_type, seed, plt):
    tf = pytest.importorskip("tensorflow")

    install_dl_builders()

    net, rates, lif_kw = rate_nengo_dl_net(neuron_type)
    n_noise = 1000  # number of noise samples per x point

    with tf.keras.backend.learning_phase_scope(1):
        with nengo_dl.Simulator(net,
                                dt=net.dt,
                                minibatch_size=n_noise,
                                seed=seed) as sim:
            input_data = {
                net.stim: np.tile(net.x[None, None, :], (n_noise, 1, 1))
            }
            sim.step(data=input_data)
            y = sim.data[net.probe][:, 0, :]

    ymean = y.mean(axis=0)
    y25 = np.percentile(y, 25, axis=0)
    y75 = np.percentile(y, 75, axis=0)
    dy25 = y25 - rates["ref"]
    dy75 = y75 - rates["ref"]

    # exponential models roughly fitted to 25/75th percentiles
    x1mask = net.x > 1.5
    x1 = net.x[x1mask]
    if isinstance(neuron_type.nengo_dl_noise, AlphaRCNoise):
        exp_model = 0.7 + 2.8 * np.exp(-0.22 * (x1 - 1))
        atol = 0.12 * exp_model.max()
    elif isinstance(neuron_type.nengo_dl_noise, LowpassRCNoise):
        exp_model = 1.5 + 2.2 * np.exp(-0.22 * (x1 - 1))
        atol = 0.2 * exp_model.max()

    rtol = 0.2
    mu_atol = 0.6  # depends on n_noise and variance of noise

    # --- plots
    plt.subplot(211)
    plt.plot(net.x, rates["med"], "--", label="LIF(tau_ref += 0.5*dt)")
    plt.plot(net.x, ymean, label="nengo_dl")
    plt.plot(net.x, y25, ":", label="25th")
    plt.plot(net.x, y75, ":", label="75th")
    plt.plot(net.x, rates["ref"], "k--", label="LoihiLIF")
    plt.legend()

    plt.subplot(212)
    plt.plot(net.x, ymean - rates["ref"], "b", label="mean")
    plt.plot(net.x, mu_atol * np.ones_like(net.x), "b:")
    plt.plot(net.x, -mu_atol * np.ones_like(net.x), "b:")
    plt.plot(net.x, y25 - rates["ref"], ":", label="25th")
    plt.plot(net.x, y75 - rates["ref"], ":", label="75th")
    plt.plot(x1, exp_model, "k--")
    plt.plot(x1, exp_model * (1 + rtol) + atol, "k:")
    plt.plot(x1, exp_model * (1 - rtol) - atol, "k:")
    plt.plot(x1, -exp_model, "k--")
    plt.legend()

    assert ymean.shape == rates["ref"].shape
    assert np.allclose(ymean, rates["ref"], atol=mu_atol)
    assert np.allclose(dy25[x1mask], -exp_model, atol=atol, rtol=rtol)
    assert np.allclose(dy75[x1mask], exp_model, atol=atol, rtol=rtol)
Esempio n. 6
0
def test_nengo_dl_neuron_grads(neuron_type, plt):
    tf = pytest.importorskip("tensorflow")

    install_dl_builders()

    net, rates, lif_kw = rate_nengo_dl_net(neuron_type)

    with tf.keras.backend.learning_phase_scope(1):
        with nengo_dl.Simulator(net, dt=net.dt) as sim:
            sim.run_steps(1, data={net.stim: net.x[None, None, :]})
            y = sim.data[net.probe][0]

    # --- compute spiking rates
    n_spike_steps = 1000
    x_spikes = net.x + np.zeros((1, n_spike_steps, 1), dtype=net.x.dtype)
    with nengo_dl.Simulator(net, dt=net.dt) as sim:
        sim.run_steps(
            n_spike_steps,
            data={net.stim: x_spikes},
        )
        y_spikes = sim.data[net.probe]
        y_spikerate = y_spikes.mean(axis=0)

    # --- compute derivatives
    if isinstance(neuron_type, LoihiLIF):
        dy_ref = SoftLIFRate(sigma=net.sigma,
                             **lif_kw).derivative(net.x, net.gain, net.bias)
    else:
        # use the derivative of rates['med'] (the smoothed Loihi tuning curve)
        dy_ref = np.zeros_like(net.j)
        dy_ref[net.j > 0] = (neuron_type.amplitude /
                             (net.j[net.j > 0] * net.tau_ref1 + 1)**2)

    with nengo_dl.Simulator(net, dt=net.dt) as sim:
        assert sim.unroll == 1
        # note: not actually checking gradients, just using this to get the
        # gradients
        analytic = sim.check_gradients(inputs=[net.x[None, None, :]],
                                       atol=1e10)[net.probe]["analytic"][0]

        dy = np.diagonal(analytic.copy())

    dx = net.x[1] - net.x[0]
    dy_est = np.diff(nengo.synapses.Alpha(10).filtfilt(rates["ref"],
                                                       dt=1)) / dx
    x1 = 0.5 * (net.x[:-1] + net.x[1:])

    # --- plots
    plt.subplot(211)
    plt.plot(net.x, rates["med"], "--", label="LIF(tau_ref += 0.5*dt)")
    plt.plot(net.x, y, label="nengo_dl")
    plt.plot(net.x, y_spikerate, label="nengo_dl spikes")
    plt.plot(net.x, rates["ref"], "k--", label="LoihiLIF")
    plt.legend(loc=4)

    plt.subplot(212)
    plt.plot(x1, dy_est, "--", label="diff(smoothed_y)")
    plt.plot(net.x, dy, label="nengo_dl")
    plt.plot(net.x, dy_ref, "k--", label="diff(SoftLIF)")
    plt.legend(loc=1)

    np.fill_diagonal(analytic, 0)
    assert np.all(analytic == 0)

    assert y.shape == rates["ref"].shape
    assert np.allclose(y, rates["ref"], atol=1e-3, rtol=1e-5)
    assert np.allclose(dy, dy_ref, atol=1e-3, rtol=1e-5)
    assert np.allclose(y_spikerate, rates["ref"], atol=1, rtol=1e-2)