def create_model(seed=None): global model #create vocabulary to show representations in gui if nengo_gui_on: vocab_angles = spa.Vocabulary(D) for name in [0, 3, 7, 12, 18, 25, 33, 42]: #vocab_angles.add('D' + str(name), np.linalg.norm(compressed_im_cued[name+90])) #take mean across phases v = compressed_im_cued[name + 90] nrm = np.linalg.norm(v) if nrm > 0: v /= nrm vocab_angles.add('D' + str(name), v) #take mean across phases v = np.dot(imagearr[-1, :] / 50, U_cued[:, :D]) nrm = np.linalg.norm(v) if nrm > 0: v /= nrm vocab_angles.add('Impulse', v) #model = nengo.Network(seed=seed) model = spa.SPA(seed=seed) with model: #input nodes inputNode_cued = nengo.Node(input_func_cued, label='input_cued') reactivate = nengo.Node(reactivate_func, label='reactivate') #sensory ensemble sensory_cued = nengo.Ensemble(Ns, D, encoders=e_cued, intercepts=Uniform(0.01, .1), radius=1, label='sensory_cued') nengo.Connection(inputNode_cued, sensory_cued, transform=U_cued[:, :D].T) #memory ensemble memory_cued = nengo.Ensemble(Nm, D, neuron_type=stpLIF(), intercepts=Uniform(0.01, .1), radius=1, label='memory_cued') nengo.Connection(reactivate, memory_cued.neurons) #potential reactivation nengo.Connection(sensory_cued, memory_cued, transform=.1) #.1) #recurrent STSP connection nengo.Connection(memory_cued, memory_cued, transform=1, learning_rule_type=STP(), solver=nengo.solvers.LstsqL2(weights=True)) #comparison represents sin, cosine of theta of both sensory and memory ensemble comparison_cued = nengo.Ensemble(Nc, dimensions=4, radius=math.sqrt(2), intercepts=Uniform(.01, 1), label='comparison_cued') nengo.Connection(sensory_cued, comparison_cued[:2], eval_points=compressed_im_cued[0:-1], function=sincos.T) nengo.Connection(memory_cued, comparison_cued[2:], eval_points=compressed_im_cued[0:-1], function=sincos.T) #decision represents the difference in theta decoded from the sensory and memory ensembles decision_cued = nengo.Ensemble(n_neurons=Nd, dimensions=1, radius=45, label='decision_cued') nengo.Connection(comparison_cued, decision_cued, eval_points=ep, scale_eval_points=False, function=arctan_func) #same for uncued if uncued: inputNode_uncued = nengo.Node(input_func_uncued, label='input_uncued') sensory_uncued = nengo.Ensemble(Ns, D, encoders=e_uncued, intercepts=Uniform(0.01, .1), radius=1, label='sensory_uncued') nengo.Connection(inputNode_uncued, sensory_uncued, transform=U_uncued[:, :D].T) memory_uncued = nengo.Ensemble(Nm, D, neuron_type=stpLIF(), intercepts=Uniform(0.01, .1), radius=1, label='memory_uncued') nengo.Connection(sensory_uncued, memory_uncued, transform=.1) nengo.Connection(memory_uncued, memory_uncued, transform=1, learning_rule_type=STP(), solver=nengo.solvers.LstsqL2(weights=True)) comparison_uncued = nengo.Ensemble(Nd, dimensions=4, radius=math.sqrt(2), intercepts=Uniform(.01, 1), label='comparison_uncued') nengo.Connection(memory_uncued, comparison_uncued[2:], eval_points=compressed_im_uncued[0:-1], function=sincos.T) nengo.Connection(sensory_uncued, comparison_uncued[:2], eval_points=compressed_im_uncued[0:-1], function=sincos.T) decision_uncued = nengo.Ensemble(n_neurons=Nd, dimensions=1, radius=45, label='decision_uncued') nengo.Connection(comparison_uncued, decision_uncued, eval_points=ep, scale_eval_points=False, function=arctan_func) #decode for gui if nengo_gui_on: model.sensory_decode = spa.State(D, vocab=vocab_angles, subdimensions=12, label='sensory_decode') for ens in model.sensory_decode.all_ensembles: ens.neuron_type = nengo.Direct() nengo.Connection(sensory_cued, model.sensory_decode.input, synapse=None) model.memory_decode = spa.State(D, vocab=vocab_angles, subdimensions=12, label='memory_decode') for ens in model.memory_decode.all_ensembles: ens.neuron_type = nengo.Direct() nengo.Connection(memory_cued, model.memory_decode.input, synapse=None) #probes if not (nengo_gui_on): if store_representations: #sim 1 trials 1-100 #p_dtheta_cued=nengo.Probe(decision_cued, synapse=0.01) model.p_mem_cued = nengo.Probe(memory_cued, synapse=0.01) #p_sen_cued=nengo.Probe(sensory_cued, synapse=0.01) if uncued: model.p_mem_uncued = nengo.Probe(memory_uncued, synapse=0.01) if store_spikes_and_resources: #sim 1 trial 1 model.p_spikes_mem_cued = nengo.Probe(memory_cued.neurons, 'spikes') model.p_res_cued = nengo.Probe(memory_cued.neurons, 'resources') model.p_cal_cued = nengo.Probe(memory_cued.neurons, 'calcium') if uncued: model.p_spikes_mem_uncued = nengo.Probe( memory_uncued.neurons, 'spikes') model.p_res_uncued = nengo.Probe(memory_uncued.neurons, 'resources') model.p_cal_uncued = nengo.Probe(memory_uncued.neurons, 'calcium') if store_decisions: #sim 2 model.p_dec_cued = nengo.Probe(decision_cued, synapse=0.01)
#Setup the environment import matplotlib.pyplot as plt import random import math import nengo from nengo import spa # Number of dimensions for the SPs dimensions = 64 # Make a model object with the SPA network model = spa.SPA(label='Task') with model: # Initial the three visual perceptual memory component and one working memory component. model.vision1 = spa.State(dimensions=dimensions, neurons_per_dimension=100, feedback=0.7) model.vision2 = spa.State(dimensions=dimensions, neurons_per_dimension=100, feedback=0.7) model.cue = spa.Buffer(dimensions=dimensions, neurons_per_dimension=100) model.representation = spa.Memory(dimensions=dimensions, neurons_per_dimension=100) # Specify the action mapping and attention function actions = spa.Actions( 'dot(cue, LEFT) --> vision1=vision1*2', 'dot(cue, RIGHT) --> vision2=vision2*2', ) cortical_actions = spa.Actions('representation=vision1+vision2', )
def model(self, p): vocab = spa.Vocabulary(p.D) items = [] fan = [] assert p.M % 6 == 0 for i in range(p.M / 6): for j in range(3): pp = vocab.parse('X%d+Y%d+%g*Q' % (6 * i + j, 6 * i + j, p.common)) pp.normalize() vocab.add('P%d' % (3 * i + j), pp) items.append(pp.v) fan.append(1) for j, v in enumerate(['AB', 'CB', 'AC']): pp = vocab.parse('%s%d+%s%d+%g*Q' % (v[0], i, v[1], i, p.common)) pp.normalize() vocab.add('P%d_2' % (3 * i + j), pp) items.append(pp.v) fan.append(2) rng = np.random.RandomState(seed=p.seed) order = np.arange(len(items)) rng.shuffle(order) items = [items[o] for o in order] fan = [fan[o] for o in order] self.fan = fan model = spa.SPA() with model: model.cue = spa.State(p.D, vocab=vocab) if p.noise > 0: for ens in model.cue.all_ensembles: ens.noise = nengo.processes.WhiteNoise( dist=nengo.dists.Gaussian(0, std=p.noise)) model.mem = nengo.networks.EnsembleArray(n_neurons=50, n_ensembles=len(items)) for ens in model.mem.all_ensembles: ens.encoders = nengo.dists.Choice([[1]]) ens.intercepts = nengo.dists.Uniform(0, 1) nengo.Connection(model.mem.output, model.mem.input, transform=np.eye(len(items)) - 1, synapse=p.syn) nengo.Connection(model.cue.output, model.mem.input, transform=np.array(items), synapse=p.input_syn) mem_activity = nengo.Node(None, size_in=1) for ens in model.mem.all_ensembles: nengo.Connection(ens.neurons, mem_activity, transform=np.ones((1, ens.n_neurons)), synapse=None) self.p_activity = nengo.Probe(mem_activity, synapse=0.01) class Env(nengo.Node): def __init__(self, items): self.items = items self.index = 0 self.switch_time = 0 self.times = [] self.correct = [] super(Env, self).__init__(self.func, size_in=len(items), size_out=p.D) def func(self, t, x): if t > self.switch_time + p.min_time + p.t_zero: values = list(sorted(x)) if values[-2] < 1e-3 or t > (self.switch_time + p.timeout): self.correct.append(np.argmax(x) == self.index) self.index = (self.index + 1) % len(self.items) self.times.append(t - self.switch_time) self.switch_time = t if len(self.times) >= len(self.items) and not p.gui: raise FinishedException() if t < self.switch_time + p.t_zero: return np.zeros(p.D) else: return self.items[self.index] with model: self.env = Env(items) nengo.Connection(self.env, model.cue.input, synapse=None) nengo.Connection(model.mem.output, self.env, synapse=0.005) return model
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
# this is for creating a new type of object in the environment class Food(grid.ContinuousAgent): color = 'green' shape = 'circle' # now we actuall make the world world = grid.World(Cell, map=map, directions=4) body = grid.ContinuousAgent() world.add(body, x=2, y=4, dir=1) world.add(Food(), x=5, y=2) #world.add(Food(), x=3, y=6) D = 32 vocab = spa.Vocabulary(D) model = spa.SPA(label="SearchFood") class State(nengo.Node): def __init__(self, body): self.body = body super(State, self).__init__(self.update) def compute_angle_and_distance(self, obj): angle = (body.dir-1) * 2 * np.pi / 4 dy = obj.y - self.body.y dx = obj.x - self.body.x obj_angle = np.arctan2(dy, dx) theta = angle - obj_angle dist = np.sqrt(dy**2 + dx**2)
import nengo from nengo import spa ### Step 1: Create the model # Notice that when you specify actions, you're determining which modules are connected to which. For example, by having a mapping that depends on the state of cortex, you are determining that the cortex and basal ganglia must be connected. As well, when you specify that the result of the action changes the state of cortex, then you are determining that thalamus must be connected to cortex. # # In[ ]: # Number of dimensions for the Semantic Pointers dimensions = 16 # Make a model object with the SPA network model = spa.SPA(label='Routed_Sequence') with model: # Specify the modules to be used model.cortex = spa.Buffer(dimensions=dimensions) model.vision = spa.Buffer(dimensions=dimensions) # Specify the action mapping actions = spa.Actions('dot(vision, START) --> cortex = vision', 'dot(cortex, A) --> cortex = B', 'dot(cortex, B) --> cortex = C', 'dot(cortex, C) --> cortex = D', 'dot(cortex, D) --> cortex = E', 'dot(cortex, E) --> cortex = A') model.bg = spa.BasalGanglia(actions=actions) model.thal = spa.Thalamus(model.bg)
# top-right shows the utility (similarity) of the current basal ganglia input # (i.e., state) with the possible vocabulary vectors. # You can see that in this model, even though the input is applied for 400ms, it # doesn't prevent the activation of the second and subsequent rules in the # sequence. #Setup the environment import nengo from nengo import spa #import spa related packages #Number of dimensions for the Semantic Pointers dimensions = 16 #Make a model object with the SPA network model = spa.SPA(label='Routed_Sequence', seed=20) with model: #Specify the modules to be used model.state = spa.State(dimensions=dimensions, feedback=1, feedback_synapse=0.01) model.vision = spa.State(dimensions=dimensions) #Specify the action mapping actions = spa.Actions('dot(vision, START) --> state = vision', 'dot(state, A) --> state = B', 'dot(state, B) --> state = C', 'dot(state, C) --> state = D', 'dot(state, D) --> state = E', 'dot(state, E) --> state = A')
def create_model(p): model = spa.SPA() if p.direct: model.config[nengo.Ensemble].neuron_type = nengo.Direct() force_neurons_cfg = nengo.Config(nengo.Ensemble) force_neurons_cfg[nengo.Ensemble].neuron_type = nengo.LIF() else: force_neurons_cfg = model.config with model: # control model.control_net = nengo.Network() with model.control_net: #assuming the model knows which hand to use (which was blocked) model.hand_input = nengo.Node(get_hand) model.target_hand = spa.State(p.Dmid, vocab=vocab_motor, feedback=1) nengo.Connection(model.hand_input, model.target_hand.input, synapse=None) model.attend = spa.State(p.D, vocab=vocab_attend, feedback=.5) # vocab_attend model.goal = spa.State(p.Dlow, vocab=vocab_goal, feedback=.7) # current goal if p.do_vision: from nengo_extras.vision import Gabor, Mask ### vision ### # set up network parameters n_vis = X_train.shape[1] # nr of pixels, dimensions of network n_hid = 1000 # nr of gabor encoders/neurons # random state to start rng = np.random.RandomState(9) encoders = Gabor().generate( n_hid, (4, 4), rng=rng) # gabor encoders, 11x11 apparently, why? encoders = Mask( (14, 90)).populate(encoders, rng=rng, flatten=True) # use them on part of the image model.visual_net = nengo.Network() with model.visual_net: #represent currently attended item model.attended_item = nengo.Node(present_item2, size_in=p.D) nengo.Connection(model.attend.output, model.attended_item) model.vision_gabor = nengo.Ensemble( n_hid, n_vis, eval_points=X_train, # neuron_type=nengo.LIF(), neuron_type=nengo.AdaptiveLIF( tau_n=.01, inc_n=.05 ), #to get a better fit, use more realistic neurons that adapt to input intercepts=nengo.dists.Uniform(-0.1, 0.1), #intercepts=nengo.dists.Choice([-0.5]), #should we comment this out? not sure what's happening #max_rates=nengo.dists.Choice([100]), encoders=encoders) #recurrent connection (time constant 500 ms) # strength = 1 - (100/500) = .8 zeros = np.zeros_like(X_train) nengo.Connection( model.vision_gabor, model.vision_gabor, synapse=0.005, #.1 eval_points=np.vstack( [X_train, zeros, np.random.randn(*X_train.shape)]), transform=.5) model.visual_representation = nengo.Ensemble(n_hid, dimensions=p.Dmid) model.visconn = nengo.Connection( model.vision_gabor, model.visual_representation, synapse=0.005, #was .005 eval_points=X_train, function=train_targets, solver=nengo.solvers.LstsqL2(reg=0.01)) nengo.Connection(model.attended_item, model.vision_gabor, synapse=.02) #.03) #synapse? # display attended item, only in gui if p.gui: # show what's being looked at model.display_attended = nengo.Node( display_func, size_in=model.attended_item.size_out) # to show input nengo.Connection(model.attended_item, model.display_attended, synapse=None) #add node to plot total visual activity model.visual_activation = nengo.Node(None, size_in=1) nengo.Connection(model.vision_gabor.neurons, model.visual_activation, transform=np.ones((1, n_hid)), synapse=None) else: model.visual_net = nengo.Network() with model.visual_net: model.fake_vision = nengo.Node(fake_vision, size_in=p.D) nengo.Connection(model.attend.output, model.fake_vision) ### central cognition ### ##### Concepts ##### model.concepts = spa.AssociativeMemory( vocab_all_words, #vocab_concepts, wta_output=True, wta_inhibit_scale=1, #was 1 #default_output_key='NONE', #what to say if input doesn't match threshold=0.3 ) # how strong does input need to be for it to recognize if p.do_vision: nengo.Connection( model.visual_representation, model.concepts.input, transform=.8 * vision_mapping ) #not too fast to concepts, might have to be increased to have model react faster to first word. else: nengo.Connection(model.fake_vision, model.concepts.input, transform=.8, synapse=0.025) #concepts accumulator with force_neurons_cfg: model.concepts_evidence = spa.State( 1, feedback=1, feedback_synapse=0.005 ) #the lower the synapse, the faster it accumulates (was .1) concepts_evidence_scale = 2.5 nengo.Connection(model.concepts.am.elem_output, model.concepts_evidence.input, transform=concepts_evidence_scale * np.ones( (1, model.concepts.am.elem_output.size_out)), synapse=0.005) #concepts switch model.do_concepts = spa.AssociativeMemory(vocab_reset, default_output_key='CLEAR', threshold=.2) nengo.Connection( model.do_concepts.am.ensembles[-1], model.concepts_evidence.all_ensembles[0].neurons, transform=np.ones( (model.concepts_evidence.all_ensembles[0].n_neurons, 1)) * -10, synapse=0.005) ###### Visual Representation ###### model.vis_pair = spa.State( p.D, vocab=vocab_all_words, feedback=1.0, feedback_synapse=.05 ) #was 2, 1.6 works ok, but everything gets activated. model.p_vis_pair = nengo.Probe(model.vis_pair.output, synapse=0.01) if p.do_familiarity: assert p.do_motor ##### Familiarity ##### # Assoc Mem with Learned Words # - familiarity signal should be continuous over all items, so no wta model.dm_learned_words = spa.AssociativeMemory(vocab_learned_words, threshold=.2) nengo.Connection(model.dm_learned_words.output, model.dm_learned_words.input, transform=.4, synapse=.02) # Familiarity Accumulator with force_neurons_cfg: model.familiarity = spa.State( 1, feedback=.9, feedback_synapse=0.1) #fb syn influences speed of acc #familiarity_scale = 0.2 #keep stable for negative fam # familiarity accumulator switch model.do_fam = spa.AssociativeMemory(vocab_reset, default_output_key='CLEAR', threshold=.2) # reset nengo.Connection( model.do_fam.am.ensembles[-1], model.familiarity.all_ensembles[0].neurons, transform=np.ones( (model.familiarity.all_ensembles[0].n_neurons, 1)) * -10, synapse=0.005) #first a sum to represent summed similarity model.summed_similarity = nengo.Ensemble(n_neurons=100, dimensions=1) nengo.Connection( model.dm_learned_words.am.elem_output, model.summed_similarity, transform=np.ones( (1, model.dm_learned_words.am.elem_output.size_out ))) #take sum #then a connection to accumulate this summed sim def familiarity_acc_transform(summed_sim): fam_scale = .5 fam_threshold = 0 #actually, kind of bias fam_max = 1 return fam_scale * (2 * ((summed_sim - fam_threshold) / (fam_max - fam_threshold)) - 1) nengo.Connection(model.summed_similarity, model.familiarity.input, function=familiarity_acc_transform) ##### Recollection & Representation ##### model.dm_pairs = spa.AssociativeMemory( vocab_learned_pairs, wta_output=True) #input_keys=list_of_pairs nengo.Connection(model.dm_pairs.output, model.dm_pairs.input, transform=.5, synapse=.05) #representation rep_scale = 0.5 model.representation = spa.State(p.D, vocab=vocab_all_words, feedback=1.0) with force_neurons_cfg: model.rep_filled = spa.State( 1, feedback=.9, feedback_synapse=.1) #fb syn influences speed of acc model.do_rep = spa.AssociativeMemory(vocab_reset, default_output_key='CLEAR', threshold=.2) nengo.Connection( model.do_rep.am.ensembles[-1], model.rep_filled.all_ensembles[0].neurons, transform=np.ones( (model.rep_filled.all_ensembles[0].n_neurons, 1)) * -10, synapse=0.005) nengo.Connection(model.representation.output, model.rep_filled.input, transform=rep_scale * np.reshape(sum(vocab_learned_pairs.vectors), ((1, p.D)))) ###### Comparison ##### model.comparison = spa.Compare(p.D, vocab=vocab_all_words, neurons_per_multiply=500, input_magnitude=.3) #turns out comparison is not an accumulator - we also need one of those. with force_neurons_cfg: model.comparison_accumulator = spa.State( 1, feedback=.9, feedback_synapse=0.05) #fb syn influences speed of acc model.do_compare = spa.AssociativeMemory( vocab_reset, default_output_key='CLEAR', threshold=.2) #reset nengo.Connection( model.do_compare.am.ensembles[-1], model.comparison_accumulator.all_ensembles[0].neurons, transform=np.ones( (model.comparison_accumulator.all_ensembles[0].n_neurons, 1)) * -10, synapse=0.005) #error because we apply a function to a 'passthrough' node, inbetween ensemble as a solution: model.comparison_result = nengo.Ensemble(n_neurons=100, dimensions=1) nengo.Connection(model.comparison.output, model.comparison_result) def comparison_acc_transform(comparison): comparison_scale = .6 comparison_threshold = 0 #actually, kind of bias comparison_max = .6 return comparison_scale * (2 * ( (comparison - comparison_threshold) / (comparison_max - comparison_threshold)) - 1) nengo.Connection(model.comparison_result, model.comparison_accumulator.input, function=comparison_acc_transform) if p.do_motor: #motor model.motor_net = nengo.Network() with model.motor_net: #input multiplier model.motor_input = spa.State(p.Dmid, vocab=vocab_motor) #higher motor area (SMA?) model.motor = spa.State(p.Dmid, vocab=vocab_motor, feedback=.7) #connect input multiplier with higher motor area nengo.Connection(model.motor_input.output, model.motor.input, synapse=.1, transform=2) #finger area model.fingers = spa.AssociativeMemory( vocab_fingers, input_keys=['L1', 'L2', 'R1', 'R2'], wta_output=True) nengo.Connection(model.fingers.output, model.fingers.input, synapse=0.1, transform=0.3) #feedback #conncetion between higher order area (hand, finger), to lower area nengo.Connection(model.motor.output, model.fingers.input, transform=.25 * motor_mapping) #was .2 #finger position (spinal?) model.finger_pos = nengo.networks.EnsembleArray(n_neurons=50, n_ensembles=4) nengo.Connection(model.finger_pos.output, model.finger_pos.input, synapse=0.1, transform=0.8) #feedback #connection between finger area and finger position nengo.Connection(model.fingers.am.elem_output, model.finger_pos.input, transform=1.0 * np.diag([0.55, .54, .56, .55])) #fix these actions = dict( #wait & start a_aa_wait='dot(goal,WAIT) - .9 --> goal=0', a_attend_item1= 'dot(goal,DO_TASK) - .0 --> goal=RECOG, attend=ITEM1, do_concepts=GO', #attend words b_attending_item1= 'dot(goal,RECOG) + dot(attend,ITEM1) - concepts_evidence - .3 --> goal=RECOG, attend=ITEM1, do_concepts=GO', # vis_pair=2.5*(ITEM1*concepts)', c_attend_item2= 'dot(goal,RECOG) + dot(attend,ITEM1) + concepts_evidence - 1.6 --> goal=RECOG2, attend=ITEM2, vis_pair=3*(ITEM1*concepts)', d_attending_item2= 'dot(goal,RECOG2+RECOG) + dot(attend,ITEM2) - concepts_evidence - .4 --> goal=RECOG2, attend=ITEM2, do_concepts=GO', e_start_familiarity= 'dot(goal,RECOG2) + dot(attend,ITEM2) + concepts_evidence - 1.8 --> goal=FAMILIARITY, vis_pair=1.9*(ITEM2*concepts)', ) if p.do_familiarity: actions[ 'd_attending_item2'] += ', dm_learned_words=1.0*(~ITEM1*vis_pair)' actions[ 'e_start_familiarity'] += ', do_fam=GO, dm_learned_words=2.0*(~ITEM1*vis_pair+~ITEM2*vis_pair)' actions.update( dict( #judge familiarity f_accumulate_familiarity= '1.1*dot(goal,FAMILIARITY) - 0.2 --> goal=FAMILIARITY-RECOG2, do_fam=GO, dm_learned_words=.8*(~ITEM1*vis_pair+~ITEM2*vis_pair)', g_respond_unfamiliar= 'dot(goal,FAMILIARITY) - familiarity - .5*dot(fingers,L1+L2+R1+R2) - .6 --> goal=RESPOND_MISMATCH-FAMILIARITY, do_fam=GO, motor_input=1.6*(target_hand+MIDDLE)', #g2_respond_familiar = 'dot(goal,FAMILIARITY) + familiarity - .5*dot(fingers,L1+L2+R1+R2) - .6 --> goal=RESPOND, do_fam=GO, motor_input=1.6*(target_hand+INDEX)', #recollection & representation h_recollection= 'dot(goal,FAMILIARITY) + familiarity - .5*dot(fingers,L1+L2+R1+R2) - .6 --> goal=RECOLLECTION-FAMILIARITY, dm_pairs = vis_pair', i_representation= 'dot(goal,RECOLLECTION) - rep_filled - .1 --> goal=RECOLLECTION, dm_pairs = vis_pair, representation=3*dm_pairs, do_rep=GO', #comparison & respond j_10_compare_word1= 'dot(goal,RECOLLECTION+1.4*COMPARE_ITEM1) + rep_filled - .9 --> goal=COMPARE_ITEM1-RECOLLECTION, do_rep=GO, do_compare=GO, comparison_A = ~ITEM1*vis_pair, comparison_B = ~ITEM1*representation', k_11_match_word1= 'dot(goal,COMPARE_ITEM1) + comparison_accumulator - .7 --> goal=COMPARE_ITEM2-COMPARE_ITEM1, do_rep=GO, comparison_A = ~ITEM1*vis_pair, comparison_B = ~ITEM1*representation', l_12_mismatch_word1= 'dot(goal,COMPARE_ITEM1) + .4 * dot(goal,RESPOND_MISMATCH) - comparison_accumulator - .7 --> goal=RESPOND_MISMATCH-COMPARE_ITEM1, do_rep=GO, motor_input=1.6*(target_hand+MIDDLE), do_compare=GO, comparison_A = ~ITEM1*vis_pair, comparison_B = ~ITEM1*representation', compare_word2= 'dot(goal,COMPARE_ITEM2) - .5 --> goal=COMPARE_ITEM2, do_compare=GO, comparison_A = ~ITEM2*vis_pair, comparison_B = ~ITEM2*representation', m_match_word2= 'dot(goal,COMPARE_ITEM2) + comparison_accumulator - .7 --> goal=RESPOND_MATCH-COMPARE_ITEM2, motor_input=1.6*(target_hand+INDEX), do_compare=GO, comparison_A = ~ITEM2*vis_pair, comparison_B = ~ITEM2*representation', n_mismatch_word2= 'dot(goal,COMPARE_ITEM2) - comparison_accumulator - dot(fingers,L1+L2+R1+R2)- .7 --> goal=RESPOND_MISMATCH-COMPARE_ITEM2, motor_input=1.6*(target_hand+MIDDLE),do_compare=GO, comparison_A = ~ITEM2*vis_pair, comparison_B = ~ITEM2*representation', #respond o_respond_match= 'dot(goal,RESPOND_MATCH) - .1 --> goal=RESPOND_MATCH, motor_input=1.6*(target_hand+INDEX)', p_respond_mismatch= 'dot(goal,RESPOND_MISMATCH) - .1 --> goal=RESPOND_MISMATCH, motor_input=1.6*(target_hand+MIDDLE)', #finish x_response_done= 'dot(goal,RESPOND_MATCH) + dot(goal,RESPOND_MISMATCH) + 2*dot(fingers,L1+L2+R1+R2) - .7 --> goal=2*END', y_end= 'dot(goal,END)-.1 --> goal=END-RESPOND_MATCH-RESPOND_MISMATCH', z_threshold='.05 --> goal=0' #possible to match complete buffer, ie is representation filled? # motor_input=1.5*target_hand+MIDDLE, )) with force_neurons_cfg: model.bg = spa.BasalGanglia(spa.Actions(**actions)) with force_neurons_cfg: model.thalamus = spa.Thalamus(model.bg) model.p_bg_input = nengo.Probe(model.bg.input) model.p_thal_output = nengo.Probe(model.thalamus.actions.output, synapse=0.01) #probes if p.do_motor: model.pr_motor_pos = nengo.Probe( model.finger_pos.output, synapse=.01) #raw vector (dimensions x time) model.pr_motor = nengo.Probe(model.fingers.output, synapse=.01) #model.pr_motor1 = nengo.Probe(model.motor.output, synapse=.01) #if not p.gui: # model.pr_vision_gabor = nengo.Probe(model.vision_gabor.neurons,synapse=.005) #do we need synapse, or should we do something with the spikes # model.pr_familiarity = nengo.Probe(model.dm_learned_words.am.elem_output,synapse=.01) #element output, don't include default # model.pr_concepts = nengo.Probe(model.concepts.am.elem_output, synapse=.01) # element output, don't include default #multiply spikes with the connection weights #input model.input = spa.Input(goal=goal_func) if p.gui: vocab_actions = spa.Vocabulary(model.bg.output.size_out) for i, action in enumerate(model.bg.actions.actions): vocab_actions.add(action.name.upper(), np.eye(model.bg.output.size_out)[i]) model.actions = spa.State(model.bg.output.size_out, subdimensions=model.bg.output.size_out, vocab=vocab_actions) nengo.Connection(model.thalamus.output, model.actions.input) for net in model.networks: if net.label is not None and net.label.startswith('channel'): net.label = '' return model
def create_model(D=512, incl_interoception=False, query=False): vocabulary = sim.Experiment() # Create semantic pointers in each network spa_voc = utils.create_spa_vocabulary(vocabulary, randomize=True, D=D) # Episodic and affect integrate two vocabularies input_episodic = utils.add_vocabularies(spa_voc, 'sensory', 'conceptualization') input_affect = utils.add_vocabularies(spa_voc, 'episodic', 'interoception') with spa.SPA('POEM') as model: # Sensory model._sensory = spa.State(D, vocab=spa_voc['sensory']) # Episodic episodic_output_keys = utils.keys_from_input(input_episodic.keys) zoo_idx = input_episodic.keys.index('Conceptualization_ZOO') episodic_output_keys[zoo_idx] = '2*ZOO-2*GLASS-2*SNAKE' model._episodic = Memory(input_vocab=input_episodic, output_vocab=spa_voc['episodic'], input_keys=input_episodic.keys, output_keys=episodic_output_keys, wta_output=True, wta_inhibit_scale=0.1, threshold_output=True) ep_output_vectors = np.array([spa_voc['episodic'].parse(key).v for key in\ episodic_output_keys], ndmin=2) if query: model.query = spa.State(D, vocab=spa_voc['sensory']) model.bind = spa.Bind(D, invert_b=True) nengo.Connection(model.bind.output, model._episodic.input, transform=3) nengo.Connection(model._sensory.input, model.bind.A) nengo.Connection(model.query.input, model.bind.B) else: nengo.Connection(model._sensory.output, model._episodic.input, transform=3) # 3 because inputs set by cloud are # not normalized # Language model._language = Memory(input_vocab=spa_voc['episodic'], output_vocab=spa_voc['language']) nengo.Connection(model._episodic.am.elem_output, model._language.input, transform=ep_output_vectors.T) # Conceptualization model._conceptualization = Memory(input_vocab=spa_voc['language'], output_vocab=spa_voc['conceptualization'], wta_output=True, wta_synapse=.05, threshold_output=True, wta_inhibit_scale=.3) trans_mat = vocabulary.custom_transform(spa_voc['language'], D=D) nengo.Connection(model._language.output, model._conceptualization.input, transform=trans_mat.T, synapse=0.01) nengo.Connection(model._conceptualization.output, model._episodic.input, synapse=0.3) # Interoception if incl_interoception: model._interoception = spa.State(D, vocab=spa_voc['interoception']) # Affect: EPA expressions at the output input_words = utils.keys_from_input(input_affect.keys) epa_expressions_words = utils.get_epa_expression(input_words) model._affect = Memory(input_vocab=input_affect, output_vocab=spa_voc['affect'], input_keys=input_affect.keys, output_keys=epa_expressions_words, wta_output=False, threshold=0.4) if incl_interoception: nengo.Connection(model._interoception.output, model._affect.input, transform=1.) print(input_affect.keys) # Emotion detection network (comment if not needed) model.active = nengo.Ensemble(n_neurons=200, dimensions=3) model.emotion_present = nengo.Ensemble(n_neurons=50, dimensions=1) nengo.Connection(model._affect.output[[0, 1, 2]], model.active) nengo.Connection(model.active, model.emotion_present, function=lambda x: np.sum(x**2)) nengo.Connection(model._episodic.output, model._affect.input) # Executive: EPA expressions at the input emotion_tags = spa_voc['executive'].keys epa_expressions = utils.get_epa_expression(emotion_tags) model._executive = Memory(input_vocab=spa_voc['affect'], output_vocab=spa_voc['executive'], input_keys=epa_expressions, output_keys=emotion_tags, threshold=0.8) nengo.Connection(model._affect.output, model._executive.input, transform=3) return model, spa_voc
def _test_sequence(Simulator, plt, seed, outfile, prune_passthrough): dimensions = 32 subdimensions = 16 T = 4.0 seq_length = 6 with spa.SPA(seed=seed) as model: model.state = spa.Memory(dimensions=dimensions, subdimensions=subdimensions) seq_actions = [ 'dot(state,A%d) --> state=A%d' % (i, (i + 1) % seq_length) for i in range(seq_length) ] model.bg = spa.BasalGanglia(spa.Actions(*seq_actions)) model.thal = spa.Thalamus(model.bg) def stim_state(t): if t < 0.1: return 'A0' else: return '0' model.input = spa.Input(state=stim_state) p_state = nengo.Probe(model.state.state.output, synapse=0.01) if 'spinnaker' in Simulator.__module__: nengo_spinnaker.add_spinnaker_params(model.config) model.config[ model.input.input_nodes['state']].function_of_time = True vocab = model.get_input_vocab('state') if prune_passthrough: model = remove_passthrough_nodes(model) sim = Simulator(model) sim.run(T) t = sim.trange() data = sim.data[p_state] ideal = np.array([vocab.parse('A%d' % i).v for i in range(seq_length)]) dotp = np.dot(data, ideal.T) best = np.argmax(dotp, axis=1) delta = np.diff(best) indexes = np.where(delta != 0) # [:, 1:] ignores the first transition, which is meaningless delta_t = np.diff(indexes)[:, 1:] * 0.001 mean = np.mean(delta_t) std = np.std(delta_t) outfile.write('"n_neurons": %d,\n' % sum(e.n_neurons for e in model.all_ensembles)) outfile.write('"simtime": %f,\n' % T) outfile.write('"timing_mean": %f,\n' % mean) outfile.write('"timing_std": %f,\n' % std) figsize = (onecolumn, 4.0) if horizontal else (onecolumn * 2, 3.0) setup(figsize=figsize, palette_args={ 'palette': "cubehelix", 'n_colors': 6 }) plt.plot(t[t < 1.0], dotp[t < 1.0]) for transition in t[indexes[0]]: plt.axvline(transition, c='0.5', lw=1, ls=':') plt.ylabel('Similarity to item') plt.xlabel('Time (s)') plt.xlim(right=1.0) sns.despine() if prune_passthrough: plt.saveas = 'results-4.svg' else: plt.saveas = 'results-5.svg' if hasattr(sim, 'close'): sim.close()
[r_2, r_22, 0.2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) vects['input3'].append( [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, r_3, r_32, r_3, r_3]) dimensions = 16 #number of dimensions for semantic pointers # change these slightly to create individual variation vocab = spa.Vocabulary(dimensions, randomize=False) # vocab.add('ON_1',[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]) # vocab.add('ON_2',[0.4,0.4,0.2,0,0,0,0,0,0,0,0,0,0,0,0,0]) # vocab.add('REST',[0,0,0,0,0,0,0,0,0,0,0,0,0.25,0.25,0.25,0.25]) vocab.add('ON_1', vects['input1'][subject]) vocab.add('ON_2', vects['input2'][subject]) vocab.add('REST', vects['input3'][subject]) model = spa.SPA(dimensions, vocabs=vocab) with model: model.cortex = spa.Buffer(dimensions=dimensions) #print model.get_default_vocab(dimensions).parse("ON_1") #action mapping actions = spa.Actions( 'dot(cortex, ON_1) --> cortex = ON_1', 'dot(cortex, ON_2) --> cortex = ON_2', 'dot(cortex, REST) --> cortex = REST', ) model.bg = spa.BasalGanglia(actions=actions) model.thal = spa.Thalamus(model.bg) ##########################
import matplotlib.pyplot as plt import nengo from nengo import spa # ## Create the model # # Notice that when you specify actions, you're determining which modules are connected to which. For example, by having a mapping that depends on the state of cortex, you are determining that the cortex and basal ganglia must be connected. As well, when you specify that the result of the action changes the state of cortex, then you are determining that thalamus must be connected to cortex. # In[ ]: # Number of dimensions for the Semantic Pointers dimensions = 128 # Make a model object with the SPA network model = spa.SPA(label='Controlled_Question_Answering') with model: # Specify the modules to be used model.vision = spa.Buffer(dimensions=dimensions, neurons_per_dimension=100) model.motor = spa.Buffer(dimensions=dimensions, neurons_per_dimension=100) model.memory = spa.Memory(dimensions=dimensions, neurons_per_dimension=100, synapse=0.1) # Specify the action mapping actions = spa.Actions( 'dot(vision, STATEMENT) --> memory = vision', 'dot(vision, QUESTION) --> motor = ~vision*memory', ) model.bg = spa.BasalGanglia(actions=actions)
def __init__(self, mapping, D_category=16, D_items=16, threshold=0.4, learning_rate=1e-4): model = spa.SPA() self.model = model self.mapping = mapping self.vocab_category = spa.Vocabulary(D_category) self.vocab_items = spa.Vocabulary(D_items) self.vocab_items_temp = spa.Vocabulary(D_items) for k in sorted( mapping.keys()): #allocating verctors for categories name self.vocab_category.parse(k) for v in mapping[k]: # allocating vectors to the items self.vocab_items.parse(v) with model: model.category = spa.State(D_category, vocab=self.vocab_category) model.items = spa.State(D_items, vocab=self.vocab_items, subdimensions=64) #encoders = ens.encoders.sample(n=ens.n_neurons, d=D) n_neuron = model.items.state_ensembles.all_ensembles[0].n_neurons encoders = model.items.state_ensembles.all_ensembles[ 0].encoders.sample(n=n_neuron, d=D_items) related = 0.5 w = 1.0 / related - 1 count = 1 for k in sorted(mapping.keys()[:4]): #Practiced Category for item in mapping[k][:2]: #low freq items for i in range((count - 1) * 50, count * 50): encoders[i] = -self.vocab_items.parse( item).v + w * encoders[i] encoders[i] = encoders[i] / np.linalg.norm(encoders[i]) count = count + 1 model.items.state_ensembles.all_ensembles[0].encoders = encoders def learned(x): cats = np.dot(self.vocab_category.vectors, x) best_index = np.argmax( cats ) #takes the category which has the largest projection of x if cats[best_index] < threshold: return self.vocab_items.parse('0').v else: #generate the sum vector k = self.vocab_category.keys[best_index] total = '+'.join(self.mapping[k]) v = self.vocab_items.parse(total).v return v / (2 * np.linalg.norm(v)) c = nengo.Connection( model.category.all_ensembles[0], model.items.input, function=learned, learning_rule_type=nengo.PES(learning_rate=learning_rate)) model.error = spa.State(D_items, vocab=self.vocab_items) nengo.Connection(model.items.output, model.error.input) nengo.Connection(model.error.output, c.learning_rule) self.stim_category_value = np.zeros(D_category) self.stim_category = nengo.Node(self.stim_category) nengo.Connection(self.stim_category, model.category.input, synapse=None) self.stim_correct_value = np.zeros(D_items) self.stim_correct = nengo.Node(self.stim_correct) nengo.Connection(self.stim_correct, model.error.input, synapse=None, transform=-1) self.stim_stoplearn_value = np.zeros(1) self.stim_stoplearn = nengo.Node(self.stim_stoplearn) for ens in model.error.all_ensembles: nengo.Connection(self.stim_stoplearn, ens.neurons, synapse=None, transform=-10 * np.ones((ens.n_neurons, 1))) self.stim_justmemorize_value = np.zeros(1) self.stim_justmemorize = nengo.Node(self.stim_justmemorize) for ens in model.items.all_ensembles: nengo.Connection(self.stim_justmemorize, ens.neurons, synapse=None, transform=-10 * np.ones((ens.n_neurons, 1))) self.probe_items = nengo.Probe(model.items.output, synapse=0.01) self.sim = nengo.Simulator(self.model)
# random locations rng_items = np.random.RandomState(seed=args.item_seed) locs = rng_items.uniform(0, 10, size=(7, 2)) items = OrderedDict({ "Tree": locs[0, :], "Pond": locs[1, :], "Well": locs[2, :], "Rock": locs[3, :], "Reed": locs[4, :], "Lake": locs[5, :], "Bush": locs[6, :] }) shape = (10, 10) model = spa.SPA(seed=seed) action_vocab = spa.Vocabulary(3, randomize=False) item_vocab = spa.Vocabulary(D, randomize=randomize) # item_vocab = spa.Vocabulary(len(items), randomize=True) """ keys = np.array([[1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1], ]) """ keys = np.eye(len(items))
vocab_goal.parse( 'START+RETRIEVE+COMPARE+RESPOND+DONE+START_COUNTING+UPDATE_RESULT+STORE_RESULT+UPDATE_COUNT+STORE_COUNT+COMPARE_COUNT' ) #vocab reset vocab_reset = spa.Vocabulary(Dlow, rng=rng_vocabs) vocab_reset.parse('CLEAR+GO') #vocab_motor vocab_motor = spa.Vocabulary(Dlow, rng=rng_vocabs) vocab_motor.parse('YES') vocab_motor.parse('NO') ###### Model ####### model = spa.SPA(seed=fseed) with model: #Vision & Goal model.vision = spa.State(D, vocab=vocab_concepts) #visual input model.goalnet = nengo.Network(seed=fseed) with model.goalnet: model.goal = spa.State(Dlow, vocab=vocab_goal, feedback=.5) #goal state #.75 model.count = spa.State( D, vocab=vocab_concepts, feedback=.8, feedback_synapse=.05) #the number of counts taken model.target = spa.State(D, vocab=vocab_concepts, feedback=1) #target answer from vision
def SyllableSequence(n_per_d, syllable_d, syllables, difference_gain=15, n_positions=7, threshold_memories=True, add_default_output=True, rng=np.random, net=None): if net is None: net = spa.SPA("Syllable sequence") assert isinstance(net, spa.SPA), "Network must be a SPA instance." vocab = spa.Vocabulary(dimensions=syllable_d, rng=rng, unitary=['INC', 'POS1']) vocab.parse('INC + POS1') for i in range(2, n_positions+1): vocab.add('POS%d' % i, vocab.parse('POS%d * INC' % (i-1))) vocab.parse(" + ".join(syllables)) syll_vocab = vocab.create_subset(syllables) net.vocab = vocab with net: # --- Input sequence net.sequence = spa.State(dimensions=syllable_d, neurons_per_dimension=n_per_d) # --- Input gated memories iterate through position vectors net.pos_curr = InputGatedMemory( n_per_d, dimensions=syllable_d, difference_gain=difference_gain) net.pos_next = InputGatedMemory( n_per_d, dimensions=syllable_d, difference_gain=difference_gain) # Switch from current to next with reciprocal connections nengo.Connection(net.pos_curr.output, net.pos_next.input) nengo.Connection(net.pos_next.output, net.pos_curr.input, transform=vocab['INC'].get_convolution_matrix()) # Switching depends on gate; get gate input elsewhere net.gate = nengo.Node(size_in=1) # pos gets gate nengo.Connection(net.gate, net.pos_curr.gate, synapse=None) # pos_next gets 1 - gate net.gate_bias = nengo.Node(output=1) nengo.Connection(net.gate_bias, net.pos_next.gate, synapse=None) nengo.Connection(net.gate, net.pos_next.gate, transform=-1, synapse=None) # --- Get syllables with unbinding net.bind = CircularConvolution( n_per_d, syllable_d, invert_a=False, invert_b=True) net.bind_next = CircularConvolution( n_per_d, syllable_d, invert_a=False, invert_b=True) nengo.Connection(net.sequence.output, net.bind.A) nengo.Connection(net.sequence.output, net.bind_next.A) nengo.Connection(net.pos_curr.output, net.bind.B) nengo.Connection(net.pos_next.output, net.bind_next.B, transform=vocab['INC'].get_convolution_matrix()) # --- Clean up noisy syllables with associative memories net.syllable = spa.AssociativeMemory( syll_vocab, wta_output=True, threshold_output=threshold_memories) net.syllable_next = spa.AssociativeMemory( syll_vocab, wta_output=True, threshold_output=threshold_memories) nengo.Connection(net.bind.output, net.syllable.input) nengo.Connection(net.bind_next.output, net.syllable_next.input) if add_default_output: default = np.zeros(syllable_d) net.syllable.am.add_default_output_vector(default) net.syllable_next.am.add_default_output_vector(default) return net
import nengo import nengo.spa as spa D = 16 model = spa.SPA(seed=1) with model: model.a = spa.Buffer(dimensions=D) model.b = spa.Buffer(dimensions=D) model.c = spa.Buffer(dimensions=D) model.cortical = spa.Cortical(spa.Actions('c = a+b', )) model.input = spa.Input( a='A', b=(lambda t: 'C*~A' if (t % 0.1 < 0.05) else 'D*~A'), ) if __name__ == '__main__': import nengo_gui nengo_gui.Viz(__file__).start()
import nengo import nengo.spa as spa import numpy as np import time dimensions = 32 vocab = spa.Vocabulary(dimensions) model = spa.SPA(label='ActionExchange') """ class ActionState(nengo.Node): def __init__(self): self.action = "WAIT" super(ActionState, self).__init__(self.update) def parse_action(t, action): turn = vocab.parse("TURN").v wait = vocab.parse("WAIT").v if np.dot(action, turn) > np.dot(action, wait): self.turn() return "WAIT" def turn(): time.sleep(2) pass def update(self, t): if self.action == "TURN": self.turn()
# vecotors A and B). # Setup the environment import nengo import nengo.spa as spa from nengo.spa import Vocabulary import numpy as np D = 32 # the dimensionality of the vectors #Creating a vocabulary rng = np.random.RandomState(0) vocab = Vocabulary(dimensions=D, rng=rng) vocab.add('C', vocab.parse('A * B')) model = spa.SPA(label="structure", vocabs=[vocab]) with model: model.A = spa.State(D) model.B = spa.State(D) model.C = spa.State(D, feedback=1) model.Sum = spa.State(D) actions = spa.Actions( 'C = A * B', 'Sum = A', 'Sum = B' ) model.cortical = spa.Cortical(actions) #Input
def model(self, p): vis_items = ['FATIGUE', 'WHISKEY'] vis_vocab = spa.Vocabulary(p.D) self.vis_items = vis_items self.vis_vocab = vis_vocab result_vocab_items = ['SAME'] input_items = ['PUSH'] action_items = ['F1'] self.result_vocab_items = result_vocab_items self.input_items = input_items self.action_items = action_items ##### Vision and motor system ######### import vision_system as v import motor_system as m reload(v) reload(m) directory = '/home/stacy/github/visual-categorization/assoc_recog_s/images/' image_list = v.load_images(directory, items=vis_items) output_list = v.vector_gen_function(vis_items, vocab=vis_vocab) self.directory = directory self.image_list = image_list self.output_list = output_list model = spa.SPA(label='MAIN') with model: model.vision_system = v.make_vision_system( image_list, output_list, n_neurons=500, AIT_V1_strength=p.AIT_V1_strength, AIT_r_transform=p.AIT_r_transform, V1_r_transform=p.V1_r_transform) model.concept = spa.State(p.D, vocab=vis_vocab) nengo.Connection(model.vision_system.AIT, model.concept.input) model.compare = spa.Compare(p.D) model.wm = spa.State(p.D, vocab=vis_vocab) model.result = spa.State(p.D, feedback=p.result_feedback) nengo.Connection(model.concept.output, model.compare.inputA, synapse=0.01) nengo.Connection(model.wm.output, model.compare.inputB) vocab = model.get_input_vocab('result') nengo.Connection(model.compare.output, model.result.input, transform=p.compare_to_result_strength * np.array([vocab.parse('SAME').v]).T) def result_to_motor(in_vocab, out_vocab): mapping = np.zeros((p.D, p.D)) for i in range(len(input_items)): mapping += np.outer( in_vocab.parse(result_vocab_items[i]).v, out_vocab.parse(input_items[i]).v) transform = mapping.T return transform model.motor_system = m.make_motor_system( input_items, action_items, motor_feedback=p.motor_feedback, motor_transform=p.motor_transform, finger_feedback=p.finger_feedback, motor_to_fingers_strength=p.motor_to_fingers_strength) nengo.Connection(model.result.output, model.motor_system.motor_input.input, transform=result_to_motor( vocab, model.motor_system.motor_vocab)) def present_func(t): if t < 1: index = 0 else: index = 1 return image_list[index] stim = nengo.Node(present_func) nengo.Connection(stim, model.vision_system.presentation_node) stim_wm = nengo.Node( model.get_input_vocab('wm').parse('FATIGUE').v) nengo.Connection(stim_wm, model.wm.input) self.V1_probe = nengo.Probe(model.vision_system.V1) self.AIT_probe = nengo.Probe(model.vision_system.AIT, synapse=0.005) self.PFC_probe = nengo.Probe(model.compare.output, synapse=0.005) self.PMC_probe = nengo.Probe(model.result.output, synapse=0.005) self.MC_probe = nengo.Probe(model.motor_system.motor.output, synapse=0.005) self.finger_probe = nengo.Probe(model.motor_system.fingers.output, synapse=0.005) self.final_probe = nengo.Probe( model.motor_system.finger_pos.output, synapse=0.005) self.mymodel = model return model
import nengo import nengo.spa as spa import numpy as np D = 32 vocab = spa.Vocabulary(D) food_vocab = spa.Vocabulary(D) sub_vocab = spa.Vocabulary(D) model = spa.SPA(label="Find Food") with model: def starter(t): if t < 0.05: return "START_FIND_FOOD" else: return "0" def food_exist_func(t): if t < 0.5: return "NO_FOOD" else: return "FOOD" def food_close_func(t): if t < 1: return "NO_FOOD" else: return "CLOSE"
import nengo.spa as spa from nengo.spa import Vocabulary import numpy as np D = 32 # the dimensionality of the vectors rng = np.random.RandomState(7) vocab = Vocabulary(dimensions=D, rng=rng, max_similarity=0.1) #Adding semantic pointers to the vocabulary CIRCLE = vocab.parse('CIRCLE') BLUE = vocab.parse('BLUE') RED = vocab.parse('RED') SQUARE = vocab.parse('SQUARE') ZERO = vocab.add('ZERO', [0] * D) model = spa.SPA(label="Question Answering", vocabs=[vocab]) with model: model.A = spa.State(D, label="color") model.B = spa.State(D, label="shape") model.C = spa.State(D, label="cue") model.D = spa.State(D, label="bound") model.E = spa.State(D, label="output") actions = spa.Actions( 'D = A * B', 'E = D * ~C', ) model.cortical = spa.Cortical(actions)
# Test binding output import nengo import nengo.spa as spa import numpy as np dimensions = 32 vocab = spa.Vocabulary(dimensions) model = spa.SPA(label='Play_w_input') def my_output(t, x): # How similar is x to STOP? eat = np.dot(x, vocab.parse('STOP').v) return eat def my_input(): pass with model: model.subgoal = spa.State(dimensions, vocab) model.food = spa.State(dimensions, vocab) model.food_percept = spa.State(dimensions, vocab, feedback=1, feedback_synapse=0.1) model.now_state = spa.State(dimensions,
# The book describes that the results of the model can be seen through the # visualizer in Nengo 1.4 GUI which has a "Utility" box and the "Rules" box. # Note that the bottom-right graph shows the same information as seen in the # "Rules" box and top-right graph shows the same information as seen in the # "Utility" box. #Setup the environment import nengo from nengo import spa #import spa related packages #Number of dimensions for the Semantic Pointers dimensions = 16 #Make a model with the SPA network model = spa.SPA(label='Sequence') with model: #Creating a working memory/cortical element model.state = spa.State(dimensions=dimensions, feedback=1, feedback_synapse=0.01) #Specifying the action mappings (rules) for BG and Thal actions = spa.Actions('dot(state, A) --> state = B', 'dot(state, B) --> state = C', 'dot(state, C) --> state = D', 'dot(state, D) --> state = E', 'dot(state, E) --> state = A') #Creating the BG and Thalamus components that confirm to the specified rules model.BG = spa.BasalGanglia(actions=actions)
def create_model(seed=None): #create vocabulary to show representations in gui if nengo_gui_on: vocab_angles = spa.Vocabulary(D) for name in [0, 3, 7, 12, 18, 25, 33, 42]: #vocab_angles.add('D' + str(name), np.linalg.norm(compressed_im_cued[name+90])) #take mean across phases v = compressed_im_cued[name + 90] nrm = np.linalg.norm(v) if nrm > 0: v /= nrm vocab_angles.add('D' + str(name), v) #take mean across phases v = np.dot(imagearr[-1, :] / 50, U_cued[:, :D]) nrm = np.linalg.norm(v) if nrm > 0: v /= nrm vocab_angles.add('Impulse', v) #model = nengo.Network(seed=seed) model = spa.SPA(seed=seed) with model: #input nodes if not batch_processing: model.inputNode_cued = nengo.Node(input_func_cued, label='input_cued') model.reactivate = nengo.Node(reactivate_func, label='reactivate') elif batch_processing: model.inputNode_cued = nengo.Node(np.zeros(128 * 128)) model.reactivate = nengo.Node(np.zeros(Nm)) #eye ensemble eye_cued = nengo.Ensemble(Ns, D, encoders=e_cued, intercepts=Uniform(0.01, 0.1), radius=1, label='eye_cued', seed=seed) nengo.Connection(model.inputNode_cued, eye_cued, transform=U_cued[:, :D].T) #sensory ensemble sensory_cued = nengo.Ensemble(Ns, D, encoders=e_cued, intercepts=Uniform(0.01, .1), radius=1, label='sensory_cued') conn_cued = nengo.Connection( eye_cued, sensory_cued, function=lambda x: x) #default: identity function if nengo_gui_on: with nengo.Simulator(network=model, progress_bar=False) as sim_cued: weights_cued = sim_cued.data[conn_cued].weights model.connections.remove(conn_cued) elif not (nengo_gui_on): with nengo_dl.Simulator( network=model, progress_bar=False, device=device) as sim_cued: #, device=device weights_cued = sim_cued.data[conn_cued].weights model.connections.remove(conn_cued) Ncluster = 1 # number of neurons per cluster clusters = np.arange(0, Ns, Ncluster) #parameters for gamma distribution where k=shape, theta=scale and mean=k*theta -> theta=mean/k # wanted distribution: lowest value 51ms and mean value 141 ms (Lamme & Roelfsema (2000)) k, mean = 2, 0.090 #(mean_synapse - lowest_synapse) #subtract lowest synapse from mean so distribution starts at 0 theta = mean / k shift = 0.051 weights_trans = 1.50 rec_trans = 0.30 noise_sd = 0.010 if uncued: # Build second simulation to fetch weights before making the large amount of connections between eye_cued and sensory_cued, place all # of uncued model inside this if statement to keep code close together if not batch_processing: model.inputNode_uncued = nengo.Node(input_func_uncued, label='input_first') elif batch_processing: model.inputNode_uncued = nengo.Node(np.zeros(128 * 128)) eye_uncued = nengo.Ensemble(Ns, D, encoders=e_uncued, intercepts=Uniform(0.01, 0.1), radius=1, label='eye_uncued', seed=seed) nengo.Connection(model.inputNode_uncued, eye_uncued, transform=U_uncued[:, :D].T) sensory_uncued = nengo.Ensemble(Ns, D, encoders=e_uncued, intercepts=Uniform(0.01, .1), radius=1, label='sensory_uncued') conn_uncued = nengo.Connection( eye_uncued, sensory_uncued, function=lambda x: x) #default: identity function if nengo_gui_on: with nengo.Simulator(network=model, progress_bar=False) as sim_uncued: weights_uncued = sim_uncued.data[conn_uncued].weights model.connections.remove(conn_uncued) elif not (nengo_gui_on): with nengo_dl.Simulator( network=model, progress_bar=False, device=device) as sim_uncued: #device=device weights_uncued = sim_uncued.data[conn_uncued].weights model.connections.remove(conn_uncued) synapse_uncued = np.random.gamma(k, theta, clusters.size) + shift for i in range(clusters.size): begin = clusters[i] end = (begin + Ncluster) nengo.Connection(eye_uncued.neurons[begin:end], sensory_uncued, transform=weights_uncued[:, begin:end] * weights_trans, synapse=synapse_uncued[i]) nengo.Connection(sensory_uncued, eye_uncued, transform=rec_trans, solver=nengo.solvers.LstsqL2(weights=True), synapse=0.200) noise_uncued = nengo.processes.WhiteNoise( dist=Gaussian(0, noise_sd)) memory_uncued = nengo.Ensemble(Nm, D, neuron_type=stpLIF(), intercepts=Uniform(0.01, .1), noise=noise_uncued, radius=1, label='memory_uncued') nengo.Connection(sensory_uncued, memory_uncued, transform=0.1) nengo.Connection(memory_uncued, memory_uncued, transform=1, learning_rule_type=STP(), solver=nengo.solvers.LstsqL2(weights=True)) comparison_uncued = nengo.Ensemble(Nd, dimensions=4, radius=math.sqrt(2), intercepts=Uniform(.01, 1), label='comparison_uncued') nengo.Connection(memory_uncued, comparison_uncued[2:], eval_points=compressed_im_uncued[0:-1], function=sincos.T) nengo.Connection(sensory_uncued, comparison_uncued[:2], eval_points=compressed_im_uncued[0:-1], function=sincos.T) decision_uncued = nengo.Ensemble(n_neurons=Nd, dimensions=1, radius=45, label='decision_uncued') nengo.Connection(comparison_uncued, decision_uncued, eval_points=ep, scale_eval_points=False, function=arctan_func) ### cued ### synapse_cued = np.random.gamma(k, theta, clusters.size) + shift for i in range(clusters.size): begin = clusters[i] end = (begin + Ncluster) nengo.Connection(eye_cued.neurons[begin:end], sensory_cued, transform=weights_cued[:, begin:end] * weights_trans, synapse=synapse_cued[i]) nengo.Connection(sensory_cued, eye_cued, transform=rec_trans, solver=nengo.solvers.LstsqL2(weights=True), synapse=0.200) #memory ensemble noise_cued = nengo.processes.WhiteNoise(dist=Gaussian(0, noise_sd)) memory_cued = nengo.Ensemble(Nm, D, neuron_type=stpLIF(), intercepts=Uniform(0.01, .1), noise=noise_cued, radius=1, label='memory_cued') nengo.Connection(sensory_cued, memory_cued, transform=0.1) nengo.Connection(model.reactivate, memory_cued.neurons) #potential reactivation #recurrent STSP connection nengo.Connection(memory_cued, memory_cued, transform=1, learning_rule_type=STP(), solver=nengo.solvers.LstsqL2(weights=True)) #comparison represents sin, cosine of theta of both sensory and memory ensemble comparison_cued = nengo.Ensemble(Nc, dimensions=4, radius=math.sqrt(2), intercepts=Uniform(.01, 1), label='comparison_cued') nengo.Connection(sensory_cued, comparison_cued[:2], eval_points=compressed_im_cued[0:-1], function=sincos.T) nengo.Connection(memory_cued, comparison_cued[2:], eval_points=compressed_im_cued[0:-1], function=sincos.T) #decision represents the difference in theta decoded from the sensory and memory ensembles decision_cued = nengo.Ensemble(n_neurons=Nd, dimensions=1, radius=45, label='decision_cued') nengo.Connection(comparison_cued, decision_cued, eval_points=ep, scale_eval_points=False, function=arctan_func) #decode for gui if nengo_gui_on: model.sensory_decode = spa.State(D, vocab=vocab_angles, subdimensions=12, label='sensory_decode') for ens in model.sensory_decode.all_ensembles: ens.neuron_type = nengo.Direct() nengo.Connection(sensory_cued, model.sensory_decode.input, synapse=None) model.memory_decode = spa.State(D, vocab=vocab_angles, subdimensions=12, label='memory_decode') for ens in model.memory_decode.all_ensembles: ens.neuron_type = nengo.Direct() nengo.Connection(memory_cued, model.memory_decode.input, synapse=None) #probes if not (nengo_gui_on): if store_representations: #sim 1 trials 1-100 model.p_mem_cued = nengo.Probe(memory_cued, synapse=0.05) model.p_mem_uncued = nengo.Probe(memory_uncued, synapse=0.05) if store_spikes_and_resources: #sim 1 trial 1 model.p_spikes_mem_cued = nengo.Probe(memory_cued.neurons, 'spikes') model.p_res_cued = nengo.Probe(memory_cued.neurons, 'resources') model.p_cal_cued = nengo.Probe(memory_cued.neurons, 'calcium') model.p_spikes_mem_uncued = nengo.Probe( memory_uncued.neurons, 'spikes') model.p_res_uncued = nengo.Probe(memory_uncued.neurons, 'resources') model.p_cal_uncued = nengo.Probe(memory_uncued.neurons, 'calcium') if store_decisions: #sim 2 model.p_dec_cued = nengo.Probe(decision_cued, synapse=0.01) if store_memory: model.p_mem_cued_raw = nengo.Probe(memory_cued.neurons, 'output', synapse=0.05) model.p_mem_uncued_raw = nengo.Probe(memory_uncued.neurons, 'output', synapse=0.05) return model
def __init__(self, mapping, threshold=0.2, learning_rate=1e-4, DimVocab=256, subdimensions=32, learned_function_scale=2.0): model = spa.SPA() self.model = model self.mapping = mapping #self.vocab_category = spa.Vocabulary(D_category) #self.vocab_items = spa.Vocabulary(D_items) self.VocabUnified = spa.Vocabulary(DimVocab) for k in sorted( mapping.keys()): #allocating verctors for categories name self.VocabUnified.parse(k) for v in mapping[k]: # allocating vectors to the items self.VocabUnified.parse(v) with model: model.cue = spa.State(DimVocab, vocab=self.VocabUnified, subdimensions=subdimensions) model.target = spa.State(DimVocab, vocab=self.VocabUnified, subdimensions=subdimensions) c = [] n_sub = len(model.cue.state_ensembles.ea_ensembles) for i in range(n_sub): cues = [] targets = [] for cue, vals in mapping.items(): for val in vals: cues.append( self.VocabUnified.parse(cue).v[i * subdimensions:(i + 1) * subdimensions]) targets.append( self.VocabUnified.parse(val).v / n_sub * learned_function_scale) cues.append( self.VocabUnified.parse(val).v[i * subdimensions:(i + 1) * subdimensions]) targets.append( self.VocabUnified.parse(cue).v / n_sub * learned_function_scale) cc = nengo.Connection( model.cue.all_ensembles[i], model.target.input, learning_rule_type=nengo.PES(learning_rate=learning_rate), **nengo.utils.connection.target_function(cues, targets)) cc.eval_points = cues cc.function = targets c.append(cc) print i #model.error = spa.State(D_items, vocab=self.vocab_items) #nengo.Connection(model.items.output, model.error.input) #nengo.Connection(model.error.output, c.learning_rule) #I am not sure how this implements the learning model.error = spa.State(DimVocab, vocab=self.VocabUnified, subdimensions=subdimensions) nengo.Connection(model.target.output, model.error.input) print 'the loop ended, right?' for cc in c: nengo.Connection(model.error.output, cc.learning_rule) self.stim_cue_value = np.zeros(DimVocab) #? self.stim_cue = nengo.Node(self.stim_cue_fun) #? nengo.Connection(self.stim_cue, model.cue.input, synapse=None) self.stim_correct_value = np.zeros(DimVocab) self.stim_correct = nengo.Node(self.stim_correct) nengo.Connection(self.stim_correct, model.error.input, synapse=None, transform=-1) self.stim_stoplearn_value = np.zeros(1) self.stim_stoplearn = nengo.Node(self.stim_stoplearn) for ens in model.error.all_ensembles: nengo.Connection(self.stim_stoplearn, ens.neurons, synapse=None, transform=-10 * np.ones((ens.n_neurons, 1))) self.stim_justmemorize_value = np.zeros(1) self.stim_justmemorize = nengo.Node(self.stim_justmemorize) for ens in model.target.all_ensembles: #? nengo.Connection(self.stim_justmemorize, ens.neurons, synapse=None, transform=-10 * np.ones((ens.n_neurons, 1))) self.probe_target = nengo.Probe(model.target.output, synapse=0.01) if __name__ != '__builtin__': self.sim = nengo.Simulator(self.model)
import nengo.spa as spa from nengo.spa import Vocabulary import numpy as np D = 64 # the dimensionality of the vectors rng = np.random.RandomState(15) vocab = Vocabulary(dimensions=D, rng=rng, max_similarity=0.1) #Adding semantic pointers to the vocabulary CIRCLE=vocab.parse('CIRCLE') BLUE=vocab.parse('BLUE') RED=vocab.parse('RED') SQUARE=vocab.parse('SQUARE') ZERO=vocab.add('ZERO', [0]*D) model = spa.SPA(label="Question Answering with Control", vocabs=[vocab]) with model: model.visual = spa.State(D) model.motor = spa.State(D) model.memory = spa.State(D, feedback=1, feedback_synapse=0.1) actions = spa.Actions( 'dot(visual, STATEMENT) --> memory=visual', 'dot(visual, QUESTION) --> motor = memory * ~visual' ) model.bg = spa.BasalGanglia(actions) model.thalamus = spa.Thalamus(model.bg) #function for providing visual input
def generate(input_signal, alpha=1000.0): beta = alpha / 4.0 # generate the Function Space forces, _, goals = forcing_functions.load_folder( 'models/locomotion_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 movement weights_a = [] # ankle weights_k = [] # knee weights_h = [] # hip # NOTE: things are added to weights based on the order files are read for ii in range(int(len(goals) / 6)): forces = force_space[ii * 6:ii * 6 + 6] # load up the forces to be output by the forcing function # calculate the corresponding weights over the basis functions weights_a.append( np.hstack([ np.dot(fs.basis.T, forces[0]), # ankle 1 np.dot(fs.basis.T, forces[1]) ])) # ankle 2 weights_h.append( np.hstack([ np.dot(fs.basis.T, forces[2]), # hip 1 np.dot(fs.basis.T, forces[3]) ])) # hip 2 weights_k.append( np.hstack([ np.dot(fs.basis.T, forces[4]), # knee 1 np.dot(fs.basis.T, forces[5]) ])) # knee 2 # Create our vocabularies sps_labels = ['GALLOP', 'RUNNING', 'WALKING'] rng = np.random.RandomState(0) dimensions = 50 # some arbitrary number vocab_input = Vocabulary(dimensions=dimensions, rng=rng) vocab_dmp_weights_a = Vocabulary(dimensions=fs.n_basis * 2, rng=rng) vocab_dmp_weights_k = Vocabulary(dimensions=fs.n_basis * 2, rng=rng) vocab_dmp_weights_h = Vocabulary(dimensions=fs.n_basis * 2, rng=rng) for ii, (label, wa, wk, wh) in enumerate(zip(sps_labels, weights_a, weights_k, weights_h)): vocab_input.parse(label) # randomly generate input vector vocab_dmp_weights_a.add(label, wa) vocab_dmp_weights_k.add(label, wk) vocab_dmp_weights_h.add(label, wh) net = spa.SPA() net.config[nengo.Ensemble].neuron_type = nengo.LIFRate() with net: config = nengo.Config(nengo.Ensemble) config[nengo.Ensemble].neuron_type = nengo.Direct() with config: # --------------------- Inputs -------------------------- # def input_func(t): # return vocab_input.parse(input_signal).v # net.input = nengo.Node(input_func) net.input = spa.State(dimensions, subdimensions=10, vocab=vocab_input) # ------------------- Point Attractors -------------------- zero = nengo.Node([0]) net.a1 = point_attractor.generate(n_neurons=1000, alpha=alpha, beta=beta) nengo.Connection(zero, net.a1.input[0], synapse=None) net.a2 = point_attractor.generate(n_neurons=1000, alpha=alpha, beta=beta) nengo.Connection(zero, net.a1.input[0], synapse=None) net.k1 = point_attractor.generate(n_neurons=1000, alpha=alpha, beta=beta) nengo.Connection(zero, net.k1.input[0], synapse=None) net.k2 = point_attractor.generate(n_neurons=1000, alpha=alpha, beta=beta) nengo.Connection(zero, net.k2.input[0], synapse=None) net.h1 = point_attractor.generate(n_neurons=1000, alpha=alpha, beta=beta) nengo.Connection(zero, net.h1.input[0], synapse=None) net.h2 = point_attractor.generate(n_neurons=1000, alpha=alpha, beta=beta) nengo.Connection(zero, net.h2.input[0], synapse=None) # -------------------- Oscillators ---------------------- kick = nengo.Node(nengo.utils.functions.piecewise({ 0: 1, .05: 0 }), label='kick') osc = oscillator.generate(net, n_neurons=3000, speed=.01) osc.label = 'oscillator' nengo.Connection(kick, osc[0]) # ------------------- Forcing Functions -------------------- with config: net.assoc_mem_a = spa.AssociativeMemory( input_vocab=vocab_input, output_vocab=vocab_dmp_weights_a, wta_output=False) nengo.Connection(net.input.output, net.assoc_mem_a.input) net.assoc_mem_k = spa.AssociativeMemory( input_vocab=vocab_input, output_vocab=vocab_dmp_weights_k, wta_output=False) nengo.Connection(net.input.output, net.assoc_mem_k.input) net.assoc_mem_h = spa.AssociativeMemory( input_vocab=vocab_input, output_vocab=vocab_dmp_weights_h, wta_output=False) nengo.Connection(net.input.output, net.assoc_mem_h.input) # -------------------- Product for decoding ----------------------- product_a1 = nengo.Network('Product A1') nengo.networks.Product(n_neurons=1000, dimensions=fs.n_basis, net=product_a1) product_a2 = nengo.Network('Product A2') nengo.networks.Product(n_neurons=1000, dimensions=fs.n_basis, net=product_a2) product_h1 = nengo.Network('Product H1') nengo.networks.Product(n_neurons=1000, dimensions=fs.n_basis, net=product_h1) product_h2 = nengo.Network('Product H2') nengo.networks.Product(n_neurons=1000, dimensions=fs.n_basis, net=product_h2) product_k1 = nengo.Network('Product K1') nengo.networks.Product(n_neurons=1000, dimensions=fs.n_basis, net=product_k1) product_k2 = nengo.Network('Product K2') nengo.networks.Product(n_neurons=1000, dimensions=fs.n_basis, net=product_k2) # 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_a.output[:fs.n_basis], net.assoc_mem_a.output[fs.n_basis:], net.assoc_mem_k.output[:fs.n_basis], net.assoc_mem_k.output[fs.n_basis:], net.assoc_mem_h.output[:fs.n_basis], net.assoc_mem_h.output[fs.n_basis:] ], [ product_a1, product_a2, product_k1, product_k2, product_h1, product_h2 ]): 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(product_a1.output, net.a1.input[1], transform=np.ones((1, fs.n_basis)) * max_basis) nengo.Connection(product_a2.output, net.a2.input[1], transform=np.ones((1, fs.n_basis)) * max_basis) nengo.Connection(product_k1.output, net.k1.input[1], transform=np.ones((1, fs.n_basis)) * max_basis) nengo.Connection(product_k2.output, net.k2.input[1], transform=np.ones((1, fs.n_basis)) * max_basis) nengo.Connection(product_h1.output, net.h1.input[1], transform=np.ones((1, fs.n_basis)) * max_basis) nengo.Connection(product_h2.output, net.h2.input[1], transform=np.ones((1, fs.n_basis)) * max_basis) # -------------------- Output ------------------------------ net.output = nengo.Node(size_in=6, label='output') nengo.Connection(net.a1.output, net.output[0], synapse=0.01) nengo.Connection(net.a2.output, net.output[1], synapse=0.01) nengo.Connection(net.k1.output, net.output[2], synapse=0.01) nengo.Connection(net.k2.output, net.output[3], synapse=0.01) nengo.Connection(net.h1.output, net.output[4], synapse=0.01) nengo.Connection(net.h2.output, net.output[5], synapse=0.01) # add in the goal offsets nengo.Connection(net.assoc_mem_a.output[[-2, -1]], net.output[[0, 1]], synapse=None) nengo.Connection(net.assoc_mem_k.output[[-2, -1]], net.output[[2, 3]], synapse=None) nengo.Connection(net.assoc_mem_h.output[[-2, -1]], net.output[[4, 5]], synapse=None) # create a node to give a plot of the represented function ff_plot_a = fs.make_plot_node(domain=domain, lines=2, ylim=[-1000000, 1000000]) nengo.Connection(net.assoc_mem_a.output, ff_plot_a, synapse=0.1) ff_plot_k = fs.make_plot_node(domain=domain, lines=2, ylim=[-1000000, 1000000]) nengo.Connection(net.assoc_mem_k.output, ff_plot_k, synapse=0.1) ff_plot_h = fs.make_plot_node(domain=domain, lines=2, ylim=[-1000000, 1000000]) nengo.Connection(net.assoc_mem_h.output, ff_plot_h, synapse=0.1) return net
import nengo import nengo.spa as spa import numpy as np D = 256 # options: 64, 128, 256, 384, 512 D_space = 256 # options: 64, 128, 256, 384, 512 vocab_space = spa.Vocabulary(D_space) model = spa.SPA() with model: model.rule = spa.State(D_space, vocab=vocab_space) model.objs = spa.State(D_space, feedback=1, vocab=vocab_space) model.status = spa.State(32) speed = 0.3 separation = 0.3 strength = 0.4 model.subjx = spa.Compare(D_space) model.subjy = spa.Compare(D_space) model.objx = spa.Compare(D_space) model.objy = spa.Compare(D_space) for ens in model.all_ensembles: ens.neuron_type = nengo.Direct()
def create_model(seed=None, memory_item_first=None, probe_first=None, memory_item_second=None, probe_second=None, Ns=None, D=None, Nm=None, Nc=None, Nd=None, e_first=None, U_first=None, compressed_im_first=None, e_second=None, U_second=None, compressed_im_second=None, store_representations=None, store_decisions=None, store_spikes_and_resources=None): # global model #create vocabulary to show representations in gui # if nengo_gui_on: # vocab_angles = spa.Vocabulary(D) # for name in [0, 5, 10, 16, 24, 32, 40]: # #vocab_angles.add('D' + str(name), np.linalg.norm(compressed_im_first[name+90])) #take mean across phases # v = compressed_im_first[name+90] # nrm = np.linalg.norm(v) # if nrm > 0: # v /= nrm # vocab_angles.add('D' + str(name), v) #take mean across phases # v = np.dot(imagearr[-1,:]/50, U_first[:,:D]) # nrm = np.linalg.norm(v) # if nrm > 0: # v /= nrm # vocab_angles.add('Impulse', v) input_partial_first = partial(input_func_first, memory_item_first=memory_item_first, probe_first=probe_first) input_partial_second = partial(input_func_second, memory_item_second=memory_item_second, probe_second=probe_second) react_partial = partial(reactivate_func, Nm=Nm) #model = nengo.Network(seed=seed) model = spa.SPA(seed=seed) with model: #input nodes inputNode_first = nengo.Node(input_partial_first, label='input_first') #sensory ensemble sensory_first = nengo.Ensemble(Ns, D, encoders=e_first, intercepts=Uniform(0.01, .1), radius=1, label='sensory_first') nengo.Connection(inputNode_first, sensory_first, transform=U_first[:, :D].T) #memory ensemble memory_first = nengo.Ensemble(Nm, D, neuron_type=stpLIF(), intercepts=Uniform(0.01, .1), radius=1, label='memory_first') nengo.Connection(sensory_first, memory_first, transform=.1) #.1) #recurrent STSP connection nengo.Connection(memory_first, memory_first, transform=1, learning_rule_type=STP(), solver=nengo.solvers.LstsqL2(weights=True)) #comparison represents sin, cosine of theta of both sensory and memory ensemble comparison_first = nengo.Ensemble(Nc, dimensions=4, radius=math.sqrt(2), intercepts=Uniform(.01, 1), label='comparison_first') nengo.Connection(sensory_first, comparison_first[:2], eval_points=compressed_im_first[0:-1], function=sincos.T) nengo.Connection(memory_first, comparison_first[2:], eval_points=compressed_im_first[0:-1], function=sincos.T) #decision represents the difference in theta decoded from the sensory and memory ensembles decision_first = nengo.Ensemble(n_neurons=Nd, dimensions=1, radius=45, label='decision_first') nengo.Connection(comparison_first, decision_first, eval_points=ep, scale_eval_points=False, function=arctan_func) #same for secondary module inputNode_second = nengo.Node(input_partial_second, label='input_second') reactivate = nengo.Node(react_partial, label='reactivate') sensory_second = nengo.Ensemble(Ns, D, encoders=e_second, intercepts=Uniform(0.01, .1), radius=1, label='sensory_second') nengo.Connection(inputNode_second, sensory_second, transform=U_second[:, :D].T) memory_second = nengo.Ensemble(Nm, D, neuron_type=stpLIF(), intercepts=Uniform(0.01, .1), radius=1, label='memory_second') nengo.Connection(sensory_second, memory_second, transform=.1) nengo.Connection(reactivate, memory_second.neurons) #potential reactivation nengo.Connection(memory_second, memory_second, transform=1, learning_rule_type=STP(), solver=nengo.solvers.LstsqL2(weights=True)) comparison_second = nengo.Ensemble(Nd, dimensions=4, radius=math.sqrt(2), intercepts=Uniform(.01, 1), label='comparison_second') nengo.Connection(memory_second, comparison_second[2:], eval_points=compressed_im_second[0:-1], function=sincos.T) nengo.Connection(sensory_second, comparison_second[:2], eval_points=compressed_im_second[0:-1], function=sincos.T) decision_second = nengo.Ensemble(n_neurons=Nd, dimensions=1, radius=45, label='decision_second') nengo.Connection(comparison_second, decision_second, eval_points=ep, scale_eval_points=False, function=arctan_func) # #decode for gui # if nengo_gui_on: # model.sensory_decode = spa.State(D, vocab=vocab_angles, subdimensions=12, label='sensory_decode') # for ens in model.sensory_decode.all_ensembles: # ens.neuron_type = nengo.Direct() # nengo.Connection(sensory_first, model.sensory_decode.input,synapse=None) # model.memory_decode = spa.State(D, vocab=vocab_angles, subdimensions=12, label='memory_decode') # for ens in model.memory_decode.all_ensembles: # ens.neuron_type = nengo.Direct() # nengo.Connection(memory_first, model.memory_decode.input,synapse=None) #probes if not (nengo_gui_on): if store_representations: #sim 1 trials 1-100 model.p_mem_first = nengo.Probe(memory_first, synapse=0.01) model.p_mem_second = nengo.Probe(memory_second, synapse=0.01) model.p_sens_first = nengo.Probe(sensory_first, synapse=0.01) model.p_sens_second = nengo.Probe(sensory_second, synapse=0.01) if store_spikes_and_resources: #sim 1 trial 1 model.p_spikes_mem_first = nengo.Probe(memory_first.neurons, 'spikes') model.p_res_first = nengo.Probe(memory_first.neurons, 'resources') model.p_cal_first = nengo.Probe(memory_first.neurons, 'calcium') model.p_spikes_mem_second = nengo.Probe( memory_second.neurons, 'spikes') model.p_res_second = nengo.Probe(memory_second.neurons, 'resources') model.p_cal_second = nengo.Probe(memory_second.neurons, 'calcium') if store_decisions: #sim 2 model.p_dec_first = nengo.Probe(decision_first, synapse=0.01) model.p_dec_second = nengo.Probe(decision_second, synapse=0.01) return model