def model(self, p): vocab_space = spa.Vocabulary(p.D_space, strict=False) vocab_space.populate('OBJ1; OBJ2; OBJ3; OBJ4; OBJ5; OBJ6; X; Y') vocab_rule = spa.Vocabulary(p.D) vocab_rule.populate( 'OBJ1; OBJ2; OBJ3; OBJ4; OBJ5; OBJ6; BELOW; ABOVE; LEFT; RIGHT; S; O; V' ) vocab_obj = vocab_rule.create_subset( ['OBJ1', 'OBJ2', 'OBJ3', 'OBJ4', 'OBJ5', 'OBJ6']) vocab_rel = vocab_rule.create_subset( ['BELOW', 'ABOVE', 'LEFT', 'RIGHT']) model = spa.Network() with model: rule = spa.State(vocab_rule) objs = spa.State(feedback=1, vocab=vocab_space) subject = spa.WTAAssocMem(threshold=0.3, input_vocab=vocab_obj) object = spa.WTAAssocMem(threshold=0.3, input_vocab=vocab_obj) relation = spa.WTAAssocMem(threshold=0.3, input_vocab=vocab_rel) status = spa.State(p.D) def input(t): t = t % 2.0 if t < 0.4: return 'OBJ1*S+LEFT*V+OBJ2*O' elif t < 0.8: return 'OBJ2*S+LEFT*V+OBJ3*O' elif t < 1.2: return 'OBJ3*S+LEFT*V+OBJ4*O' elif t < 1.6: return 'OBJ4*S+LEFT*V+OBJ5*O' elif t < 2.0: return 'OBJ5*S+LEFT*V+OBJ6*O' input = spa.Transcode(input, output_vocab=vocab_rule) speed = 0.7 separation = 0.7 strength = 0.5 spa.Actions(''' input -> rule reinterpret(rule*~S) ->subject reinterpret(rule*~O) ->object reinterpret(rule*~V) ->relation ifmax (dot(relation, BELOW-ABOVE-LEFT-RIGHT)*strength + (dot(objs, translate(subject)*Y) - dot(objs, translate(object)*Y)) + separation): BAD -> status speed*Y*translate(object)-speed*Y*translate(subject) -> objs elifmax (dot(relation, BELOW-ABOVE-LEFT-RIGHT)*strength - (dot(objs, translate(subject)*Y) - dot(objs, translate(object)*Y)) - separation): GOOD -> status ifmax (dot(relation, ABOVE-BELOW-LEFT-RIGHT)*strength + (dot(objs, translate(subject)*Y) - dot(objs, translate(object)*Y)) + separation): BAD -> status speed*Y*translate(object)-speed*Y*translate(subject) -> objs elifmax (dot(relation, ABOVE-BELOW-LEFT-RIGHT)*strength - (dot(objs, translate(subject)*Y) - dot(objs, translate(object)*Y)) - separation): GOOD -> status ifmax (dot(relation, LEFT-RIGHT-ABOVE-BELOW)*strength + (dot(objs, translate(subject)*X) - dot(objs, translate(object)*X)) + separation): BAD -> status speed*X*translate(object)-speed*X*translate(subject) -> objs elifmax (dot(relation, LEFT-RIGHT-ABOVE-BELOW)*strength - (dot(objs, translate(subject)*X) - dot(objs, translate(object)*X)) - separation): GOOD -> status ifmax (dot(relation, RIGHT-ABOVE-BELOW-LEFT)*strength + (dot(objs, translate(subject)*X) - dot(objs, translate(object)*X)) + separation): BAD -> status speed*X*translate(object)-speed*X*translate(subject) -> objs elifmax (dot(relation, RIGHT-ABOVE-BELOW-LEFT)*strength - (dot(objs, translate(subject)*X) - dot(objs, translate(object)*X)) - separation): GOOD -> status ''') def display_node(t, x): return x display = nengo.Node(display_node, size_in=12) nengo.Connection(objs.output, display[0], transform=[vocab_space.parse('OBJ1*X').v]) nengo.Connection(objs.output, display[1], transform=[vocab_space.parse('OBJ1*Y').v]) nengo.Connection(objs.output, display[2], transform=[vocab_space.parse('OBJ2*X').v]) nengo.Connection(objs.output, display[3], transform=[vocab_space.parse('OBJ2*Y').v]) nengo.Connection(objs.output, display[4], transform=[vocab_space.parse('OBJ3*X').v]) nengo.Connection(objs.output, display[5], transform=[vocab_space.parse('OBJ3*Y').v]) nengo.Connection(objs.output, display[6], transform=[vocab_space.parse('OBJ4*X').v]) nengo.Connection(objs.output, display[7], transform=[vocab_space.parse('OBJ4*Y').v]) nengo.Connection(objs.output, display[8], transform=[vocab_space.parse('OBJ5*X').v]) nengo.Connection(objs.output, display[9], transform=[vocab_space.parse('OBJ5*Y').v]) nengo.Connection(objs.output, display[10], transform=[vocab_space.parse('OBJ6*X').v]) nengo.Connection(objs.output, display[11], transform=[vocab_space.parse('OBJ6*Y').v]) for net in model.networks: if net.label is not None: if net.label.startswith('dot'): net.label = '' if net.label.startswith('channel'): net.label = '' with model: self.result = nengo.Probe(display, synapse=0.03) return model
# #Nengo Example: Addition # In this example, we will construct a network that adds two inputs. The # network utilizes two communication channels into the same neural population. # Addition is thus somewhat free, since the incoming currents from # different synaptic connections interact linearly (though two inputs dont # have to combine in this way: see the combining demo). import nengo model = nengo.Network() with model: # Create 3 ensembles each containing 100 leaky integrate-and-fire neurons a = nengo.Ensemble(n_neurons=100, dimensions=1) b = nengo.Ensemble(n_neurons=100, dimensions=1) c = nengo.Ensemble(n_neurons=100, dimensions=1) # Create input nodes representing constant values stim_a = nengo.Node(0.5) stim_b = nengo.Node(0.3) # Connect the input nodes to the appropriate ensembles nengo.Connection(stim_a, a) nengo.Connection(stim_b, b) # Connect input ensembles a and b to output ensemble c nengo.Connection(a, c) nengo.Connection(b, c)
import nengo import numpy as np D = 10 model = nengo.Model('Basal Ganglia') with model: input = nengo.Node([1] * D, label='input') def printout(t, x): print t, x output = nengo.Node(printout, size_in=D, label='output') bg = nengo.networks.BasalGanglia(D, 20, label='BG') nengo.Connection(input, bg.input, filter=0.01) nengo.Connection(bg.output, output, filter=0.01) import nengo_spinnaker sim = nengo_spinnaker.Simulator(model) sim.builder.print_connections() #sim = nengo.Simulator(model) #sim.run(0.1)
In this code, we create a simple input node. The Node takes in a lambda t value, representing time, and produces an output proportional to time. We can graph this input node over the course of a second. """ def aligned(n_neurons, radius=0.9): intercepts = np.linspace(-radius, radius, n_neurons) encoders = np.tile([[1], [-1]], (n_neurons // 2, 1)) intercepts *= encoders[:, 0] return intercepts, encoders model = nengo.Network(label="NEF summary") with model: input = nengo.Node(lambda t: t * 2 - 1) input_probe = nengo.Probe(input) with nengo.Simulator(model) as sim: sim.run(1.0) plt.figure() plt.plot(sim.trange(), sim.data[input_probe], lw=2) plt.title("Input signal") plt.xlabel("Time (s)") plt.xlim(0, 1) plt.show() hide_input() """ Notes: In this code, we create a simple input node. The Node takes in
# neurons_per_dim = 64 neurons_per_dim = 256 # neurons_per_dim = 1024 n_neurons = dims * neurons_per_dim r = 1e-4 W0 = np.random.uniform(-r, r, size=(n_neurons, n_neurons)) runtime = 1.0 # --- model target_fn = lambda x: x**2 pes = nengo.PES() with nengo.Network(label="ProfilePES", seed=3) as model: x = nengo.Node(nengo.processes.WhiteSignal(runtime, high=1.0), size_out=dims) y = nengo.Node(size_in=dims) nengo.Connection(x, y, function=target_fn) a = nengo.Ensemble(n_neurons, dims) b = nengo.Ensemble(n_neurons, dims) nengo.Connection(x, a, synapse=None) e = nengo.Ensemble(dims * neurons_per_dim, dims) # nengo.Connection(b, e) # time-consuming decoder finding involved nengo.Connection(y, e, transform=-1) c = nengo.Connection(a.neurons, b.neurons, transform=W0, learning_rule_type=pes)
def __init__(self, motor_gain=2.0, label=None, seed=None, add_to_container=None): """ :param label: Name of the model. Defaults to None. :type label: str :param seed: Random number seed that will be fed to the random number generator. Setting this seed makes the creation of the model a deterministic process; however, each new ensemble in the network advances the random number generator, so if the network creation code changes, the entire model changes. :type seed: int :param add_to_container: Determines if this Network will be added to the current container. Defaults to true iff currently with a Network :type add_to_container: bool """ super(Robot, self).__init__(label, seed, add_to_container) n_neurons = 100 tau = 0.1 self.motor_gain = motor_gain def error(vector): return np.sign(vector[0] - vector[1]) * ( (vector[0] - vector[1])**2) self.servos = Container() self.controls = Container() # white_noise_process = WhiteNoise(Uniform(-1., 1.), scale=True) # white_noise_process.default_size_out = 6 with self: # Inputs self.action = ControlSignal(container=self.controls, size_out=3, label='action') self.direction = ControlSignal(container=self.controls, size_out=2, label='direction') self.sound = nengo.Node( lambda t: np.sin(1 / 1.6 * t) - np.cos(2 * t)) self.silence = ControlSignal(container=self.controls, size_out=3, label='silence') self.silence_ens = nengo.Ensemble(3 * n_neurons, 3) nengo.Connection(self.silence, self.silence_ens, synapse=tau) self.zero = ControlSignal(container=self.controls, size_out=3, label="zero") self.zero_ens = nengo.Ensemble(3 * n_neurons, 3) nengo.Connection(self.zero, self.zero_ens, synapse=tau) # Initialisation self.controls.update(self.action, np.asarray([1., 0., 0.])) self.controls.update(self.direction, np.asarray([1., 0.])) self.controls.update(self.silence, np.asarray([.3, .7, 1])) self.controls.update(self.zero, np.asarray([-.7, -.5, -.6])) # Hidden layer self.left_current_position = nengo.networks.EnsembleArray( n_neurons, 3) self.right_current_position = nengo.networks.EnsembleArray( n_neurons, 3) self.left_target_position = nengo.networks.EnsembleArray( n_neurons, 3) self.right_target_position = nengo.networks.EnsembleArray( n_neurons, 3) self.left_error = nengo.networks.EnsembleArray(5 * n_neurons, n_ensembles=3, ens_dimensions=2, radius=1.3) self.right_error = nengo.networks.EnsembleArray(5 * n_neurons, n_ensembles=3, ens_dimensions=2, radius=1.3) nengo.Connection(self.left_target_position.output, self.left_error.input[[0, 2, 4]]) nengo.Connection(self.left_current_position.output, self.left_error.input[[1, 3, 5]]) nengo.Connection(self.right_target_position.output, self.right_error.input[[0, 2, 4]]) nengo.Connection(self.right_current_position.output, self.right_error.input[[1, 3, 5]]) nengo.Connection(self.silence_ens, self.left_target_position.input) nengo.Connection(self.silence_ens, self.right_target_position.input) nengo.Connection(self.zero_ens, self.left_error.input[[0, 2, 4]], transform=np.eye(3)) nengo.Connection(self.zero_ens, self.right_error.input[[0, 2, 4]], transform=np.eye(3)) # Feedback left_error = self.left_error.add_output("error", error) right_error = self.right_error.add_output("error", error) nengo.Connection(left_error, self.left_current_position.input, synapse=tau) nengo.Connection(right_error, self.right_current_position.input, synapse=tau) nengo.Connection(self.left_current_position.output, self.left_current_position.input, synapse=2 * tau) nengo.Connection(self.right_current_position.output, self.right_current_position.input, synapse=2 * tau) # Output self.left_motors = nengo.Node(size_in=3) self.right_motors = nengo.Node(size_in=3) nengo.Connection(left_error, self.left_motors, synapse=tau, transform=np.eye(3) * self.motor_gain) nengo.Connection(right_error, self.right_motors, synapse=tau, transform=np.eye(3) * self.motor_gain) self.left_servos = Servo(container=self.servos, size_in=3, label="left_servos") self.right_servos = Servo(container=self.servos, size_in=3, label="right_servos") nengo.Connection(self.left_current_position.output, self.left_servos, synapse=tau) nengo.Connection(self.right_current_position.output, self.right_servos, synapse=tau) # Action selection # The 2 actions are: silence and gesture # Gesture inhibits a target function (goes idle) # Silence inhibits sound and give a target position of its own self.bg = nengo.networks.BasalGanglia(3, output_weight=-3) nengo.Connection(self.action, self.bg.input) self.action_thalamus = nengo.networks.Thalamus(3) nengo.Connection(self.bg.output, self.action_thalamus.input, synapse=tau) # Sound connections self.rhythm = nengo.Ensemble(n_neurons, 1) nengo.Connection(self.sound, self.rhythm, transform=[.9]) nengo.Connection(self.rhythm, self.left_target_position.input[[0]], transform=[[-1]], synapse=tau) nengo.Connection(self.rhythm, self.right_target_position.input[[0]], transform=[[1.]], synapse=tau) nengo.Connection(self.rhythm, self.left_target_position.input[[1]], transform=[[1.]], synapse=tau) nengo.Connection(self.rhythm, self.right_target_position.input[[1]], transform=[[-1.]], synapse=tau) nengo.Connection(self.rhythm, self.left_target_position.input[[2]], transform=[[-1.]], synapse=tau) nengo.Connection(self.rhythm, self.right_target_position.input[[2]], transform=[[1.]], synapse=tau) # If silencing, inhibit rhythm nengo.Connection(self.action_thalamus.output[1], self.rhythm.neurons, transform=[[-2.]] * self.rhythm.n_neurons, synapse=tau) # If silencing, also inhibit "zero" nengo.Connection(self.action_thalamus.output[1], self.zero_ens.neurons, transform=[[-2.]] * self.silence_ens.n_neurons, synapse=tau) # When silencing, provide a target function (in degrees) for each of the the joints # Done. Provided from the "outside" nengo.Connection(self.action_thalamus.output[0], self.silence_ens.neurons, transform=[[-2.]] * self.silence_ens.n_neurons, synapse=tau) # Idling inhibits silence and rhythm nengo.Connection(self.action_thalamus.output[2], self.silence_ens.neurons, transform=[[-2.]] * self.silence_ens.n_neurons, synapse=tau) nengo.Connection(self.action_thalamus.output[2], self.rhythm.neurons, transform=[[-2.]] * self.rhythm.n_neurons, synapse=tau) # Select an arm for silencing / gesturing self.left_dp = DotProduct() self.right_dp = DotProduct() self.left = nengo.Node(output=LEFT) self.right = nengo.Node(output=RIGHT) nengo.Connection(self.direction, self.left_dp.in_A) nengo.Connection(self.left, self.left_dp.in_B) nengo.Connection(self.direction, self.right_dp.in_A) nengo.Connection(self.right, self.right_dp.in_B) L = nengo.Ensemble(100, 1) R = nengo.Ensemble(100, 1) nengo.Connection(self.left_dp.output, L) nengo.Connection(self.right_dp.output, R) self.arm_selector = nengo.networks.BasalGanglia(2) nengo.Connection(L, self.arm_selector.input[0], function=lambda x: np.abs(x)) nengo.Connection(R, self.arm_selector.input[1], function=lambda x: np.abs(x)) # Basal ganglia will now inhibit the opposing arm for ensemble in self.left_target_position.all_ensembles: nengo.Connection(self.arm_selector.output[1], ensemble.neurons, transform=[[1]] * ensemble.n_neurons) for ensemble in self.right_target_position.all_ensembles: nengo.Connection(self.arm_selector.output[0], ensemble.neurons, transform=[[1]] * ensemble.n_neurons)
if x > -6: sig = 1 / (1 + math.exp(-x)) else: sig = 0 return sig def sigmoid_grad(x): sig_grad = sigmoid(x) * (1 - sigmoid(x)) return sig_grad with model: #input vector. Pixels to be identified inp = [1, 0] Input = nengo.Node(inp) Input_Node_1 = nengo.Ensemble(100, dimensions=1) Input_Node_2 = nengo.Ensemble(100, dimensions=1) Hidden_Node_1 = nengo.Ensemble(100, dimensions=1) Hidden_Node_2 = nengo.Ensemble(100, dimensions=1) Output_Node_1 = nengo.Ensemble(100, dimensions=1) Output_Node_2 = nengo.Ensemble(100, dimensions=1) nengo.Connection(Input[0], Input_Node_1) nengo.Connection(Input[1], Input_Node_2) nengo.Connection(Input_Node_1, Hidden_Node_1, transform=weights1[0][0], function=sigmoid)
"-t", type=float, default=2, help="The time in seconds over which to run the simulator.", ) args = parser.parse_args() def input_func(t): return [np.sin(t * 10)] with nengo.Network() as model: # Reference signal input_node = nengo.Node(input_func, label="input signal") # FPGA neural ensemble pes_ens = FpgaPesEnsembleNetwork( args.board, n_neurons=100, dimensions=1, learning_rate=0, label="ensemble" ) nengo.Connection(input_node, pes_ens.input) # Reference value passthrough node ref_node = nengo.Node(size_in=1) nengo.Connection(input_node, ref_node) # Output probes p_fpga = nengo.Probe(pes_ens.output, synapse=0.005) p_ref = nengo.Probe(ref_node, synapse=0.005)
with model: vocab = spa.Vocabulary(semantic_pointer_dimensions) # Build ensemble to represent input semantic pointer state = nengo.Ensemble(64 * semantic_pointer_dimensions, semantic_pointer_dimensions, label='state') # Build ensemble array with 64 neurons per dimension to represent Q values Q = nengo.networks.EnsembleArray(64, num_inputs, label='Q') vocab_strings = [num_to_string(i, 1) for i in range(num_inputs)] vocab_transform = [vocab.parse(s).v for s in vocab_strings] # Create node to provide sequence of vocab semantic pointers input_node = nengo.Node(lambda t: vocab_transform[int( math.floor(t / input_presentation_time)) % num_inputs]) # Transform state in semantic pointer to Q values based on similarity with vocab state.vocab = vocab nengo.Connection(state, Q.input, transform=vocab_transform) nengo.Connection(input_node, state) q_probe = nengo.Probe(Q.output, synapse=0.01) bg = nengo.networks.BasalGanglia(num_inputs) nengo.Connection(Q.output, bg.input) R = nengo.networks.EnsembleArray(64, num_inputs, label='R',
def add_nengo_objects(self, page): # create a Node and a Connection so the Node will be given the # data we want to show while the model is running. with page.model: self.node = nengo.Node(self.gather_data, size_in=self.obj.size_out) self.conn = nengo.Connection(self.obj, self.node, synapse=0.01)
def __init__(self, n_neurons=100, matrix_A=np.eye(3), matrix_B=np.zeros((3, 1)), radius=1.0, label=None, seed=None, add_to_container=None): """ A network that does matrix multiplication based on two previously provided matrices that have the same shapes as the ones to be multiplied. For a more detailed presentation see this_ Nengo example. .. _this: https://pythonhosted.org/nengo/examples/matrix_multiplication.html :param n_neurons: The number of neurons. :type n_neurons: int :param matrix_A: A matrix that looks like the first one to be multiplied (the shape is the same, values are irrelevant) :type matrix_A: numpy.ndarray :param matrix_B: A matrix that looks like the first one to be multiplied (the shape is the same, values are irrelevant) :type matrix_B: numpy.ndarray :param radius: The min and max value to be represented :type radius: float :param label: Name of the model. Defaults to None. :type label: str :param seed: Random number seed that will be fed to the random number generator. Setting this seed makes the creation of the model a deterministic process; however, each new ensemble in the network advances the random number generator, so if the network creation code changes, the entire model changes. :type seed: int :param add_to_container: Determines if this Network will be added to the current container. Defaults to true iff currently with a Network :type add_to_container: bool """ super(MatrixMultiplication, self).__init__(label, seed, add_to_container) self.n_neurons = n_neurons self.radius = radius self.matrix_A = matrix_A self.matrix_B = matrix_B if self.matrix_A.shape[1] != self.matrix_B.shape[0]: raise ArithmeticError("Matrix dimensions must agree") with self: self.in_A = nengo.Node(size_in=matrix_A.size) self.in_B = nengo.Node(size_in=matrix_B.size) self.A = nengo.networks.EnsembleArray(self.n_neurons, matrix_A.size) self.B = nengo.networks.EnsembleArray(self.n_neurons, matrix_B.size) nengo.Connection(self.in_A, self.A.input) nengo.Connection(self.in_B, self.B.input) self.C = nengo.networks.EnsembleArray( self.n_neurons, n_ensembles=self.matrix_A.size * self.matrix_B.shape[1], ens_dimensions=2, radius=1.5 * radius, encoders=Choice([[1, 1], [-1, 1], [1, -1], [-1, -1]])) transform_a = np.zeros((self.C.dimensions, self.matrix_A.size)) transform_b = np.zeros((self.C.dimensions, self.matrix_B.size)) for i in range(self.matrix_A.shape[0]): for j in range(self.matrix_A.shape[1]): for k in range(self.matrix_B.shape[1]): tmp = (j + k * self.matrix_A.shape[1] + i * self.matrix_B.size) transform_a[tmp * 2][j + i * self.matrix_A.shape[1]] = 1 transform_b[tmp * 2 + 1][k + j * self.matrix_B.shape[1]] = 1 with self: nengo.Connection(self.A.output, self.C.input, transform=transform_a) nengo.Connection(self.B.output, self.C.input, transform=transform_b) self.D = nengo.networks.EnsembleArray( self.n_neurons, n_ensembles=self.matrix_A.shape[0] * self.matrix_B.shape[1], radius=radius) transform_c = np.zeros( (self.D.dimensions, self.matrix_A.size * self.matrix_B.shape[1])) for i in range(self.matrix_A.size * self.matrix_B.shape[1]): transform_c[i // self.matrix_B.shape[0]][i] = 1 with self: prod = self.C.add_output("product", product) nengo.Connection(prod, self.D.input, transform=transform_c) self.output = nengo.Node(size_in=self.D.dimensions) nengo.Connection(self.D.output, self.output)
def DetectChange(net=None, dimensions=1, n_neurons=50, diff_scale=0.2, item_magnitude=1): # item_magnitude: expected magnitude of one pixel in the input image if net is None: net = nengo.Network(label="Detect Change Network") with net: net.input = nengo.Node(size_in=dimensions) # Negative attention signal generation. Generates a high valued signal # when the input is changing or when there is nothing being presented # to the visual system input_diff = nengo.networks.EnsembleArray(n_neurons, dimensions, label='Input Differentiator', intercepts=Uniform(0.1, 1)) input_diff.add_output('abs', lambda x: abs(x)) nengo.Connection(net.input, input_diff.input, synapse=0.005, transform=1.0 / item_magnitude) nengo.Connection(net.input, input_diff.input, synapse=0.020, transform=-1.0 / item_magnitude) ####################################################################### net.output = nengo.Ensemble(n_neurons, 1, intercepts=Uniform(0.5, 1), encoders=Choice([[1]]), eval_points=Uniform(0.5, 1)) nengo.Connection(input_diff.abs, net.output, synapse=0.005, transform=[[diff_scale] * dimensions]) item_detect = nengo.Ensemble(n_neurons * 3, 1) nengo.Connection(net.input, item_detect, synapse=0.005, transform=[[1.0 / item_magnitude] * dimensions]) nengo.Connection(item_detect, net.output, synapse=0.005, function=lambda x: 1 - abs(x)) blank_detect = nengo.Ensemble(n_neurons, 1, intercepts=Uniform(0.7, 1), encoders=Choice([[1]]), eval_points=Uniform(0.7, 1)) nengo.Connection(item_detect, blank_detect, synapse=0.005, function=lambda x: 1 - abs(x)) nengo.Connection(blank_detect, net.output, synapse=0.01, transform=2) ####################################################################### net.input_diff = input_diff.output net.item_detect = item_detect net.blank_detect = blank_detect return net
actions[2] = hover_todo actions[3] = -angle_todo actions[1] = angle_todo return actions model = nengo.Network() with model: #pid = PIDNode(dimensions=1) environment = NengoGymLunarLander() env = nengo.Node(environment, size_in=4, size_out=8) #correction = nengo.Node(None, size_in=1, size_out=4) #sensors = nengo.Ensemble(n_neurons=500, dimensions=8, neuron_type=nengo.Direct()) #sensors = nengo.Ensemble(n_neurons=500, dimensions=2) #controls = nengo.Ensemble(n_neurons=500, dimensions=4, neuron_type=nengo.Direct()) sensors = nengo.Ensemble(n_neurons=500, dimensions=8, neuron_type=nengo.Direct()) #sensors = nengo.Ensemble(n_neurons=500, dimensions=2) controls = nengo.Ensemble(n_neurons=500, dimensions=4, neuron_type=nengo.Direct()) nengo.Connection(env, sensors) nengo.Connection(sensors, controls, function=heuristic, synapse=None) nengo.Connection(controls, env, synapse=0) #nengo.Connection(correction, env)
input_shape = (24, 24, 3) n_parallel = 16 size_out = 20 nengo_loihi.set_defaults() with nengo.Network(seed=0) as net: # set up the default parameters for ensembles/connections nengo_loihi.add_params(net) net.config[nengo.Ensemble].neuron_type = (nengo.LIF(amplitude=amp)) net.config[nengo.Ensemble].max_rates = nengo.dists.Choice([max_rate]) net.config[nengo.Ensemble].intercepts = nengo.dists.Choice([0]) net.config[nengo.Connection].synapse = None # the input node that will be used to feed in input images inp = nengo.Node(nengo.processes.PresentInput(test_data[0], presentation_time), size_out=24 * 24 * 3) # the output node provides the 10-dimensional classification out = nengo.Node(size_in=10) layer, conv = conv_layer(inp, 3, input_shape, kernel_size=(1, 1), strides=(1, 1), init=np.ones((1, 1, 3, 3))) # first layer is off-chip to translate the images into spikes net.config[layer.ensemble].on_chip = False
self.u = nengo.Node(None, size_in=1) nengo.Connection(self.u, self.pendulum[0], synapse=0) self.q = nengo.Node(None, size_in=1) self.dq = nengo.Node(None, size_in=1) nengo.Connection(self.pendulum[0], self.q, synapse=None) nengo.Connection(self.pendulum[1], self.dq, synapse=None) self.extra_mass = nengo.Node(None, size_in=1) nengo.Connection(self.extra_mass, self.pendulum[2], synapse=None) model = nengo.Network(seed=3) with model: env = PendulumNetwork(mass=4, max_torque=100, seed=1) q_target = nengo.Node(0) nengo.Connection(q_target, env.q_target, synapse=None) dq_target = nengo.Node(None, size_in=1) nengo.Connection(q_target, dq_target, synapse=None, transform=1000) nengo.Connection(q_target, dq_target, synapse=0, transform=-1000) q_diff = nengo.Ensemble(n_neurons=100, dimensions=1) nengo.Connection(env.q_target, q_diff, synapse=None) nengo.Connection(env.q, q_diff, synapse=None, transform=-1) Kp = 1.0 nengo.Connection(q_diff, env.u, transform=Kp, synapse=None)
def Ramp_Signal_Network(net=None, net_label='RAMP SIGNAL'): if net is None: net = nengo.Network(label=net_label) with net: bias_node = nengo.Node(output=1) # Ramp init hold ramp_init_hold = \ cfg.make_thresh_ens_net(0.07, thresh_func=lambda x: x) nengo.Connection(ramp_init_hold.output, ramp_init_hold.input) nengo.Connection(bias_node, ramp_init_hold.input, transform=-cfg.mtr_ramp_init_hold_transform / 5.0) # Ramp reset hold ramp_reset_hold = \ cfg.make_thresh_ens_net(0.07, thresh_func=lambda x: x) nengo.Connection(ramp_reset_hold.output, ramp_reset_hold.input) nengo.Connection(bias_node, ramp_reset_hold.input, transform=-cfg.mtr_ramp_reset_hold_transform) # Ramp integrator go signal (stops ramp integrator when <= 0.5) ramp_int_go = cfg.make_thresh_ens_net() nengo.Connection(bias_node, ramp_int_go.input) nengo.Connection(ramp_init_hold.output, ramp_int_go.input, transform=-20) nengo.Connection(ramp_reset_hold.output, ramp_int_go.input, transform=-10) # Ramp integrator stop signal (inverse of ramp integrator go signal) ramp_int_stop = cfg.make_thresh_ens_net() nengo.Connection(bias_node, ramp_int_stop.input) nengo.Connection(ramp_int_go.output, ramp_int_stop.input, transform=-2) # Ramp integrator ramp_integrator = nengo.Ensemble(cfg.n_neurons_cconv, 1, radius=1.1) nengo.Connection(ramp_int_stop.output, ramp_integrator.neurons, transform=[[-5]] * ramp_integrator.n_neurons) # -- Note: We could use the ramp_int_go signal here, but it is noisier # than the motor_go signal nengo.Connection(ramp_integrator, ramp_integrator, synapse=cfg.mtr_ramp_synapse) # Ramp integrator reset circuitry -- Works like the difference gate in # the gated integrator ramp_int_reset = nengo.Ensemble(cfg.n_neurons_cconv, 1, radius=1.1) nengo.Connection(ramp_integrator, ramp_int_reset) nengo.Connection(ramp_int_reset, ramp_integrator, transform=-10, synapse=cfg.mtr_ramp_synapse) nengo.Connection(ramp_int_go.output, ramp_int_reset.neurons, transform=[[-3]] * ramp_int_reset.n_neurons) # Ramp end reset signal generator -- Signals when the ramp integrator # reaches the top of the ramp slope. ramp_reset_thresh = cfg.make_thresh_ens_net(0.91, radius=1.1) nengo.Connection(ramp_reset_thresh.output, ramp_reset_hold.input, transform=5.0, synapse=0.015) # Misc ramp threshold outputs ramp_75 = cfg.make_thresh_ens_net(0.75) ramp_50_75 = cfg.make_thresh_ens_net(0.5) nengo.Connection(ramp_integrator, ramp_reset_thresh.input) nengo.Connection(ramp_integrator, ramp_75.input) nengo.Connection(ramp_integrator, ramp_50_75.input) nengo.Connection(ramp_75.output, ramp_50_75.input, transform=-3) # ----------------------- Inputs and Outputs -------------------------- net.ramp = ramp_integrator net.ramp_50_75 = ramp_50_75.output net.go = ramp_int_go.input net.end = ramp_reset_thresh.input net.init = ramp_init_hold.input net.reset = ramp_reset_hold.input net.stop = ramp_int_stop.output net.init_hold = ramp_init_hold.output net.reset_hold = ramp_reset_hold.output return net
# action[0] = -1 # #action[1] = -1 # if angle_todo > +0.05: # action[0] = 1 return action model = nengo.Network() with model: #pid = PIDNode(dimensions=1) environment = NengoGymLunarLander() env = nengo.Node(environment, size_in=2, size_out=8) correction = nengo.Node(None, size_in=2, size_out=2) sensors = nengo.Ensemble(n_neurons=500, dimensions=8, radius=np.sqrt(8), neuron_type=nengo.Direct()) controls = nengo.Ensemble(n_neurons=500, dimensions=2) nengo.Connection(env, sensors) nengo.Connection(sensors, controls, function=heuristic, synapse=0.02) nengo.Connection(controls, correction) nengo.Connection(correction, env) # nengo.Connection(env, pid, synapse=0.02) # nengo.Connection(pid, control, transform=1)
import nengo model = nengo.Model('Many Neurons') A = nengo.Ensemble(nengo.LIF(100), dimensions=1, label="A") import numpy as np sin = nengo.Node(output=lambda t: np.sin(8 * t)) # Input is a sine" nengo.Connection(sin, A, filter=0.01) # 10ms filter"
model.now_state = spa.State(dimensions, vocab, feedback=1, feedback_synapse=0.01) actions = spa.Actions( 'dot(now_state, START) --> now_state=CHECK_FOOD', 'dot(now_state, CHECK_FOOD) --> food_percept=food, now_state=ACTION', 'dot(now_state, ACTION) - 10*dot(food_percept, FOOD) --> now_state=CHECK_FOOD', 'dot(food_percept, FOOD) --> now_state=STOP', ) model.bg = spa.BasalGanglia(actions=actions) model.thal = spa.Thalamus(model.bg) output = nengo.Node(my_output, size_in=dimensions) def food_input(t): if t < 0.2: return "NOFOOD" else: return "FOOD" def state_input(t): if t < 0.1: return "START" else: return '0'
import nengo model = nengo.Network(label="Feedforward") with model: input = nengo.Node([0, 0]) a = nengo.Ensemble(100, dimensions=2, label="a") b = nengo.Ensemble(100, dimensions=2, label="b") c = nengo.Ensemble(100, dimensions=2, label="c") j = nengo.Ensemble(100, dimensions=2, label="j") subnet = nengo.Network(label="subnet") with subnet: d = nengo.Ensemble(100, dimensions=2, label="d") f = nengo.Ensemble(100, dimensions=2, label="f") g = nengo.Ensemble(100, dimensions=2, label="g") h = nengo.Ensemble(100, dimensions=2, label="h") z = nengo.Ensemble(100, dimensions=2, label="z") subnet2 = nengo.Network(label="subnet1") with subnet2: d0 = nengo.Ensemble(100, dimensions=2, label="d0") d1 = nengo.Ensemble(100, dimensions=2, label="d1") f1 = nengo.Ensemble(100, dimensions=2, label="f1") g1 = nengo.Ensemble(100, dimensions=2, label="g1") h1 = nengo.Ensemble(100, dimensions=2, label="h1") e = nengo.Ensemble(100, dimensions=2, label="e") i = nengo.Ensemble(100, dimensions=2, label="i") nengo.Connection(input, a)
rng_noise = np.random.RandomState(seed=13) noisy_vectors_train += rng_noise.normal(loc=0, scale=args.sigma, size=noisy_vectors_train.shape) else: clean_vectors_train = clean_vectors noisy_vectors_train = noisy_vectors coords_train = coords model = nengo.Network(seed=13) with model: ssp_input = nengo.Node( lambda t: noisy_vectors[int(np.floor(t * 10.)), :], size_in=0, size_out=args.dim, ) coord_input = nengo.Node( lambda t: coords[int(np.floor(t * 10.)), :], size_in=0, size_out=2, ) spatial_cleanup = nengo.Ensemble(n_neurons=args.dim * args.neurons_per_dim, dimensions=args.dim, neuron_type=nengo.LIF()) location_ssp = nengo.Ensemble(n_neurons=args.dim * args.neurons_per_dim, dimensions=args.dim, neuron_type=nengo.LIF())
def generate(input_signal, alpha=1000.0): beta = alpha / 4.0 # Read in the class mean for numbers from vision network weights_data = np.load('models/mnist_vision_data/params.npz') weights = weights_data['Wc'] means_data = np.load('models/mnist_vision_data/class_means.npz') means = np.matrix(1.0 / means_data['means']) sps = np.multiply(weights.T, means.T)[:10] sps_labels = [ 'ZERO', 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE'] dimensions = weights.shape[0] # generate the Function Space forces, _, _ = forcing_functions.load_folder( 'models/handwriting_trajectories', rhythmic=True, alpha=alpha, beta=beta) # make an array out of all the possible functions we want to represent force_space = np.vstack(forces) # use this array as our space to perform svd over fs = nengo.FunctionSpace(space=force_space, n_basis=10) # store the weights for each trajectory weights_x = [] weights_y = [] for ii in range(len(forces)): forces = force_space[ii*2:ii*2+2] # load up the forces to be output by the forcing function # calculate the corresponding weights over the basis functions weights_x.append(np.dot(fs.basis.T, forces[0])) weights_y.append(np.dot(fs.basis.T, forces[1])) # Create our vocabularies rng = np.random.RandomState(0) vocab_vision = Vocabulary(dimensions=dimensions, rng=rng) vocab_dmp_weights_x = Vocabulary(dimensions=fs.n_basis, rng=rng) vocab_dmp_weights_y = Vocabulary(dimensions=fs.n_basis, rng=rng) for label, sp, wx, wy in zip( sps_labels, sps, weights_x, weights_y): vocab_vision.add( label, np.array(sp)[0] / np.linalg.norm(np.array(sp)[0])) vocab_dmp_weights_x.add(label, wx) vocab_dmp_weights_y.add(label, wy) net = spa.SPA() # net.config[nengo.Ensemble].neuron_type = nengo.Direct() with net: # --------------------- Inputs -------------------------- # def input_func(t): # return vocab_vision.parse(input_signal).v # net.input = nengo.Node(input_func, label='input') net.input = spa.State(dimensions, subdimensions=10, vocab=vocab_vision) number = nengo.Node(output=[0], label='number') # ------------------- Point Attractors -------------------- net.x = point_attractor.generate( n_neurons=1000, alpha=alpha, beta=beta) net.y = point_attractor.generate( n_neurons=1000, alpha=alpha, beta=beta) # -------------------- Oscillators ---------------------- kick = nengo.Node(nengo.utils.functions.piecewise({0: 1, .05: 0}), label='kick') osc = oscillator.generate(net, n_neurons=2000, speed=.05) osc.label = 'oscillator' nengo.Connection(kick, osc[0]) # ------------------- Forcing Functions -------------------- net.assoc_mem_x = spa.AssociativeMemory( input_vocab=vocab_vision, output_vocab=vocab_dmp_weights_x, wta_output=False) nengo.Connection(net.input.output, net.assoc_mem_x.input) net.assoc_mem_y = spa.AssociativeMemory( input_vocab=vocab_vision, output_vocab=vocab_dmp_weights_y, wta_output=False) nengo.Connection(net.input.output, net.assoc_mem_y.input) # -------------------- Product for decoding ----------------------- net.product_x = nengo.Network('Product X') nengo.networks.Product(n_neurons=1000, dimensions=fs.n_basis, net=net.product_x, input_magnitude=1.0) net.product_y = nengo.Network('Product Y') nengo.networks.Product(n_neurons=1000, dimensions=fs.n_basis, net=net.product_y, input_magnitude=1.0) # get the largest basis function value for normalization max_basis = np.max(fs.basis*fs.scale) domain = np.linspace(-np.pi, np.pi, fs.basis.shape[0]) domain_cossin = np.array([np.cos(domain), np.sin(domain)]).T for ff, product in zip([net.assoc_mem_x.output, net.assoc_mem_y.output], [net.product_x, net.product_y]): for ii in range(fs.n_basis): # find the value of a basis function at a value of (x, y) target_function = nengo.utils.connection.target_function( domain_cossin, fs.basis[:, ii]*fs.scale/max_basis) nengo.Connection(osc, product.B[ii], **target_function) # multiply the value of each basis function at x by its weight nengo.Connection(ff, product.A) nengo.Connection(net.product_x.output, net.x.input[1], transform=np.ones((1, fs.n_basis)) * max_basis, synapse=None) nengo.Connection(net.product_y.output, net.y.input[1], transform=np.ones((1, fs.n_basis)) * max_basis, synapse=None) # -------------------- Output ------------------------------ net.output = nengo.Node(size_in=2, label='output') nengo.Connection(net.x.output, net.output[0]) nengo.Connection(net.y.output, net.output[1]) # create a node to give a plot of the represented function ff_plot = fs.make_plot_node(domain=domain, lines=2, ylim=[-1000000, 1000000]) nengo.Connection(net.assoc_mem_x.output, ff_plot[:fs.n_basis], synapse=0.1) nengo.Connection(net.assoc_mem_y.output, ff_plot[fs.n_basis:], synapse=0.1) return net
import nengo import nengo_dl import numpy as np import tensorflow as tf with nengo.Network(seed=0) as net: # these parameter settings aren't necessary, but they set things up in # a more standard machine learning way, for familiarity net.config[nengo.Ensemble].neuron_type = nengo.RectifiedLinear() net.config[nengo.Ensemble].gain = nengo.dists.Choice([1]) net.config[nengo.Ensemble].bias = nengo.dists.Uniform(-1, 1) net.config[nengo.Connection].synapse = None # connect up our input node, and 3 ensembles in series a = nengo.Node([0.5]) b = nengo.Ensemble(30, 1) c = nengo.Ensemble(30, 1) d = nengo.Ensemble(30, 1) nengo.Connection(a, b) nengo.Connection(b, c) nengo.Connection(c, d) # define our outputs with a probe on the last ensemble in the chain p = nengo.Probe(d) n_steps = 5 # the number of simulation steps we want to run our model for mini_size = 100 # minibatch size with nengo_dl.Simulator(net, minibatch_size=mini_size, device="/cpu:0") as sim:
def test_evaluate(Simulator): minibatch_size = 3 n_steps = 10 n_batches = 2 with nengo.Network() as net: inp0 = nengo.Node([0]) inp1 = nengo.Node([0]) p0 = nengo.Probe(inp0) p1 = nengo.Probe(inp1) with Simulator(net, minibatch_size=minibatch_size) as sim: # single probe sim.compile(loss={"probe": tf.losses.mse}) targets = np.ones((minibatch_size, n_steps, 1)) with pytest.warns(UserWarning, match="Batch size is determined statically"): loss = sim.evaluate(n_steps=n_steps, y=targets, batch_size=-1) assert np.allclose(loss["loss"], 1) assert np.allclose(loss["probe_loss"], 1) assert "probe_1_loss" not in loss # multiple probes sim.compile(loss=tf.losses.mse) loss = sim.evaluate(n_steps=n_steps, y={p0: targets, p1: targets}) assert np.allclose(loss["loss"], 2) assert np.allclose(loss["probe_loss"], 1) assert np.allclose(loss["probe_1_loss"], 1) # default inputs loss = sim.evaluate( y={ p0: np.zeros((minibatch_size, n_steps, 1)), p1: np.zeros((minibatch_size, n_steps, 1)), }, n_steps=n_steps, ) assert np.allclose(loss["loss"], 0) assert np.allclose(loss["probe_loss"], 0) assert np.allclose(loss["probe_1_loss"], 0) # list inputs inputs = np.ones((minibatch_size * n_batches, n_steps, 1)) targets = inputs.copy() loss = sim.evaluate(x=[inputs, inputs * 2], y={ p0: targets, p1: targets }) assert np.allclose(loss["loss"], 1) assert np.allclose(loss["probe_loss"], 0) assert np.allclose(loss["probe_1_loss"], 1) # tensor inputs if compat.eager_enabled(): loss = sim.evaluate( x=[tf.constant(inputs), tf.constant(inputs * 2)], y={ p0: tf.constant(targets), p1: tf.constant(targets) }, ) assert np.allclose(loss["loss"], 1) assert np.allclose(loss["probe_loss"], 0) assert np.allclose(loss["probe_1_loss"], 1) gen = (( { "node": np.ones((minibatch_size, n_steps, 1)), "node_1": np.ones((minibatch_size, n_steps, 1)) * 2, "n_steps": np.ones((minibatch_size, 1)) * n_steps, }, { "probe": np.ones((minibatch_size, n_steps, 1)), "probe_1": np.ones((minibatch_size, n_steps, 1)), }, ) for _ in range(n_batches)) loss = sim.evaluate(gen, steps=n_batches) assert np.allclose(loss["loss"], 1) assert np.allclose(loss["probe_loss"], 0) assert np.allclose(loss["probe_1_loss"], 1) # check custom objective def constant_error(y_true, y_pred): return tf.constant(3.0) sim.compile(loss={p0: constant_error}) assert np.allclose( sim.evaluate(y={p0: np.zeros((minibatch_size, n_steps, 1))}, n_steps=n_steps)["loss"], 3, ) # test metrics sim.compile( loss=tf.losses.mse, metrics={ p0: constant_error, p1: [constant_error, "mae"] }, ) output = sim.evaluate( y={ p0: np.ones((minibatch_size, n_steps, 1)), p1: np.ones((minibatch_size, n_steps, 1)) * 2, }, n_steps=n_steps, ) assert np.allclose(output["loss"], 5) assert np.allclose(output["probe_loss"], 1) assert np.allclose(output["probe_1_loss"], 4) assert np.allclose(output["probe_constant_error"], 3) assert np.allclose(output["probe_1_constant_error"], 3) assert "probe_mae" not in output assert np.allclose(output["probe_1_mae"], 2)
b = rng.normal(scale=np.sqrt(1. / dim), size=dim) c = circconv(a, b) assert np.abs(a).max() < radius assert np.abs(b).max() < radius assert np.abs(c).max() < radius # check FFT magnitude tr_A = transform_in(dim, 'A', invert=False) tr_B = transform_in(dim, 'B', invert=False) d = np.dot(tr_A, a) * np.dot(tr_B, b) assert np.abs(d).max() < (4 * radius) # ^ TODO: 4 * radius just seems to work from looking at Nengo code. Why? # --- model with nengo.Network(label="ProfileConv", seed=3) as model: inputA = nengo.Node(a, label="inputA") inputB = nengo.Node(b, label="inputB") A = nengo.networks.EnsembleArray(neurons_per_product, dim, radius=radius, label='A') B = nengo.networks.EnsembleArray(neurons_per_product, dim, radius=radius, label='B') C = nengo.networks.EnsembleArray(neurons_per_product, dim, radius=radius, label='C') D = nengo.networks.CircularConvolution(neurons_per_product, dim,
def test_fit(Simulator, seed): minibatch_size = 4 n_hidden = 20 with nengo.Network(seed=seed) as net: net.config[nengo.Ensemble].gain = nengo.dists.Choice([1]) net.config[nengo.Ensemble].bias = nengo.dists.Choice([0]) net.config[nengo.Connection].synapse = None # note: we have these weird input setup just so that we can test # training with two distinct inputs inp_a = nengo.Node([0]) inp_b = nengo.Node([0]) inp = nengo.Node(size_in=2) nengo.Connection(inp_a, inp[0], transform=1) nengo.Connection(inp_b, inp[1], transform=1) ens = nengo.Ensemble(n_hidden + 1, n_hidden, neuron_type=nengo.Sigmoid(tau_ref=1)) out = nengo.Ensemble(1, 1, neuron_type=nengo.Sigmoid(tau_ref=1)) nengo.Connection(inp, ens.neurons, transform=dists.Glorot()) nengo.Connection(ens.neurons, out.neurons, transform=dists.Glorot()) nengo.Probe(out.neurons) with Simulator(net, minibatch_size=minibatch_size, unroll_simulation=1, seed=seed) as sim: x = np.asarray([[[0.0, 0.0]], [[0.0, 1.0]], [[1.0, 0.0]], [[1.0, 1.0]]]) y = np.asarray([[[0.1]], [[0.9]], [[0.9]], [[0.1]]]) sim.compile(optimizer=tf.optimizers.Adam(0.01), loss=tf.losses.mse) # note: batch_size should be ignored with pytest.warns(UserWarning, match="Batch size is determined statically"): history = sim.fit( [x[..., [0]], x[..., [1]]], y, validation_data=([x[..., [0]], x[..., [1]]], y), epochs=200, verbose=0, batch_size=-1, ) assert history.history["loss"][-1] < 5e-4 assert history.history["val_loss"][-1] < 5e-4 # check that validation_sample_weights work correctly history = sim.fit( [x[..., [0]], x[..., [1]]], y, validation_data=([x[..., [0]], x[..., [1]]], y, np.zeros(y.shape[0])), epochs=1, verbose=0, ) assert np.allclose(history.history["val_loss"][-1], 0) if compat.eager_enabled(): sim.reset() history = sim.fit( [tf.constant(x[..., [0]]), tf.constant(x[..., [1]])], tf.constant(y), epochs=200, verbose=0, ) assert history.history["loss"][-1] < 5e-4 sim.reset() history = sim.fit( (((x[..., [0]], x[..., [1]], np.ones((4, 1), dtype=np.int32)), y) for _ in range(200)), epochs=20, steps_per_epoch=10, verbose=0, ) assert history.history["loss"][-1] < 5e-4 history = sim.fit( tf.data.Dataset.from_tensors( ((x[..., [0]], x[..., [1]], np.ones((4, 1), dtype=np.int32)), y)), validation_data=tf.data.Dataset.from_tensors( ((x[..., [0]], x[..., [1]], np.ones((4, 1), dtype=np.int32)), y)), epochs=200, verbose=0, ) assert history.history["loss"][-1] < 5e-4 assert history.history["val_loss"][-1] < 5e-4
model = nengo.Network() with model: # Example 1: a timer def timer_function(t): if t < 1.0: timer_function._nengo_html_ = '<h1>Ready...</h1>' return 0 elif t < 2.0: timer_function._nengo_html_ = '<h1>Set...</h1>' return 0 else: timer_function._nengo_html_ = '<h1>Go!</h1>' return 1 timer = nengo.Node(timer_function) # Example 2: displaying a value def amount_function(t, x): if x < -0.5: amount_function._nengo_html_ = '<h2>small</h2>' elif -0.5 < x < 0.5: amount_function._nengo_html_ = '<h2>medium</h2>' else: amount_function._nengo_html_ = '<h2>large</h2>' stim_amount = nengo.Node(0) amount = nengo.Ensemble(n_neurons=100, dimensions=1) display_amount = nengo.Node(amount_function, size_in=1) nengo.Connection(stim_amount, amount) nengo.Connection(amount, display_amount)
def test_predict(Simulator, seed): n_steps = 100 with nengo.Network(seed=seed) as net: a = nengo.Node([2], label="a") b = nengo.Ensemble(10, 1) nengo.Connection(a, b) p = nengo.Probe(b) with Simulator(net, minibatch_size=4) as sim: a_vals = np.ones((12, n_steps, 1)) n_batches = a_vals.shape[0] // sim.minibatch_size sim.run_steps(n_steps) data_noinput = sim.data[p] sim.reset(include_trainable=False, include_processes=False) sim.run_steps(n_steps, data={a: a_vals[:4]}) data_tile = np.tile(sim.data[p], (n_batches, 1, 1)) sim.reset(include_probes=False, include_trainable=False, include_processes=False) # no input (also checking batch_size is ignored) with pytest.warns(UserWarning, match="Batch size is determined statically"): output = sim.predict(n_steps=n_steps, batch_size=-1) assert np.allclose(output[p], data_noinput) # numpy input (single batch) output = sim.predict_on_batch(a_vals[:4]) assert np.allclose(output[p], sim.data[p]) # numpy input (multiple batches) output = sim.predict(a_vals) assert np.allclose(output[p], data_tile) # tf input if compat.eager_enabled(): output = sim.predict(tf.constant(a_vals)) assert np.allclose(output[p], data_tile) # dict input for key in [a, "a"]: output = sim.predict({key: a_vals}) assert np.allclose(output[p], data_tile) # generator input output = sim.predict( ([ a_vals[i * sim.minibatch_size:(i + 1) * sim.minibatch_size], np.ones((sim.minibatch_size, 1), dtype=np.int32) * n_steps, ] for i in range(n_batches)), steps=n_batches, ) assert np.allclose(output[p], data_tile) # dataset input dataset = tf.data.Dataset.from_tensor_slices({ "a": tf.constant(a_vals), "n_steps": tf.ones((12, 1), dtype=np.int32) * n_steps, }).batch(sim.minibatch_size) output = sim.predict(dataset) assert np.allclose(output[p], data_tile)
N = 10 def comparator_func(t, x): R1 = np.correlate(x[:N], x[N:]) print(R1) return R1 with nengo.Network() as net: ens1 = nengo.Ensemble(10, dimensions=1, seed=0, intercepts=nengo.dists.Choice([-0.1]), max_rates=nengo.dists.Choice([100])) ens2 = nengo.Ensemble(10, dimensions=1, seed=0, intercepts=nengo.dists.Choice([-0.1]), max_rates=nengo.dists.Choice([100])) node = nengo.Node(size_in=20, output=comparator_func) # Neuron to neuron weights = np.eye(ens1.n_neurons, ens1.n_neurons) nengo.Connection(ens1.neurons, node[:10], transform=weights) nengo.Connection(ens2.neurons, node[10:], transform=weights) with nengo.Simulator(net) as sim: sim.run(0.1)
#two inputs into one population with two dimensions #2d pop inputs to 1d pop and performs function utilizing both initial dimesions import nengo model = nengo.Network() with model: stim_a = nengo.Node([0]) stim_b = nengo.Node([0]) a = nengo.Ensemble(n_neurons=100, dimensions=1) b = nengo.Ensemble(n_neurons=100, dimensions=1) c = nengo.Ensemble(n_neurons=200, dimensions=2, radius=2) d = nengo.Ensemble(n_neurons=100, dimensions=1) def product(x): return x[0] * x[1] nengo.Connection(stim_a, a) nengo.Connection(stim_b, b) nengo.Connection(a, c[0]) nengo.Connection(b, c[1]) nengo.Connection(c, d, function=product)