def execute_trial(self, p): if p.debug: logging.basicConfig(level=logging.DEBUG) model = self.model(p) import nengo if not isinstance(model, nengo.Network): raise ValueError('model() must return a nengo.Network') if p.neuron_type != 'default': if isinstance(p.neuron_type, basestring): neuron_type = eval(p.neuron_type) else: neuron_type = p.neuron_type if not isinstance(neuron_type, nengo.neurons.NeuronType): raise AttributeError('%s is not a NeuronType' % p.neuron_type) for ens in model.all_ensembles: ens.neuron_type = neuron_type if p.gui: locals_dict = getattr(self, 'locals', dict(model=model)) import nengo_gui try: nengo_gui.GUI(model=model, filename=sys.argv[1], locals=locals_dict, editor=False, ).start() except TypeError: # support nengo_gui v0.2.0 and previous nengo_gui.GUI(model=model, filename=sys.argv[1], locals=locals_dict, interactive=False, allow_file_change=False, ).start() else: if ':' in p.backend: backend, clsname = p.backend.split(':', 1) else: backend = p.backend clsname = 'Simulator' module = importlib.import_module(backend) Simulator = getattr(module, clsname) args = inspect.getargspec(Simulator.__init__)[0] if (not p.verbose and 'progress_bar' in args): self.sim = Simulator(model, dt=p.dt, progress_bar=False) else: self.sim = Simulator(model, dt=p.dt) with self.sim: return super(NengoTrial, self).execute_trial(p)
def start_server(cls, cfg, model): # Make sure only one server is writing the same config. server_thread = cls.threads.get(cfg, None) server = cls.servers.get(cfg, None) existent = server_thread is not None and server is not None if existent and server_thread.is_alive(): warnings.warn( ConfigReuseWarning( "Reusing config. Only the most recent visualization will " "update the config.")) for page in server.gui.pages: page.save_config(force=True) page.filename_cfg = get_ipython().mktempfile() cls.servers[page.filename_cfg] = server cls.threads[page.filename_cfg] = server_thread name = model.label gui = nengo_gui.GUI(name, cfg=cfg, model=model, locals=get_ipython().user_ns, interactive=False, allow_file_change=False) server = gui.prepare_server(port=0, browser=False) server_thread = threading.Thread(target=gui.begin_lifecycle, kwargs={'server': server}) server_thread.start() cls.servers[cfg] = server cls.threads[cfg] = server_thread cls.configs.add(cfg) return server_thread, server
def do_gui(): logging.info("Launching nengo_gui (port=%d)", port) gui = nengo_gui.GUI(modelpath, interactive=False) try: gui.start(port=port, browser=False) except: logging.debug("Server raised exception, suppressed:\n%s", tb.format_exc(()))
print "MODEL N_CONNECTIONS: %i" % (len(model.all_connections)) # ----- 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)
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
import numpy as np import nengo model = nengo.Network() with model: stimulus_A = nengo.Node([1], label='stim A') stimulus_B = nengo.Node(lambda t: np.sin(2 * np.pi * t)) ens = nengo.Ensemble(n_neurons=1000, dimensions=2) result = nengo.Ensemble(n_neurons=50, dimensions=1) nengo.Connection(stimulus_A, ens[0]) nengo.Connection(stimulus_B, ens[1]) nengo.Connection(ens, result, function=lambda x: x[0] * x[1], synapse=0.01) with nengo.Network(label='subnet') as subnet: a = nengo.Ensemble(100, 1) b = nengo.Ensemble(100, 1) nengo.Connection(a, b) nengo.Connection(b, b) with nengo.Network() as subsubnet: c = nengo.Ensemble(100, 1) d = nengo.Ensemble(100, 1) nengo.Connection(c, d) nengo.Connection(b, c) nengo.Connection(d, a) nengo.Connection(result, a) if __name__ == '__main__': import nengo_gui nengo_gui.GUI(__file__).start()
# specify torque input to arm input_node = nengo.Node(output=[1, .1]) # to send a target to an ensemble which then connections to the arm ens = nengo.Ensemble(n_neurons=500, dimensions=2, radius=20) nengo.Connection(input_node, ens[:2]) # to send target info to ensemble # connect ens to arm nengo.Connection(ens, arm_node)#, function=some_function) # -------------------------------------------------------- if run_in_GUI: # to run in GUI, comment out next 4 lines for running without GUI import nengo_gui nengo_gui.GUI(model=model, filename=__file__, locals=locals(), interactive=False, allow_file_change=False).start() import sys sys.exit() else: # to run in command line with model: probe_input = nengo.Probe(input_node) probe_arm = nengo.Probe(arm_node[arm.DOF*2]) print 'building model...' sim = nengo.Simulator(model, dt=.001) print 'build complete.' sim.run(10) t = sim.trange()
def run(self, **kwargs): p, fn = self.process_args(**kwargs) if p.debug: logging.basicConfig(level=logging.DEBUG) else: logging.basicConfig(level=logging.ERROR) print('running %s' % fn) np.random.seed(p.seed) model = self.model(p) if p.gui: import nengo_gui nengo_gui.GUI(model=model, filename=self.__class__.__name__, locals=dict(model=model), interactive=False, allow_file_change=False).start() return module = importlib.import_module(p.backend) Simulator = module.Simulator if p.backend == 'nengo_spinnaker': try: _ = model.config[nengo.Node].function_of_time except AttributeError: import nengo_spinnaker nengo_spinnaker.add_spinnaker_params(model.config) for node in model.all_nodes: if (node.size_in == 0 and node.size_out > 0 and callable(node.output)): model.config[node].function_of_time = True if p.save_figs or p.show_figs: plt = matplotlib.pyplot plt.figure() else: plt = None sim = Simulator(model, dt=p.dt) self.start_time = time.time() self.sim_speed = None result = self.evaluate(p, sim, plt) if p.backend == 'nengo_spinnaker': sim.close() if self.sim_speed is not None and 'sim_speed' not in result: result['sim_speed'] = self.sim_speed text = [] for k, v in sorted(result.items()): text.append('%s = %s' % (k, repr(v))) if plt is not None and not p.hide_overlay: plt.suptitle(fn + '\n' + '\n'.join(text), fontsize=8) plt.figtext(0.13, 0.12, '\n'.join(self.args_text)) text = self.args_text + text text = '\n'.join(text) if not os.path.exists(p.data_dir): os.mkdir(p.data_dir) fn = os.path.join(p.data_dir, fn) if p.save_figs: plt.savefig(fn + '.png', dpi=300) with open(fn + '.txt', 'w') as f: f.write(text) print(text) if p.save_raw: db = shelve.open(fn + '.db') db['trange'] = sim.trange() for k, v in inspect.getmembers(self): if isinstance(v, nengo.Probe): db[k] = sim.data[v] db.close() if p.show_figs: plt.show() return result
def evaluate(self, p, plt): files = [] sets = [] for f in os.listdir(p.dataset_dir): if f.endswith('events'): files.append(os.path.join(p.dataset_dir, f)) if p.test_set == 'one': test_file = random.sample(files, 1)[0] files.remove(test_file) if p.n_data != -1: files = random.sample(files, p.n_data) inputs = [] targets = [] for f in files: times, imgs, targs = davis_track.load_data( f, dt=p.dt, decay_time=p.decay_time, separate_channels=p.separate_channels, saturation=p.saturation, merge=p.merge) inputs.append(imgs) targets.append(targs[:, :2]) inputs_all = np.vstack(inputs) targets_all = np.vstack(targets) if p.test_set == 'odd': inputs_train = inputs_all[::2] inputs_test = inputs_all[1::2] targets_train = targets_all[::2] targets_test = targets_all[1::2] dt_test = p.dt * 2 elif p.test_set == 'one': times, imgs, targs = davis_track.load_data( test_file, dt=p.dt_test, decay_time=p.decay_time, separate_channels=p.separate_channels, saturation=p.saturation, merge=p.merge) inputs_test = imgs targets_test = targs[:, :2] inputs_train = inputs_all targets_train = targets_all dt_test = p.dt_test if p.augment: inputs_train, targets_train = davis_track.augment( inputs_train, targets_train, separate_channels=p.separate_channels) if p.separate_channels: shape = (2, 180 // p.merge, 240 // p.merge) else: shape = (1, 180 // p.merge, 240 // p.merge) dimensions = shape[0] * shape[1] * shape[2] eval_points_train = inputs_train.reshape(-1, dimensions) eval_points_test = inputs_test.reshape(-1, dimensions) max_rate = 100 amp = 1 / max_rate model = nengo.Network() with model: model.config[nengo.Ensemble].neuron_type = nengo.RectifiedLinear( amplitude=amp) model.config[nengo.Ensemble].max_rates = nengo.dists.Choice( [max_rate]) model.config[nengo.Ensemble].intercepts = nengo.dists.Choice([0]) model.config[nengo.Connection].synapse = None inp = nengo.Node( nengo.processes.PresentInput( inputs_test.reshape(-1, dimensions), dt_test), size_out=dimensions, ) out = nengo.Node(None, size_in=2) if not p.split_spatial: # do a standard convnet conv1 = nengo.Convolution(p.n_features_1, shape, channels_last=False, strides=(p.stride_1, p.stride_1), kernel_size=(p.kernel_size_1, p.kernel_size_1)) layer1 = nengo.Ensemble(conv1.output_shape.size, dimensions=1) nengo.Connection(inp, layer1.neurons, transform=conv1) conv2 = nengo.Convolution(p.n_features_2, conv1.output_shape, channels_last=False, strides=(p.stride_2, p.stride_2), kernel_size=(p.kernel_size_2, p.kernel_size_2)) layer2 = nengo.Ensemble(conv2.output_shape.size, dimensions=1) nengo.Connection(layer1.neurons, layer2.neurons, transform=conv2) nengo.Connection(layer2.neurons, out, transform=nengo_dl.dists.Glorot()) else: convnet = spatial_convnet.ConvNet(nengo.Network()) convnet.make_input_layer(shape, spatial_stride=(p.spatial_stride, p.spatial_stride), spatial_size=(p.spatial_size, p.spatial_size)) nengo.Connection(inp, convnet.input) convnet.make_middle_layer(n_features=p.n_features_1, n_parallel=p.n_parallel, n_local=1, n_remote=0, kernel_stride=(p.stride_1, p.stride_1), kernel_size=(p.kernel_size_1, p.kernel_size_1)) convnet.make_middle_layer(n_features=p.n_features_2, n_parallel=p.n_parallel, n_local=1, n_remote=0, kernel_stride=(p.stride_2, p.stride_2), kernel_size=(p.kernel_size_2, p.kernel_size_2)) convnet.make_output_layer(2) nengo.Connection(convnet.output, out) p_out = nengo.Probe(out) if p.gui: locals_dict = getattr(self, 'locals', dict(model=model)) import nengo_gui import webbrowser if hasattr(nengo_gui, 'guibackend'): host = 'localhost' port = 8080 server_settings = nengo_gui.guibackend.GuiServerSettings( (host, port)) model_context = nengo_gui.guibackend.ModelContext( model=model, locals=locals_dict, filename=sys.argv[1], writeable=False) page_settings = nengo_gui.page.PageSettings( filename_cfg=sys.argv[1] + '.cfg', backend='nengo', editor_class=nengo_gui.components.editor.NoEditor) server = nengo_gui.gui.BaseGUI(model_context, server_settings, page_settings) if hasattr(server.server, 'gen_one_time_token'): wb = webbrowser.get().open( '%s://%s:%d/?token=%s' % ('http', host, port, server.server.gen_one_time_token())) else: wb = webbrowser.get().open('%s://%s:%d/' % ('http', host, port)) server.start() else: try: nengo_gui.GUI( model=model, filename=sys.argv[1], locals=locals_dict, editor=False, ).start() except TypeError: # support nengo_gui v0.2.0 and previous nengo_gui.GUI( model=model, filename=sys.argv[1], locals=locals_dict, interactive=False, allow_file_change=False, ).start() N = len(inputs_train) n_steps = int(np.ceil(N / p.minibatch_size)) dl_train_data = { inp: np.resize(inputs_train, (p.minibatch_size, n_steps, dimensions)), p_out: np.resize(targets_train, (p.minibatch_size, n_steps, 2)) } N = len(inputs_test) n_steps = int(np.ceil(N / p.minibatch_size)) dl_test_data = { inp: np.resize(inputs_test, (p.minibatch_size, n_steps, dimensions)), p_out: np.resize(targets_test, (p.minibatch_size, n_steps, 2)) } with nengo_dl.Simulator(model, minibatch_size=p.minibatch_size) as sim: #loss_pre = sim.loss(dl_test_data) if p.n_epochs > 0: sim.train( dl_train_data, tf.train.RMSPropOptimizer(learning_rate=p.learning_rate), n_epochs=p.n_epochs) #loss_post = sim.loss(dl_test_data) sim.run_steps(n_steps, data=dl_test_data) data = sim.data[p_out].reshape(-1, 2)[:len(targets_test)] filt = nengo.synapses.Lowpass(p.output_filter) data = filt.filt(data, dt=dt_test) rmse_test = np.sqrt(np.mean( (targets_test - data)**2, axis=0)) * p.merge if plt: plt.plot(data) plt.plot(targets_test, ls='--') return dict( #loss_pre=loss_pre, #loss_post=loss_post rmse_test=rmse_test, max_n_neurons=max([ens.n_neurons for ens in model.all_ensembles]))
if args.nengo_gui: # Set environment variables (for nengo_gui) if cfg.use_opencl: if args.ocl_platform >= 0 and args.ocl_device >= 0: os.environ['PYOPENCL_CTX'] = '%s:%s' % (args.ocl_platform, args.ocl_device) else: raise RuntimeError('Error - OCL platform and device must be' + 'specified when using ocl with nengo_gui.' + ' Use the --ocl_platform and --ocl_device' + ' argument options to set.') print("STARTING NENGO_GUI") import nengo_gui nengo_gui.GUI(__file__, model=model, locals=locals(), editor=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()))) if args.ocl_platform >= 0: pltf = cl.get_platforms()[args.ocl_platform] print("USING PLATFORM:") print(' ' + str(pltf))