def test_linear_ABBCC(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') B = TransferMechanism(function=Linear(intercept=4.0), name='B') C = TransferMechanism(function=Linear(intercept=1.5), name='C') for m in [A, B, C]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), B) comp.add_projection(B, MappingProjection(), C) sched = Scheduler(composition=comp) sched.add_condition(A, Any(AtPass(0), EveryNCalls(C, 2))) sched.add_condition(B, Any(JustRan(A), JustRan(B))) sched.add_condition(C, Any(EveryNCalls(B, 2), JustRan(C))) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls( C, 4, time_scale=TimeScale.TRIAL) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A, B, B, C, C, A, B, B, C, C] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_no_termination_conds(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') B = TransferMechanism(function=Linear(intercept=4.0), name='B') C = TransferMechanism(function=Linear(intercept=1.5), name='C') for m in [A, B, C]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), B) comp.add_projection(B, MappingProjection(), C) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, EveryNCalls(A, 2)) sched.add_condition(C, EveryNCalls(B, 3)) output = list(sched.run()) expected_output = [ A, A, B, A, A, B, A, A, B, C, ] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output)
def test_6_two_trials(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') B = TransferMechanism(function=Linear(intercept=4.0), name='B') C = TransferMechanism(function=Linear(intercept=1.5), name='C') for m in [A, B, C]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), B) comp.add_projection(B, MappingProjection(), C) sched = Scheduler(composition=comp) sched.add_condition(A, BeforePass(5)) sched.add_condition(B, AfterNCalls(A, 5)) sched.add_condition(C, AfterNCalls(B, 1)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(2) termination_conds[TimeScale.TRIAL] = AfterNCalls(C, 3) comp.run(inputs={A: range(6)}, scheduler_processing=sched, termination_processing=termination_conds) output = sched.execution_list expected_output = [ A, A, A, A, A, B, C, B, C, B, C, A, A, A, A, A, B, C, B, C, B, C ] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output)
def test_triangle_4b(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') B = TransferMechanism(function=Linear(intercept=4.0), name='B') C = TransferMechanism(function=Linear(intercept=1.5), name='C') for m in [A, B, C]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), B) comp.add_projection(A, MappingProjection(), C) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, EveryNCalls(A, 2)) sched.add_condition(C, All(WhenFinished(A), AfterNCalls(B, 3))) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(C, 1) output = [] i = 0 for step in sched.run(termination_conds=termination_conds): if i == 10: A.is_finished = True output.append(step) i += 1 expected_output = [A, A, B, A, A, B, A, A, B, A, A, set([B, C])] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output)
def test_invtriangle_1(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') B = TransferMechanism(function=Linear(intercept=4.0), name='B') C = TransferMechanism(function=Linear(intercept=1.5), name='C') for m in [A, B, C]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), C) comp.add_projection(B, MappingProjection(), C) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, EveryNCalls(A, 2)) sched.add_condition(C, Any(AfterNCalls(A, 3), AfterNCalls(B, 3))) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls( C, 4, time_scale=TimeScale.TRIAL) output = list(sched.run(termination_conds=termination_conds)) expected_output = [ A, set([A, B]), A, C, set([A, B]), C, A, C, set([A, B]), C ] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output)
def test_checkmark2_1(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') B = TransferMechanism(function=Linear(intercept=4.0), name='B') C = TransferMechanism(function=Linear(intercept=1.5), name='C') D = TransferMechanism(function=Linear(intercept=.5), name='D') for m in [A, B, C, D]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), B) comp.add_projection(A, MappingProjection(), D) comp.add_projection(B, MappingProjection(), D) comp.add_projection(C, MappingProjection(), D) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, EveryNCalls(A, 2)) sched.add_condition(C, EveryNCalls(A, 2)) sched.add_condition(D, All(EveryNCalls(B, 2), EveryNCalls(C, 2))) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls( D, 1, time_scale=TimeScale.TRIAL) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A, set([A, C]), B, A, set([A, C]), B, D] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_6(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='scheduler-pytests-A') B = TransferMechanism(function=Linear(intercept=4.0), name='scheduler-pytests-B') C = TransferMechanism(function=Linear(intercept=1.5), name='scheduler-pytests-C') for m in [A, B, C]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), B) comp.add_projection(B, MappingProjection(), C) sched = Scheduler(composition=comp) sched.add_condition(A, BeforePass(5)) sched.add_condition(B, AfterNCalls(A, 5)) sched.add_condition(C, AfterNCalls(B, 1)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(C, 3) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A, A, A, A, A, B, C, B, C, B, C] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output)
def test_WhenFinishedAll_2(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') A.is_finished = False B = TransferMechanism(function=Linear(intercept=4.0), name='B') B.is_finished = True C = TransferMechanism(function=Linear(intercept=1.5), name='C') for m in [A, B, C]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), C) comp.add_projection(B, MappingProjection(), C) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, EveryNPasses(1)) sched.add_condition(C, WhenFinishedAll(A, B)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(A, 5) output = list(sched.run(termination_conds=termination_conds)) expected_output = [ set([A, B]), set([A, B]), set([A, B]), set([A, B]), set([A, B]), ] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_multisource_2(self): comp = Composition() A1 = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A1') A2 = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A2') B1 = TransferMechanism(function=Linear(intercept=4.0), name='B1') B2 = TransferMechanism(function=Linear(intercept=4.0), name='B2') B3 = TransferMechanism(function=Linear(intercept=4.0), name='B3') C1 = TransferMechanism(function=Linear(intercept=1.5), name='C1') C2 = TransferMechanism(function=Linear(intercept=.5), name='C2') for m in [A1, A2, B1, B2, B3, C1, C2]: comp.add_mechanism(m) comp.add_projection(A1, MappingProjection(), B1) comp.add_projection(A1, MappingProjection(), B2) comp.add_projection(A2, MappingProjection(), B1) comp.add_projection(A2, MappingProjection(), B2) comp.add_projection(A2, MappingProjection(), B3) comp.add_projection(B1, MappingProjection(), C1) comp.add_projection(B2, MappingProjection(), C1) comp.add_projection(B1, MappingProjection(), C2) comp.add_projection(B3, MappingProjection(), C2) sched = Scheduler(composition=comp) sched.add_condition_set({ A1: Always(), A2: Always(), B1: EveryNCalls(A1, 2), B3: EveryNCalls(A2, 2), B2: All(EveryNCalls(A1, 4), EveryNCalls(A2, 4)), C1: Any(AfterNCalls(B1, 2), AfterNCalls(B2, 2)), C2: Any(AfterNCalls(B2, 2), AfterNCalls(B3, 2)), }) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = All(AfterNCalls(C1, 1), AfterNCalls(C2, 1)) output = list(sched.run(termination_conds=termination_conds)) expected_output = [ set([A1, A2]), set([A1, A2]), set([B1, B3]), set([A1, A2]), set([A1, A2]), set([B1, B2, B3]), set([C1, C2]) ] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_leabra_prec_with_train(self): in_size = 4 out_size = 4 num_hidden = 1 num_trials = 4 train = True inputs = [[0, 1, .5, -.2]] * num_trials train_data = [[.2, .5, 1, -.5]] * num_trials precision = 0.000000001 # how far we accept error between PNL and Leabra output random_seed = 2 # because Leabra network initializes with small random weights random.seed(random_seed) L_spec = LeabraMechanism(input_size=in_size, output_size=out_size, hidden_layers=num_hidden, training_flag=train) random.seed(random_seed) leabra_net = build_leabra_network(in_size, out_size, num_hidden, None, train) leabra_net2 = copy.deepcopy(leabra_net) L_net = LeabraMechanism(leabra_net2) # leabra_net should be identical to the network inside L_net T1_spec = TransferMechanism(name='T1', size=in_size, function=Linear) T2_spec = TransferMechanism(name='T2', size=out_size, function=Linear) T1_net = TransferMechanism(name='T1', size=in_size, function=Linear) T2_net = TransferMechanism(name='T2', size=out_size, function=Linear) p1_spec = Process(pathway=[T1_spec, L_spec]) proj_spec = MappingProjection(sender=T2_spec, receiver=L_spec.input_states[1]) p2_spec = Process(pathway=[T2_spec, proj_spec, L_spec]) s_spec = System(processes=[p1_spec, p2_spec]) p1_net = Process(pathway=[T1_net, L_net]) proj_net = MappingProjection(sender=T2_net, receiver=L_net.input_states[1]) p2_net = Process(pathway=[T2_net, proj_net, L_net]) s_net = System(processes=[p1_net, p2_net]) for i in range(num_trials): out_spec = s_spec.run(inputs={T1_spec: inputs[i], T2_spec: train_data[i]}) pnl_output_spec = out_spec[-1][0] leabra_output = train_leabra_network(leabra_net, inputs[i], train_data[i]) diffs_spec = np.abs(np.array(pnl_output_spec) - np.array(leabra_output)) out_net = s_net.run(inputs={T1_net: inputs[i], T2_net: train_data[i]}) pnl_output_net = out_net[-1][0] diffs_net = np.abs(np.array(pnl_output_net) - np.array(leabra_output)) assert all(diffs_spec < precision) and all(diffs_net < precision) out_spec = s_spec.run(inputs={T1_spec: inputs, T2_spec: train_data}) pnl_output_spec = np.array(out_spec[-1][0]) for i in range(len(inputs)): leabra_output = np.array(train_leabra_network(leabra_net, inputs[i], train_data[i])) diffs_spec = np.abs(pnl_output_spec - leabra_output) out_net = s_net.run(inputs={T1_net: inputs, T2_net: train_data}) pnl_output_net = np.array(out_net[-1][0]) diffs_net = np.abs(pnl_output_net - leabra_output) assert all(diffs_spec < precision) and all(diffs_net < precision)
def test_projection_no_args_projection_spec_with_default(self): p = MappingProjection() T = TransferMechanism(default_variable=[0, 0], input_states=[p]) np.testing.assert_array_equal(T.instance_defaults.variable, np.array([[0, 0]])) assert len(T.input_states) == 1
def test_projection_with_sender_and_default(self): t = TransferMechanism(size=3) p = MappingProjection(sender=t) T = TransferMechanism(default_variable=[[0, 0]], input_states=[p]) np.testing.assert_array_equal(T.instance_defaults.variable, np.array([[0, 0]])) assert len(T.input_states) == 1
def test_9(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') B = TransferMechanism(function=Linear(intercept=4.0), name='B') for m in [A, B]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), B) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, WhenFinished(A)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(B, 2) output = [] i = 0 for step in sched.run(termination_conds=termination_conds): if i == 3: A.is_finished = True output.append(step) i += 1 expected_output = [A, A, A, A, B, A, B] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_termination_conditions_reset(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='scheduler-pytests-A') B = TransferMechanism(function=Linear(intercept=4.0), name='scheduler-pytests-B') for m in [A, B]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), B) sched = Scheduler(composition=comp) sched.add_condition(B, EveryNCalls(A, 2)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(B, 2) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A, A, B, A, A, B] assert output == pytest.helpers.setify_expected_output(expected_output) # reset the RUN because schedulers run TRIALs sched.clock._increment_time(TimeScale.RUN) sched._reset_counts_total(TimeScale.RUN) output = list(sched.run()) expected_output = [A, A, B] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_projection_with_matrix_and_sender(self): m = TransferMechanism(size=2) p = MappingProjection(sender=m, matrix=[[0, 0, 0], [0, 0, 0]]) T = TransferMechanism(input_states=[p]) np.testing.assert_array_equal(T.instance_defaults.variable, np.array([[0, 0, 0]])) assert len(T.input_states) == 1
def test_outputstate_(self): with pytest.raises(MechanismError) as error_text: p = MappingProjection() TransferMechanism(default_variable=[0, 0], input_states=[{ VARIABLE: [0, 0, 0], PROJECTIONS: [p] }]) assert mismatches_default_variable_error_text in str(error_text.value)
def test_projection_no_args_dict_spec(self): p = MappingProjection() T = TransferMechanism(input_states=[{ VARIABLE: [0, 0, 0], PROJECTIONS: [p] }]) np.testing.assert_array_equal(T.instance_defaults.variable, np.array([[0, 0, 0]])) assert len(T.input_states) == 1
def test_transfer_mech_process_matrix_change(self): from psyneulink.components.projections.pathway.mappingprojection import MappingProjection T1 = TransferMechanism(size=4, function=Linear) proj = MappingProjection( matrix=[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]) T2 = TransferMechanism(size=4, function=Linear) p = Process(size=4, pathway=[T1, proj, T2]) p.run(inputs={T1: [[1, 2, 3, 4]]}) proj.matrix = [[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]] assert np.allclose( proj.matrix, [[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]]) # p.run(inputs={T1: [[1, 2, 3, 4]]}) T1.execute([[1, 2, 3, 4]]) proj.execute(context="EXECUTING testing projection") assert np.allclose( proj.matrix, np.array([[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]]))
def test_projection_in_tuple(self): R2 = TransferMechanism(size=3) P = MappingProjection(sender=R2) T = TransferMechanism(size=2, input_states=[(R2, None, None, P)]) np.testing.assert_array_equal(T.instance_defaults.variable, np.array([[0, 0]])) assert len(T.input_states) == 1 assert len(T.input_state.path_afferents[0].sender.instance_defaults. variable) == 3 assert len(T.input_state.instance_defaults.variable) == 2 T.execute()
def test_AtPass_underconstrained(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') B = TransferMechanism(function=Linear(intercept=4.0), name='B') C = TransferMechanism(function=Linear(intercept=1.5), name='C') for m in [A, B, C]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), B) comp.add_projection(B, MappingProjection(), C) sched = Scheduler(composition=comp) sched.add_condition(A, AtPass(0)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(C, 2) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A, B, C, B, C] assert output == pytest.helpers.setify_expected_output( expected_output)
def test_WhenFinishedAll_noargs(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') B = TransferMechanism(function=Linear(intercept=4.0), name='B') C = TransferMechanism(function=Linear(intercept=1.5), name='C') for m in [A, B, C]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), C) comp.add_projection(B, MappingProjection(), C) sched = Scheduler(composition=comp) sched.add_condition(A, Always()) sched.add_condition(B, Always()) sched.add_condition(C, Always()) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = WhenFinishedAll() output = [] i = 0 for step in sched.run(termination_conds=termination_conds): if i == 3: A.is_finished = True B.is_finished = True if i == 4: C.is_finished = True output.append(step) i += 1 expected_output = [ set([A, B]), C, set([A, B]), C, set([A, B]), ] assert output == pytest.helpers.setify_expected_output(expected_output)
def add_linear_processing_pathway(self, pathway): # First, verify that the pathway begins with a mechanism if isinstance(pathway[0], Mechanism): self.add_mechanism(pathway[0]) else: # 'MappingProjection has no attribute _name' error is thrown when pathway[0] is passed to the error msg raise CompositionError( "The first item in a linear processing pathway must be a Mechanism." ) # Then, add all of the remaining mechanisms in the pathway for c in range(1, len(pathway)): # if the current item is a mechanism, add it if isinstance(pathway[c], Mechanism): self.add_mechanism(pathway[c]) # Then, loop through and validate that the mechanism-projection relationships make sense # and add MappingProjections where needed for c in range(1, len(pathway)): if isinstance(pathway[c], Mechanism): if isinstance(pathway[c - 1], Mechanism): # if the previous item was also a mechanism, add a mapping projection between them self.add_projection( pathway[c - 1], MappingProjection(sender=pathway[c - 1], receiver=pathway[c]), pathway[c]) # if the current item is a projection elif isinstance(pathway[c], Projection): if c == len(pathway) - 1: raise CompositionError( "{} is the last item in the pathway. A projection cannot be the last item in" " a linear processing pathway.".format(pathway[c])) # confirm that it is between two mechanisms, then add the projection if isinstance(pathway[c - 1], Mechanism) and isinstance( pathway[c + 1], Mechanism): self.add_projection(pathway[c - 1], pathway[c], pathway[c + 1]) else: raise CompositionError( "{} is not between two mechanisms. A Projection in a linear processing pathway must be preceded" " by a Mechanism and followed by a Mechanism".format( pathway[c])) else: raise CompositionError( "{} is not a Projection or Mechanism. A linear processing pathway must be made " "up of Projections and Mechanisms.".format(pathway[c]))
def _create_input_mechanisms(self): ''' builds a dictionary of { Mechanism : InputMechanism } pairs where each 'ORIGIN' Mechanism has a corresponding InputMechanism ''' is_origin = self.get_mechanisms_by_role(MechanismRole.ORIGIN) has_input_mechanism = self.input_mechanisms.keys() # consider all of the mechanisms that are only origins OR have input mechanisms for mech in is_origin.difference(has_input_mechanism): # If mech IS AN ORIGIN mechanism but it doesn't have an input mechanism, ADD input mechanism if mech not in has_input_mechanism: new_input_mech = CompositionInterfaceMechanism() self.input_mechanisms[mech] = new_input_mech MappingProjection(sender=new_input_mech, receiver=mech) # If mech HAS AN INPUT mechanism but isn't an origin, REMOVE the input mechanism else: del self.input_mechanisms[mech]
def test_9b(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') A.is_finished = False B = TransferMechanism(function=Linear(intercept=4.0), name='B') for m in [A, B]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), B) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, WhenFinished(A)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AtPass(5) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A, A, A, A, A] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_NWhen_AfterNCalls(self, n, expected_output): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') B = TransferMechanism(function=Linear(intercept=4.0), name='B') for m in [A, B]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), B) sched = Scheduler(composition=comp) sched.add_condition(A, Always()) sched.add_condition(B, NWhen(AfterNCalls(A, 3), n)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(A, 6) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A if x == 'A' else B for x in expected_output] assert output == pytest.helpers.setify_expected_output( expected_output)
def test_linear_AAB(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') B = TransferMechanism(function=Linear(intercept=4.0), name='B') for m in [A, B]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), B) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, EveryNCalls(A, 2)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNCalls( B, 2, time_scale=TimeScale.RUN) termination_conds[TimeScale.TRIAL] = AfterNCalls( B, 2, time_scale=TimeScale.TRIAL) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A, A, B, A, A, B] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_gating(): Input_Layer = TransferMechanism(name='Input Layer', function=Logistic, default_variable=np.zeros((2, ))) Hidden_Layer_1 = TransferMechanism(name='Hidden Layer_1', function=Logistic(), default_variable=np.zeros((5, ))) Hidden_Layer_2 = TransferMechanism(name='Hidden Layer_2', function=Logistic(), default_variable=[0, 0, 0, 0]) Output_Layer = TransferMechanism(name='Output Layer', function=Logistic, default_variable=[0, 0, 0]) Gating_Mechanism = GatingMechanism( # default_gating_policy=0.0, size=[1], gating_signals=[ Hidden_Layer_1, Hidden_Layer_2, Output_Layer, ]) 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) # TEST PROCESS.LEARNING WITH: # CREATION OF FREE STANDING PROJECTIONS THAT HAVE NO LEARNING (Input_Weights, Middle_Weights and Output_Weights) # INLINE CREATION OF PROJECTIONS (Input_Weights, Middle_Weights and Output_Weights) # NO EXPLICIT CREATION OF PROJECTIONS (Input_Weights, Middle_Weights and # Output_Weights) # 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 = 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 mechanismss in the pathway Middle_Weights = MappingProjection(name='Middle Weights', sender=Hidden_Layer_1, receiver=Hidden_Layer_2, matrix={ VALUE: Middle_Weights_matrix, FUNCTION: ConstantIntegrator, FUNCTION_PARAMS: { INITIALIZER: Middle_Weights_matrix, RATE: Middle_Weights_matrix }, }) Output_Weights = MappingProjection(name='Output Weights', sender=Hidden_Layer_2, receiver=Output_Layer, matrix=Output_Weights_matrix) z = Process( # default_variable=[0, 0], size=2, pathway=[ Input_Layer, # The following reference to Input_Weights is needed to use it in the pathway # since it's sender and receiver args are not specified in its # declaration above Input_Weights, Hidden_Layer_1, # No projection specification is needed here since the sender arg for Middle_Weights # is Hidden_Layer_1 and its receiver arg is Hidden_Layer_2 # Middle_Weights, Hidden_Layer_2, # Output_Weights does not need to be listed for the same reason as Middle_Weights # If Middle_Weights and/or Output_Weights is not declared above, then the process # will assign a default for missing projection # Output_Weights, Output_Layer ], clamp_input=SOFT_CLAMP, learning=LEARNING, learning_rate=1.0, target=[0, 0, 1], prefs={ VERBOSE_PREF: False, REPORT_OUTPUT_PREF: True }) g = Process(default_variable=[1.0], pathway=[Gating_Mechanism]) stim_list = {Input_Layer: [[-1, 30]], Gating_Mechanism: [1.0]} target_list = {Output_Layer: [[0, 0, 1]]} def print_header(): print("\n\n**** TRIAL: ", CentralClock.trial) def show_target(): i = s.input t = s.target_input_states[0].value print('\nOLD WEIGHTS: \n') print('- Input Weights: \n', Input_Weights.matrix) print('- Middle Weights: \n', Middle_Weights.matrix) print('- Output Weights: \n', Output_Weights.matrix) print('\nSTIMULI:\n\n- Input: {}\n- Target: {}\n'.format(i, t)) print('ACTIVITY FROM OLD WEIGHTS: \n') print('- Middle 1: \n', Hidden_Layer_1.value) print('- Middle 2: \n', Hidden_Layer_2.value) print('- Output:\n', Output_Layer.value) s = System(processes=[z, g], targets=[0, 0, 1], learning_rate=1.0) s.reportOutputPref = True # s.show_graph(show_learning=True) results = s.run( num_trials=10, inputs=stim_list, targets=target_list, call_before_trial=print_header, call_after_trial=show_target, )
def test_projection_with_matrix_and_sender_mismatches_default(self): with pytest.raises(MechanismError) as error_text: m = TransferMechanism(size=2) p = MappingProjection(sender=m, matrix=[[0, 0, 0], [0, 0, 0]]) TransferMechanism(default_variable=[0, 0], input_states=[p]) assert mismatches_default_variable_error_text in str(error_text.value)
def test_leabra_prec_half_train(self): in_size = 4 out_size = 4 num_hidden = 1 num_trials = 2 train = True inputs = [[0, 1, .5, -.2]] * num_trials train_data = [[.2, .5, 1, -.5]] * num_trials precision = 0.000000001 # how far we accept error between PNL and Leabra output random_seed = 3 # because Leabra network initializes with small random weights random.seed(random_seed) L_spec = LeabraMechanism(input_size=in_size, output_size=out_size, hidden_layers=num_hidden, training_flag=train) random.seed(random_seed) leabra_net = build_leabra_network(in_size, out_size, num_hidden, None, train) leabra_net2 = copy.deepcopy(leabra_net) L_net = LeabraMechanism(leabra_net2) # leabra_net should be identical to the network inside L_net T1_spec = TransferMechanism(name='T1', size=in_size, function=Linear) T2_spec = TransferMechanism(name='T2', size=out_size, function=Linear) T1_net = TransferMechanism(name='T1', size=in_size, function=Linear) T2_net = TransferMechanism(name='T2', size=out_size, function=Linear) p1_spec = Process(pathway=[T1_spec, L_spec]) proj_spec = MappingProjection(sender=T2_spec, receiver=L_spec.input_states[1]) p2_spec = Process(pathway=[T2_spec, proj_spec, L_spec]) s_spec = System(processes=[p1_spec, p2_spec]) p1_net = Process(pathway=[T1_net, L_net]) proj_net = MappingProjection(sender=T2_net, receiver=L_net.input_states[1]) p2_net = Process(pathway=[T2_net, proj_net, L_net]) s_net = System(processes=[p1_net, p2_net]) for i in range(num_trials): # training round out_spec = s_spec.run(inputs={T1_spec: inputs[i], T2_spec: train_data[i]}) pnl_output_spec = out_spec[-1][0] leabra_output = train_leabra_network(leabra_net, inputs[i], train_data[i]) diffs_spec = np.abs(np.array(pnl_output_spec) - np.array(leabra_output)) out_net = s_net.run(inputs={T1_net: inputs[i], T2_net: train_data[i]}) pnl_output_net = out_net[-1][0] diffs_net = np.abs(np.array(pnl_output_net) - np.array(leabra_output)) assert all(diffs_spec < precision) and all(diffs_net < precision) # assert np.sum(np.abs(pnl_output_spec - np.array(train_data[0]))) < 0.1 # assert np.sum(np.abs(pnl_output_net - np.array(train_data[0]))) < 0.1 # set all learning rules false for conn in leabra_net.connections: conn.spec.lrule = None L_net.training_flag = False L_spec.training_flag = False for i in range(num_trials): # non-training round out_spec = s_spec.run(inputs={T1_spec: inputs[i], T2_spec: train_data[i]}) pnl_output_spec = out_spec[-1][0] leabra_output = run_leabra_network(leabra_net, inputs[i]) diffs_spec = np.abs(np.array(pnl_output_spec) - np.array(leabra_output)) out_net = s_net.run(inputs={T1_net: inputs[i], T2_net: train_data[i]}) pnl_output_net = out_net[-1][0] diffs_net = np.abs(np.array(pnl_output_net) - np.array(leabra_output)) assert all(diffs_spec < precision) and all(diffs_net < precision) # class TestLeabraMechInSystem: # # def test_leabra_mech_learning(self): # T1 = TransferMechanism(size=5, function=Linear) # T2 = TransferMechanism(size=3, function=Linear) # L = LeabraMechanism(input_size=5, output_size=3, hidden_layers=2, hidden_sizes=[4, 4]) # train_data_proj = MappingProjection(sender=T2, receiver=L.input_states[1]) # out = TransferMechanism(size=3, function=Logistic(bias=2)) # p1 = Process(pathway=[T1, L, out], learning=LEARNING, learning_rate=1.0, target=[0, .1, .8]) # p2 = Process(pathway=[T2, train_data_proj, L, out]) # s = System(processes=[p1, p2]) # s.run(inputs = {T1: [1, 2, 3, 4, 5], T2: [0, .5, 1]})
def test_stroop_model_learning(self): process_prefs = { REPORT_OUTPUT_PREF: True, VERBOSE_PREF: False, } system_prefs = { REPORT_OUTPUT_PREF: True, VERBOSE_PREF: False, } colors = TransferMechanism( default_variable=[0, 0], function=Linear, name="Colors", ) words = TransferMechanism( default_variable=[0, 0], function=Linear, name="Words", ) hidden = TransferMechanism( default_variable=[0, 0], function=Logistic, name="Hidden", ) response = TransferMechanism( default_variable=[0, 0], function=Logistic(), name="Response", ) TransferMechanism( default_variable=[0, 0], function=Logistic, name="Output", ) CH_Weights_matrix = np.arange(4).reshape((2, 2)) WH_Weights_matrix = np.arange(4).reshape((2, 2)) HO_Weights_matrix = np.arange(4).reshape((2, 2)) CH_Weights = MappingProjection( name='Color-Hidden Weights', matrix=CH_Weights_matrix, ) WH_Weights = MappingProjection( name='Word-Hidden Weights', matrix=WH_Weights_matrix, ) HO_Weights = MappingProjection( name='Hidden-Output Weights', matrix=HO_Weights_matrix, ) color_naming_process = Process( default_variable=[1, 2.5], pathway=[colors, CH_Weights, hidden, HO_Weights, response], learning=LEARNING, target=[2, 2], name='Color Naming', prefs=process_prefs, ) word_reading_process = Process( default_variable=[.5, 3], pathway=[words, WH_Weights, hidden], name='Word Reading', learning=LEARNING, target=[3, 3], prefs=process_prefs, ) s = System( processes=[color_naming_process, word_reading_process], targets=[20, 20], name='Stroop Model', prefs=system_prefs, ) def show_target(): print('\nColor Naming\n\tInput: {}\n\tTarget: {}'.format( colors.input_states.values_as_lists, s.targets)) print('Wording Reading:\n\tInput: {}\n\tTarget: {}\n'.format( words.input_states.values_as_lists, s.targets)) print('Response: \n', response.output_values[0]) print('Hidden-Output:') print(HO_Weights.matrix) print('Color-Hidden:') print(CH_Weights.matrix) print('Word-Hidden:') print(WH_Weights.matrix) stim_list_dict = {colors: [[1, 1]], words: [[-2, -2]]} target_list_dict = {response: [[1, 1]]} results = s.run( num_trials=2, inputs=stim_list_dict, targets=target_list_dict, call_after_trial=show_target, ) results_list = [] for elem in s.results: for nested_elem in elem: nested_elem = nested_elem.tolist() try: iter(nested_elem) except TypeError: nested_elem = [nested_elem] results_list.extend(nested_elem) objective_response = s.mechanisms[3] objective_hidden = s.mechanisms[7] expected_output = [ (colors.output_states[0].value, np.array([1., 1.])), (words.output_states[0].value, np.array([-2., -2.])), (hidden.output_states[0].value, np.array([0.13227553, 0.01990677])), (response.output_states[0].value, np.array([0.51044657, 0.5483048])), (objective_response.output_states[0].value, np.array([0.48955343, 0.4516952])), (objective_response.output_states[MSE].value, np.array(0.22184555903789838)), (objective_hidden.output_states[0].value, np.array([0., 0.])), (CH_Weights.matrix, np.array([ [0.02512045, 1.02167245], [2.02512045, 3.02167245], ])), (WH_Weights.matrix, np.array([ [-0.05024091, 0.9566551], [1.94975909, 2.9566551], ])), (HO_Weights.matrix, np.array([ [0.03080958, 1.02830959], [2.00464242, 3.00426575], ])), (results, [[np.array([0.50899214, 0.54318254])], [np.array([0.51044657, 0.5483048])]]), ] for i in range(len(expected_output)): val, expected = expected_output[i] # setting absolute tolerance to be in accordance with reference_output precision # if you do not specify, assert_allcose will use a relative tolerance of 1e-07, # which WILL FAIL unless you gather higher precision values to use as reference np.testing.assert_allclose( val, expected, atol=1e-08, err_msg='Failed on expected_output[{0}]'.format(i))