def test_learning_output_shape(self, mode, minibatch_size): ''' Tests for correct output from composition.learn Expected: All results from last epoch ''' xor_in = pnl.TransferMechanism(name='xor_in', default_variable=np.zeros(2)) xor_hid = pnl.TransferMechanism(name='xor_hid', default_variable=np.zeros(10), function=pnl.Logistic()) xor_out = pnl.TransferMechanism(name='xor_out', default_variable=np.zeros(1), function=pnl.Logistic()) hid_map = pnl.MappingProjection(matrix=np.random.rand(2, 10), sender=xor_in, receiver=xor_hid) out_map = pnl.MappingProjection(matrix=np.random.rand(10, 1)) xor = pnl.AutodiffComposition() xor.add_node(xor_in) xor.add_node(xor_hid) xor.add_node(xor_out) xor.add_projection(sender=xor_in, projection=hid_map, receiver=xor_hid) xor.add_projection(sender=xor_hid, projection=out_map, receiver=xor_out) xor_inputs = np.array( # the inputs we will provide to the model [[0, 0], [0, 1], [1, 0], [1, 1]]) xor_targets = np.array( # the outputs we wish to see from the model [[0], [1], [1], [0]]) results = xor.learn(inputs={ "inputs": { xor_in: xor_inputs }, "targets": { xor_out: xor_targets }, "epochs": 10 }, minibatch_size=minibatch_size, bin_execute=mode) assert len(results) == 4 // minibatch_size
def _generate_layers(self, bias=None, gain=1): if not bias: bias = -1 * self.bias self.task_layer = pnl.TransferMechanism(size=self.num_tasks, name='task_input') self.hidden_layer = pnl.TransferMechanism(size=self.hidden_layer_size, name='hidden', function=pnl.Logistic( bias=bias, gain=gain)) # self.hidden_bias = pnl.TransferMechanism(default_variable=np.ones((self.hidden_layer_size,)) * self.bias, # name='hidden bias') self.input_layers = self._generate_io_layers('input') self.output_layers = self._generate_io_layers( 'output', func=pnl.Logistic(bias=bias, gain=gain))
def _generate_indirect_layer(self): self.indirect_shape_layer = pnl.TransferMechanism(size=self.indirect_layer_size, name='shape_indirect', function=pnl.Logistic(bias=self.indirect_bias), integrator_mode=self.integrator_mode, integration_rate=self.integration_rate, noise=self._generate_noise_function())
def test_formats_for_control_specification_for_mechanism_and_function_params( self): control_spec_list = [ pnl.CONTROL, pnl.CONTROL_SIGNAL, pnl.CONTROL_PROJECTION, pnl.ControlSignal, pnl.ControlSignal(), pnl.ControlProjection, "CP_OBJECT", pnl.ControlMechanism, pnl.ControlMechanism(), (0.3, pnl.CONTROL), (0.3, pnl.CONTROL_SIGNAL), (0.3, pnl.CONTROL_PROJECTION), (0.3, pnl.ControlSignal), (0.3, pnl.ControlSignal()), (0.3, pnl.ControlProjection), (0.3, "CP_OBJECT"), (0.3, pnl.ControlMechanism), (0.3, pnl.ControlMechanism()) ] for i, ctl_tuple in enumerate( [j for j in zip(control_spec_list, reversed(control_spec_list))]): C1, C2 = ctl_tuple # This shenanigans is to avoid assigning the same instantiated ControlProjection more than once if C1 is 'CP_OBJECT': C1 = pnl.ControlProjection() elif isinstance(C1, tuple) and C1[1] is 'CP_OBJECT': C1 = (C1[0], pnl.ControlProjection()) if C2 is 'CP_OBJECT': C2 = pnl.ControlProjection() elif isinstance(C2, tuple) and C2[1] is 'CP_OBJECT': C2 = (C2[0], pnl.ControlProjection()) R = pnl.RecurrentTransferMechanism(noise=C1, function=pnl.Logistic(gain=C2)) assert R.parameter_states[pnl.NOISE].mod_afferents[0].name in \ 'ControlProjection for RecurrentTransferMechanism-{}[noise]'.format(i) assert R.parameter_states[pnl.GAIN].mod_afferents[0].name in \ 'ControlProjection for RecurrentTransferMechanism-{}[gain]'.format(i)
def composition_xor_animation(): in_to_hidden_matrix = np.random.rand(2, 10) hidden_to_out_matrix = np.random.rand(10, 1) inp = pnl.TransferMechanism(name="Input", default_variable=np.zeros(2)) hidden = pnl.TransferMechanism(name="Hidden", default_variable=np.zeros(10), function=pnl.Logistic()) output = pnl.TransferMechanism(name="Output", default_variable=np.zeros(1), function=pnl.Logistic()) in_to_hidden = pnl.MappingProjection( name="Input Weights", matrix=in_to_hidden_matrix.copy(), sender=inp, receiver=hidden, ) hidden_to_out = pnl.MappingProjection( name="Output Weights", matrix=hidden_to_out_matrix.copy(), sender=hidden, receiver=output, ) xor_comp = pnl.Composition() xor_comp.add_backpropagation_learning_pathway( [inp, in_to_hidden, hidden, hidden_to_out, output], learning_rate=10, ) xor_inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) xor_comp.learn( inputs={inp: xor_inputs}, animate={ pnl.SHOW_LEARNING: True, pnl.MOVIE_DIR: args.directory, pnl.MOVIE_NAME: "Composition_XOR_animation", }, )
def _generate_layers(self, bias=None, gain=1): if bias is None: bias = -1 * self.bias self.task_layer = pnl.TransferMechanism(size=self.num_tasks, name='task_input') self.hidden_layer = pnl.TransferMechanism(size=self.hidden_layer_size, name='hidden', function=pnl.Logistic( bias=bias, gain=gain)) self.hidden_bias = pnl.TransferMechanism(default_variable=np.ones( (self.hidden_layer_size, )) * self.bias, name='hidden bias') self.input_layer = pnl.TransferMechanism(size=self.num_dimensions * self.num_features, name='input') self.output_layer = pnl.TransferMechanism( size=self.num_dimensions * self.num_features, name='output', function=pnl.Logistic(bias=bias, gain=gain)) self.output_bias = pnl.TransferMechanism(default_variable=np.ones( (self.num_dimensions * self.num_features, )) * self.bias, name='output-bias')
def test_gating(benchmark, comp_mode): Input_Layer = pnl.TransferMechanism( name='Input_Layer', default_variable=np.zeros((2,)), function=pnl.Logistic() ) Output_Layer = pnl.TransferMechanism( name='Output_Layer', default_variable=[0, 0, 0], function=pnl.Linear(), output_ports={ pnl.NAME: 'RESULTS USING UDF', pnl.FUNCTION: pnl.Linear(slope=pnl.GATING) } ) Gating_Mechanism = pnl.GatingMechanism( size=[1], gating_signals=[Output_Layer.output_port] ) p_pathway = [Input_Layer, Output_Layer] stim_list = { Input_Layer: [[-1, 30], [-1, 30], [-1, 30], [-1, 30]], Gating_Mechanism: [[0.0], [0.5], [1.0], [2.0]] } comp = pnl.Composition(name="comp") comp.add_linear_processing_pathway(p_pathway) comp.add_node(Gating_Mechanism) comp.run(num_trials=4, inputs=stim_list, execution_mode=comp_mode) expected_results = [ [np.array([0., 0., 0.])], [np.array([0.63447071, 0.63447071, 0.63447071])], [np.array([1.26894142, 1.26894142, 1.26894142])], [np.array([2.53788284, 2.53788284, 2.53788284])] ] np.testing.assert_allclose(comp.results, expected_results) if benchmark.enabled: benchmark(comp.run, num_trials=4, inputs=stim_list, execution_mode=comp_mode)
def test_grid_search_random_selection(self): A = pnl.ProcessingMechanism(name='A') A.log.set_log_conditions(items="mod_slope") B = pnl.ProcessingMechanism(name='B', function=pnl.Logistic()) comp = pnl.Composition(name='comp') comp.add_linear_processing_pathway([A, B]) search_range = pnl.SampleSpec(start=15., stop=35., step=5) control_signal = pnl.ControlSignal( projections=[(pnl.SLOPE, A)], function=pnl.Linear, variable=1.0, allocation_samples=search_range, intensity_cost_function=pnl.Linear(slope=0.)) objective_mech = pnl.ObjectiveMechanism(monitor=[B]) ocm = pnl.OptimizationControlMechanism( agent_rep=comp, features=[A.input_state], objective_mechanism=objective_mech, function=pnl.GridSearch(select_randomly_from_optimal_values=True), control_signals=[control_signal]) comp.add_controller(ocm) inputs = {A: [[[1.0]]]} comp.run(inputs=inputs, num_trials=10, execution_id='outer_comp') log_arr = A.log.nparray_dictionary() # control signal value (mod slope) is chosen randomly from all of the control signal values # that correspond to a net outcome of 1 assert np.allclose([[1.], [15.], [15.], [20.], [20.], [15.], [20.], [25.], [15.], [35.]], log_arr['outer_comp']['mod_slope'])
cueInterval = pnl.TransferMechanism(default_variable=[[0.0]], size=1, function=pnl.Linear(slope=1, intercept=0), output_ports=[pnl.RESULT], name='Cue-Stimulus Interval') taskLayer = pnl.TransferMechanism(default_variable=[[0.0, 0.0]], size=2, function=pnl.Linear(slope=1, intercept=0), output_ports=[pnl.RESULT], name='Task Input [I1, I2]') activation = pnl.LCAMechanism(default_variable=[[0.0, 0.0]], size=2, function=pnl.Logistic(gain=1), leak=.5, competition=2, noise=0, time_step_size=.1, termination_measure=pnl.TimeScale.TRIAL, termination_threshold=3, name='Task Activations [Act 1, Act 2]') # response = pnl.ProcessingMechanism() # Create controller csiController = pnl.ControlMechanism(monitor_for_control=cueInterval, control_signals=[ (pnl.TERMINATION_THRESHOLD, activation)
def get_trained_network_multLCA(bipartite_graph, num_features=3, num_hidden=200, epochs=10, learning_rate=20, attach_LCA=True, competition=0.2, self_excitation=0.2, leak=0.4, threshold=1e-4, exec_limit=EXEC_LIMIT): # Get all tasks from bipartite graph (edges) and strip 'i/o' suffix all_tasks = get_all_tasks(bipartite_graph) # Analyze bipartite graph for network properties onodes = [ n for n, d in bipartite_graph.nodes(data=True) if d['bipartite'] == 0 ] inodes = [ n for n, d in bipartite_graph.nodes(data=True) if d['bipartite'] == 1 ] input_dims = len(inodes) output_dims = len(onodes) num_tasks = len(all_tasks) # Start building network as PsyNeuLink object # Layer parameters nh = num_hidden D_i = num_features * input_dims D_c = num_tasks D_h = nh D_o = num_features * output_dims # Weight matrices (defaults provided by Dillon) wih = np.random.rand(D_i, D_h) * 0.02 - 0.01 wch = np.random.rand(D_c, D_h) * 0.02 - 0.01 wco = np.random.rand(D_c, D_o) * 0.02 - 0.01 who = np.random.rand(D_h, D_o) * 0.02 - 0.01 # Training params (defaults provided by Dillon) patience = 10 min_delt = 0.00001 lr = learning_rate # Instantiate layers and projections il = pnl.TransferMechanism(size=D_i, name='input') cl = pnl.TransferMechanism(size=D_c, name='control') hl = pnl.TransferMechanism(size=D_h, name='hidden', function=pnl.Logistic(bias=-2)) ol = pnl.TransferMechanism(size=D_o, name='output', function=pnl.Logistic(bias=-2)) pih = pnl.MappingProjection(matrix=wih) pch = pnl.MappingProjection(matrix=wch) pco = pnl.MappingProjection(matrix=wco) pho = pnl.MappingProjection(matrix=who) # Create training data for network # We train across all possible inputs, one task at a time input_examples, output_examples, control_examples = generate_training_data(all_tasks, num_features, input_dims, output_dims) # Training parameter set input_set = { 'inputs': { il: input_examples.tolist(), cl: control_examples.tolist() }, 'targets': { ol: output_examples.tolist() }, 'epochs': 10 #epochs # LCA doesn't settle for 1000 epochs } # Build network mnet = pnl.AutodiffComposition(learning_rate=learning_rate, name='mnet') mnet.output_CIM.parameters.value._set_history_max_length(1000) mnet.add_node(il) mnet.add_node(cl) mnet.add_node(hl) mnet.add_node(ol) mnet.add_projection(projection=pih, sender=il, receiver=hl) mnet.add_projection(projection=pch, sender=cl, receiver=hl) mnet.add_projection(projection=pco, sender=cl, receiver=ol) mnet.add_projection(projection=pho, sender=hl, receiver=ol) # Train network print("training 2:", MNET_BIN_EXECUTE) t1 = time.time() mnet.learn( inputs=input_set, minibatch_size=input_set['epochs'], bin_execute=MNET_BIN_EXECUTE, patience=patience, min_delta=min_delt, ) t2 = time.time() print("training 2:", MNET_BIN_EXECUTE, t2-t1) for projection in mnet.projections: if hasattr(projection.parameters, 'matrix'): weights = projection.parameters.matrix.get(mnet) projection.parameters.matrix.set(weights, None) # Apply LCA transform (values from Sebastian's code -- supposedly taken from the original LCA paper from Marius & Jay) if attach_LCA: lci = pnl.LeakyCompetingIntegrator(rate=leak, time_step_size=0.01) lca_matrix = get_LCA_matrix(output_dims, num_features, self_excitation, competition) lca = pnl.RecurrentTransferMechanism(size=D_o, matrix=lca_matrix, integrator_mode=True, integrator_function=lci, name='lca', termination_threshold=threshold, reset_stateful_function_when=pnl.AtTrialStart()) # Wrapper composition used to pass values between mnet (AutodiffComposition) and lca (LCAMechanism) wrapper_composition = pnl.Composition() # Add mnet and lca to outer_composition wrapper_composition.add_linear_processing_pathway([mnet, lca]) # Dummy to save mnet results if str(LCA_BIN_EXECUTE).startswith("LLVM"): dummy = pnl.TransferMechanism(size=D_o, name="MNET_OUT") wrapper_composition.add_linear_processing_pathway([mnet, dummy]) # Set execution limit lca.parameters.max_executions_before_finished.set(exec_limit, wrapper_composition) # # Logging/Debugging # lca.set_log_conditions('value', pnl.LogCondition.EXECUTION) return wrapper_composition return mnet
def get_trained_network(bipartite_graph, num_features=3, num_hidden=200, epochs=10, learning_rate=20, attach_LCA=True, competition=0.2, self_excitation=0.2, leak=0.4, threshold=1e-4): # Get all tasks from bipartite graph (edges) and strip 'i/o' suffix all_tasks = get_all_tasks(bipartite_graph) # Analyze bipartite graph for network properties onodes = [ n for n, d in bipartite_graph.nodes(data=True) if d['bipartite'] == 0 ] inodes = [ n for n, d in bipartite_graph.nodes(data=True) if d['bipartite'] == 1 ] input_dims = len(inodes) output_dims = len(onodes) num_tasks = len(all_tasks) # Start building network as PsyNeuLink object # Layer parameters nh = num_hidden D_i = num_features * input_dims D_c = num_tasks D_h = nh D_o = num_features * output_dims # Weight matrices (defaults provided by Dillon) wih = np.random.rand(D_i, D_h) * 0.02 - 0.01 wch = np.random.rand(D_c, D_h) * 0.02 - 0.01 wco = np.random.rand(D_c, D_o) * 0.02 - 0.01 who = np.random.rand(D_h, D_o) * 0.02 - 0.01 # Training params (defaults provided by Dillon) patience = 10 min_delt = 0.00001 lr = learning_rate # Instantiate layers and projections il = pnl.TransferMechanism(size=D_i, name='input') cl = pnl.TransferMechanism(size=D_c, name='control') hl = pnl.TransferMechanism(size=D_h, name='hidden', function=pnl.Logistic(bias=-2)) ol = pnl.TransferMechanism(size=D_o, name='output', function=pnl.Logistic(bias=-2)) pih = pnl.MappingProjection(matrix=wih) pch = pnl.MappingProjection(matrix=wch) pco = pnl.MappingProjection(matrix=wco) pho = pnl.MappingProjection(matrix=who) # Create training data for network # We train across all possible inputs, one task at a time input_examples, output_examples, control_examples = generate_training_data(all_tasks, num_features, input_dims, output_dims) # Training parameter set input_set = { 'inputs': { il: input_examples.tolist(), cl: control_examples.tolist() }, 'targets': { ol: output_examples.tolist() }, 'epochs': epochs } mnet = pnl.AutodiffComposition(learning_rate=learning_rate, name='mnet') mnet.output_CIM.parameters.value._set_history_max_length(100000) mnet.add_node(il) mnet.add_node(cl) mnet.add_node(hl) mnet.add_node(ol) mnet.add_projection(projection=pih, sender=il, receiver=hl) mnet.add_projection(projection=pch, sender=cl, receiver=hl) mnet.add_projection(projection=pco, sender=cl, receiver=ol) mnet.add_projection(projection=pho, sender=hl, receiver=ol) # Train network print("training 1") t1 = time.time() mnet.learn( inputs=input_set, minibatch_size=1, bin_execute=MNET_BIN_EXECUTE, patience=patience, min_delta=min_delt, ) t2 = time.time() print("training 1:", MNET_BIN_EXECUTE, t2-t1) # Apply LCA transform (values from Sebastian's code -- supposedly taken from the original LCA paper from Marius & Jay) if attach_LCA: lca = pnl.LCAMechanism(size=D_o, leak=leak, competition=competition, self_excitation=self_excitation, time_step_size=0.01, threshold=threshold, threshold_criterion=pnl.CONVERGENCE, reset_stateful_function_when=pnl.AtTrialStart(), name='lca') # Wrapper composition used to pass values between mnet (AutodiffComposition) and lca (LCAMechanism) wrapper_composition = pnl.Composition() # Add mnet and lca to outer_composition wrapper_composition.add_linear_processing_pathway([mnet, lca]) return wrapper_composition return mnet
import functools import numpy as np import psyneulink as pnl Input_Layer = pnl.TransferMechanism(name='Input Layer', function=pnl.Logistic, default_variable=np.zeros((2, ))) Hidden_Layer_1 = pnl.TransferMechanism(name='Hidden Layer_1', function=pnl.Logistic(), default_variable=np.zeros((5, ))) Hidden_Layer_2 = pnl.TransferMechanism(name='Hidden Layer_2', function=pnl.Logistic(), default_variable=[0, 0, 0, 0]) Output_Layer = pnl.TransferMechanism(name='Output Layer', function=pnl.Logistic, default_variable=[0, 0, 0]) Input_Weights_matrix = (np.arange(2 * 5).reshape((2, 5)) + 1) / (2 * 5) Middle_Weights_matrix = (np.arange(5 * 4).reshape((5, 4)) + 1) / (5 * 4) Output_Weights_matrix = (np.arange(4 * 3).reshape((4, 3)) + 1) / (4 * 3) # This Projection will be used by the Process below by referencing it in the Process' pathway; # note: sender and receiver args don't need to be specified Input_Weights = pnl.MappingProjection(name='Input Weights', matrix=Input_Weights_matrix) # This Projection will be used by the Process below by assigning its sender and receiver args # to mechanisms in the pathway
# In[1]: import psyneulink as pnl import numpy as np # In[2]: # ECin = pnl.KWTA(size=8, function=pnl.Linear) # DG = pnl.KWTA(size=400, function=pnl.Linear) # CA3 = pnl.KWTA(size=80, function=pnl.Linear) # CA1 = pnl.KWTA(size=100, function=pnl.Linear) # ECout = pnl.KWTA(size=8, function=pnl.Linear) ECin = pnl.TransferMechanism(size=8, function=pnl.Linear(), name='ECin') DG = pnl.TransferMechanism(size=400, function=pnl.Logistic(), name='DG') CA3 = pnl.TransferMechanism(size=80, function=pnl.Logistic(), name='CA3') CA1 = pnl.TransferMechanism(size=100, function=pnl.Linear(), name='CA1') ECout = pnl.TransferMechanism(size=8, function=pnl.Logistic(), name='ECout') # In[3]: def make_mask(in_features, out_features, connectivity): mask = np.zeros((in_features, out_features)) rand = np.random.random(mask.shape) idxs = np.where(rand < connectivity) mask[idxs[0], idxs[1]] = 1 return mask
# Linear input units, colors: ('red', 'green'), words: ('RED','GREEN') colors_input_layer = pnl.TransferMechanism(size=3, function=pnl.Linear, name='COLORS_INPUT') words_input_layer = pnl.TransferMechanism(size=3, function=pnl.Linear, name='WORDS_INPUT') task_input_layer = pnl.TransferMechanism(size=2, function=pnl.Linear, name='TASK_INPUT') # Task layer, tasks: ('name the color', 'read the word') task_layer = pnl.RecurrentTransferMechanism(size=2, function=pnl.Logistic(), hetero=-2, integrator_mode=True, integration_rate=0.1, name='TASK') # Hidden layer units, colors: ('red','green') words: ('RED','GREEN') colors_hidden_layer = pnl.RecurrentTransferMechanism( size=3, function=pnl.Logistic(bias=4.0), integrator_mode=True, hetero=-2.0, # noise=pnl.NormalDist(mean=0.0, standard_dev=.0).function, integration_rate=0.1, # cohen-huston text says 0.01 name='COLORS HIDDEN')
# Linear input units, colors: ('red', 'green'), words: ('RED','GREEN') colors_input_layer = pnl.TransferMechanism(size=3, function=pnl.Linear, name='COLORS_INPUT') words_input_layer = pnl.TransferMechanism(size=3, function=pnl.Linear, name='WORDS_INPUT') task_input_layer = pnl.TransferMechanism(size=2, function=pnl.Linear, name='TASK_INPUT') # Task layer, tasks: ('name the color', 'read the word') task_layer = pnl.RecurrentTransferMechanism(size=2, function=pnl.Logistic(), hetero=-2, integrator_mode=True, integration_rate=0.1, name='TASK') # Hidden layer units, colors: ('red','green') words: ('RED','GREEN') colors_hidden_layer = pnl.RecurrentTransferMechanism( size=3, function=pnl.Logistic(x_0=4.0), integrator_mode=True, hetero=-2.0, # noise=pnl.NormalDist(mean=0.0, standard_deviation=.0), integration_rate=0.1, # cohen-huston text says 0.01 name='COLORS HIDDEN')
n_hidden = 5 n_output = 1 max_entries = 7 # training params num_epochs = 3 learning_rate = .1 wts_init_scale = .1 # layers input = pnl.TransferMechanism(name='input', default_variable=np.zeros(n_input)) hidden = pnl.TransferMechanism(name='hidden', default_variable=np.zeros(n_hidden), function=pnl.Logistic()) output = pnl.TransferMechanism(name='output', default_variable=np.zeros(n_output), function=pnl.Logistic()) # weights w_ih = pnl.MappingProjection(name='input_to_hidden', matrix=np.random.randn(n_input, n_hidden) * wts_init_scale, sender=input, receiver=hidden) w_ho = pnl.MappingProjection(name='hidden_to_output', matrix=np.random.randn(n_hidden, n_output) * wts_init_scale,
# Built python function that takes output of special logistic function and computes conflict by multiplying # output both task units with each over times 500 def my_conflict_function(variable): maxi = variable - 0.0180 new = np.fmax([0], maxi) out = [new[0] * new[1] * 500] return out # Create color feature layer, word feature layer, task demand layer and response layer color_feature_layer = pnl.RecurrentTransferMechanism( size=2, # Define unit size function=pnl.Logistic(gain=4, x_0=1), # to 4 & bias to 1 integrator_mode=True, # Set IntegratorFunction mode to True integration_rate=Lambda, # smoothing factor == integration rate hetero=inhibition, # Inhibition among units within a layer output_ports=[{ # Create new OutputPort by applying pnl.NAME: 'SPECIAL_LOGISTIC', # the "my_special_Logistic" function pnl.VARIABLE: (pnl.OWNER_VALUE, 0), pnl.FUNCTION: my_special_Logistic }], name='COLOR_LAYER') # The word_feature_layer is set up as the color_feature_layer word_feature_layer = pnl.RecurrentTransferMechanism( size=2, # Define unit size function=pnl.Logistic(gain=4, x_0=1), # to 4 & bias to 1 integrator_mode=True, # Set IntegratorFunction mode to True
def get_stroop_model(unit_noise_std=.01, dec_noise_std=.1): # model params # TODO bad practice, to be moved hidden_func = pnl.Logistic(gain=1.0, x_0=4.0) integration_rate = .2 leak = 0 competition = 1 # input layer, color and word inp_clr = pnl.TransferMechanism(size=N_UNITS, function=pnl.Linear, name='COLOR INPUT') inp_wrd = pnl.TransferMechanism(size=N_UNITS, function=pnl.Linear, name='WORD INPUT') # task layer, represent the task instruction; color naming / word reading inp_task = pnl.TransferMechanism(size=N_UNITS, function=pnl.Linear, name='TASK') # hidden layer for color and word hid_clr = pnl.TransferMechanism( size=N_UNITS, function=hidden_func, integrator_mode=True, integration_rate=integration_rate, noise=pnl.NormalDist(standard_deviation=unit_noise_std).function, name='COLORS HIDDEN') hid_wrd = pnl.TransferMechanism( size=N_UNITS, function=hidden_func, integrator_mode=True, integration_rate=integration_rate, noise=pnl.NormalDist(standard_deviation=unit_noise_std).function, name='WORDS HIDDEN') # output layer output = pnl.TransferMechanism( size=N_UNITS, function=pnl.Logistic, integrator_mode=True, integration_rate=integration_rate, noise=pnl.NormalDist(standard_deviation=unit_noise_std).function, name='OUTPUT') # decision layer, some accumulator decision = pnl.LCAMechanism( size=N_UNITS, leak=leak, competition=competition, # MAX_VS_NEXT=lca_mvn, noise=pnl.UniformToNormalDist( standard_deviation=dec_noise_std).function, name='DECISION') # PROJECTIONS, weights copied from cohen et al (1990) wts_clr_ih = pnl.MappingProjection(matrix=[[2.2, -2.2], [-2.2, 2.2]], name='COLOR INPUT TO HIDDEN') wts_wrd_ih = pnl.MappingProjection(matrix=[[2.6, -2.6], [-2.6, 2.6]], name='WORD INPUT TO HIDDEN') wts_clr_ho = pnl.MappingProjection(matrix=[[1.3, -1.3], [-1.3, 1.3]], name='COLOR HIDDEN TO OUTPUT') wts_wrd_ho = pnl.MappingProjection(matrix=[[2.5, -2.5], [-2.5, 2.5]], name='WORD HIDDEN TO OUTPUT') wts_tc = pnl.MappingProjection(matrix=[[4.0, 4.0], [0, 0]], name='COLOR NAMING') wts_tw = pnl.MappingProjection(matrix=[[0, 0], [4.0, 4.0]], name='WORD READING') # build the model model = pnl.Composition(name='STROOP model') model.add_node(inp_clr) model.add_node(inp_wrd) model.add_node(hid_clr) model.add_node(hid_wrd) model.add_node(inp_task) model.add_node(output) model.add_node(decision) model.add_linear_processing_pathway([inp_clr, wts_clr_ih, hid_clr]) model.add_linear_processing_pathway([inp_wrd, wts_wrd_ih, hid_wrd]) model.add_linear_processing_pathway([hid_clr, wts_clr_ho, output]) model.add_linear_processing_pathway([hid_wrd, wts_wrd_ho, output]) model.add_linear_processing_pathway([inp_task, wts_tc, hid_clr]) model.add_linear_processing_pathway([inp_task, wts_tw, hid_wrd]) model.add_linear_processing_pathway( [output, pnl.IDENTITY_MATRIX, decision]) # # LOGGING # hid_clr.set_log_conditions('value') # hid_wrd.set_log_conditions('value') # output.set_log_conditions('value') # collect the node handles nodes = [inp_clr, inp_wrd, inp_task, hid_clr, hid_wrd, output, decision] metadata = [integration_rate, dec_noise_std, unit_noise_std] return model, nodes, metadata
def test_default_lc_control_mechanism(self, benchmark, mode): G = 1.0 k = 0.5 starting_value_LC = 2.0 user_specified_gain = 1.0 A = pnl.TransferMechanism(function=pnl.Logistic(gain=user_specified_gain), name='A') B = pnl.TransferMechanism(function=pnl.Logistic(gain=user_specified_gain), name='B') # B.output_states[0].value *= 0.0 # Reset after init | Doesn't matter here b/c default var = zero, no intercept LC = pnl.LCControlMechanism( modulated_mechanisms=[A, B], base_level_gain=G, scaling_factor_gain=k, objective_mechanism=pnl.ObjectiveMechanism( function=pnl.Linear, monitored_output_states=[B], name='LC ObjectiveMechanism' ) ) for output_state in LC.output_states: output_state.value *= starting_value_LC P = pnl.Process(pathway=[A, B, LC]) S = pnl.System(processes=[P]) LC.reinitialize_when = pnl.Never() # THIS CURRENTLY DOES NOT WORK: # P = pnl.Process(pathway=[A, B]) # P2 = pnl.Process(pathway=[LC]) # S = pnl.System(processes=[P, P2]) # S.show_graph() gain_created_by_LC_output_state_1 = [] mod_gain_assigned_to_A = [] base_gain_assigned_to_A = [] mod_gain_assigned_to_B = [] base_gain_assigned_to_B = [] def report_trial(): gain_created_by_LC_output_state_1.append(LC.output_states[0].value[0]) mod_gain_assigned_to_A.append(A.mod_gain) mod_gain_assigned_to_B.append(B.mod_gain) base_gain_assigned_to_A.append(A.function_object.gain) base_gain_assigned_to_B.append(B.function_object.gain) benchmark(S.run, inputs={A: [[1.0], [1.0], [1.0], [1.0], [1.0]]}, call_after_trial=report_trial) # (1) First value of gain in mechanisms A and B must be whatever we hardcoded for LC starting value assert mod_gain_assigned_to_A[0] == starting_value_LC # (2) _gain should always be set to user-specified value for i in range(5): assert base_gain_assigned_to_A[i] == user_specified_gain assert base_gain_assigned_to_B[i] == user_specified_gain # (3) LC output on trial n becomes gain of A and B on trial n + 1 assert np.allclose(mod_gain_assigned_to_A[1:], gain_created_by_LC_output_state_1[0:-1]) # (4) mechanisms A and B should always have the same gain values (b/c they are identical) assert np.allclose(mod_gain_assigned_to_A, mod_gain_assigned_to_B)
def test_bustamante_Stroop_model(self): # INPUT UNITS # colors: ('red', 'green'), words: ('RED','GREEN') colors_input_layer = pnl.TransferMechanism( size=2, function=psyneulink.core.components.functions.transferfunctions. Linear, name='COLORS_INPUT') words_input_layer = pnl.TransferMechanism( size=2, function=psyneulink.core.components.functions.transferfunctions. Linear, name='WORDS_INPUT') # Task layer, tasks: ('name the color', 'read the word') task_layer = pnl.TransferMechanism(size=2, function=psyneulink.core.components. functions.transferfunctions.Linear, name='TASK') # HIDDEN LAYER UNITS # colors_hidden: ('red','green') # Logistic activation function, Gain = 1.0, Bias = -4.0 (in PNL bias is subtracted so enter +4.0 to get negative bias) # randomly distributed noise to the net input # time averaging = integration_rate = 0.1 unit_noise = 0.005 colors_hidden_layer = pnl.TransferMechanism( size=2, function=psyneulink.core.components.functions.transferfunctions. Logistic(gain=1.0, x_0=4.0), # should be able to get same result with offset = -4.0 integrator_mode=True, noise=psyneulink.core.components.functions.distributionfunctions. NormalDist(mean=0, standard_deviation=unit_noise).function, integration_rate=0.1, name='COLORS HIDDEN') # words_hidden: ('RED','GREEN') words_hidden_layer = pnl.TransferMechanism( size=2, function=pnl.Logistic(gain=1.0, x_0=4.0), integrator_mode=True, noise=pnl.NormalDist(mean=0, standard_deviation=unit_noise).function, integration_rate=0.1, name='WORDS HIDDEN') # OUTPUT UNITS # Response layer, provide input to accumulator, responses: ('red', 'green') # time averaging = tau = 0.1 # randomly distributed noise to the net input response_layer = pnl.TransferMechanism( size=2, function=psyneulink.core.components.functions.transferfunctions. Logistic, name='RESPONSE', integrator_mode=True, noise=psyneulink.core.components.functions.distributionfunctions. NormalDist(mean=0, standard_deviation=unit_noise).function, integration_rate=0.1) # Respond red accumulator # alpha = rate of evidence accumlation = 0.1 # sigma = noise = 0.1 # noise will be: squareroot(time_step_size * noise) * a random sample from a normal distribution accumulator_noise = 0.1 respond_red_accumulator = pnl.IntegratorMechanism( function=pnl.SimpleIntegrator(noise=pnl.NormalDist( mean=0, standard_deviation=accumulator_noise).function, rate=0.1), name='respond_red_accumulator') # Respond green accumulator respond_green_accumulator = pnl.IntegratorMechanism( function=pnl.SimpleIntegrator(noise=pnl.NormalDist( mean=0, standard_deviation=accumulator_noise).function, rate=0.1), name='respond_green_accumulator') # LOGGING colors_hidden_layer.set_log_conditions('value') words_hidden_layer.set_log_conditions('value') response_layer.set_log_conditions('value') respond_red_accumulator.set_log_conditions('value') respond_green_accumulator.set_log_conditions('value') # SET UP CONNECTIONS # rows correspond to sender # columns correspond to: weighting of the contribution that a given sender makes to the receiver # INPUT TO HIDDEN # row 0: input_'red' to hidden_'red', hidden_'green' # row 1: input_'green' to hidden_'red', hidden_'green' color_weights = pnl.MappingProjection(matrix=np.atleast_2d( [[2.2, -2.2], [-2.2, 2.2]]), name='COLOR_WEIGHTS') # row 0: input_'RED' to hidden_'RED', hidden_'GREEN' # row 1: input_'GREEN' to hidden_'RED', hidden_'GREEN' word_weights = pnl.MappingProjection(matrix=np.atleast_2d([[2.6, -2.6], [-2.6, 2.6]]), name='WORD_WEIGHTS') # HIDDEN TO RESPONSE # row 0: hidden_'red' to response_'red', response_'green' # row 1: hidden_'green' to response_'red', response_'green' color_response_weights = pnl.MappingProjection( matrix=np.atleast_2d([[1.3, -1.3], [-1.3, 1.3]]), name='COLOR_RESPONSE_WEIGHTS') # row 0: hidden_'RED' to response_'red', response_'green' # row 1: hidden_'GREEN' to response_'red', response_'green' word_response_weights = pnl.MappingProjection( matrix=np.atleast_2d([[2.5, -2.5], [-2.5, 2.5]]), name='WORD_RESPONSE_WEIGHTS') # TASK TO HIDDEN LAYER # row 0: task_CN to hidden_'red', hidden_'green' # row 1: task_WR to hidden_'red', hidden_'green' task_CN_weights = pnl.MappingProjection(matrix=np.atleast_2d( [[4.0, 4.0], [0, 0]]), name='TASK_CN_WEIGHTS') # row 0: task_CN to hidden_'RED', hidden_'GREEN' # row 1: task_WR to hidden_'RED', hidden_'GREEN' task_WR_weights = pnl.MappingProjection(matrix=np.atleast_2d( [[0, 0], [4.0, 4.0]]), name='TASK_WR_WEIGHTS') # RESPONSE UNITS TO ACCUMULATORS # row 0: response_'red' to respond_red_accumulator # row 1: response_'green' to respond_red_accumulator respond_red_differencing_weights = pnl.MappingProjection( matrix=np.atleast_2d([[1.0], [-1.0]]), name='RESPOND_RED_WEIGHTS') # row 0: response_'red' to respond_green_accumulator # row 1: response_'green' to respond_green_accumulator respond_green_differencing_weights = pnl.MappingProjection( matrix=np.atleast_2d([[-1.0], [1.0]]), name='RESPOND_GREEN_WEIGHTS') # CREATE COMPOSITION FROM PATHWAYS my_Stroop = pnl.Composition(pathways=[ { 'WORD_PATHWAY': [ words_input_layer, word_weights, words_hidden_layer, word_response_weights, response_layer ] }, { 'COLO_PATHWAY': [ colors_input_layer, color_weights, colors_hidden_layer, color_response_weights, response_layer ] }, { 'TASK_CN_PATHWAY': [task_layer, task_CN_weights, colors_hidden_layer] }, { 'TASK_WR_PATHWAY': [task_layer, task_WR_weights, words_hidden_layer] }, { 'RESPOND_RED_PATHWAY': [ response_layer, respond_red_differencing_weights, respond_red_accumulator ] }, { 'RESPOND_GREEN_PATHWAY': [ response_layer, respond_green_differencing_weights, respond_green_accumulator ] }, ]) # my_Stroop.show() # my_Stroop.show_graph(show_dimensions=pnl.ALL) # Function to create test trials # a RED word input is [1,0] to words_input_layer and GREEN word is [0,1] # a red color input is [1,0] to colors_input_layer and green color is [0,1] # a color-naming trial is [1,0] to task_layer and a word-reading trial is [0,1] def trial_dict(red_color, green_color, red_word, green_word, CN, WR): trialdict = { colors_input_layer: [red_color, green_color], words_input_layer: [red_word, green_word], task_layer: [CN, WR] } return trialdict # CREATE THRESHOLD FUNCTION # first value of DDM's value is DECISION_VARIABLE # context is always passed to Condition functions and is the context # in which the function gets called - below, during system execution def pass_threshold(mech1, mech2, thresh, context=None): results1 = mech1.output_ports[0].parameters.value.get(context) results2 = mech2.output_ports[0].parameters.value.get(context) for val in results1: if val >= thresh: return True for val in results2: if val >= thresh: return True return False accumulator_threshold = 1.0 mechanisms_to_update = [ colors_hidden_layer, words_hidden_layer, response_layer ] def switch_integrator_mode(mechanisms, mode): for mechanism in mechanisms: mechanism.integrator_mode = mode def switch_noise(mechanisms, noise): for mechanism in mechanisms: mechanism.noise.base = noise def switch_to_initialization_trial(mechanisms): # Turn off accumulation switch_integrator_mode(mechanisms, False) # Turn off noise switch_noise(mechanisms, 0) # Execute once per trial my_Stroop.termination_processing = { pnl.TimeScale.TRIAL: pnl.AllHaveRun() } def switch_to_processing_trial(mechanisms): # Turn on accumulation switch_integrator_mode(mechanisms, True) # Turn on noise switch_noise( mechanisms, psyneulink.core.components.functions.distributionfunctions. NormalDist(mean=0, standard_deviation=unit_noise).function) # Execute until one of the accumulators crosses the threshold my_Stroop.termination_processing = { pnl.TimeScale.TRIAL: pnl.While(pass_threshold, respond_red_accumulator, respond_green_accumulator, accumulator_threshold) } def switch_trial_type(): # Next trial will be a processing trial if isinstance( my_Stroop.termination_processing[pnl.TimeScale.TRIAL], pnl.AllHaveRun): switch_to_processing_trial(mechanisms_to_update) # Next trial will be an initialization trial else: switch_to_initialization_trial(mechanisms_to_update) CN_trial_initialize_input = trial_dict(0, 0, 0, 0, 1, 0) WR_trial_initialize_input = trial_dict(0, 0, 0, 0, 0, 1) # Start with an initialization trial switch_to_initialization_trial(mechanisms_to_update) my_Stroop.run( inputs=trial_dict(0, 1, 1, 0, 1, 0), # termination_processing=change_termination_processing, num_trials=4, call_after_trial=switch_trial_type)
import psyneulink as pnl comp = pnl.Composition(name="comp") A = pnl.TransferMechanism( name="A", function=pnl.Linear(intercept=2.0, slope=5.0, default_variable=[[0]]), termination_measure=pnl.Distance( metric=pnl.MAX_ABS_DIFF, default_variable=[[[0]], [[0]]] ), ) B = pnl.TransferMechanism( name="B", function=pnl.Logistic(default_variable=[[0]]), termination_measure=pnl.Distance( metric=pnl.MAX_ABS_DIFF, default_variable=[[[0]], [[0]]] ), ) C = pnl.RecurrentTransferMechanism( name="C", function=pnl.Linear(default_variable=[[0]]), initial_value=[[0]], output_ports=["RESULTS"], termination_measure=pnl.Distance( metric=pnl.MAX_ABS_DIFF, default_variable=[[[0]], [[0]]] ), ) D = pnl.IntegratorMechanism( name="D", function=pnl.SimpleIntegrator(initializer=[[0]], default_variable=[[0]]), )
import psyneulink as pnl Stroop_model = pnl.Composition(name="Stroop_model") color_input = pnl.ProcessingMechanism( name="color_input", function=pnl.Linear(default_variable=[[0.0, 0.0]]), default_variable=[[0.0, 0.0]], ) color_hidden = pnl.ProcessingMechanism( name="color_hidden", function=pnl.Logistic(bias=-4.0, default_variable=[[0.0, 0.0]]), default_variable=[[0.0, 0.0]], ) OUTPUT = pnl.ProcessingMechanism( name="OUTPUT", function=pnl.Logistic(default_variable=[[0.0, 0.0]]), default_variable=[[0.0, 0.0]], ) word_input = pnl.ProcessingMechanism( name="word_input", function=pnl.Linear(default_variable=[[0.0, 0.0]]), default_variable=[[0.0, 0.0]], ) word_hidden = pnl.ProcessingMechanism( name="word_hidden", function=pnl.Logistic(bias=-4.0, default_variable=[[0.0, 0.0]]), default_variable=[[0.0, 0.0]], ) task_input = pnl.ProcessingMechanism( name="task_input",
def get_stroop_model(unit_noise_std=.01, dec_noise_std=.1): # model params integration_rate = 1 hidden_func = pnl.Logistic(gain=1.0, x_0=4.0) # input layer, color and word reward = pnl.TransferMechanism(name='reward') punish = pnl.TransferMechanism(name='punish') inp_clr = pnl.TransferMechanism( size=N_UNITS, function=pnl.Linear, name='COLOR INPUT' ) inp_wrd = pnl.TransferMechanism( size=N_UNITS, function=pnl.Linear, name='WORD INPUT' ) # task layer, represent the task instruction; color naming / word reading inp_task = pnl.TransferMechanism( size=N_UNITS, function=pnl.Linear, name='TASK' ) # hidden layer for color and word hid_clr = pnl.TransferMechanism( size=N_UNITS, function=hidden_func, integrator_mode=True, integration_rate=integration_rate, # noise=pnl.NormalDist(standard_deviation=unit_noise_std).function, noise=pnl.NormalDist(standard_deviation=unit_noise_std), name='COLORS HIDDEN' ) hid_wrd = pnl.TransferMechanism( size=N_UNITS, function=hidden_func, integrator_mode=True, integration_rate=integration_rate, # noise=pnl.NormalDist(standard_deviation=unit_noise_std).function, noise=pnl.NormalDist(standard_deviation=unit_noise_std), name='WORDS HIDDEN' ) # output layer output = pnl.TransferMechanism( size=N_UNITS, function=pnl.Logistic, integrator_mode=True, integration_rate=integration_rate, # noise=pnl.NormalDist(standard_deviation=unit_noise_std).function, noise=pnl.NormalDist(standard_deviation=unit_noise_std), name='OUTPUT' ) # decision layer, some accumulator signalSearchRange = pnl.SampleSpec(start=0.05, stop=5, step=0.05) decision = pnl.DDM(name='Decision', input_format=pnl.ARRAY, function=pnl.DriftDiffusionAnalytical(drift_rate=1, threshold =1, noise=1, starting_point=0, t0=0.35), output_ports=[pnl.RESPONSE_TIME, pnl.PROBABILITY_UPPER_THRESHOLD, pnl.PROBABILITY_LOWER_THRESHOLD] ) driftrate_control_signal = pnl.ControlSignal(projections=[(pnl.SLOPE, inp_clr)], variable=1.0, intensity_cost_function=pnl.Exponential(rate=1),#pnl.Exponential(rate=0.8),#pnl.Exponential(rate=1), allocation_samples=signalSearchRange) threshold_control_signal = pnl.ControlSignal(projections=[(pnl.THRESHOLD, decision)], variable=1.0, intensity_cost_function=pnl.Linear(slope=0), allocation_samples=signalSearchRange) reward_rate = pnl.ObjectiveMechanism(function=pnl.LinearCombination(operation=pnl.PRODUCT, exponents=[[1],[1],[-1]]), monitor=[reward, decision.output_ports[pnl.PROBABILITY_UPPER_THRESHOLD], decision.output_ports[pnl.RESPONSE_TIME]]) punish_rate = pnl.ObjectiveMechanism(function=pnl.LinearCombination(operation=pnl.PRODUCT, exponents=[[1],[1],[-1]]), monitor=[punish, decision.output_ports[pnl.PROBABILITY_LOWER_THRESHOLD], decision.output_ports[pnl.RESPONSE_TIME]]) objective_mech = pnl.ObjectiveMechanism(function=pnl.LinearCombination(operation=pnl.SUM, weights=[[1],[-1]]), monitor=[reward_rate, punish_rate]) # objective_mech = pnl.ObjectiveMechanism(function=object_function, # monitor=[reward, # punish, # decision.output_ports[pnl.PROBABILITY_UPPER_THRESHOLD], # decision.output_ports[pnl.PROBABILITY_LOWER_THRESHOLD], # (decision.output_ports[pnl.RESPONSE_TIME])]) # PROJECTIONS, weights copied from cohen et al (1990) wts_clr_ih = pnl.MappingProjection( matrix=[[2.2, -2.2], [-2.2, 2.2]], name='COLOR INPUT TO HIDDEN') wts_wrd_ih = pnl.MappingProjection( matrix=[[2.6, -2.6], [-2.6, 2.6]], name='WORD INPUT TO HIDDEN') wts_clr_ho = pnl.MappingProjection( matrix=[[1.3, -1.3], [-1.3, 1.3]], name='COLOR HIDDEN TO OUTPUT') wts_wrd_ho = pnl.MappingProjection( matrix=[[2.5, -2.5], [-2.5, 2.5]], name='WORD HIDDEN TO OUTPUT') wts_tc = pnl.MappingProjection( matrix=[[4.0, 4.0], [0, 0]], name='COLOR NAMING') wts_tw = pnl.MappingProjection( matrix=[[0, 0], [4.0, 4.0]], name='WORD READING') # build the model model = pnl.Composition(name='STROOP model') model.add_node(decision, required_roles=pnl.NodeRole.OUTPUT) model.add_node(reward, required_roles=pnl.NodeRole.OUTPUT) model.add_node(punish, required_roles=pnl.NodeRole.OUTPUT) model.add_linear_processing_pathway([inp_clr, wts_clr_ih, hid_clr]) model.add_linear_processing_pathway([inp_wrd, wts_wrd_ih, hid_wrd]) model.add_linear_processing_pathway([hid_clr, wts_clr_ho, output]) model.add_linear_processing_pathway([hid_wrd, wts_wrd_ho, output]) model.add_linear_processing_pathway([inp_task, wts_tc, hid_clr]) model.add_linear_processing_pathway([inp_task, wts_tw, hid_wrd]) model.add_linear_processing_pathway([output, pnl.IDENTITY_MATRIX, decision]) # 3/15/20 # model.add_linear_processing_pathway([output, [[1,-1]], (decision, pnl.NodeRole.OUTPUT)]) # 3/15/20 # model.add_linear_processing_pathway([output, [[1],[-1]], decision]) # 3/15/20 model.add_nodes([reward_rate, punish_rate]) controller = pnl.OptimizationControlMechanism(agent_rep=model, features=[inp_clr.input_port, inp_wrd.input_port, inp_task.input_port, reward.input_port, punish.input_port], feature_function=pnl.AdaptiveIntegrator(rate=0.1), objective_mechanism=objective_mech, function=pnl.GridSearch(), control_signals=[driftrate_control_signal, threshold_control_signal]) model.add_controller(controller=controller) # collect the node handles nodes = [inp_clr, inp_wrd, inp_task, hid_clr, hid_wrd, output, decision, reward, punish,controller] metadata = [integration_rate, dec_noise_std, unit_noise_std] return model, nodes, metadata
# First, we create the 3 layers of the behavioral network, i.e. INPUT LAYER, DECISION LAYER, and RESPONSE LAYER. input_layer = pnl.TransferMechanism( size=3, # Number of units in input layer initial_value=[[0.0, 0.0, 0.0]], # Initial input values name='INPUT LAYER' # Define the name of the layer; this is optional, ) # but will help you to overview your model later on # Create Decision Layer --- [ Target 1, Target 2, Distractor ] decision_layer = pnl.LCAMechanism( size=3, # Number of units in input layer initial_value=[[0.0, 0.0, 0.0]], # Initial input values time_step_size=dt, # Integration step size leak=1.0, # Sets off diagonals to negative values self_excitation=selfdwt, # Set diagonals to self excitate competition=inhwt, # Set off diagonals to inhibit function=pnl.Logistic( x_0=decbias), # Set the Logistic function with bias = decbias # noise=pnl.UniformToNormalDist(standard_deviation = SD).function, # The UniformToNormalDist function will integrator_mode= True, # set the noise with a seed generator that is compatible with name='DECISION LAYER' # MATLAB random seed generator 22 (rsg=22) ) # decision_layer.set_log_conditions('RESULT') # Log RESULT of the decision layer decision_layer.set_log_conditions('value') # Log value of the decision layer for output_port in decision_layer.output_ports: output_port.value *= 0.0 # Set initial output values for decision layer to 0 # Create Response Layer --- [ Target1, Target2 ] response_layer = pnl.LCAMechanism( size=2, # Number of units in input layer
colors_input_layer = pnl.TransferMechanism(size=3, function=pnl.Linear, name='COLORS_INPUT') words_input_layer = pnl.TransferMechanism(size=3, function=pnl.Linear, name='WORDS_INPUT') task_input_layer = pnl.TransferMechanism(size=2, function=pnl.Linear, name='TASK_INPUT') # Task layer, tasks: ('name the color', 'read the word') task_layer = pnl.RecurrentTransferMechanism(size=2, function=pnl.Logistic(), hetero=-2, integrator_mode=True, integration_rate=0.01, name='TASK_LAYER') # Hidden layer # colors: ('red','green', 'neutral') words: ('RED','GREEN', 'NEUTRAL') colors_hidden_layer = pnl.RecurrentTransferMechanism(size=3, function=pnl.Logistic(x_0=4.0), # bias 4.0 is -4.0 in the paper see Docs for description integrator_mode=True, hetero=-2, integration_rate=0.01, # cohen-huston text says 0.01 name='COLORS_HIDDEN') words_hidden_layer = pnl.RecurrentTransferMechanism(size=3,
# Input Layer --- [ Target, Distractor ] input_layer = pnl.TransferMechanism(size=2, initial_value=np.array([[0.0, 0.0]]), name='INPUT LAYER') # Create Decision Layer --- [ Target, Distractor ] decision_layer = pnl.LCA( size=2, time_step_size=dt, leak=-1.0, self_excitation=w_XiXi, competition=w_XiXj, # Recurrent matrix: [ w_XiXi -w_XiXj ] # [ -w_XiXj w_XiXi ] function=pnl.Logistic(bias=b_decision), noise=pnl.NormalDist(standard_dev=SD).function, integrator_mode=True, name='DECISION LAYER') # Create Response Layer --- [ Target ] response_layer = pnl.LCA( size=1, time_step_size=dt, leak=-1.0, self_excitation=w_X3X3, # Recurrent matrix: [w_X3X3] # Competition param does not apply because there is only one unit function=pnl.Logistic(bias=b_response), noise=pnl.NormalDist(standard_dev=SD).function,
colors_input_layer = pnl.TransferMechanism(size=2, function=pnl.Linear, name='COLORS_INPUT') words_input_layer = pnl.TransferMechanism(size=2, function=pnl.Linear, name='WORDS_INPUT') # Hidden layer units, colors: ('red','green') words: ('RED','GREEN') # Logistic activation function, Gain = 1.0, Bias = -4.0 #should be randomly distributed noise to the net input of each unit (except input unit) #should have tau = smoothing_factor = 0.1 colors_hidden_layer = pnl.TransferMechanism( size=2, function=pnl.Logistic(gain=1.0, bias=4.0), # function=pnl.Logistic(gain=1.0, offset=-4.0), integrator_mode=False, noise=pnl.NormalDist(mean=0.0, standard_dev=.01).function, smoothing_factor=0.1, name='COLORS HIDDEN') #should be randomly distributed noise to the net input of each unit (except input unit) #should have tau words_hidden_layer = pnl.TransferMechanism( size=2, function=pnl.Logistic(gain=1.0, bias=4.0), # function=pnl.Logistic(gain=1.0, offset=-4.0), integrator_mode=False, noise=pnl.NormalDist(mean=0.0, standard_dev=.01).function, smoothing_factor=0.1, name='WORDS HIDDEN')
import psyneulink as pnl import numpy as np # CONSTRUCT THE MODEL *********************************** # Construct the color naming pathway: color_input = pnl.ProcessingMechanism( name="color_input", size=2 ) # Note: default function is Linear color_input_to_hidden_wts = np.array([[2, -2], [-2, 2]]) color_hidden = pnl.ProcessingMechanism( name="color_hidden", size=2, function=pnl.Logistic(bias=-4) ) color_hidden_to_output_wts = np.array([[2, -2], [-2, 2]]) output = pnl.ProcessingMechanism(name="OUTPUT", size=2, function=pnl.Logistic) color_pathway = [ color_input, color_input_to_hidden_wts, color_hidden, color_hidden_to_output_wts, output, ] # Construct the word reading pathway (using the same output_layer) word_input = pnl.ProcessingMechanism(name="word_input", size=2) word_input_to_hidden_wts = np.array([[3, -3], [-3, 3]]) word_hidden = pnl.ProcessingMechanism( name="word_hidden", size=2, function=pnl.Logistic(bias=-4) ) word_hidden_to_output_wts = np.array([[3, -3], [-3, 3]]) word_pathway = [
# Linear input units, colors: ('red', 'green'), words: ('RED','GREEN') colors_input_layer = pnl.TransferMechanism(size=3, function=pnl.Linear, name='COLORS_INPUT') words_input_layer = pnl.TransferMechanism(size=3, function=pnl.Linear, name='WORDS_INPUT') task_input_layer = pnl.TransferMechanism(size=2, function=pnl.Linear, name='TASK_INPUT') # Task layer, tasks: ('name the color', 'read the word') task_layer = pnl.RecurrentTransferMechanism(size=2, function=pnl.Logistic(), hetero=inhibition, integrator_mode=True, integration_rate=rate, name='TASK') # Hidden layer units, colors: ('red','green') words: ('RED','GREEN') colors_hidden_layer = pnl.RecurrentTransferMechanism( size=3, function=pnl.Logistic(x_0=bias), integrator_mode=True, hetero=inhibition, # noise=pnl.NormalDist(mean=0.0, standard_deviation=.0), integration_rate=rate, # cohen-huston text says 0.01 name='COLORS HIDDEN')
# Input Layer --- [ Target, Distractor ] input_layer = pnl.TransferMechanism(size=2, initial_value=np.array([[0.0, 0.0]]), name='INPUT LAYER') # Create Decision Layer --- [ Target, Distractor ] decision_layer = pnl.LCAMechanism( size=2, time_step_size=dt, leak=1.0, self_excitation=w_XiXi, competition=w_XiXj, # Recurrent matrix: [ w_XiXi -w_XiXj ] # [ -w_XiXj w_XiXi ] function=pnl.Logistic(x_0=b_decision), noise=pnl.NormalDist(standard_deviation=SD), integrator_mode=True, name='DECISION LAYER') # Create Response Layer --- [ Target ] response_layer = pnl.LCAMechanism( size=1, time_step_size=dt, leak=1.0, self_excitation=w_X3X3, # Recurrent matrix: [w_X3X3] # Competition param does not apply because there is only one unit function=pnl.Logistic(x_0=b_response), noise=pnl.NormalDist(standard_deviation=SD),