# ssp.encoders = encoders_place_cell # ssp.encoders = encoders_mixed ssp.encoders = encoders_grid_cell # ssp.eval_points = encoders_place_cell # ssp.eval_points = np.vstack([encoders_place_cell, encoders_band_cell, encoders_grid_cell]) # ssp.eval_points = encoders_place_cell ssp.eval_points = eval_points # ssp.intercepts = [0.25]*n_neurons # ssp.intercepts = [0.0] * n_neurons ssp.intercepts = rng.uniform(0, .75, size=(n_neurons, )) # ssp.intercepts = mixed_intercepts # ssp.intercepts = [0.50] * n_neurons feedback_conn = nengo.Connection( ssp, ssp, function=feedback_real, synapse=tau, solver=nengo.solvers.LstsqL2(weights=True), ) nengo.Connection(kick, ssp) heatmap_node = nengo.Node( SpatialHeatmap(heatmap_vectors, xs, ys, cmap='plasma', vmin=vmin, vmax=vmax), size_in=dim, size_out=0,
# # Nengo Tip: In this particular case, those two functions are both linear # functions, and so we could implement them much more easily using the # "transform=" approach (see tutorial 10). This is left as an exercise to the # user. # # Try running the model and seeing that y is a slowed-down, smoother version # of x. What happens if you change the input up and down quickly? What # happens with tau=0.1? What about tau=0.01? import nengo model = nengo.Network() with model: stim_x = nengo.Node(0) x = nengo.Ensemble(n_neurons=50, dimensions=1) nengo.Connection(stim_x, x) y = nengo.Ensemble(n_neurons=50, dimensions=1) tau = 0.5 synapse = 0.1 def input_function(x): return x / tau * synapse def recurrent_function(y): return (-y / tau) * synapse + y nengo.Connection(x, y, synapse=synapse, function=input_function) nengo.Connection(y, y, synapse=synapse, function=recurrent_function)
def convolution(module, target_name, effect, n_neurons_cconv, synapse): """Implement an action_objects.Convolution. Parameters ---------- module : spa.Module The module that will own this convolution target_name : string The name of the object to send the convolution result to effect : action_objects.Convolution The details of the convolution to implement n_neurons_cconv : int Number of neurons in each product population synapse : float (or nengo.Synapse) The synapse to use for connections into and out of the convolution Returns the created nengo.networks.CircularConvolution. """ source1 = effect.source1 source2 = effect.source2 target_module = module.spa.get_module(target_name) target, target_vocab = module.spa.get_module_input(target_name) s1_output, s1_vocab = module.spa.get_module_output(source1.name) s2_output, s2_vocab = module.spa.get_module_output(source2.name) with target_module: cconv = nengo.networks.CircularConvolution(n_neurons_cconv, s1_vocab.dimensions, invert_a=False, invert_b=False, label='cconv_%s' % str(effect)) with module.spa: # compute the requested transform t = s1_vocab.parse(str(effect.transform)).get_convolution_matrix() # handle conversion between different Vocabularies if target_vocab is not s1_vocab: t = np.dot(s1_vocab.transform_to(target_vocab), t) nengo.Connection(cconv.output, target, transform=t, synapse=synapse) t1 = s1_vocab.parse(source1.transform.symbol).get_convolution_matrix() if source1.inverted: D = s1_vocab.dimensions t1 = np.dot(t1, np.eye(D)[-np.arange(D)]) nengo.Connection(s1_output, cconv.input_a, transform=t1, synapse=synapse) t2 = s2_vocab.parse(source2.transform.symbol).get_convolution_matrix() if source2.inverted: D = s2_vocab.dimensions t2 = np.dot(t2, np.eye(D)[-np.arange(D)]) if s1_vocab is not s2_vocab: t2 = np.dot(s2_vocab.transform_to(s1_vocab), t2) nengo.Connection(s2_output, cconv.input_b, transform=t2, synapse=synapse) return cconv
else: temp = 0 return temp if __name__ == '__main__': import matplotlib.pyplot as plt import nengo from nengo.processes import WhiteSignal model = nengo.Network(label="Delayed connection") with model: # We'll use white noise as input inp = nengo.Node(WhiteSignal(2, high=5), size_out=1) A = nengo.Ensemble(40, dimensions=1) nengo.Connection(inp, A) # We'll make a simple object to implement the delayed connection class Delay(object): def __init__(self, dimensions, timesteps=50): self.history = np.zeros((timesteps, dimensions)) def step(self, t, x): self.history = np.roll(self.history, -1) self.history[-1] = x print(self.history) return np.mean(self.history) dt = 0.001 delay = Delay(1, timesteps=int(0.01 / 0.001))
#also safeguards against slow evidence accumulation - import nengo model = nengo.Network() with model: stim = nengo.Node([0]) acc = nengo.Ensemble(300, 2, radius=1.5, noise=nengo.processes.WhiteSignal(period=10, high=100, rms=3)) nengo.Connection(stim, acc[0], transform=0.1) def feedback(x): #if x[0] has accumulated evidence to boundary, return the boundary reached #and the choice flag if x[0] > 0.9: return 1, 1 elif x[0] < -0.9: return -1, 1 #once choice has been made, keep decision at boundary and choice flag up elif x[1] > 0.5: if x[0] > 0: return 1, 1 else: return -1, 1 #else return the evidence accumulated so far, and don't throw choice flag
def setup_connections(self, parent_net): # Set up connections from vision module if hasattr(parent_net, 'vis'): # VIS ITEM Input nengo.Connection(parent_net.vis.output, self.item_input) # POS MB Control signals pos_mb_gate_sp_vecs = \ vocab.main.parse('+'.join(vocab.num_sp_strs)).v pos_mb_rst_sp_vecs = vocab.main.parse('A+OPEN+QM').v nengo.Connection(parent_net.vis.output, self.pos_inc.gate, transform=[pos_mb_gate_sp_vecs * 1.25], synapse=0.01) nengo.Connection(parent_net.vis.neg_attention, self.pos_inc.gate, transform=-1.25, synapse=0.01) nengo.Connection(parent_net.vis.output, self.pos_inc.reset, transform=[pos_mb_rst_sp_vecs]) # POS MB ACC Control signals pos_mb_acc_rst_sp_vecs = vocab.main.parse('A+OPEN').v nengo.Connection(parent_net.vis.output, self.pos_mb_acc.gate, transform=[pos_mb_gate_sp_vecs]) nengo.Connection(parent_net.vis.neg_attention, self.pos_mb_acc.gate, transform=-1.25, synapse=0.01) nengo.Connection(parent_net.vis.output, self.pos_mb_acc.reset, transform=[pos_mb_acc_rst_sp_vecs]) # TODO: Fix no resetting for REV recall # - Disable reset during QM # - Set INC selector to ~INC # Encode item in encoding (POSxITEM + ITEM) # nengo.Connection(parent_net.vis.output, self.enc_output, # synapse=None) else: warn("InfoEncoding Module - Cannot connect from 'vis'") # Set up connections from production system module if hasattr(parent_net, 'ps'): # Suppress the pos acc gate signal when in the decoding task stage pos_mb_acc_no_gate_sp_vecs = vocab.main.parse('DEC').v nengo.Connection(parent_net.ps.task, self.pos_mb_acc.gate, transform=[-1.25 * pos_mb_acc_no_gate_sp_vecs]) else: warn("InfoEncoding Module - Cannot connect from 'ps'") # Set up connections from decoding module if hasattr(parent_net, 'dec'): nengo.Connection(parent_net.dec.pos_mb_gate_bias.output, self.pos_inc.gate, transform=4, synapse=0.01) nengo.Connection(parent_net.dec.pos_mb_gate_sig.output, self.pos_inc.gate, transform=-4, synapse=0.01) else: warn("InfoEncoding Module - Cannot connect from 'dec'")
#xdot(1) = x(1) * (1 - b*x(2)^2)*0.1 - w0^2*x(2); #xdot(2) = x(1); # best parameter synpase = 0.1, n_neurons = 200 # synpase = 0.05, n_neurons = 400 # ## Step 2: Provide Input to the Model # A brief input signal is provided to trigger the oscillatory behavior of the neural representation. from nengo.utils.functions import piecewise with model: # Create an input signal input = nengo.Node(piecewise({0: [1, 0], 0.1: [0, 0]})) #input = nengo.Node(piecewise({0.1, 0.1})) # Connect the input signal to the neural ensemble nengo.Connection(input, x) # Create the feedback connection nengo.Connection(x, x, synapse=synapse, function=vdp) with model: # input_probe = nengo.Probe(input, 'output') neuron_probe = nengo.Probe(x, 'decoded_output', synapse=0.1) # Create the simulator with nengo.Simulator(model) as sim: # Run it for 5 seconds sim.run(20) plt.plot(sim.trange(), sim.data[neuron_probe]) plt.xlabel('Time (s)', fontsize='large')
def Product(n_neurons, dimensions, input_magnitude=1., net=None, **kwargs): """Computes the element-wise product of two equally sized vectors. The network used to calculate the product is described in `Gosmann, 2015`_. A simpler version of this network can be found in the `Multiplication example <https://www.nengo.ai/nengo/examples/multiplication.html>`_. Note that this network is optimized under the assumption that both input values (or both values for each input dimensions of the input vectors) are uniformly and independently distributed. Visualized in a joint 2D space, this would give a square of equal probabilities for pairs of input values. This assumption is violated with non-uniform input value distributions (for example, if the input values follow a Gaussian or cosine similarity distribution). In that case, no square of equal probabilities is obtained, but a probability landscape with circular equi-probability lines. To obtain the optimal network accuracy, scale the *input_magnitude* by a factor of ``1 / sqrt(2)``. .. _Gosmann, 2015: https://nbviewer.jupyter.org/github/ctn-archive/technical-reports/blob/ master/Precise-multiplications-with-the-NEF.ipynb#An-alternative-network Parameters ---------- n_neurons : int Number of neurons per dimension in the vector. .. note:: These neurons will be distributed evenly across two ensembles. If an odd number of neurons is specified, the extra neuron will not be used. dimensions : int Number of dimensions in each of the vectors to be multiplied. input_magnitude : float, optional (Default: 1.) The expected magnitude of the vectors to be multiplied. This value is used to determine the radius of the ensembles computing the element-wise product. kwargs Keyword arguments passed through to ``nengo.Network``. Returns ------- net : Network The newly built product network, or the provided ``net``. Attributes ---------- net.input_a : Node The first vector to be multiplied. net.input_b : Node The second vector to be multiplied. net.output : Node The resulting product. net.sq1 : EnsembleArray Represents the first squared term. See `Gosmann, 2015`_ for details. net.sq2 : EnsembleArray Represents the second squared term. See `Gosmann, 2015`_ for details. """ if net is None: kwargs.setdefault('label', "Product") net = nengo.Network(**kwargs) else: warnings.warn("The 'net' argument is deprecated.", DeprecationWarning) with net: net.input_a = net.A = nengo.Node(size_in=dimensions, label="input_a") net.input_b = net.B = nengo.Node(size_in=dimensions, label="input_b") net.output = nengo.Node(size_in=dimensions, label="output") net.sq1 = EnsembleArray(max(1, n_neurons // 2), n_ensembles=dimensions, ens_dimensions=1, radius=input_magnitude * np.sqrt(2)) net.sq2 = EnsembleArray(max(1, n_neurons // 2), n_ensembles=dimensions, ens_dimensions=1, radius=input_magnitude * np.sqrt(2)) tr = 1. / np.sqrt(2.) nengo.Connection(net.input_a, net.sq1.input, transform=tr, synapse=None) nengo.Connection(net.input_b, net.sq1.input, transform=tr, synapse=None) nengo.Connection(net.input_a, net.sq2.input, transform=tr, synapse=None) nengo.Connection(net.input_b, net.sq2.input, transform=-tr, synapse=None) sq1_out = net.sq1.add_output('square', np.square) nengo.Connection(sq1_out, net.output, transform=.5, synapse=None) sq2_out = net.sq2.add_output('square', np.square) nengo.Connection(sq2_out, net.output, transform=-.5, synapse=None) return net
<circle cx="{xt}" cy="{yt}" r="40" style="fill:red"/> <line x1="{x0}" y1="{y0}" x2="{x1}" y2="{y1}" style="stroke:black;stroke-width:10px"/> <line x1="{x1}" y1="{y1}" x2="{x2}" y2="{y2}" style="stroke:black;stroke-width:10px"/> <line x1="{x2}" y1="{y2}" x2="{x3}" y2="{y3}" style="stroke:black;stroke-width:10px"/> </svg> '''.format(**locals()) #stim_angles = nengo.Node([0,0,0]) arm = nengo.Node(arm_function, size_in=5) angles = nengo.Node((lambda t, x: x), size_in=3) stim_target = nengo.Node([0, -578]) error = nengo.Node(None, size_in=2) nengo.Connection(stim_target, error) nengo.Connection(angles, error, function=fwd, transform=-1) nengo.Connection(angles, arm[:3]) nengo.Connection(stim_target, arm[3:]) def Jtrans(t, x): q = x[2:] dx = x[:2] J = jacobian(q) delta = np.dot(J.T, dx) return delta * 0.00001 jtrans = nengo.Node(Jtrans, size_in=5)
# Nengo Example: Inhibitory Gating of Ensembles # # ## Step 1: Create the network # # Our model consists of two ensembles (called A and B) that receive inputs from # a common sine wave signal generator. # # Ensemble A is gated using the output of a node, while Ensemble B is gated # using the output of a third ensemble (C). This is to demonstrate that # ensembles can be gated using either node outputs, or decoded outputs from # ensembles. import nengo model = nengo.Network() with model: a = nengo.Ensemble(n_neurons=30, dimensions=1) b = nengo.Ensemble(n_neurons=30, dimensions=1) c = nengo.Ensemble(n_neurons=30, dimensions=1) stim = nengo.Node(0) inhibition = nengo.Node(0) nengo.Connection(stim, a) nengo.Connection(stim, b) nengo.Connection(inhibition, a.neurons, transform=[[-2.5]] * 30) nengo.Connection(inhibition, c) nengo.Connection(c, b.neurons, transform=[[-2.5]] * 30)
def test_convolution(Simulator, plt, seed): D = 5 with spa.SPA(seed=seed) as model: model.inA = spa.Buffer(dimensions=D) model.inB = spa.Buffer(dimensions=D) model.outAB = spa.Buffer(dimensions=D) model.outABinv = spa.Buffer(dimensions=D) model.outAinvB = spa.Buffer(dimensions=D) model.outAinvBinv = spa.Buffer(dimensions=D) model.cortical = spa.Cortical(spa.Actions( 'outAB = inA * inB', 'outABinv = inA * ~inB', 'outAinvB = ~inA * inB', 'outAinvBinv = ~inA * ~inB', )) nengo.Connection(nengo.Node([0, 1, 0, 0, 0]), model.inA.state.input) nengo.Connection(nengo.Node([0, 0, 1, 0, 0]), model.inB.state.input) pAB = nengo.Probe(model.outAB.state.output, synapse=0.03) pABinv = nengo.Probe(model.outABinv.state.output, synapse=0.03) pAinvB = nengo.Probe(model.outAinvB.state.output, synapse=0.03) pAinvBinv = nengo.Probe(model.outAinvBinv.state.output, synapse=0.03) sim = Simulator(model) sim.run(0.2) t = sim.trange() plt.subplot(4, 1, 1) plt.ylabel('A*B') plt.axhline(0.85, c='k') plt.plot(t, sim.data[pAB]) plt.subplot(4, 1, 2) plt.ylabel('A*~B') plt.axhline(0.85, c='k') plt.plot(t, sim.data[pABinv]) plt.subplot(4, 1, 3) plt.ylabel('~A*B') plt.axhline(0.85, c='k') plt.plot(t, sim.data[pAinvB]) plt.subplot(4, 1, 4) plt.ylabel('~A*~B') plt.axhline(0.85, c='k') plt.plot(t, sim.data[pAinvBinv]) # Check results. Since A is [0,1,0,0,0] and B is [0,0,1,0,0], this means: # ~A = [0,0,0,0,1] # ~B = [0,0,0,1,0] # A*B = [0,0,0,1,0] # A*~B = [0,0,0,0,1] # ~A*B = [0,1,0,0,0] # ~A*~B = [0,0,1,0,0] # (Remember that X*[1,0,0,0,0]=X (identity transform) and X*[0,1,0,0,0] # is X rotated to the right once) # Ideal answer: A*B = [0,0,0,1,0] assert np.allclose(np.mean(sim.data[pAB][-10:], axis=0), np.array([0, 0, 0, 1, 0]), atol=0.15) # Ideal answer: A*~B = [0,0,0,0,1] assert np.allclose(np.mean(sim.data[pABinv][-10:], axis=0), np.array([0, 0, 0, 0, 1]), atol=0.15) # Ideal answer: ~A*B = [0,1,0,0,0] assert np.allclose(np.mean(sim.data[pAinvB][-10:], axis=0), np.array([0, 1, 0, 0, 0]), atol=0.15) # Ideal answer: ~A*~B = [0,0,1,0,0] assert np.allclose(np.mean(sim.data[pAinvBinv][-10:], axis=0), np.array([0, 0, 1, 0, 0]), atol=0.15)
def test_circularconv(Simulator, nl, dims=4, neurons_per_product=128): rng = np.random.RandomState(4238) n_neurons = neurons_per_product n_neurons_d = 2 * neurons_per_product radius = 1 a = rng.normal(scale=np.sqrt(1. / dims), size=dims) b = rng.normal(scale=np.sqrt(1. / dims), size=dims) result = circconv(a, b) assert np.abs(a).max() < radius assert np.abs(b).max() < radius assert np.abs(result).max() < radius # --- model model = nengo.Network(label="circular convolution") with model: inputA = nengo.Node(output=a) inputB = nengo.Node(output=b) A = EnsembleArray(nl(n_neurons), dims, radius=radius) B = EnsembleArray(nl(n_neurons), dims, radius=radius) cconv = nengo.networks.CircularConvolution(neurons=nl(n_neurons_d), dimensions=dims) res = EnsembleArray(nl(n_neurons), dims, radius=radius) nengo.Connection(inputA, A.input) nengo.Connection(inputB, B.input) nengo.Connection(A.output, cconv.A) nengo.Connection(B.output, cconv.B) nengo.Connection(cconv.output, res.input) A_p = nengo.Probe(A.output, synapse=0.03) B_p = nengo.Probe(B.output, synapse=0.03) res_p = nengo.Probe(res.output, synapse=0.03) # --- simulation sim = Simulator(model) sim.run(1.0) t = sim.trange() with Plotter(Simulator, nl) as plt: def plot(actual, probe, title=""): ref_y = np.tile(actual, (len(t), 1)) sim_y = sim.data[probe] colors = ['b', 'g', 'r', 'c', 'm', 'y'] for i in range(min(dims, len(colors))): plt.plot(t, ref_y[:, i], '--', color=colors[i]) plt.plot(t, sim_y[:, i], '-', color=colors[i]) plt.title(title) plt.subplot(311) plot(a, A_p, title="A") plt.subplot(312) plot(b, B_p, title="B") plt.subplot(313) plot(result, res_p, title="Result") plt.tight_layout() plt.savefig('test_circularconv.test_circularconv_%d.pdf' % dims) plt.close() # --- results tmask = t > (0.5 + sim.dt / 2) assert sim.data[A_p][tmask].shape == (499, dims) a_sim = sim.data[A_p][tmask].mean(axis=0) b_sim = sim.data[B_p][tmask].mean(axis=0) res_sim = sim.data[res_p][tmask].mean(axis=0) rtol, atol = 0.1, 0.05 assert np.allclose(a, a_sim, rtol=rtol, atol=atol) assert np.allclose(b, b_sim, rtol=rtol, atol=atol) assert rmse(result, res_sim) < 0.075
import nengo model = nengo.Network() with model: # create the neurons a = nengo.Ensemble(n_neurons=50, dimensions=2) # anything that's not neurons is a Node stim = nengo.Node([0,0]) # make the connection nengo.Connection(stim, a)
model.loc_to_object = spa.AssociativeMemory(input_vocab=obj_vocab, output_vocab=obj_id_vocab, input_keys=objects, output_keys=objects, wta_output=False, threshold=.5) model.obj_to_action = spa.AssociativeMemory(input_vocab=act_vocab, output_vocab=act_id_vocab, input_keys=actions, output_keys=actions, wta_output=True, threshold=.9) model.action_to_effect = spa.AssociativeMemory(input_vocab=act_id_vocab, output_vocab=base_vocab, input_keys=actions, output_keys=effects, wta_output=True) model.cleanup_action = spa.AssociativeMemory(act_id_vocab, threshold=0.15, wta_output=True) model.action_to_precon = AssociativeMemory(input_vectors=inp_vecs, output_vectors=out_vecs) nengo.Connection(model.location.output, model.loc_to_object.input, transform=base_vocab['LOCATION'].get_convolution_matrix()) nengo.Connection(model.m_goal.state.output, model.loc_to_object.input, transform=base_vocab['GOAL'].get_convolution_matrix()) nengo.Connection(model.loc_to_object.output, model.obj_to_action.input, transform=base_vocab['OBJECT'].get_convolution_matrix()) nengo.Connection(model.i_goal.state.output, model.obj_to_action.input, transform=base_vocab['EFFECTS'].get_convolution_matrix()) # Multiple Precons Test model.get_precon = spa.State(dimensions=D, vocab=act_id_vocab) nengo.Connection(model.get_precon.output, model.action_to_precon.input) nengo.Connection(model.action_to_precon.output, model.precon.state.input) # Stack implementation model.push = spa.Memory(dimensions=D, vocab=act_id_vocab, synapse=0.005, tau=0.05)
import nengo N = 100 D = 3 model = nengo.Network() with model: stim = nengo.Node([0] * D) ens = nengo.Ensemble(n_neurons=N, dimensions=D) nengo.Connection(stim, ens)
# build model model = nengo.Network(label="Combining") with model: A = nengo.Ensemble(100, dimensions=1) B = nengo.Ensemble(100, dimensions=1) output = nengo.Ensemble(200, dimensions=2, label="2D population") with model: sin = nengo.Node(output=np.sin) cos = nengo.Node(output=np.cos) with model: nengo.Connection(sin, A) nengo.Connection(cos, B) nengo.Connection(A, output[1]) nengo.Connection(B, output[0]) # probes with model: sin_probe = nengo.Probe(sin) cos_probe = nengo.Probe(cos) A_probe = nengo.Probe(A, synapse=0.01) B_probe = nengo.Probe(B, synapse=0.01) out_probe = nengo.Probe(output, synapse=0.01)
height=dim_env, width=dim_env, fov=125, normalize_sensor_output=True, reward_location=(10,10), maze_shape=MazeShape.MAZE_HANLON, maze_kwargs={'n_objects': 15} ), size_in=4, size_out=2*n_sensors + 6, ) linear_velocity = nengo.Ensemble(n_neurons=n_motor, dimensions=1) angular_velocity = nengo.Ensemble(n_neurons=n_motor, dimensions=1) nengo.Connection(map_selector, environment[3]) # dimension 4 ang_sensors = nengo.Node(output=lambda t, x: x, size_in=n_sensors) nengo.Connection(environment[5:n_sensors+5], ang_sensors) nengo.Connection(linear_velocity, environment[0], synapse=tau_sensory) # dimension 1 nengo.Connection(angular_velocity, environment[2], synapse=tau_sensory) ang_control_func = partial(sense_to_ang_vel, n_sensors = n_sensors) nengo.Connection(ang_sensors, angular_velocity, function=ang_control_func, synapse=tau_sensory) lin_control_func = partial(sense_to_lin_vel, n_sensors = n_sensors) nengo.Connection(ang_sensors, linear_velocity, function=lin_control_func,
import nengo model = nengo.Network() with model: stim = nengo.Node([0]) a = nengo.Ensemble(n_neurons=100, dimensions=1) b = nengo.Ensemble(n_neurons=100, dimensions=1) nengo.Connection(a, b, synapse=0.005) nengo.Connection(stim, a) def recurrent(b): return 1 + b #nengo.Connection(b, b, function=recurrent)
#goalie = desired[0] #defenders = desired[1]''' # PD Control desired = nengo.Node(get_pos, size_out=1) #desired = nengo.Node([0]) derror = nengo.Ensemble(n_neurons=64, dimensions=1) error = nengo.Ensemble(n_neurons=64, dimensions=1) PD = nengo.Ensemble(n_neurons=64, dimensions=1) motor = nengo.Node(ard_output, size_in=1, size_out=0) position = nengo.Node(table_inout, size_in=0, size_out=1) nengo.Connection(desired, error, transform=-1, synapse=0.005) nengo.Connection(position, error, transform=-1, synapse=0.005) nengo.Connection(error, derror, transform=1, synapse=0.005) nengo.Connection(error, derror, transform=-1, synapse=0.01) nengo.Connection(error, PD, transform=.7, synapse=0.005) nengo.Connection(PD, motor, synapse=0.01) nengo.Connection(derror, PD, transform=3., synapse=0.005) deriv_vis = nengo.Node(None, size_in=2, label="error") nengo.Connection(error, deriv_vis[0], synapse=None) nengo.Connection(derror, deriv_vis[1], synapse=None) # Learning adaptive = nengo.Ensemble(400, dimensions=2) nengo.Connection(position, adaptive[0], transform=1000, synapse=None)
# second). # # But, in this case, we make the Ensemble be 3-dimensional and use the third # dimension (x[2]) to represent s. You can control it with a separate input. # This shows how neurons can affect the pattern of activity of another # group of neurons. import nengo model = nengo.Network() with model: x = nengo.Ensemble(n_neurons=400, dimensions=3) synapse = 0.1 def oscillator(x): r = 1 s = 10 * x[2] return [ synapse * -x[1] * s + x[0] * (r - x[0]**2 - x[1]**2) + x[0], synapse * x[0] * s + x[1] * (r - x[0]**2 - x[1]**2) + x[1] ] nengo.Connection(x, x[:2], synapse=synapse, function=oscillator) stim_speed = nengo.Node(0) speed = nengo.Ensemble(n_neurons=50, dimensions=1) nengo.Connection(stim_speed, speed) nengo.Connection(speed, x[2])
return sampled / np.linalg.norm(sampled) * sample_length(t) d = 32 n = 25 duration = 120. with nengo.Network(seed=1) as model: pre = nengo.Ensemble(n * d, d, intercepts=nengo.dists.CosineSimilarity(d + 2), eval_points=nengo.dists.CosineSimilarity(d + 2), neuron_type=nengo.LIF()) post = nengo.Node(size_in=n * d) def_post = nengo.Node(size_in=d) def_conn = nengo.Connection(pre, def_post) in_signal = nengo.Node(SignalGenerator(duration), size_out=d) nengo.Connection(in_signal, pre) conn = nengo.Connection(pre, post, function=lambda x: np.zeros(n * d), learning_rule_type=WeightSymmetryLR(1e-13)) square = nengo.networks.EnsembleArray(n, d) nengo.Connection(pre, square.input) nengo.Connection(square.add_output('square', np.square), conn.learning_rule, transform=np.ones((1, d)))
def __init__(self, n_neurons, n_ensembles, ens_dimensions=1, neuron_nodes=False, label=None, seed=None, add_to_container=None, **ens_kwargs): if "dimensions" in ens_kwargs: raise TypeError( "'dimensions' is not a valid argument to EnsembleArray. " "To set the number of ensembles, use 'n_ensembles'. To set " "the number of dimensions per ensemble, use 'ens_dimensions'.") super(EnsembleArray, self).__init__(label, seed, add_to_container) self.config[nengo.Ensemble].update(ens_kwargs) label_prefix = "" if label is None else label + "_" self.n_neurons = n_neurons self.n_ensembles = n_ensembles self.dimensions_per_ensemble = ens_dimensions self.ea_ensembles = [] with self: self.input = nengo.Node(size_in=self.dimensions, label="input") if neuron_nodes: self.neuron_input = nengo.Node(size_in=n_neurons * n_ensembles, label="neuron_input") self.neuron_output = nengo.Node(size_in=n_neurons * n_ensembles, label="neuron_output") for i in range(n_ensembles): e = nengo.Ensemble(n_neurons, self.dimensions_per_ensemble, label=label_prefix + str(i)) nengo.Connection(self.input[i * ens_dimensions:(i + 1) * ens_dimensions], e, synapse=None) if (neuron_nodes and not isinstance(e.neuron_type, nengo.Direct)): nengo.Connection(self.neuron_input[i * n_neurons:(i + 1) * n_neurons], e.neurons, synapse=None) nengo.Connection(e.neurons, self.neuron_output[i * n_neurons:(i + 1) * n_neurons], synapse=None) self.ea_ensembles.append(e) if neuron_nodes and isinstance(e.neuron_type, nengo.Direct): warnings.warn("Creating neuron nodes in an EnsembleArray" " with Direct neurons") self.add_output('output', function=None)
# (or any other linear system). # Create the model object import nengo from nengo.utils.functions import piecewise model = nengo.Network(label='Oscillator') with model: # Create the ensemble for the oscillator neurons = nengo.Ensemble(200, dimensions=2, label="neurons") # Create an input signal input = nengo.Node(piecewise({0: [1, 0], 0.1: [0, 0]}), label="input") # Connect the input signal to the neural ensemble nengo.Connection(input, neurons) # Create the feedback connection nengo.Connection(neurons, neurons, transform=[[1, 1], [-1, 1]], synapse=0.1) input_probe = nengo.Probe(input, 'output') neuron_probe = nengo.Probe(neurons, 'decoded_output', synapse=0.1) import nengo_gui gui = nengo_gui.Config() gui[model].scale = 2.3147645815836775 gui[model].offset = 163.4299195712814, 173.37512132629337 gui[neurons].pos = 150.000, 0.000
def test_eval_points(Simulator, nl_nodirect, plt, seed, rng, logger): n = 100 d = 5 filter = 0.08 eval_points = np.logspace(np.log10(300), np.log10(5000), 11) eval_points = np.round(eval_points).astype('int') max_points = eval_points.max() n_trials = 1 rmses = np.nan * np.zeros((len(eval_points), n_trials)) for j in range(n_trials): points = rng.normal(size=(max_points, d)) points *= (rng.uniform(size=max_points) / norm(points, axis=-1))[:, None] rng_j = np.random.RandomState(348 + j) seed = 903824 + j # generate random input in unit hypersphere x = rng_j.normal(size=d) x *= rng_j.uniform() / norm(x) for i, n_points in enumerate(eval_points): model = nengo.Network(seed=seed) with model: model.config[nengo.Ensemble].neuron_type = nl_nodirect() u = nengo.Node(output=x) a = nengo.Ensemble(n * d, dimensions=d, eval_points=points[:n_points]) nengo.Connection(u, a, synapse=0) up = nengo.Probe(u) ap = nengo.Probe(a) with Timer() as timer: sim = Simulator(model) sim.run(10 * filter) sim.close() t = sim.trange() xt = nengo.Lowpass(filter).filtfilt(sim.data[up], dt=sim.dt) yt = nengo.Lowpass(filter).filtfilt(sim.data[ap], dt=sim.dt) t0 = 5 * filter t1 = 7 * filter tmask = (t > t0) & (t < t1) rmses[i, j] = rms(yt[tmask] - xt[tmask]) logger.info('trial %d', j) logger.info(' n_points: %d', n_points) logger.info(' duration: %0.3f s', timer.duration) # subtract out mean for each model rmses_norm = rmses - rmses.mean(0, keepdims=True) mean = rmses_norm.mean(1) low = rmses_norm.min(1) high = rmses_norm.max(1) plt.semilogx(eval_points, mean, 'k-') plt.semilogx(eval_points, high, 'r-') plt.semilogx(eval_points, low, 'b-') plt.xlim([eval_points[0], eval_points[-1]]) plt.xticks(eval_points, eval_points)
from nengo_interfaces.airsim import AirSim import nengo airsim = AirSim() airsim.connect() with nengo.Network() as net: input = nengo.Node([1000] * 4) anode = nengo.Node(airsim, label="AirSim") nengo.Connection(input, anode) with nengo.Simulator(net) as sim: sim.run(0.1) # Eventually we should be able to do this automatically (but not yet) airsim.disconnect()
def Product(n_neurons, dimensions, input_magnitude=1., net=None): """Computes the element-wise product of two equally sized vectors. The network used to calculate the product is described in `Gosmann, 2015`_. A simpler version of this network can be found in the `Multiplication example <http://pythonhosted.org/nengo/examples/multiplication.html>`_. .. _Gosmann, 2015: http://nbviewer.jupyter.org/github/ctn-archive/technical-reports/blob/ master/Precise-multiplications-with-the-NEF.ipynb#An-alternative-network Parameters ---------- n_neurons : int Number of neurons per dimension in the vector. .. note:: These neurons will be distributed evenly across two ensembles. If an odd number of neurons is specified, the extra neuron will not be used. dimensions : int Number of dimensions in each of the vectors to be multiplied. input_magnitude : float, optional (Default: 1.) The expected magnitude of the vectors to be multiplied. This value is used to determine the radius of the ensembles computing the element-wise product. net : Network, optional (Default: None) A network in which the network components will be built. This is typically used to provide a custom set of Nengo object defaults through modifying ``net.config``. Returns ------- net : Network The newly built product network, or the provided ``net``. Attributes ---------- net.A : Node The first vector to be multiplied. net.B : Node The second vector to be multiplied. net.output : Node The resulting product. net.sq1 : EnsembleArray Represents the first squared term. See `Gosmann, 2015`_ for details. net.sq2 : EnsembleArray Represents the second squared term. See `Gosmann, 2015`_ for details. """ if net is None: net = nengo.Network(label="Product") with net: net.A = nengo.Node(size_in=dimensions, label="A") net.B = nengo.Node(size_in=dimensions, label="B") net.output = nengo.Node(size_in=dimensions, label="output") net.sq1 = EnsembleArray( max(1, n_neurons // 2), n_ensembles=dimensions, ens_dimensions=1, radius=input_magnitude * np.sqrt(2)) net.sq2 = EnsembleArray( max(1, n_neurons // 2), n_ensembles=dimensions, ens_dimensions=1, radius=input_magnitude * np.sqrt(2)) tr = 1. / np.sqrt(2.) nengo.Connection(net.A, net.sq1.input, transform=tr, synapse=None) nengo.Connection(net.B, net.sq1.input, transform=tr, synapse=None) nengo.Connection(net.A, net.sq2.input, transform=tr, synapse=None) nengo.Connection(net.B, net.sq2.input, transform=-tr, synapse=None) sq1_out = net.sq1.add_output('square', np.square) nengo.Connection(sq1_out, net.output, transform=.5, synapse=None) sq2_out = net.sq2.add_output('square', np.square) nengo.Connection(sq2_out, net.output, transform=-.5, synapse=None) return net
# The only difference between this network and most # models you've seen so far is that we're going to # set the decoder solver in the communication channel # to generate a full connection weight matrix # which we can then learn using typical delta learning rules. # In[ ]: model = nengo.Network() with model: sin = nengo.Node(lambda t: np.sin(t * 4)) pre = nengo.Ensemble(100, dimensions=1) post = nengo.Ensemble(100, dimensions=1) nengo.Connection(sin, pre) conn = nengo.Connection(pre, post, solver=nengo.solvers.LstsqL2(weights=True)) pre_p = nengo.Probe(pre, synapse=0.01) post_p = nengo.Probe(post, synapse=0.01) # In[ ]: # Verify that it does a communication channel with nengo.Simulator(model) as sim: sim.run(2.0) plt.plot(sim.trange(), sim.data[pre_p], label="Pre") plt.plot(sim.trange(), sim.data[post_p], label="Post")
html = html + '</br>' + '<h3>Final Expression = ' + final_expression + '</h3>' html = html + '</br>' + '<h3>Var = ' + var + '</h3>' html = html + '</br>' + '<h3>Count = ' + str(count) + '</h3>' arm_function._nengo_html_ = html return angles inputs = nengo.Node([0.0, 0.0, 0.0]) ensembles = nengo.Ensemble(n_neurons=1500, dimensions=3) outputs = nengo.Node(arm_function, size_in=3) nengo.Connection(inputs, ensembles) nengo.Connection(ensembles, outputs) #operator input ensemble. op_inputs = nengo.Node( [ 0.0, 0.0, 0.0 ] ) op_ensembles = nengo.Ensemble(n_neurons=1500, dimensions=3) op_output = nengo.Node( oper_fun, size_in=3 ) nengo.Connection(op_inputs, op_ensembles) nengo.Connection(op_ensembles, op_output) final_ensemble = nengo.Ensemble(n_neurons=1500, dimensions=3) nengo.Connection(outputs, final_ensemble) nengo.Connection(op_ensembles, op_output)
def __init__(self, n_neurons, dimensions, feedback=1.0, difference_gain=1.0, recurrent_synapse=0.1, difference_synapse=None, **kwargs): if "net" in kwargs: raise ObsoleteError("The 'net' argument is no longer supported.") kwargs.setdefault("label", "Input gated memory") super().__init__(**kwargs) if difference_synapse is None: difference_synapse = recurrent_synapse n_total_neurons = n_neurons * dimensions with self: # integrator to store value self.mem = EnsembleArray(n_neurons, dimensions, label="mem") nengo.Connection( self.mem.output, self.mem.input, transform=feedback, synapse=recurrent_synapse, ) # calculate difference between stored value and input self.diff = EnsembleArray(n_neurons, dimensions, label="diff") nengo.Connection(self.mem.output, self.diff.input, transform=-1) # feed difference into integrator nengo.Connection( self.diff.output, self.mem.input, transform=difference_gain, synapse=difference_synapse, ) # gate difference (if gate==0, update stored value, # otherwise retain stored value) self.gate = nengo.Node(size_in=1) self.diff.add_neuron_input() nengo.Connection( self.gate, self.diff.neuron_input, transform=np.ones((n_total_neurons, 1)) * -10, synapse=None, ) # reset input (if reset=1, remove all values, and set to 0) self.reset = nengo.Node(size_in=1) nengo.Connection( self.reset, self.mem.add_neuron_input(), transform=np.ones((n_total_neurons, 1)) * -3, synapse=None, ) self.input = self.diff.input self.output = self.mem.output
def connect_to(self, sink, **kwargs): return nengo.Connection(self.construct(), sink, **kwargs)