def test_valid_only_one_node_provides_input_spec(self): A = ProcessingMechanism(name="A", default_variable=[[ 1.5 ]]) # default variable will be used as input to this INPUT node B = ProcessingMechanism(name="B") C = ProcessingMechanism(name="C") comp = Composition(name="COMP") comp.add_linear_processing_pathway([A, C]) comp.add_linear_processing_pathway([B, C]) inputs_to_B = [[1.0], [2.0], [3.0], [4.0]] # increment on every trial results_A = [] results_B = [] results_C = [] def call_after_trial(): results_A.append(A.parameters.value.get(comp)) results_B.append(B.parameters.value.get(comp)) results_C.append(C.parameters.value.get(comp)) comp.run(inputs={B: inputs_to_B}, call_after_trial=call_after_trial) assert np.allclose(results_A, [[[1.5]], [[1.5]], [[1.5]], [[1.5]]]) assert np.allclose(results_B, [[[1.0]], [[2.0]], [[3.0]], [[4.0]]]) assert np.allclose(results_C, [[[2.5]], [[3.5]], [[4.5]], [[5.5]]])
def test_invalid_matrix_specs(self): with pytest.raises(FunctionError) as error_text: PM_mismatched_float = ProcessingMechanism(function=LinearMatrix(default_variable=0.0, matrix=[[1.0, 0.0, 0.0, 0.0], [0.0, 2.0, 0.0, 0.0], [0.0, 0.0, 3.0, 0.0], [0.0, 0.0, 0.0, 4.0]]), default_variable=0.0) assert "Specification of matrix and/or default_variable" in str(error_text.value) and \ "not compatible for multiplication" in str(error_text.value) with pytest.raises(FunctionError) as error_text: PM_mismatched_matrix = ProcessingMechanism(function=LinearMatrix(default_variable=[[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], matrix=[[1.0, 0.0, 0.0, 0.0], [0.0, 2.0, 0.0, 0.0], [0.0, 0.0, 3.0, 0.0], [0.0, 0.0, 0.0, 4.0]]), default_variable=[[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]) assert "Specification of matrix and/or default_variable" in str(error_text.value) and \ "not compatible for multiplication" in str(error_text.value)
def test_equivalance_of_threshold_and_termination_specifications_just_threshold( self, comp_mode): # Note: This tests the equivalence of using LCAMechanism-specific threshold arguments and # generic TransferMechanism termination_<*> arguments lca_thresh = LCAMechanism( size=2, leak=0.5, threshold=0.7) # Note: , execute_to_threshold=True by default response = ProcessingMechanism(size=2) comp = Composition() comp.add_linear_processing_pathway([lca_thresh, response]) result1 = comp.run(inputs={lca_thresh: [1, 0]}, execution_mode=comp_mode) lca_termination = LCAMechanism(size=2, leak=0.5, termination_threshold=0.7, termination_measure=max, termination_comparison_op='>=') comp2 = Composition() response2 = ProcessingMechanism(size=2) comp2.add_linear_processing_pathway([lca_termination, response2]) result2 = comp2.run(inputs={lca_termination: [1, 0]}, execution_mode=comp_mode) assert np.allclose(result1, result2)
def test_processing_mechanism_multiple_input_ports(self): PM1 = ProcessingMechanism(size=[4, 4], function=LinearCombination, input_ports=['input_1', 'input_2']) PM2 = ProcessingMechanism(size=[2, 2, 2], function=LinearCombination, input_ports=['1', '2', '3']) PM1.execute([[1, 2, 3, 4], [5, 4, 2, 2]]) PM2.execute([[2, 0], [1, 3], [1, 0]]) assert np.allclose(PM1.value, [6, 6, 5, 6]) assert np.allclose(PM2.value, [4, 3])
def test_equivalance_of_threshold_and_when_finished_condition(self): # Note: This tests the equivalence of results when: # execute_until_finished is True for the LCAMechanism (by default) # and the call to execution loops until it reaches threshold (1st test) # vs. when execute_until_finished is False and a condition is added to the scheduler # that causes the LCAMechanism it to execute until it reaches threshold (2nd test). # loop Mechanism's call to execute lca_until_thresh = LCAMechanism( size=2, leak=0.5, threshold=0.7) # Note: , execute_to_threshold=True by default response = ProcessingMechanism(size=2) comp = Composition() comp.add_linear_processing_pathway([lca_until_thresh, response]) result1 = comp.run(inputs={lca_until_thresh: [1, 0]}) # loop Composition's call to Mechanism lca_single_step = LCAMechanism(size=2, leak=0.5, threshold=0.7, execute_until_finished=False) comp2 = Composition() response2 = ProcessingMechanism(size=2) comp2.add_linear_processing_pathway([lca_single_step, response2]) comp2.scheduler.add_condition(response2, WhenFinished(lca_single_step)) result2 = comp2.run(inputs={lca_single_step: [1, 0]}) assert np.allclose(result1, result2)
def test_valid_mismatched_input_lens(self): A = ProcessingMechanism(name="A") B = ProcessingMechanism(name="B") C = ProcessingMechanism(name="C") comp = Composition(name="COMP") comp.add_linear_processing_pathway([A, C]) comp.add_linear_processing_pathway([B, C]) inputs_to_A = [[1.0]] # same (1.0) on every trial inputs_to_B = [[1.0], [2.0], [3.0], [4.0]] # increment on every trial results_A = [] results_B = [] results_C = [] def call_after_trial(): results_A.append(A.parameters.value.get(comp)) results_B.append(B.parameters.value.get(comp)) results_C.append(C.parameters.value.get(comp)) comp.run(inputs={ A: inputs_to_A, B: inputs_to_B }, call_after_trial=call_after_trial) assert np.allclose(results_A, [[[1.0]], [[1.0]], [[1.0]], [[1.0]]]) assert np.allclose(results_B, [[[1.0]], [[2.0]], [[3.0]], [[4.0]]]) assert np.allclose(results_C, [[[2.0]], [[3.0]], [[4.0]], [[5.0]]])
def test_dict_of_subdicts(self): input_labels_dict_M1 = {"red": [1, 1], "green": [0, 0]} output_labels_dict_M2 = {0: {"red": [0, 0], "green": [1, 1]}} M1 = ProcessingMechanism( size=2, params={INPUT_LABELS_DICT: input_labels_dict_M1}) M2 = ProcessingMechanism( size=2, params={OUTPUT_LABELS_DICT: output_labels_dict_M2}) P = Process(pathway=[M1, M2], learning=ENABLED, learning_rate=0.25) S = System(processes=[P]) learned_matrix = [] count = [] def record_matrix_after_trial(): learned_matrix.append(M2.path_afferents[0].get_mod_matrix(S)) count.append(1) S.run(inputs=['red', 'green', 'green', 'red'], targets=['red', 'green', 'green', 'red'], call_after_trial=record_matrix_after_trial) assert np.allclose(S.results, [[[1, 1]], [[0., 0.]], [[0., 0.]], [[0.5, 0.5]]]) assert np.allclose(learned_matrix, [ np.array([[0.75, -0.25], [-0.25, 0.75]]), np.array([[0.75, -0.25], [-0.25, 0.75]]), np.array([[0.75, -0.25], [-0.25, 0.75]]), np.array([[0.625, -0.375], [-0.375, 0.625]]) ])
def test_processing_mechanism_linear_function(self): PM1 = ProcessingMechanism() PM1.execute(1.0) assert np.allclose(PM1.value, 1.0) PM2 = ProcessingMechanism(function=Linear(slope=2.0, intercept=1.0)) PM2.execute(1.0) assert np.allclose(PM2.value, 3.0)
def test_simplified_greedy_agent_random(benchmark, mode): # These should probably be replaced by reference to ForagerEnv constants: obs_len = 2 action_len = 2 player_coord_idx = slice(0, 2) predator_coord_idx = slice(3, 5) prey_coord_idx = slice(6, 8) player_value_idx = 2 predator_value_idx = 5 prey_value_idx = 8 player_len = prey_len = predator_len = obs_len player = ProcessingMechanism(size=prey_len, function=GaussianDistort, name="PLAYER OBS") prey = ProcessingMechanism(size=prey_len, function=GaussianDistort, name="PREY OBS") # Use ComparatorMechanism to compute direction of action as difference of coordinates between player and prey: # note: unitization is done in main loop, to allow compilation of LinearCombination function) (TBI) greedy_action_mech = ComparatorMechanism(name='MOTOR OUTPUT', sample=player, target=prey) agent_comp = Composition(name='PREDATOR-PREY COMPOSITION') agent_comp.add_node(player) agent_comp.add_node(prey) agent_comp.add_node(greedy_action_mech) # Projections to greedy_action_mech were created by assignments of sample and target args in its constructor, # so just add them to the Composition). for projection in greedy_action_mech.projections: agent_comp.add_projection(projection) run_results = agent_comp.run(inputs={ player: [[619, 177]], prey: [[419, 69]] }, bin_execute=mode) # KDM 12/4/19: modified results due to global seed offset of # GaussianDistort assignment. # to produce old numbers, run get_global_seed once before creating # each Mechanism with GaussianDistort above assert np.allclose(run_results, [[-199.5484223217141, -107.79361870517444]]) if benchmark.enabled: benchmark( agent_comp.run, **{ 'inputs': { player: [[619, 177]], prey: [[419, 69]], }, 'bin_execute': mode })
def test_non_origin_partial_input_spec(self): A = ProcessingMechanism(name='A', function=Linear(slope=2.0)) B = ProcessingMechanism(name='B', input_states=[[0.], A.input_state]) comp = Composition(name='comp') comp.add_linear_processing_pathway([A, B]) comp.run(inputs={A: [[1.23]]}) assert np.allclose(B.get_input_values(comp), [[2.46], [1.23]])
def test_simplified_greedy_agent_random(benchmark, mode): # These should probably be replaced by reference to ForagerEnv constants: obs_len = 2 action_len = 2 player_coord_idx = slice(0, 2) predator_coord_idx = slice(3, 5) prey_coord_idx = slice(6, 8) player_value_idx = 2 predator_value_idx = 5 prey_value_idx = 8 player_len = prey_len = predator_len = obs_len player = ProcessingMechanism(size=prey_len, function=GaussianDistort, name="PLAYER OBS") prey = ProcessingMechanism(size=prey_len, function=GaussianDistort, name="PREY OBS") # Use ComparatorMechanism to compute direction of action as difference of coordinates between player and prey: # note: unitization is done in main loop, to allow compilation of LinearCombination function) (TBI) greedy_action_mech = ComparatorMechanism(name='MOTOR OUTPUT', sample=player, target=prey) agent_comp = Composition(name='PREDATOR-PREY COMPOSITION') agent_comp.add_node(player) agent_comp.add_node(prey) agent_comp.add_node(greedy_action_mech) # Projections to greedy_action_mech were created by assignments of sample and target args in its constructor, # so just add them to the Composition). for projection in greedy_action_mech.projections: agent_comp.add_projection(projection) run_results = agent_comp.run(inputs={ player: [[619, 177]], prey: [[419, 69]] }, bin_execute=mode) assert np.allclose(run_results, [[-200.61352420749841, -109.9811418701135]]) benchmark( agent_comp.run, **{ 'inputs': { player: [[619, 177]], prey: [[419, 69]], }, 'bin_execute': mode })
def test_nested_composition_run_trials_inputs(benchmark, executions, mode): benchmark.group = "Nested Composition mutliple trials/inputs multirun {}".format( executions) # mechanisms A = ProcessingMechanism(name="A", function=AdaptiveIntegrator(rate=0.1)) B = ProcessingMechanism(name="B", function=Logistic) inner_comp = Composition(name="inner_comp") inner_comp.add_linear_processing_pathway([A, B]) inner_comp._analyze_graph() sched = Scheduler(composition=inner_comp) outer_comp = Composition(name="outer_comp") outer_comp.add_node(inner_comp) outer_comp._analyze_graph() sched = Scheduler(composition=outer_comp) # The input dict should assign inputs origin nodes (inner_comp in this case) var = {inner_comp: [[[2.0]], [[3.0]]]} expected = [[[0.549833997312478]], [[0.617747874769249]], [[0.6529428177055896]], [[0.7044959416252289]]] if executions > 1: var = [var for _ in range(executions)] if mode == 'Python': def f(v, num_trials, res=False): results = [] for i in range(executions): outer_comp.run(v[i], execution_id=i, num_trials=num_trials) if res: # copy the results immediately, otherwise it's empty results.append(outer_comp.results.copy()) return results res = f(var, 4, True) if executions > 1 else f([var], 4, True) benchmark(f if executions > 1 else outer_comp.run, var, num_trials=4) elif mode == 'LLVM': e = pnlvm.execution.CompExecution(outer_comp, [None for _ in range(executions)]) res = e.run(var, 4, 2) benchmark(e.run, var, 4, 2) elif mode == 'PTX': e = pnlvm.execution.CompExecution(outer_comp, [None for _ in range(executions)]) res = e.cuda_run(var, 4, 2) benchmark(e.cuda_run, var, 4, 2) assert np.allclose(res, [expected for _ in range(executions)]) assert len(res) == executions or executions == 1
def test_one_to_two(self): A = ProcessingMechanism(name='A') B = ProcessingMechanism(name='B') C = ProcessingMechanism(name='C', input_states=[A.input_state]) comp = Composition(name='comp') comp.add_linear_processing_pathway([A, B]) comp.add_node(C) comp.run(inputs={A: [[1.23]]}) assert np.allclose(A.parameters.value.get(comp), [[1.23]]) assert np.allclose(B.parameters.value.get(comp), [[1.23]]) assert np.allclose(C.parameters.value.get(comp), [[1.23]])
def test_origin_input_source_true_no_input(self): A = ProcessingMechanism(name='A') B = ProcessingMechanism(name='B') C = ProcessingMechanism(name='C', default_variable=[[4.56]]) comp = Composition(name='comp') comp.add_linear_processing_pathway([A, B]) comp.add_node(C) comp.run(inputs={A: [[1.23]]}) assert np.allclose(A.parameters.value.get(comp), [[1.23]]) assert np.allclose(B.parameters.value.get(comp), [[1.23]]) assert np.allclose(C.parameters.value.get(comp), [[4.56]])
def test_buffer_as_function_of_origin_mech_in_composition(self): P = ProcessingMechanism(function=Buffer( default_variable=[[0.0]], initializer=[[0.0]], history=3)) C = Composition(pathways=[P]) P.reset_stateful_function_when = Never() full_result = [] def assemble_full_result(): full_result.append(P.parameters.value.get(C)) C.run(inputs={P: [[1.0], [2.0], [3.0], [4.0], [5.0]]}, call_after_trial=assemble_full_result) # only returns index 0 item of the deque on each trial (OutputPort value) assert np.allclose(np.asfarray(C.results), [[[0.0]], [[0.0]], [[1.0]], [[2.0]], [[3.0]]]) # stores full mechanism value (full deque) on each trial expected_full_result = [ np.array([[0.], [1.]]), np.array([[0.], [1.], [2.]]), np.array([[1.], [2.], [3.]]), # Shape change np.array([[2.], [3.], [4.]]), np.array([[3.], [4.], [5.]]) ] for i in range(5): assert np.allclose(expected_full_result[i], np.asfarray(full_result[i]))
def test_dict_of_arrays(self): input_labels_dict = { "red": [1, 0, 0], "green": [0, 1, 0], "blue": [0, 0, 1] } M = ProcessingMechanism(default_variable=[[0, 0, 0]], params={INPUT_LABELS_DICT: input_labels_dict}) P = Process(pathway=[M]) S = System(processes=[P]) store_input_labels = [] def call_after_trial(): store_input_labels.append(M.get_input_labels(S)) S.run(inputs=['red', 'green', 'blue', 'red'], call_after_trial=call_after_trial) assert np.allclose( S.results, [[[1, 0, 0]], [[0, 1, 0]], [[0, 0, 1]], [[1, 0, 0]]]) assert store_input_labels == [['red'], ['green'], ['blue'], ['red']] S.run(inputs='red') assert np.allclose( S.results, [[[1, 0, 0]], [[0, 1, 0]], [[0, 0, 1]], [[1, 0, 0]], [[1, 0, 0]]]) S.run(inputs=['red']) assert np.allclose(S.results, [[[1, 0, 0]], [[0, 1, 0]], [[0, 0, 1]], [[1, 0, 0]], [[1, 0, 0]], [[1, 0, 0]]])
def test_buffer_as_function_of_origin_mech_in_system(self): P = ProcessingMechanism(function=Buffer(default_variable=[[0.0]], initializer=[[0.0]], history=3)) process = Process(pathway=[P]) system = System(processes=[process]) P.reinitialize_when = Never() full_result = [] def assemble_full_result(): full_result.append(P.parameters.value.get(system)) result = system.run(inputs={P: [[1.0], [2.0], [3.0], [4.0], [5.0]]}, call_after_trial=assemble_full_result) # only returns index 0 item of the deque on each trial (output state value) assert np.allclose(result, [[[0.0]], [[0.0]], [[1.0]], [[2.0]], [[3.0]]]) # stores full mechanism value (full deque) on each trial expected_full_result = [np.array([[0.], [1.]]), np.array([[0.], [1.], [2.]]), np.array([[1.], [2.], [3.]]), # Shape change np.array([[2.], [3.], [4.]]), np.array([[3.], [4.], [5.]])] for i in range(5): assert np.allclose(expected_full_result[i], full_result[i])
def test_dict_of_arrays(self): input_labels_dict = {"red": [1.0, 0.0], "green": [0.0, 1.0]} output_labels_dict = {"red": [1.0, 0.0], "green": [0.0, 1.0]} M = ProcessingMechanism(size=2, params={ INPUT_LABELS_DICT: input_labels_dict, OUTPUT_LABELS_DICT: output_labels_dict }) P = Process(pathway=[M]) S = System(processes=[P]) store_output_labels = [] def call_after_trial(): store_output_labels.append(M.get_output_labels(S)) S.run(inputs=['red', 'green', 'green', 'red'], call_after_trial=call_after_trial) assert np.allclose( S.results, [[[1.0, 0.0]], [[0.0, 1.0]], [[0.0, 1.0]], [[1.0, 0.0]]]) assert store_output_labels == [['red'], ['green'], ['green'], ['red']] store_output_labels = [] S.run(inputs=[[1.0, 0.0], 'green', [0.0, 1.0], 'red'], call_after_trial=call_after_trial) assert np.allclose( S.results, [[[1.0, 0.0]], [[0.0, 1.0]], [[0.0, 1.0]], [[1.0, 0.0]], [[1.0, 0.0]], [[0.0, 1.0]], [[0.0, 1.0]], [[1.0, 0.0]]]) assert store_output_labels == [['red'], ['green'], ['green'], ['red']]
def test_not_all_output_port_values_have_label(self): input_labels_dict = { "red": [1.0, 0.0], "green": [0.0, 1.0], "blue": [2.0, 2.0] } output_labels_dict = {"red": [1.0, 0.0], "green": [0.0, 1.0]} M = ProcessingMechanism(size=2, params={ INPUT_LABELS_DICT: input_labels_dict, OUTPUT_LABELS_DICT: output_labels_dict }) P = Process(pathway=[M]) S = System(processes=[P]) store_output_labels = [] def call_after_trial(): store_output_labels.append(M.get_output_labels(S)) S.run(inputs=['red', 'blue', 'green', 'blue'], call_after_trial=call_after_trial) assert np.allclose( S.results, [[[1.0, 0.0]], [[2.0, 2.0]], [[0.0, 1.0]], [[2.0, 2.0]]]) assert store_output_labels[0] == ['red'] assert np.allclose(store_output_labels[1], [[2.0, 2.0]]) assert store_output_labels[2] == ['green'] assert np.allclose(store_output_labels[3], [[2.0, 2.0]])
def test_output_ports(self, mech_mode, op, expected, benchmark): benchmark.group = "Output Port Op: {}".format(op) PM1 = ProcessingMechanism(default_variable=[0, 0, 0], output_ports=[op]) var = [1, 2, 4] if op in {MEAN, MEDIAN, STANDARD_DEVIATION, VARIANCE} else [1, 2, -4] ex = pytest.helpers.get_mech_execution(PM1, mech_mode) res = benchmark(ex, var) assert np.allclose(res, expected)
def test_invalid_mismatched_input_lens(self): A = ProcessingMechanism(name="A") B = ProcessingMechanism(name="B") C = ProcessingMechanism(name="C") comp = Composition(name="COMP") comp.add_linear_processing_pathway([A, C]) comp.add_linear_processing_pathway([B, C]) inputs_to_A = [[1.0], [2.0]] # 2 input specs inputs_to_B = [[1.0], [2.0], [3.0], [4.0]] # 4 input specs with pytest.raises(CompositionError) as error_text: comp.run(inputs={A: inputs_to_A, B: inputs_to_B}) assert "input dictionary for COMP contains input specifications of different lengths" in str( error_text.value)
def test_buffer_as_function_of_processing_mech(self): P = ProcessingMechanism(function=Buffer(default_variable=[[0.0]], initializer=[0.0], history=3)) val = P.execute(1.0) assert np.allclose(val, [[0., 1.]])
def test_accumulator_as_function_of_processing_mech(self): P = ProcessingMechanism(function=AccumulatorIntegrator( initializer=[0.0], rate=.02, increment=1)) P.execute(1.0) P.execute(1.0) val = P.execute(1.0) assert np.allclose(val, [[1.0204]])
def test_nested_composition_execution(benchmark, executions, mode): benchmark.group = "Nested Composition execution multirun {}".format( executions) # mechanisms A = ProcessingMechanism(name="A", function=AdaptiveIntegrator(rate=0.1)) B = ProcessingMechanism(name="B", function=Logistic) inner_comp = Composition(name="inner_comp") inner_comp.add_linear_processing_pathway([A, B]) inner_comp._analyze_graph() sched = Scheduler(composition=inner_comp) outer_comp = Composition(name="outer_comp") outer_comp.add_node(inner_comp) outer_comp._analyze_graph() sched = Scheduler(composition=outer_comp) # The input dict should assign inputs origin nodes (inner_comp in this case) var = {inner_comp: [[1.0]]} expected = [[0.52497918747894]] if executions > 1: var = [var for _ in range(executions)] if mode == 'Python': f = lambda x: [ outer_comp.execute(x[i], execution_id=i) for i in range(executions) ] res = f(var) if executions > 1 else outer_comp.execute(var) benchmark(f if executions > 1 else outer_comp.execute, var) elif mode == 'LLVM': e = pnlvm.execution.CompExecution(outer_comp, [None for _ in range(executions)]) e.execute(var) res = e.extract_node_output(outer_comp.output_CIM) benchmark(e.execute, var) elif mode == 'PTX': e = pnlvm.execution.CompExecution(outer_comp, [None for _ in range(executions)]) e.cuda_execute(var) res = e.extract_node_output(outer_comp.output_CIM) benchmark(e.cuda_execute, var) assert np.allclose(res, [expected for _ in range(executions)]) assert len(res) == executions
def test_buffer_as_function_of_processing_mech(self, benchmark): P = ProcessingMechanism(function=Buffer( default_variable=[[0.0]], initializer=[0.0], history=3)) val = P.execute(1.0) assert np.allclose(np.asfarray(val), [[0., 1.]]) if benchmark.enabled: benchmark(P.execute, 5.0)
def test_processing_mechanism_default_function(self, mode, variable, benchmark): PM = ProcessingMechanism(default_variable=[0, 0, 0, 0]) if mode == "Python": ex = PM.execute elif mode == "LLVM": ex = pnlvm.MechExecution(PM).execute elif mode == "PTX": ex = pnlvm.MechExecution(PM).cuda_execute res = benchmark(ex, variable) assert np.allclose(res, [[1., 2., 3., 4.]])
def test_valid_input_float(self): A = ProcessingMechanism(name="A") comp = Composition(name="comp") comp.add_node(A) comp.run(inputs={A: 5.0}) assert np.allclose(comp.results, [[5.0]]) comp.run(inputs={A: [5.0, 10.0, 15.0]}) assert np.allclose(comp.results, [[[5.0]], [[5.0]], [[10.0]], [[15.0]]])
def test_mix_and_match_input_sources(self): A = ProcessingMechanism(name='A') B = ProcessingMechanism(name='B', default_variable=[[0.], [0.]]) C = ProcessingMechanism( name='C', input_states=[B.input_states[1], A.input_state, B.input_states[0]]) input_dict = {A: [[2.0]], B: [[3.0], [1.0]]} comp = Composition(name="comp") comp.add_node(A) comp.add_node(B) comp.add_node(C) comp.run(inputs=input_dict) assert np.allclose(A.parameters.value.get(comp), [[2.]]) assert np.allclose(B.parameters.value.get(comp), [[3.], [1.]]) assert np.allclose(C.parameters.value.get(comp), [[1.], [2.], [3.]])
def test_equivalance_of_threshold_and_termination_specifications_max_vs_next(self): # Note: This tests the equivalence of using LCAMechanism-specific threshold arguments and # generic TransferMechanism termination_<*> arguments lca_thresh = LCAMechanism(size=3, leak=0.5, threshold=0.1, threshold_criterion=MAX_VS_NEXT) response = ProcessingMechanism(size=3) comp = Composition() comp.add_linear_processing_pathway([lca_thresh, response]) result1 = comp.run(inputs={lca_thresh:[1,0.5,0]}) lca_termination = LCAMechanism(size=3, leak=0.5, termination_threshold=0.1, termination_measure=max_vs_next, termination_comparison_op='>=') comp2 = Composition() response2 = ProcessingMechanism(size=3) comp2.add_linear_processing_pathway([lca_termination,response2]) result2 = comp2.run(inputs={lca_termination:[1,0.5,0]}) assert np.allclose(result1, result2)
def test_dict_of_floats(self): input_labels_dict_M1 = {"red": 1, "green": 0} output_labels_dict_M2 = {"red": 0, "green": 1} M1 = ProcessingMechanism(params={INPUT_LABELS_DICT: input_labels_dict_M1}) M2 = ProcessingMechanism(params={OUTPUT_LABELS_DICT: output_labels_dict_M2}) P = Process(pathway=[M1, M2], learning=ENABLED, learning_rate=0.25) S = System(processes=[P]) learned_matrix = [] def record_matrix_after_trial(): learned_matrix.append(M2.path_afferents[0].get_mod_matrix(S)) S.run(inputs=['red', 'green', 'green', 'red'], targets=['red', 'green', 'green', 'red'], call_after_trial=record_matrix_after_trial) assert np.allclose(S.results, [[[1.]], [[0.]], [[0.]], [[0.75]]]) assert np.allclose(learned_matrix, [[[0.75]], [[0.75]], [[0.75]], [[0.5625]]])