def test_static_neurons(plt, rng, neuron_type): with nengo.Network(seed=0) as model: u = nengo.Node(nengo.processes.WhiteNoise(scale=False)) a = nengo.Ensemble(31, 1, neuron_type=neuron_type) nengo.Connection(u, a, synapse=None) xp = nengo.Probe(a.neurons, "input") yp = nengo.Probe(a.neurons) with nengo_ocl.Simulator(model) as sim: sim.run(1.0) x = sim.data[xp].ravel() y = sim.data[yp].ravel() r = neuron_type.rates(x, 1.0, 0.0).ravel() # --- plot (i, ) = ((x > -10) & (x < 10)).nonzero() n_show = 100 if len(i) > n_show: i = rng.choice(i, size=n_show, replace=False) plt.plot(x[i], r[i], "kx") plt.plot(x[i], y[i], ".") assert np.allclose(y, r, atol=1e-4, rtol=1e-3)
def test_greedy_planner_feedforward(): model, _ = feedforward_network() with nengo_ocl.Simulator(model, planner=greedy_planner) as sim: check_op_groups(sim) assert count_op_group(sim, nengo.builder.neurons.SimNeurons) == 1 assert len(sim.op_groups) == 10
def test_reset(): seed = 3 class CustomProcess(nengo.Process): # pylint: disable=missing-class-docstring def make_state(self, shape_in, shape_out, dt, dtype=None): return {} def make_step(self, shape_in, shape_out, dt, rng, state): def step(t): return rng.uniform(size=shape_out).ravel() return step with nengo.Network() as model: u = nengo.Node(CustomProcess()) up = nengo.Probe(u) with nengo_ocl.Simulator(model, seed=seed) as sim: sim.run_steps(10) ua = np.array(sim.data[up]) sim.reset() sim.run_steps(10) ub = np.array(sim.data[up]) assert np.allclose(ua, ub)
def test_multidotinc_compress(monkeypatch): if nengo.version.version_info < (2, 3, 1): # LEGACY # Nengo versions <= 2.3.0 have more stringent op validation which # required PreserveValue. That's been removed, so the strict # validation causes this test to fail despite it working. monkeypatch.setattr(nengo.utils.simulator, "validate_ops", lambda *args: None) a = Signal([0, 0]) b = Signal([0, 0]) A = Signal([[1, 2], [0, 1]]) B = Signal([[2, 1], [-1, 1]]) x = Signal([1, 1]) y = Signal([1, -1]) m = Model(dt=0) m.operators += [Reset(a), DotInc(A, x, a), DotInc(B, y, a)] m.operators += [DotInc(A, y, b), DotInc(B, x, b)] with nengo_ocl.Simulator(None, model=m) as sim: sim.step() assert np.allclose(sim.signals[a], [4, -1]) assert np.allclose(sim.signals[b], [2, -1]) sim.step() assert np.allclose(sim.signals[a], [4, -1]) assert np.allclose(sim.signals[b], [4, -2])
def test_error_on_version_in_blacklist(monkeypatch): with nengo.Network() as model: nengo.Ensemble(10, 1) monkeypatch.setattr(nengo.version, "version_info", (2, 1, 1)) with pytest.raises(ValueError): with nengo_ocl.Simulator(model): pass
def test_warn_on_future_version(monkeypatch): with nengo.Network() as model: nengo.Ensemble(10, 1) future_version = tuple(v + 1 for v in latest_nengo_version_info) monkeypatch.setattr(nengo.version, "version_info", future_version) with pytest.warns(UserWarning): with nengo_ocl.Simulator(model): pass
def prepare_sim(seed=None): print '---- BUILDING SIMULATOR ----' global sim print('\t' + str(sum(ens.n_neurons for ens in model.all_ensembles)) + ' neurons') print('\t' + str(len(vocab_concepts.keys)) + ' concepts') start = time.time() if ocl: sim = nengo_ocl.Simulator(model, context=ctx) else: sim = nengo.Simulator(model) print('\n ---- DONE in ' + str(round(time.time() - start, 2)) + ' seconds ----\n')
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
def compare_backends(ctx, batch, n_neurons): load = ctx.obj["load"] reps = ctx.obj["reps"] device = ctx.obj["device"] save = ctx.obj["save"] bench_names = ["integrator", "cconv", "basal_ganglia", "pes"] n_range = [n_neurons] d_range = [64, 128, 192] neuron_types = [nengo.RectifiedLinear()] backends = ["nengo_dl", "nengo_ocl", "nengo"] sim_time = 5.0 params = list( itertools.product(bench_names, n_range, d_range, neuron_types, backends)) if load: with open("compare_backends_%d_data_saved.pkl" % batch, "rb") as f: results = pickle.load(f) else: results = [{ "times": [], "benchmark": bench, "n_neurons": n_neurons, "dimensions": dimensions, "neuron_type": neuron_type, "backend": backend } for bench, n_neurons, dimensions, neuron_type, backend in params] if reps > 0: for i, (bench, neurons_per_ens, dimensions, neuron_type, backend) in enumerate(params): print("%d/%d: %s %s %s %s %s" % (i + 1, len(params), bench, neurons_per_ens, dimensions, neuron_type, backend)) net = getattr(benchmarks, bench)(dimensions=dimensions, neurons_per_d=neurons_per_ens // dimensions, neuron_type=neuron_type) with net: nengo_dl.configure_settings(inference_only=True) if "nengo_dl" in backend: sim = nengo_dl.Simulator(net, unroll_simulation=25, minibatch_size=batch, device=device, progress_bar=False) elif backend == "nengo": sim = nengo.Simulator(net, progress_bar=False, optimize=True) elif backend == "nengo_ocl": import nengo_ocl sim = nengo_ocl.Simulator(net, progress_bar=False) with sim: # run once to eliminate startup overhead sim.run(0.1, progress_bar=False) for _ in range(reps): start = time.time() for b in range(1 if "nengo_dl" in backend else batch): if b > 0: sim.reset() sim.run(sim_time, progress_bar=False) results[i]["times"].append( (time.time() - start) / sim_time) print(" ", min(results[i]["times"]), max(results[i]["times"]), np.mean(results[i]["times"])) with open("compare_backends_%d_data.pkl" % batch, "wb") as f: pickle.dump(results, f) # plotting subplots = int(np.ceil(np.sqrt(len(bench_names)))) f, axes = plt.subplots(subplots, subplots, sharey=True, sharex=False, figsize=(5 * subplots, 5 * subplots), gridspec_kw={ "hspace": 0.2, "top": 0.95, "bottom": 0.05, "left": 0.07, "right": 0.95 }) n_bars = len(d_range) neuron_type = nengo.RectifiedLinear() colours = plt.rcParams["axes.prop_cycle"].by_key()["color"] y_max = 2.5 * batch for k, m in enumerate(bench_names): subplot_idx = (k // subplots, k % subplots) x_pos = np.arange(n_bars) for j, b in enumerate(backends): bottoms = np.zeros(n_bars) c = 0 for n in n_range: data = np.asarray([ bootstrap_ci(t) for t in filter_results(results, benchmark=m, neuron_type=neuron_type, n_neurons=n, backend=b) ]) axes[subplot_idx].bar( x_pos, data[:, 0], yerr=abs(np.transpose(data[:, 1:] - data[:, [0]])), width=0.5, bottom=bottoms, color=colours[(j + 1) % len(backends)]) for i, d in enumerate(data[:, 0]): if d > y_max: axes[subplot_idx].annotate("%.1f" % d, (x_pos[i], y_max * 0.9), ha="center", va="center", rotation="vertical", color="white") bottoms += data[:, 0] c += 1 x_pos += n_bars + 1 axes[subplot_idx].set_title("%s" % m) if k == 0 and len(n_range) > 1: axes[subplot_idx].legend(["N=%d" % n for n in n_range]) axes[subplot_idx].set_xticks( np.concatenate([ np.arange(i * (n_bars + 1), i * (n_bars + 1) + n_bars) for i in range(len(backends)) ])) axes[subplot_idx].set_xticklabels( [t for _ in range(len(backends)) for t in d_range]) for i, b in enumerate(backends): axes[subplot_idx].annotate( b, (((n_bars - 1) / 2 + (n_bars + 1) * i + 1) / ((n_bars + 1) * len(backends)), -0.1), xycoords="axes fraction", ha="center") axes[subplot_idx].set_ylim([0, y_max]) axes[subplot_idx].set_xlim([-1, (n_bars + 1) * len(backends) - 1]) if k % subplots == 0: axes[subplot_idx].set_ylabel("real time / simulated time") if save: plt.savefig("compare_backends_%d.%s" % (batch, save))
learnedWeightsProbe = nengo.Probe(\ plasticConnEE,'weights',sample_every=weightdt,label='EEweights') # feedforward weights probe learnedInWeightsProbe = nengo.Probe(\ InEtoE,'weights',sample_every=weightdt,label='InEEweights') if initLearned: pass ## if not initLearned, we don't care about matching weights to ideal ## this reduces a large set of connections, esp if Nexc is large #model.connections.remove(EtoEfake) ################################# ### Run Nengo network ################################# if OCL: sim = nengo_ocl.Simulator(mainModel, dt) else: sim = nengo.Simulator(mainModel, dt) Eencoders = sim.data[ratorOut].encoders # randomize decoders ################################# ### important to initialize weights before, ### so that they can be overridden after if continueLearning or testLearned ################################# ################################# ### load previously learned weights, if requested and file exists ################################# if errorLearning and (continueLearning or testLearned) and isfile(weightsLoadFileName): print('loading previously learned weights for ratorOut from',
def run_trial(params, ens_seed=1337, train_trials_seed=1337, test_trials_seed=1234, train_trials_per_cond=5, test_trials_per_cond=5, eval_strict=True, cl_platform_vendor='none'): cl_ctx = None for plat in cl.get_platforms(): if plat.vendor.upper().startswith(cl_platform_vendor.upper()): cl_ctx = cl.Context(dev_type=cl.device_type.ALL, properties=[(cl.context_properties.PLATFORM, plat)]) break if cl_ctx is None: cl_ctx = cl.create_some_context(interactive=False) # Create model for training srate = 1000 factory_kwargs = { 'q': int(params['q']), 'theta': params['theta'], 'tau': params['tau'], 'n_neurons': int(params['n_neurons']), 'max_rates': params['max_rates'], 'dales_law': params['dales_law'], 'hetero_tau': params['hetero_tau'], 'ssp_dim': 0 } # Generate training data train_model, train_probes = make_lmu_dms( seed=ens_seed, out_transform=None, n_trials_per_cond=train_trials_per_cond, trial_seed=train_trials_seed, **factory_kwargs) n_train_trials = train_trials_per_cond * 8 * 2 # 16 conditions with nengo_ocl.Simulator(train_model, context=cl_ctx) as train_sim: train_sim.run(6 * n_train_trials) # 6 seconds per trial # Solve for weights - Data are generally too large for nengo's filt so we slice # them to only evaluate specific ranges if eval_strict: eval_ranges = [(0, 0.25), (0.75, 1.75), (2.75, 3.75), (4.75, 6.0)] else: eval_ranges = [(1.0, 1.5), (5.0, 6.0)] trial_tvec = np.arange(0, 6.0, 1 / srate) b_eval = np.zeros(trial_tvec.shape, dtype=bool) for t_win in eval_ranges: b_eval = np.logical_or( b_eval, np.logical_and(t_win[0] <= trial_tvec, trial_tvec < t_win[1])) b_eval = np.tile(b_eval, n_train_trials) filt = nengo.synapses.Lowpass(0.01) Y = filt.filt(train_sim.data[train_probes['ideal']][b_eval]) A = filt.filt(train_sim.data[train_probes['ensemble']][b_eval]) D, info = nengo.solvers.LstsqL2(solver=lss.LSMRScipy())(A, Y) Y = A = None # Create a new model with the learned weights test_model, test_probes = make_lmu_dms( seed=ens_seed, out_transform=D.T, n_trials_per_cond=test_trials_per_cond, trial_seed=test_trials_seed, **factory_kwargs) # Run test simulation - break after N trials to report cumulative accuracy def get_labels_from_sim(sim, n_trials): samps = 6 * srate * n_trials Y = sim.data[test_probes['ideal']][-samps:] A = sim.data[test_probes['output']][-samps:] b_slice = Y != 0 label = Y[b_slice].reshape(n_trials, -1)[:, 0] score = np.mean(A[b_slice].reshape(n_trials, -1), axis=-1) return label, score total_test_trials = test_trials_per_cond * 8 * 2 test_sim = nengo_ocl.Simulator(test_model, context=cl_ctx) test_ix = min(20, total_test_trials) test_sim.run(6 * test_ix) labels, scores = get_labels_from_sim(test_sim, test_ix) trial_step = 10 while test_ix < total_test_trials: test_sim.run(6 * min(trial_step, total_test_trials - test_ix)) label, score = get_labels_from_sim(test_sim, trial_step) labels = np.append(labels, label) scores = np.append(scores, score) fpr, tpr, thresholds = metrics.roc_curve(labels, scores) test_auc = metrics.auc(fpr, tpr) nni.report_intermediate_result(test_auc) test_ix += trial_step test_sim.close() logger.debug('test_sim.close() returned.') logger.debug('Final result is: %d', test_auc) nni.report_final_result(test_auc) logger.debug('Sent final result. Trial complete.')
xencp = nengo.Probe(xenc.output, sample_every=pdt) # --- learners learners = [] # learners.append(ShallowNetwork(xenc.output, ystar, weights, **network_args)) # learners.append(FASkipNetwork(xenc.output, ystar, weights, **fa_args)) learners.append(FATwoStepNetwork(xenc.output, ystar, weights, **fa_args)) # fa2 = FATwoStepNetwork(xenc.output, ystar, weights, **fa_args) # fa2.hps = [nengo.Probe(h.neurons) for h in fa2.layers] # learners.append(fa2) # with nengo.Simulator(model, optimize=False) as sim: import nengo_ocl # with nengo_ocl.Simulator(model) as sim: with nengo_ocl.Simulator(model, progress_bar=False) as sim: sim.run(t1) Ttestrms = rms(Ttest, axis=1).mean() XEtest = xenc.encode(Xtest, sim=sim) for learner in learners: learner.Ttest = learner.forward(sim, XEtest) e = (np.argmax(learner.Ttest, axis=1) != Ytest).mean() print("%s: %0.2e" % (learner, 100 * e)) sim.run(t2 - t1) # --- save results s_sizes = '-'.join('%d' % s for s in [din] + dhids + [dout]) s_now = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") filename = 'results/online_mnist_%s_t=%s_eta=%0.1e_%s.npz' % (
def __init__(self, n_input, n_output, n_neurons=1000, seed=1, pes_learning_rate=1e-6, intercepts=(0.5, 1.0), weights_file=None, backend='nengo', **kwargs): self.input_signal = np.zeros(n_input) self.training_signal = np.zeros(n_output) self.output = np.zeros(n_output) if weights_file is None: weights_file = [''] nengo_model = nengo.Network(seed=seed) with nengo_model: def input_signals_func(t): """ Get the input and -1 * training signal """ return np.hstack([self.input_signal, -self.training_signal]) input_signals = nengo.Node(input_signals_func, size_out=n_input + n_output) # make the adaptive population output accessible def output_func(t, x): """ stores the adaptive output for use in control() """ self.output = np.copy(x) output = nengo.Node(output_func, size_in=n_output, size_out=0) # specify intercepts such that neurons are # active throughout the whole range specified intercepts = AreaIntercepts(dimensions=n_input, base=nengo.dists.Uniform( intercepts[0], intercepts[1])) adapt_ens = nengo.Ensemble(n_neurons=n_neurons, dimensions=n_input, intercepts=intercepts, **kwargs) try: # if the NengoLib is installed, use it # to optimize encoder placement import nengolib adapt_ens.encoders = (nengolib.stats.ScatteredHypersphere( surface=True)) print('NengoLib used to optimize encoders placement') except ImportError: print('Nengo lib not installed, encoder ' + 'placement will be sub-optimal.') # hook up input signal to adaptive population to provide context nengo.Connection(input_signals[:n_input], adapt_ens, synapse=0.005) # load weights from file if they exist, otherwise use zeros if os.path.isfile('%s' % weights_file): transform = np.load(weights_file)['weights'][-1][0] print('Loading weights: \n', transform) print('Loaded weights all zeros: ', np.allclose(transform, 0)) else: print('No weights found, starting with zeros') transform = np.zeros((adapt_ens.n_neurons, n_output)).T # set up learning connections if backend == 'nengo_spinnaker': conn_learn = nengo.Connection( adapt_ens, output, learning_rule_type=nengo.PES(pes_learning_rate), solver=DummySolver(transform.T)) else: conn_learn = nengo.Connection( adapt_ens.neurons, output, learning_rule_type=nengo.PES(pes_learning_rate), transform=transform) # hook up the training signal to the learning rule nengo.Connection(input_signals[n_input:], conn_learn.learning_rule, synapse=0.01) nengo.cache.DecoderCache().invalidate() if backend == 'nengo': self.sim = nengo.Simulator(nengo_model, dt=.001) elif backend == 'nengo_ocl': try: import nengo_ocl except ImportError: raise Exception('Nengo OCL not installed, ' + 'cannot use this backend.') import pyopencl as cl # Here, the context would be to use all devices from platform [0] ctx = cl.Context(cl.get_platforms()[0].get_devices()) self.sim = nengo_ocl.Simulator(nengo_model, context=ctx, dt=.001) elif backend == 'nengo_spinnaker': try: import nengo_spinnaker except ImportError: raise Exception('Nengo SpiNNaker not installed, ' + 'cannot use this backend.') self.sim = nengo_spinnaker.Simulator(nengo_model) # start running the spinnaker model self.sim.async_run_forever() else: raise Exception('Invalid backend specified') self.backend = backend
def ocl_only_sim(*args, **kwargs): return nengo_ocl.Simulator(*args, context=ctx, if_python_code="error", **kwargs)
def run_model(model, mpi_savename, mpi_saveext, runtime, make_probes, probe_cfg, cur_dir, probe_anim_cfg, anim_probe_data_filename): # ----- Spaun simulation build ----- print "START BUILD" timestamp = time.time() if args.nengo_gui: # Set environment variables (for nengo_gui) if cfg.use_opencl: os.environ['PYOPENCL_CTX'] = '%s:%s' % (args.ocl_platform, args.ocl_device) print "STARTING NENGO_GUI" import nengo_gui nengo_gui.GUI(__file__, model=model, locals=locals(), interactive=False).start() print "NENGO_GUI STOPPED" sys.exit() if cfg.use_opencl: import pyopencl as cl import nengo_ocl print "------ OCL ------" print "AVAILABLE PLATFORMS:" print ' ' + '\n '.join(map(str, cl.get_platforms())) pltf = cl.get_platforms()[args.ocl_platform] print "USING PLATFORM:" print ' ' + str(pltf) print "AVAILABLE DEVICES:" print ' ' + '\n '.join(map(str, pltf.get_devices())) if args.ocl_device >= 0: ctx = cl.Context([pltf.get_devices()[args.ocl_device]]) print "USING DEVICE:" print ' ' + str(pltf.get_devices()[args.ocl_device]) else: ctx = cl.Context(pltf.get_devices()) print "USING DEVICES:" print ' ' + '\n '.join(map(str, pltf.get_devices())) sim = nengo_ocl.Simulator(model, dt=cfg.sim_dt, context=ctx, profiling=args.ocl_profile) elif cfg.use_mpi: import nengo_mpi mpi_savefile = \ ('+'.join([cfg.get_probe_data_filename(mpi_savename)[:-4], ('%ip' % args.mpi_p if not args.mpi_p_auto else 'autop'), '%0.2fs' % experiment.get_est_simtime()]) + '.' + mpi_saveext) mpi_savefile = os.path.join(cfg.data_dir, mpi_savefile) print "USING MPI - Saving to: %s" % (mpi_savefile) if args.mpi_p_auto: assignments = {} for n, module in enumerate(model.modules): assignments[module] = n sim = nengo_mpi.Simulator(model, dt=cfg.sim_dt, assignments=assignments, save_file=mpi_savefile) else: partitioner = nengo_mpi.Partitioner(args.mpi_p) sim = nengo_mpi.Simulator(model, dt=cfg.sim_dt, partitioner=partitioner, save_file=mpi_savefile) else: sim = nengo.Simulator(model, dt=cfg.sim_dt) t_build = time.time() - timestamp timestamp = time.time() print "BUILD FINISHED - build time: %fs" % t_build # ----- Spaun simulation run ----- experiment.reset() if cfg.use_opencl or cfg.use_ref: print "START SIM - est_runtime: %f" % runtime sim.run(runtime) # Close output logging file logger.close() if args.ocl_profile: sim.print_plans() sim.print_profiling() t_simrun = time.time() - timestamp print "MODEL N_NEURONS: %i" % (get_total_n_neurons(model)) print "FINISHED! - Build time: %fs, Sim time: %fs" % (t_build, t_simrun) else: print "MODEL N_NEURONS: %i" % (get_total_n_neurons(model)) print "FINISHED! - Build time: %fs" % (t_build) if args.mpi_compress_save: import gzip print "COMPRESSING net file to '%s'" % (mpi_savefile + '.gz') with open(mpi_savefile, 'rb') as f_in: with gzip.open(mpi_savefile + '.gz', 'wb') as f_out: f_out.writelines(f_in) os.remove(mpi_savefile) print "UPLOAD '%s' to MPI cluster and decompress to run" % \ (mpi_savefile + '.gz') else: print "UPLOAD '%s' to MPI cluster to run" % mpi_savefile t_simrun = -1 # ----- Generate debug printouts ----- n_bytes_ev = 0 n_bytes_gain = 0 n_bytes_bias = 0 n_ens = 0 for ens in sim.model.toplevel.all_ensembles: n_bytes_ev += sim.model.params[ens].eval_points.nbytes n_bytes_gain += sim.model.params[ens].gain.nbytes n_bytes_bias += sim.model.params[ens].bias.nbytes n_ens += 1 if args.debug: print("## DEBUG: num bytes used for eval points: %s B" % ("{:,}".format(n_bytes_ev))) print("## DEBUG: num bytes used for gains: %s B" % ("{:,}".format(n_bytes_gain))) print("## DEBUG: num bytes used for biases: %s B" % ("{:,}".format(n_bytes_bias))) print("## DEBUG: num ensembles: %s" % n_ens) # ----- Close simulator ----- if hasattr(sim, 'close'): sim.close() # ----- Write probe data to file ----- if make_probes and not cfg.use_mpi: print "WRITING PROBE DATA TO FILE" probe_cfg.write_simdata_to_file(sim, experiment) if args.showgrph: subprocess_call_list = [ "python", os.path.join(cur_dir, 'disp_probe_data.py'), '"' + cfg.probe_data_filename + '"', '--data_dir', '"' + cfg.data_dir + '"', '--showgrph' ] # Log subprocess call logger.write("\n# " + " ".join(subprocess_call_list)) # Open subprocess print "CALLING: %s" % (" ".join(subprocess_call_list)) import subprocess subprocess.Popen(subprocess_call_list) if (args.showanim or args.showiofig or args.probeio) and not cfg.use_mpi: print "WRITING ANIMATION PROBE DATA TO FILE" probe_anim_cfg.write_simdata_to_file(sim, experiment) if args.showanim or args.showiofig: subprocess_call_list = [ "python", os.path.join(cur_dir, 'disp_probe_data.py'), '"' + anim_probe_data_filename + '"', '--data_dir', '"' + cfg.data_dir + '"' ] if args.showanim: subprocess_call_list += ['--showanim'] if args.showiofig: subprocess_call_list += ['--showiofig'] # Log subprocess call logger.write("\n# " + " ".join(subprocess_call_list)) # Open subprocess print "CALLING: %s" % (" ".join(subprocess_call_list)) import subprocess subprocess.Popen(subprocess_call_list) # ----- Write runtime data ----- runtime_filename = os.path.join(cfg.data_dir, 'runtimes.txt') rt_file = open(runtime_filename, 'a') rt_file.write('# ---------- TIMESTAMP: %i -----------\n' % timestamp) rt_file.write( 'Backend: %s | Num neurons: %i | Tag: %s | Seed: %i\n' % (cfg.backend, get_total_n_neurons(model), args.tag, cfg.seed)) if args.config is not None: rt_file.write('Config options: %s\n' % (str(args.config))) rt_file.write('Build time: %fs | Model sim time: %fs | ' % (t_build, runtime)) rt_file.write('Sim wall time: %fs\n' % (t_simrun)) rt_file.close() # ----- Cleanup ----- model = None sim = None probe_data = None
def run(loadfile, savefile=None, histload=None, count_spikes=False, n_max=None, backend='nengo', ocl_profile=False, presentation_time=None, synapse_type=None, synapse_tau=None): if backend == 'nengo': Simulator = nengo.Simulator elif backend == 'nengo_ocl': import nengo_ocl Simulator = nengo_ocl.Simulator else: raise ValueError("Unsupported backend %r" % backend) layers, data, dp = load_network(loadfile, sort_layers=True) hists = np.load(histload) if histload is not None else {} if 0: # use fixed point weights for layer in layers.values(): round_layer(layer, 2**8, clip_percent=0) # --- build network in Nengo network = nengo.Network(seed=0) # seed not used, but just in case synapse = None if synapse_type == 'lowpass': synapse = nengo.synapses.Lowpass(synapse_tau) elif synapse_type == 'alpha': synapse = nengo.synapses.Alpha(synapse_tau) else: raise ValueError("synapse type: %r" % synapse_type) network.config[nengo.Connection].synapse = synapse outputs = build_target_layer( 'logprob', layers, data, network, hists=hists, pt=presentation_time) # test whole network with network: # xp = nengo.Probe(outputs['data'], synapse=None) yp = nengo.Probe(outputs['probs'], synapse=None) # zp = nengo.Probe(outputs['logprob'], synapse=None) if count_spikes: spikes_p = OrderedDict( (name, nengo.Probe(outputs[name])) for name in layers if layers[name]['type'] == 'neuron') if ocl_profile: # profile import nengo_ocl with nengo_ocl.Simulator(network, profiling=True) as sim: sim.run(presentation_time) sim.print_profiling(sort=1) else: n = len(data[0]) # test on all examples if n_max is not None: n = min(n, n_max) print("Running %d examples for %0.3f s each" % (n, presentation_time)) with Simulator(network) as sim: sim.run(n * presentation_time) dt = sim.dt t = sim.trange() y = sim.data[yp] # z = sim.data[zp] get_ind = lambda t: int(t / presentation_time) inds = slice(0, get_ind(t[-2]) + 1) images = data[0][inds] labels = data[1][inds] data_mean = dp.data_mean label_names = dp.batch_meta['label_names'] spikes = None if count_spikes: trange = float(t[-1] - t[0]) spikes = OrderedDict((k, sim.data[v]) for k, v in spikes_p.items()) counts = [(v > 0).sum() / trange for v in spikes.values()] neurons = [v.shape[1] for v in spikes.values()] rates = [c / n for c, n in zip(counts, neurons)] print("Spike rates: {%s}" % ', '.join( "%s: %0.1f" % (k, r) for k, r in zip(spikes.keys(), rates))) print("Spike rate [Hz]: %0.3f" % (sum(counts) / sum(neurons))) if savefile is not None: np.savez(savefile, images=images, labels=labels, data_mean=data_mean, label_names=label_names, dt=dt, pt=presentation_time, t=t, y=y, spikes=spikes) print("Saved '%s'" % savefile) errors, top5errors, _, _ = error(dt, presentation_time, labels, t, y) print("Error: %0.4f, %0.4f (%d samples)" % (errors.mean(), top5errors.mean(), errors.size))
pltf = cl.get_platforms()[args.ocl_platform] print "USING PLATFORM:" print ' ' + str(pltf) print "AVAILABLE DEVICES:" print ' ' + '\n '.join(map(str, pltf.get_devices())) if args.ocl_device >= 0: ctx = cl.Context([pltf.get_devices()[args.ocl_device]]) print "USING DEVICE:" print ' ' + str(pltf.get_devices()[args.ocl_device]) else: ctx = cl.Context(pltf.get_devices()) print "USING DEVICES:" print ' ' + '\n '.join(map(str, pltf.get_devices())) sim = nengo_ocl.Simulator(model, dt=cfg.sim_dt, context=ctx, profiling=args.ocl_profile) elif cfg.use_mpi: import nengo_mpi mpi_savefile = \ ('+'.join([cfg.get_probe_data_filename(mpi_savename)[:-4], ('%ip' % args.mpi_p if not args.mpi_p_auto else 'autop'), '%0.2fs' % experiment.get_est_simtime()]) + '.' + mpi_saveext) mpi_savefile = os.path.join(cfg.data_dir, mpi_savefile) print "USING MPI - Saving to: %s" % (mpi_savefile) if args.mpi_p_auto: assignments = {}
model.config[nengo.Connection].synapse = nengo.Alpha(0.005) nengo.Connection(inputA, A.input, synapse=None) nengo.Connection(inputB, B.input, synapse=None) nengo.Connection(A.output, D.A) nengo.Connection(B.output, D.B) nengo.Connection(D.output, C.input) model.config[nengo.Probe].synapse = nengo.Alpha(0.03) A_p = nengo.Probe(A.output) B_p = nengo.Probe(B.output) C_p = nengo.Probe(C.output) D_p = nengo.Probe(D.product.output) # --- simulation with nengo_ocl.Simulator(model, context=ctx, profiling=True) as sim: sim.run(1.0) sim.print_profiling(sort=1) # --- results import matplotlib.pyplot as plt t = sim.trange() def plot(sim, a, A, title=""): a_ref = np.tile(a, (len(t), 1)) a_sim = sim.data[A] colors = ['b', 'g', 'r', 'c', 'm', 'y'] for i in xrange(min(dim, len(colors))): plt.plot(t, a_ref[:, i], '--', color=colors[i])
def OclOnlySimulator(*args, **kwargs): return nengo_ocl.Simulator( *args, context=ctx, if_python_code='error', **kwargs)
inspect.currentframe()))) # script path #open cl settings if sys.platform == 'darwin': os.environ["PYOPENCL_CTX"] = "0:1" elif socket.gethostname() == 'ai17864': print('ai comp') else: os.environ["PYOPENCL_CTX"] = "0" # define the model with nengo.Network() as model: stim = nengo.Node(np.sin) a = nengo.Ensemble(100, 1) b = nengo.Ensemble(100, 1) nengo.Connection(stim, a) nengo.Connection(a, b, function=lambda x: x**2) probe_a = nengo.Probe(a, synapse=0.01) probe_b = nengo.Probe(b, synapse=0.01) # build and run the model with nengo_ocl.Simulator(model) as sim: #with nengo.Simulator(model) as sim: sim.run(10) # plot the results plt.plot(sim.trange(), sim.data[probe_a]) plt.plot(sim.trange(), sim.data[probe_b]) plt.show()
def TestSimulator(net, *args, **kwargs): kwargs.setdefault("progress_bar", False) return nengo_ocl.Simulator(net, *args, **kwargs)
def __init__(self, n_input, n_output, n_neurons=1000, n_ensembles=1, seed=None, pes_learning_rate=1e-6, intercepts=(0.5, 1.0), weights_file=None, backend='nengo', session=None, run=None, test_name='test', autoload=False, function=None, send_redis_spikes=False, encoders=None, probe_weights=False, debug_print=False, **kwargs): self.input_signal = np.zeros(n_input) self.training_signal = np.zeros(n_output) self.output = np.zeros(n_output) # if a weights_file is not specified and auto_load is desired, # check the test_name directory for the most recent weights if weights_file is None and autoload: weights_file = self.load_weights(session=session, run=run, test_name=test_name) if weights_file is None: weights_file = '' self.nengo_model = nengo.Network(seed=seed) self.nengo_model.config[nengo.Connection].synapse = None # Set the nerual model to use if backend == 'nengo': self.nengo_model.config[nengo.Ensemble].neuron_type = nengo.LIF() elif backend == 'nengo_spinnaker': self.nengo_model.config[nengo.Ensemble].neuron_type = nengo.LIF() with self.nengo_model: def input_signals_func(t): """ Get the input and -1 * training signal """ return self.input_signal input_signals = nengo.Node(input_signals_func, size_out=n_input) def training_signals_func(t): return -self.training_signal training_signals = nengo.Node(training_signals_func, size_out=n_output) # make the adaptive population output accessible def output_func(t, x): """ stores the adaptive output for use in control() """ self.output = np.copy(x) output = nengo.Node(output_func, size_in=n_output, size_out=0) # specify intercepts such that neurons are # active throughout the whole range specified intercepts = AreaIntercepts(dimensions=n_input, base=Triangular(-0.9, -0.9, 0.0)) self.adapt_ens = [] self.conn_learn = [] for ii in range(n_ensembles): self.adapt_ens.append( nengo.Ensemble(n_neurons=n_neurons, dimensions=n_input, intercepts=intercepts, radius=np.sqrt(n_input), **kwargs)) print('*** ENSEMBLE %i ***' % ii) try: # if the NengoLib is installed, use it # to optimize encoder placement import nengolib if encoders is None: self.adapt_ens[ii].encoders = ( nengolib.stats.ScatteredHypersphere(surface=True)) print('NengoLib used to optimize encoders placement') else: self.adapt_ens[ii].encoders = encoders print('Using user defined encoder values') except ImportError: print('Nengo lib not installed, encoder ' + 'placement will be sub-optimal.') # hook up input signal to adaptive population for context nengo.Connection(input_signals, self.adapt_ens[ii]) # load weights from file if they exist, otherwise use zeros if weights_file == '~': weights_file = os.path.expanduser(weights_file) print('Backend: ', backend) print('Weights file: %s' % weights_file) if not os.path.isfile('%s' % weights_file): print('No weights found, starting with zeros') transform = np.zeros( (n_output, self.adapt_ens[ii].n_neurons)) # set up learning connections if function is not None and (weights_file is None or weights_file is ''): print("Using provided function to bootstrap learning") eval_points = Concatenate([ nengo.dists.Choice([0]), nengo.dists.Choice([0]), nengo.dists.Uniform(-1, 1), nengo.dists.Uniform(-1, 1) ]) self.conn_learn.append( nengo.Connection( self.adapt_ens[ii], output, learning_rule_type=nengo.PES(pes_learning_rate), function=function, eval_points=eval_points)) else: if backend == 'nengo_spinnaker': if os.path.isfile('%s' % weights_file): if n_ensembles == 1: transform = np.squeeze( np.load(weights_file)['weights']).T else: transform = np.squeeze( np.load(weights_file)['weights'])[ii].T print('Loading weights: \n', transform) print('Loaded weights all zeros: ', np.allclose(transform, 0)) print(transform.T.shape) self.conn_learn.append( nengo.Connection( self.adapt_ens[ii], output, function=lambda x: np.zeros(n_output), learning_rule_type=nengo.PES( pes_learning_rate), solver=DummySolver(transform.T))) else: if os.path.isfile('%s' % weights_file): if n_ensembles == 1: transform = np.load(weights_file)['weights'] else: transform = ( np.load(weights_file)['weights'][ii]) # remove third dimension if present if len(transform.shape) > 2: transform = np.squeeze(transform) print('Loading weights: \n', transform) print('Loaded weights all zeros: ', np.allclose(transform, 0)) self.conn_learn.append( nengo.Connection(self.adapt_ens[ii].neurons, output, learning_rule_type=nengo.PES( pes_learning_rate), transform=transform)) # hook up the training signal to the learning rule nengo.Connection(training_signals, self.conn_learn[ii].learning_rule, synapse=None) if backend != 'nengo_spinnaker' and send_redis_spikes: # Send spikes via redis def send_spikes(t, x): v = np.where(x != 0)[0] if len(v) > 0: msg = struct.pack('%dI' % len(v), *v) else: msg = '' r.set('spikes', msg) self.activity = x source_node = nengo.Node(send_spikes, size_in=n_neurons) nengo.Connection(self.adapt_ens[0].neurons[:n_neurons], source_node, synapse=None) def save_x(t, x): self.x = x x_node = nengo.Node(save_x, size_in=n_input) nengo.Connection(self.adapt_ens[0], x_node, synapse=None) if backend == 'nengo' and probe_weights: self.nengo_model.weights_probe = nengo.Probe( self.conn_learn[0], 'weights', synapse=None) nengo.cache.DecoderCache().invalidate() if backend == 'nengo': self.sim = nengo.Simulator(self.nengo_model, dt=.001) elif backend == 'nengo_ocl': try: import nengo_ocl except ImportError: raise Exception('Nengo OCL not installed, ' + 'cannot use this backend.') import pyopencl as cl # Here, the context would be to use all devices from platform [0] ctx = cl.Context(cl.get_platforms()[0].get_devices()) self.sim = nengo_ocl.Simulator(self.nengo_model, context=ctx, dt=.001) elif backend == 'nengo_spinnaker': try: import nengo_spinnaker except ImportError: raise Exception('Nengo SpiNNaker not installed, ' + 'cannot use this backend.') if debug_print: # turn on debug printing logging.basicConfig(level=logging.DEBUG) self.sim = nengo_spinnaker.Simulator(self.nengo_model) # start running the spinnaker model self.sim.async_run_forever() else: raise Exception('Invalid backend specified') self.backend = backend
with network: for ii in range(0, len(net.layers)): type_id=net.layers[ii].type type=layers_type.get(type_id) if type=='conv' or type=="pool" or type=="fc" or type=="drop" or type=="relu": out,input_shape=build_layer(net.layers,ii,output_layers,input_shape,type,c3d_net) output_layers.append(out) elif type=="data": out=build_layer_data(frames) output_layers.append(out) yp = nengo.Probe(output_layers[-1], synapse=None) start_time = time.time() print("Creating Simulator...") sim = nengo_ocl.Simulator(network) print("--- Simulator Created in %s seconds ---" % (time.time() - start_time)) print("Starting Simulation...") sim.run(num_clip_test * presentation_time) print 'Getting probe...' dt = sim.dt t = sim.trange() y = sim.data[yp] print 'Probe ok!' s = nengo.synapses.Alpha(0.005) y_filt = nengo.synapses.filtfilt(y, s, dt) write_files(y,dt,y_filt) y_pred,my_errors=my_error_new(dt, labels, t, y, y_filt) print "Done!"