def run_twice_in_system(mech, input1, input2=None): if input2 is None: input2 = input1 simple_prefs = {REPORT_OUTPUT_PREF: False, VERBOSE_PREF: False} simple_process = Process(size=mech.size[0], pathway=[mech], name='simple_process') simple_system = System(processes=[simple_process], name='simple_system', prefs=simple_prefs) first_output = simple_system.run(inputs={mech: [input1]}) second_output = simple_system.run(inputs={mech: [input2]}) return second_output[1][0]
def test_kwta_threshold_float(self): K = KWTAMechanism(name='K', size=4, threshold=0.5) p = Process(pathway=[K], prefs=TestKWTARatio.simple_prefs) s = System(processes=[p], prefs=TestKWTARatio.simple_prefs) s.run(inputs={K: [1, 2, 3, 3]}) assert np.allclose( K.parameters.value.get(s), [[0.2689414213699951, 0.5, 0.7310585786300049, 0.7310585786300049] ])
def test_kwta_threshold_int(self): K = KWTAMechanism(name='K', size=4, threshold=-1) p = Process(pathway=[K], prefs=TestKWTAThreshold.simple_prefs) s = System(processes=[p], prefs=TestKWTAThreshold.simple_prefs) s.run(inputs={K: [1, 2, 3, 4]}) assert np.allclose(K.parameters.value.get(s), [[ 0.07585818002124355, 0.18242552380635635, 0.3775406687981454, 0.6224593312018546 ]])
def test_is_finished_stops_system(self): D = DDM(name='DDM', function=DriftDiffusionIntegrator(threshold=10.0)) P = Process(pathway=[D]) S = System(processes=[P], reinitialize_mechanisms_when=Never()) S.run(inputs={D: 2.0}, termination_processing={TimeScale.TRIAL: WhenFinished(D)}) # decision variable's value should match threshold assert D.parameters.value.get(S)[0] == 10.0 # it should have taken 5 executions (and time_step_size = 1.0) assert D.parameters.value.get(S)[1] == 5.0
def test_heterogeneous_variables(self): # from psyneulink.core.components.mechanisms.processing.objectivemechanism import ObjectiveMechanism a = TransferMechanism(name='a', default_variable=[[0.0], [0.0, 0.0]]) p1 = Process(pathway=[a]) s = System(processes=[p1]) inputs = {a: [[[1.1], [2.1, 2.1]], [[1.2], [2.2, 2.2]]]} s.run(inputs)
def test_kwta_k_value_empty_size_4(self): K = KWTAMechanism( name='K', size=4 ) assert K.k_value == 0.5 p = Process(pathway=[K], prefs=TestKWTARatio.simple_prefs) s = System(processes=[p], prefs=TestKWTARatio.simple_prefs) s.run(inputs={K: [1, 2, 3, 4]}) assert np.allclose(K.parameters.value.get(s), [[0.18242552380635635, 0.3775406687981454, 0.6224593312018546, 0.8175744761936437]])
def test_kwta_average_k_1(self): K = KWTAMechanism(name='K', size=4, k_value=1, threshold=0, function=Linear, average_based=True) p = Process(pathway=[K], prefs=TestKWTAAverageBased.simple_prefs) s = System(processes=[p], prefs=TestKWTAAverageBased.simple_prefs) kwta_input = {K: [[1, 2, 3, 4]]} s.run(inputs=kwta_input) assert np.allclose(K.parameters.value.get(s), [[-2, -1, 0, 1]])
def test_kwta_ratio_empty(self): K = KWTAMechanism( name='K', size=4 ) p = Process(pathway = [K], prefs = TestKWTARatio.simple_prefs) s = System(processes=[p], prefs = TestKWTARatio.simple_prefs) s.run(inputs = {K: [2, 4, 1, 6]}) assert np.allclose(K.parameters.value.get(s), [[0.2689414213699951, 0.7310585786300049, 0.11920292202211755, 0.9525741268224334]]) s.run(inputs = {K: [1, 2, 3, 4]}) assert np.allclose(K.parameters.value.get(s), [[0.09271329298112314, 0.7368459299092773, 0.2631540700907225, 0.9842837170829899]])
def test_dict_target_spec_length2(self): A = TransferMechanism(name="learning-process-mech-A") B = TransferMechanism(name="learning-process-mech-B", default_variable=[[0.0, 0.0]]) LP = Process(name="learning-process", pathway=[A, B], learning=ENABLED) S = System(name="learning-system", processes=[LP]) S.run(inputs={A: 1.0}, targets={B: [2.0, 3.0]}) S.run(inputs={A: 1.0}, targets={B: [[2.0, 3.0]]})
def test_ris_simple(self): R2 = RecurrentTransferMechanism(default_variable=[[0.0, 0.0, 0.0]], matrix=[[1.0, 2.0, 3.0], [2.0, 1.0, 2.0], [3.0, 2.0, 1.0]], has_recurrent_input_state=True) R2.execute(input=[1, 3, 2]) p2 = Process(pathway=[R2]) s2 = System(processes=[p2]) s2.run(inputs=[[1, 3, 2]]) np.testing.assert_allclose(R2.parameters.value.get(s2), [[14., 12., 13.]]) assert len(R2.input_states) == 2 assert "Recurrent Input State" not in R2.input_state.name # make sure recurrent input state isn't primary
def test_kwta_ratio_1(self): K = KWTAMechanism( name='K', size=4, ratio=1 ) p = Process(pathway = [K], prefs = TestKWTARatio.simple_prefs) s = System(processes=[p], prefs = TestKWTARatio.simple_prefs) s.run(inputs = {K: [2, 4, 1, 6]}) assert np.allclose(K.parameters.value.get(s), [[0.5, 0.8807970779778823, 0.2689414213699951, 0.9820137900379085]]) s.run(inputs = {K: [1, 2, 3, 4]}) assert np.allclose(K.parameters.value.get(s), [[0.30054433998850033, 0.8868817857039745, 0.5, 0.9897010588046231]])
def test_input_not_provided_to_run(self): T = TransferMechanism(name='T', default_variable=[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]) T2 = TransferMechanism(name='T2', function=Linear(slope=2.0), default_variable=[[0.0, 0.0]]) P = Process(pathway=[T, T2]) S = System(processes=[P]) run_result = S.run() assert np.allclose(T.parameters.value.get(S), [[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]) assert np.allclose(run_result, [[np.array([2.0, 4.0])]])
def test_kwta_ratio_0(self): K = KWTAMechanism( name='K', size=4, ratio=0 ) p = Process(pathway = [K], prefs = TestKWTARatio.simple_prefs) s = System(processes=[p], prefs = TestKWTARatio.simple_prefs) s.run(inputs = {K: [2, 4, 1, 6]}) assert np.allclose(K.parameters.value.get(s), [[0.11920292202211755, 0.5, 0.04742587317756678, 0.8807970779778823]]) s.run(inputs = {K: [1, 2, 3, 4]}) assert np.allclose(K.parameters.value.get(s), [[0.051956902301427035, 0.5, 0.22048012438199008, 0.9802370486903237]])
def test_kwta_ratio_0_3(self): K = KWTAMechanism( name='K', size=4, ratio=0.3 ) p = Process(pathway=[K], prefs=TestKWTARatio.simple_prefs) s = System(processes=[p], prefs=TestKWTARatio.simple_prefs) s.run(inputs={K: [2, 4, 1, 6]}) assert np.allclose(K.parameters.value.get(s), [[0.19781611144141834, 0.6456563062257956, 0.08317269649392241, 0.9308615796566533]]) s.run(inputs={K: [1, 2, 3, 4]}) assert np.allclose(K.parameters.value.get(s), [[0.06324086143390241, 0.6326786177649943, 0.21948113371757957, 0.9814716617176014]])
def test_LCAMechanism_length_1(self): T = TransferMechanism(function=Linear(slope=1.0)) L = LCAMechanism( function=Linear(slope=2.0), self_excitation=3.0, leak=0.5, competition= 1.0, # competition does not matter because we only have one unit time_step_size=0.1) P = Process(pathway=[T, L]) S = System(processes=[P]) L.reinitialize_when = Never() # - - - - - - - Equations to be executed - - - - - - - # new_transfer_input = # previous_transfer_input # + (leak * previous_transfer_input_1 + self_excitation * result1 + competition * result2 + outside_input1) * dt # + noise # result = new_transfer_input*2.0 # recurrent_matrix = [[3.0]] # - - - - - - - - - - - - - - - - - - - - - - - - - - results = [] def record_execution(): results.append(L.parameters.value.get(S)[0][0]) S.run(inputs={T: [1.0]}, num_trials=3, call_after_trial=record_execution) # - - - - - - - TRIAL 1 - - - - - - - # new_transfer_input = 0.0 + ( 0.5 * 0.0 + 3.0 * 0.0 + 0.0 + 1.0)*0.1 + 0.0 = 0.1 # f(new_transfer_input) = 0.1 * 2.0 = 0.2 # - - - - - - - TRIAL 2 - - - - - - - # new_transfer_input = 0.1 + ( 0.5 * 0.1 + 3.0 * 0.2 + 0.0 + 1.0)*0.1 + 0.0 = 0.265 # f(new_transfer_input) = 0.265 * 2.0 = 0.53 # - - - - - - - TRIAL 3 - - - - - - - # new_transfer_input = 0.265 + ( 0.5 * 0.265 + 3.0 * 0.53 + 0.0 + 1.0)*0.1 + 0.0 = 0.53725 # f(new_transfer_input) = 0.53725 * 2.0 = 1.0745 assert np.allclose(results, [0.2, 0.53, 1.0745])
def test_process(self): a = TransferMechanism(name="a", default_variable=[0, 0, 0]) b = TransferMechanism(name="b") p = Process(name="p", pathway=[a, b]) s = System(name="s", processes=[p]) a_label = s._get_label(a, ALL) b_label = s._get_label(b, ALL) assert "out (3)" in a_label and "in (3)" in a_label assert "out (1)" in b_label and "in (1)" in b_label
def test_recurrent_mech_auto_associative_projection(self): T = TransferMechanism(default_variable=[[0.0, 0.0, 0.0]]) recurrent_mech = RecurrentTransferMechanism(default_variable=[[0.0, 0.0, 0.0]], matrix=AutoAssociativeProjection) p = Process(pathway=[T, recurrent_mech]) s = System(processes=[p]) results = [] def record_trial(): results.append(recurrent_mech.parameters.value.get(s)) s.run(inputs=[[1.0, 1.0, 1.0], [2.0, 2.0, 2.0]], call_after_trial=record_trial)
def test_four_ABBCD(self): A = TransferMechanism( name='A', default_variable=[0], function=Linear(slope=2.0), ) B = IntegratorMechanism(name='B', default_variable=[0], function=SimpleIntegrator(rate=.5)) C = IntegratorMechanism(name='C', default_variable=[0], function=SimpleIntegrator(rate=.5)) D = TransferMechanism( name='D', default_variable=[0], function=Linear(slope=1.0), ) p = Process(default_variable=[0], pathway=[A, B, D], name='p') q = Process(default_variable=[0], pathway=[A, C, D], name='q') s = System(processes=[p, q], name='s') term_conds = {TimeScale.TRIAL: AfterNCalls(D, 1)} stim_list = {A: [[1]]} sched = Scheduler(system=s) sched.add_condition(B, EveryNCalls(A, 1)) sched.add_condition(C, EveryNCalls(A, 2)) sched.add_condition(D, Any(EveryNCalls(B, 3), EveryNCalls(C, 3))) s.scheduler_processing = sched s.run(inputs=stim_list, termination_processing=term_conds) terminal_mechs = [D] expected_output = [ [ numpy.array([4.]), ], ] for m in range(len(terminal_mechs)): for i in range(len(expected_output[m])): numpy.testing.assert_allclose( expected_output[m][i], terminal_mechs[m].get_output_values(s)[i])
def test_function_target_spec(self): A = TransferMechanism(name="multilayer-mech-A") B = TransferMechanism(name="multilayer-mech-B") C = TransferMechanism(name="multilayer-mech-C") P = Process(name="multilayer-process", pathway=[A, B, C], learning=ENABLED) S = System(name="learning-system", processes=[P]) def target_function(): val_1 = NormalDist(mean=3.0)() return val_1 S.run(inputs={A: 1.0}, targets={C: target_function})
def test_dict_target_spec(self): A = TransferMechanism(name="multilayer-mech-A") B = TransferMechanism(name="multilayer-mech-B") C = TransferMechanism(name="multilayer-mech-C") P = Process(name="multilayer-process", pathway=[A, B, C], learning=ENABLED) S = System(name="learning-system", processes=[P]) S.run(inputs={A: 1.0}, targets={C: 2.0}) S.run(inputs={A: 1.0}, targets={C: [2.0]}) S.run(inputs={A: 1.0}, targets={C: [[2.0]]})
def test_termination_conditions_reset(self): A = IntegratorMechanism(name='A', default_variable=[0], function=SimpleIntegrator(rate=.5)) B = TransferMechanism( name='B', default_variable=[0], function=Linear(slope=2.0), ) p = Process(default_variable=[0], pathway=[A, B], name='p') s = System(processes=[p], name='s', reinitialize_mechanisms_when=Never()) term_conds = {TimeScale.TRIAL: AfterNCalls(B, 2)} stim_list = {A: [[1]]} sched = Scheduler(system=s) sched.add_condition(B, EveryNCalls(A, 2)) s.scheduler_processing = sched s.run(inputs=stim_list, termination_processing=term_conds) # A should run four times terminal_mech = B expected_output = [ numpy.array([4.]), ] for i in range(len(expected_output)): numpy.testing.assert_allclose( expected_output[i], terminal_mech.get_output_values(s)[i]) s.run(inputs=stim_list, ) # A should run an additional two times terminal_mech = B expected_output = [ numpy.array([6.]), ] for i in range(len(expected_output)): numpy.testing.assert_allclose( expected_output[i], terminal_mech.get_output_values(s)[i])
def test_recurrent_mech_auto_auto_hetero(self): T = TransferMechanism(default_variable=[[0.0, 0.0, 0.0]]) recurrent_mech = RecurrentTransferMechanism(default_variable=[[0.0, 0.0, 0.0]], auto=3.0, hetero=-7.0) p = Process(pathway=[T, recurrent_mech]) s = System(processes=[p]) results = [] def record_trial(): results.append(recurrent_mech.parameters.value.get(s)) s.run(inputs=[[1.0, 1.0, 1.0], [2.0, 2.0, 2.0]], call_after_trial=record_trial)
def test_some_inputs_not_provided_to_run(self): Origin1 = TransferMechanism(name='Origin1', default_variable=[[1.0, 2.0]]) Origin2 = TransferMechanism(name='Origin2', default_variable=[[3.0, 4.0]]) Terminal = TransferMechanism(name='Terminal') P1 = Process(pathway=[Origin1, Terminal]) P2 = Process(pathway=[Origin2, Terminal]) S = System(processes=[P1, P2]) run_result = S.run(inputs={Origin1: [[5.0, 6.0]]}) # inputs={Origin1: [[5.0, 6.0], [7.0, 8.0]]}) # NOT currently allowed because inputs would be different lengths assert np.allclose(Origin1.parameters.value.get(S), [[5.0, 6.0]]) assert np.allclose(Origin2.parameters.value.get(S), [[3.0, 4.0]]) assert np.allclose(run_result, [[np.array([18.0])]])
def test_3_targets_4_inputs(self): A = TransferMechanism(name="learning-process-mech-A") B = TransferMechanism(name="learning-process-mech-B") LP = Process(name="learning-process", pathway=[A, B], learning=ENABLED) S = System( name="learning-system", processes=[LP], ) with pytest.raises(RunError) as error_text: S.run(inputs={A: [[[1.0]], [[2.0]], [[3.0]], [[4.0]]]}, targets={B: [[1.0], [2.0], [3.0]]}) assert 'Number of target values specified (3) for each learning sequence' in str(error_text.value) and \ 'must equal the number of input values specified (4)' in str(error_text.value)
def test_dict_target_spec(self): A = TransferMechanism(name="learning-process-mech-A") B = TransferMechanism(name="learning-process-mech-B") LP = Process(name="learning-process", pathway=[A, B], learning=ENABLED) S = System( name="learning-system", processes=[LP], ) # S.run(inputs={A: 1.0}, # targets={B: 2.0}) S.run(inputs={A: 1.0}, targets={B: [2.0]}) S.run(inputs={A: 1.0}, targets={B: [[2.0]]})
def test_reinitialize_run(self): R = RecurrentTransferMechanism(name="R", initial_value=0.5, integrator_mode=True, integration_rate=0.1, auto=1.0, noise=0.0) P = Process(name="P", pathway=[R]) S = System(name="S", processes=[P]) R.reinitialize_when = Never() assert np.allclose(R.integrator_function.previous_value, 0.5) S.run(inputs={R: 1.0}, num_trials=2, initialize=True, initial_values={R: 0.0}) # Trial 1 | variable = 1.0 + 0.0 # integration: 0.9*0.5 + 0.1*1.0 + 0.0 = 0.55 ---> previous value = 0.55 # linear fn: 0.55*1.0 = 0.55 # Trial 2 | variable = 1.0 + 0.55 # integration: 0.9*0.55 + 0.1*1.55 + 0.0 = 0.65 ---> previous value = 0.65 # linear fn: 0.65*1.0 = 0.65 assert np.allclose(R.integrator_function.parameters.previous_value.get(S), 0.65) R.integrator_function.reinitialize(0.9, execution_context=S) assert np.allclose(R.integrator_function.parameters.previous_value.get(S), 0.9) assert np.allclose(R.parameters.value.get(S), 0.65) R.reinitialize(0.5, execution_context=S) assert np.allclose(R.integrator_function.parameters.previous_value.get(S), 0.5) assert np.allclose(R.parameters.value.get(S), 0.5) S.run(inputs={R: 1.0}, num_trials=2) # Trial 3 # integration: 0.9*0.5 + 0.1*1.5 + 0.0 = 0.6 ---> previous value = 0.6 # linear fn: 0.6*1.0 = 0.6 # Trial 4 # integration: 0.9*0.6 + 0.1*1.6 + 0.0 = 0.7 ---> previous value = 0.7 # linear fn: 0.7*1.0 = 0.7 assert np.allclose(R.integrator_function.parameters.previous_value.get(S), 0.7)
def test_three_integrators(self): A = IntegratorMechanism(name='A', default_variable=[0], function=SimpleIntegrator(rate=1)) B = IntegratorMechanism(name='B', default_variable=[0], function=SimpleIntegrator(rate=1)) C = IntegratorMechanism(name='C', default_variable=[0], function=SimpleIntegrator(rate=1)) p = Process(default_variable=[0], pathway=[A, C], name='p') q = Process(default_variable=[0], pathway=[B, C], name='q') s = System(processes=[p, q], name='s') term_conds = {TimeScale.TRIAL: AfterNCalls(C, 2)} stim_list = {A: [[1]], B: [[1]]} sched = Scheduler(system=s) sched.add_condition(B, EveryNCalls(A, 2)) sched.add_condition(C, Any(EveryNCalls(A, 1), EveryNCalls(B, 1))) s.scheduler_processing = sched s.run(inputs=stim_list, termination_processing=term_conds) mechs = [A, B, C] expected_output = [ [ numpy.array([2.]), ], [ numpy.array([1.]), ], [ numpy.array([4.]), ], ] for m in range(len(mechs)): for i in range(len(expected_output[m])): numpy.testing.assert_allclose(expected_output[m][i], mechs[m].get_output_values(s)[i])
def test_function_target_spec(self): A = TransferMechanism(name="learning-process-mech-A") B = TransferMechanism(name="learning-process-mech-B", default_variable=np.array([[0.0, 0.0]])) LP = Process(name="learning-process", pathway=[A, B], learning=ENABLED) S = System(name="learning-system", processes=[LP]) def target_function(): val_1 = NormalDist(mean=3.0)() val_2 = NormalDist(mean=3.0)() target_value = np.array([val_1, val_2]) return target_value S.run(inputs={A: [[[1.0]], [[2.0]], [[3.0]]]}, targets={B: target_function})
def test_recurrent_mech_with_learning_warning(self): R = RecurrentTransferMechanism(size=2, function=Linear, matrix=np.full((2, 2), 0.1), enable_learning=True) P = Process(pathway=[R]) with pytest.warns(UserWarning) as record: S = System(processes=[P], prefs={VERBOSE_PREF: True}) # hack to find a specific warning (12 warnings are generated by the System construction) correct_message_found = False for warning in record: if "This is okay if the learning (e.g. Hebbian learning) does not need a target." in str(warning.message): correct_message_found = True break assert correct_message_found
def test_three_ABAC_convenience(self): A = IntegratorMechanism(name='A', default_variable=[0], function=SimpleIntegrator(rate=.5)) B = TransferMechanism( name='B', default_variable=[0], function=Linear(slope=2.0), ) C = TransferMechanism( name='C', default_variable=[0], function=Linear(slope=2.0), ) p = Process(default_variable=[0], pathway=[A, B], name='p') q = Process(default_variable=[0], pathway=[A, C], name='q') s = System(processes=[p, q], name='s') term_conds = {TimeScale.TRIAL: AfterNCalls(C, 1)} stim_list = {A: [[1]]} s.scheduler_processing.add_condition( B, Any(AtNCalls(A, 1), EveryNCalls(A, 2))) s.scheduler_processing.add_condition(C, EveryNCalls(A, 2)) s.run(inputs=stim_list, termination_processing=term_conds) terminal_mechs = [B, C] expected_output = [ [ numpy.array([1.]), ], [ numpy.array([2.]), ], ] for m in range(len(terminal_mechs)): for i in range(len(expected_output[m])): numpy.testing.assert_allclose( expected_output[m][i], terminal_mechs[m].get_output_values(s)[i])