def test_nengo_comm_channel_compare(simtype, Simulator, seed, plt, allclose): if simtype == 'simreal': Simulator = lambda *args: nengo_loihi.Simulator(*args, target='simreal') with nengo.Network(seed=seed) as model: u = nengo.Node(lambda t: np.sin(6 * t)) a = nengo.Ensemble(50, 1) b = nengo.Ensemble(50, 1) nengo.Connection(u, a) nengo.Connection(a, b, function=lambda x: x**2, solver=nengo.solvers.LstsqL2(weights=True)) ap = nengo.Probe(a, synapse=0.03) bp = nengo.Probe(b, synapse=0.03) with nengo.Simulator(model) as nengo_sim: nengo_sim.run(1.0) with Simulator(model) as loihi_sim: loihi_sim.run(1.0) plt.subplot(2, 1, 1) plt.plot(nengo_sim.trange(), nengo_sim.data[ap]) plt.plot(loihi_sim.trange(), loihi_sim.data[ap]) plt.subplot(2, 1, 2) plt.plot(nengo_sim.trange(), nengo_sim.data[bp]) plt.plot(loihi_sim.trange(), loihi_sim.data[bp]) assert allclose(loihi_sim.data[ap], nengo_sim.data[ap], atol=0.1, rtol=0.2) assert allclose(loihi_sim.data[bp], nengo_sim.data[bp], atol=0.1, rtol=0.2)
def loihi_factory(model, dt, tau=tau): loihi_model = nengo_loihi.builder.Model(dt=dt) # https://github.com/nengo/nengo-loihi/issues/97 assert loihi_model.decode_tau == 0.005 loihi_model.decode_tau = tau # used by spike generator return nengo_loihi.Simulator(model, model=loihi_model, precompute=True, dt=dt)
def simulate_with_backend(backend, model, duration, timestep): if backend == 'CPU': sim = nengo.Simulator(model, dt=timestep) elif backend == 'GPU': import nengo_ocl import pyopencl as cl # set device to avoid being prompted every time platform = cl.get_platforms() my_gpu_devices = platform[0].get_devices( device_type=cl.device_type.GPU) ctx = cl.Context(devices=my_gpu_devices) sim = nengo_ocl.Simulator(model, context=ctx, dt=timestep) elif backend == 'LOIHI': import nengo_loihi sim = nengo_loihi.Simulator(model, dt=timestep) with sim: sim.run(duration) return sim
# store trained parameters back into the network sim.freeze_params(net) for conn in net.all_connections: conn.synapse = 0.005 if do_training: with nengo_dl.Simulator(net, minibatch_size=minibatch_size) as sim: sim.compile(loss={out_p_filt: classification_accuracy}) print("accuracy w/ synapse: %.2f%%" % sim.evaluate(test_data[inp], {out_p_filt: test_data[out_p_filt]}, verbose=0)["loss"]) n_presentations = 2000 with nengo_loihi.Simulator(net, dt=dt, precompute=False) as sim: # if running on Loihi, increase the max input spikes per step if 'loihi' in sim.sims: sim.sims['loihi'].snip_max_spikes_per_step = 120 # run the simulation on Loihi sim.run(n_presentations * presentation_time) # check classification error step = int(presentation_time / dt) output = sim.data[out_p_filt][step - 1::step] #print ("output", output) error_percentage = 100 * np.mean( np.argmax(output, axis=-1) != test_data[out_p_filt][:n_presentations, -1, 0] ) acc = 100 * np.mean(
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)
# Calculate the error signal with another ensemble error = nengo.Ensemble(100, dimensions=1) # Error = actual - target = post - pre nengo.Connection(post, error) nengo.Connection(pre, error, transform=-1) # Connect the error into the learning rule nengo.Connection(error, conn.learning_rule) stim_p = nengo.Probe(stim) pre_p = nengo.Probe(pre, synapse=0) post_p = nengo.Probe(post, synapse=0) error_p = nengo.Probe(error, synapse=0) with nengo_loihi.Simulator(model) as sim: sim.run(10) t = sim.trange() def plot_decoded(t, data): fig = plt.figure(figsize=(12, 12)) plt.subplot(2, 1, 1) plt.plot(t, data[stim_p].T[0], label="Input") plt.plot(t, data[pre_p].T[0], label="pre") plt.plot(t, data[post_p].T[0], label="post") plt.ylabel("Decoded output") plt.legend(loc="best") plt.subplot(2, 1, 2) plt.plot(t, data[error_p]) plt.ylim(-1, 1)
L1_shape = L1_layer.output_shape[1:] net.config[nengo_converter.layers[L1]. ensemble].block_shape = nengo_loihi.BlockShape((50, ), L1_shape) L2_shape = L2_layer.output_shape[1:] net.config[nengo_converter.layers[L2]. ensemble].block_shape = nengo_loihi.BlockShape((50, ), L2_shape) L3_shape = L3_layer.output_shape[1:] net.config[nengo_converter.layers[L3]. ensemble].block_shape = nengo_loihi.BlockShape((50, ), L3_shape) print_neurons_type(nengo_converter) # build Nengo Loihi Simulator and run network with nengo_loihi.Simulator(net) as loihi_sim: loihi_sim.run(n_test * pres_time) # get output (last timestep of each presentation period) pres_steps = int(round(pres_time / loihi_sim.dt)) output = loihi_sim.data[nengo_output][pres_steps - 1::pres_steps] # compute the Loihi accuracy loihi_predictions = np.argmax(output, axis=-1) correct = test_truth[:n_test, 0, 0] accuracy = 100 * np.mean(loihi_predictions == correct) print("Loihi accuracy: %.2f%%" % accuracy) predicted = np.array(loihi_predictions, dtype=int) correct = np.array(correct, dtype=int)
) elif mode == "run": # this mode uses the Nengo or NengoLoihi simulators, and input images are # presented sequentially to the network, appropriate for spiking neurons images = images.squeeze() def send_image_in(t): # dimensions should be (n_timesteps, image.flatten()) return images[int(t / 0.001) - 1] with sim: sim.freeze_params(net) vision.input.output = send_image_in with net: nengo_loihi.add_params(net) net.config[vision.conv0.ensemble].on_chip = False sim = nengo_loihi.Simulator(net, dt=dt) with sim: sim.run(dt * images.shape[0]) data = sim.data dl_utils.plot_prediction_error( predictions=np.asarray(data[vision.probe_dense]), target_vals=targets, save_name="%s_prediction_error" % mode, show_plot=True, )
def test_conv_connection(channels, Simulator, seed, rng, plt, allclose): # channels_last = True channels_last = False if channels > 1: pytest.xfail("Cannot send population spikes to chip") # 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) test_x = test_x[:, :, None] # single channel input_shape = ImageShape(test_x.shape[0], test_x.shape[1], channels, 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, (0, 2, 3, 1)) # filters last 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(nengo.processes.PresentInput([test_x.ravel()], pres_time), 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.rows * input_shape.cols nengo.Connection(u, a.neurons[:k], transform=1, synapse=None) nengo.Connection(u, a.neurons[k:], transform=-1, synapse=None) filters = np.vstack([filters, -filters]) else: raise ValueError("Test not configured for more than two channels") conv2d_transform = Conv2D.from_kernel(filters, input_shape, strides=strides) 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, default TensorFlow does not support channels first in conv use_nengo_dl = nengo_dl is not None and channels_last ndl_out = np.zeros_like(ref_out) if use_nengo_dl: with nengo_dl.Simulator(model, dt=dt) as sim: sim.run(pres_time) ndl_out = sim.data[bp].mean(axis=0).reshape(output_shape.shape()) with nengo_loihi.Simulator(model, dt=dt, target='simreal') as sim: sim.run(pres_time) real_out = sim.data[bp].mean(axis=0).reshape(output_shape.shape()) with Simulator(model, dt=dt) as sim: sim.run(pres_time) sim_out = sim.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)
def TestSimulator(net, *args, **kwargs): """Simulator constructor to be used in tests""" kwargs.setdefault("target", target) return nengo_loihi.Simulator(net, *args, **kwargs)
net.config[nengo_converter.layers[L4]. ensemble].block_shape = nengo_loihi.BlockShape((2, 2, 64), L4_shape) L5_shape = L5_layer.output_shape[1:] net.config[nengo_converter.layers[L5]. ensemble].block_shape = nengo_loihi.BlockShape((50, ), L5_shape) L6_shape = L6_layer.output_shape[1:] net.config[nengo_converter.layers[L6]. ensemble].block_shape = nengo_loihi.BlockShape((50, ), L6_shape) print_neurons_type(nengo_converter) # build Nengo Loihi Simulator and run network in simulation or on Intel Loihi Hardware with nengo_loihi.Simulator(net, remove_passthrough=False) as loihi_sim: loihi_sim.run(n_test * pres_time) # get output (last timestep of each presentation period) pres_steps = int(round(pres_time / loihi_sim.dt)) output = loihi_sim.data[nengo_output][pres_steps - 1::pres_steps] # compute the Loihi accuracy loihi_predictions = np.argmax(output, axis=-1) correct = test_truth[:n_test, 0, 0] accuracy = 100 * np.mean(loihi_predictions == correct) print("Loihi accuracy: %.2f%%" % accuracy) predicted = np.array(loihi_predictions, dtype=int) correct = np.array(correct, dtype=int)
weights = weights[:-4] net = demo( backend=backend, collect_ground_truth=collect_ground_truth, motor_neuron_type=LoihiSpikingRectifiedLinear(), neural_vision=True, plot_mounted_camera_freq=None, # how often to plot image from cameras weights_name=weights, ) try: if backend == "loihi": sim = nengo_loihi.Simulator( net, target="sim", # set equal to "loihi" to run on Loihi hardware hardware_options=dict(snip_max_spikes_per_step=300), ) print(sim.model.utilization_summary()) elif backend == "cpu": sim = nengo.Simulator(net, progress_bar=False) else: raise Exception("Invalid backend specified") with sim: start_time = timeit.default_timer() sim.run(sim_runtime) print("\nRun time: %.5f\n" % (timeit.default_timer() - start_time)) except ExitSim: pass
def factory(network, dt): return nengo_loihi.Simulator( network, dt=dt, precompute=True, remove_passthrough=True )