Example #1
0
    def test_convergent(self):
        a = TransferMechanism(name='a', default_variable=[0, 0])
        b = TransferMechanism(name='b')
        c = TransferMechanism(name='c')
        c = TransferMechanism(name='c', default_variable=[0])
        d = TransferMechanism(name='d')
        e = TransferMechanism(name='e')

        p1 = Process(pathway=[a, b, e], name='p1')
        p2 = Process(pathway=[c, d, e], name='p2')

        s = System(
            processes=[p1, p2],
            name='Convergent System',
            initial_values={a: [1, 1]},
        )

        inputs = {a: [[2, 2]], c: [[0]]}
        s.run(inputs=inputs)

        assert set([a, c]) == set(s.origin_mechanisms.mechanisms)
        assert [e] == s.terminal_mechanisms.mechanisms

        assert a.systems[s] == ORIGIN
        assert b.systems[s] == INTERNAL
        assert c.systems[s] == ORIGIN
        assert d.systems[s] == INTERNAL
        assert e.systems[s] == TERMINAL
    def test_four_integrators_mixed(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))

        D = IntegratorMechanism(name='D',
                                default_variable=[0],
                                function=SimpleIntegrator(rate=1))

        p = Process(default_variable=[0], pathway=[A, C], name='p')

        p1 = Process(default_variable=[0], pathway=[A, D], name='p1')

        q = Process(default_variable=[0], pathway=[B, C], name='q')

        q1 = Process(default_variable=[0], pathway=[B, D], name='q1')

        s = System(processes=[p, p1, q, q1], name='s')

        term_conds = {
            TimeScale.TRIAL: All(AfterNCalls(C, 1), AfterNCalls(D, 1))
        }
        stim_list = {A: [[1]], B: [[1]]}

        sched = Scheduler(system=s)
        sched.add_condition(B, EveryNCalls(A, 2))
        sched.add_condition(C, EveryNCalls(A, 1))
        sched.add_condition(D, EveryNCalls(B, 1))
        s.scheduler_processing = sched

        s.run(inputs=stim_list, termination_processing=term_conds)

        mechs = [A, B, C, D]
        expected_output = [
            [
                numpy.array([2.]),
            ],
            [
                numpy.array([1.]),
            ],
            [
                numpy.array([4.]),
            ],
            [
                numpy.array([3.]),
            ],
        ]

        for m in range(len(mechs)):
            for i in range(len(expected_output[m])):
                numpy.testing.assert_allclose(expected_output[m][i],
                                              mechs[m].output_values[i])
Example #3
0
    def cyclic_extended_loop(self):
        a = TransferMechanism(name='a', default_variable=[0, 0])
        b = TransferMechanism(name='b')
        c = TransferMechanism(name='c')
        d = TransferMechanism(name='d')
        e = TransferMechanism(name='e', default_variable=[0])
        f = TransferMechanism(name='f')

        p1 = Process(pathway=[a, b, c, d], name='p1')
        p2 = Process(pathway=[e, c, f, b, d], name='p2')

        s = System(
            processes=[p1, p2],
            name='Cyclic System with Extended Loop',
            initial_values={a: [1, 1]},
        )

        inputs = {a: [2, 2], e: [0]}
        s.run(inputs=inputs)

        assert set([a, c]) == set(s.origin_mechanisms.mechanisms)
        assert [d] == s.terminal_mechanisms.mechanisms

        assert a.systems[s] == ORIGIN
        assert b.systems[s] == CYCLE
        assert c.systems[s] == INTERNAL
        assert d.systems[s] == TERMINAL
        assert e.systems[s] == ORIGIN
        assert f.systems[s] == INITIALIZE_CYCLE
Example #4
0
    def test_2_target_mechanisms_fn_spec(self):
        A = TransferMechanism(name="learning-process-mech-A")
        B = TransferMechanism(name="learning-process-mech-B")
        C = TransferMechanism(name="learning-process-mech-C")

        LP = Process(name="learning-process", pathway=[A, B], learning=ENABLED)
        LP2 = Process(name="learning-process2",
                      pathway=[A, C],
                      learning=ENABLED)

        S = System(
            name="learning-system",
            processes=[LP, LP2],
        )

        def target_function():
            val_1 = NormalDist(mean=3.0).function()
            val_2 = NormalDist(mean=3.0).function()
            return [val_1, val_2]

        with pytest.raises(RunError) as error_text:

            S.run(inputs={A: [[[1.0]]]}, targets=target_function)

        assert 'Target values for' in str(error_text.value) and \
               'must be specified in a dictionary' in str(error_text.value)
Example #5
0
    def test_bypass(self):
        a = TransferMechanism(name='a', default_variable=[0, 0])
        b = TransferMechanism(name='b', default_variable=[0, 0])
        c = TransferMechanism(name='c')
        d = TransferMechanism(name='d')

        p1 = Process(pathway=[a, b, c, d], name='p1')
        p2 = Process(pathway=[a, b, d], name='p2')

        s = System(
            processes=[p1, p2],
            name='Bypass System',
            initial_values={a: [1, 1]},
        )

        inputs = {a: [[2, 2], [0, 0]]}
        s.run(inputs=inputs)

        assert [a] == s.origin_mechanisms.mechanisms
        assert [d] == s.terminal_mechanisms.mechanisms

        assert a.systems[s] == ORIGIN
        assert b.systems[s] == INTERNAL
        assert c.systems[s] == INTERNAL
        assert d.systems[s] == TERMINAL
Example #6
0
    def test_dict_list_and_function(self):
        A = TransferMechanism(name="diverging-learning-pathways-mech-A")
        B = TransferMechanism(name="diverging-learning-pathways-mech-B")
        C = TransferMechanism(name="diverging-learning-pathways-mech-C")
        D = TransferMechanism(name="diverging-learning-pathways-mech-D")
        E = TransferMechanism(name="diverging-learning-pathways-mech-E")

        P1 = Process(name="learning-pathway-1",
                     pathway=[A, B, C],
                     learning=ENABLED)
        P2 = Process(name="learning-pathway-2",
                     pathway=[A, D, E],
                     learning=ENABLED)

        S = System(name="learning-system", processes=[P1, P2])

        def target_function():
            val_1 = NormalDist(mean=3.0).function()
            return val_1

        S.run(inputs={A: 1.0}, targets={C: 2.0, E: target_function})

        S.run(inputs={A: 1.0}, targets={C: [2.0], E: target_function})

        S.run(inputs={A: 1.0}, targets={C: [[2.0]], E: target_function})
    def test_five_ABABCDE(self):
        A = TransferMechanism(
            name='A',
            default_variable=[0],
            function=Linear(slope=2.0),
        )

        B = TransferMechanism(
            name='B',
            default_variable=[0],
            function=Linear(slope=2.0),
        )

        C = IntegratorMechanism(name='C',
                                default_variable=[0],
                                function=SimpleIntegrator(rate=.5))

        D = TransferMechanism(
            name='D',
            default_variable=[0],
            function=Linear(slope=1.0),
        )

        E = TransferMechanism(
            name='E',
            default_variable=[0],
            function=Linear(slope=2.0),
        )

        p = Process(default_variable=[0], pathway=[A, C, D], name='p')

        q = Process(default_variable=[0], pathway=[B, C, E], name='q')

        s = System(processes=[p, q], name='s')

        term_conds = {TimeScale.TRIAL: AfterNCalls(E, 1)}
        stim_list = {A: [[1]], B: [[2]]}

        sched = Scheduler(system=s)
        sched.add_condition(C, Any(EveryNCalls(A, 1), EveryNCalls(B, 1)))
        sched.add_condition(D, EveryNCalls(C, 1))
        sched.add_condition(E, EveryNCalls(C, 1))
        s.scheduler_processing = sched

        s.run(inputs=stim_list, termination_processing=term_conds)

        terminal_mechs = [D, E]
        expected_output = [
            [
                numpy.array([3.]),
            ],
            [
                numpy.array([6.]),
            ],
        ]

        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].output_values[i])
Example #8
0
    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_converging_pathways(self):
        a = TransferMechanism(name="a", default_variable=[0, 0, 0])
        b = TransferMechanism(name="b")
        c = TransferMechanism(name="c", default_variable=[0, 0, 0, 0, 0])
        p = Process(name="p", pathway=[a, c], learning=ENABLED)
        p2 = Process(name="p2", pathway=[b, c], learning=ENABLED)
        s = System(name="s", processes=[p, p2])

        a_label = s._get_label(a, ALL)
        b_label = s._get_label(b, ALL)
        c_label = s._get_label(c, 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
        assert "out (5)" in c_label and "in (5)" in c_label
Example #10
0
def test_DDM():
    myMechanism = DDM(
        function=BogaczEtAl(
            drift_rate=(1.0),
            threshold=(10.0),
            starting_point=0.0,
        ),
        name='My_DDM',
    )

    myMechanism_2 = DDM(function=BogaczEtAl(drift_rate=2.0, threshold=20.0),
                        name='My_DDM_2')

    myMechanism_3 = DDM(
        function=BogaczEtAl(drift_rate=3.0, threshold=30.0),
        name='My_DDM_3',
    )

    z = Process(
        default_variable=[[30], [10]],
        pathway=[
            myMechanism, (IDENTITY_MATRIX), myMechanism_2,
            (FULL_CONNECTIVITY_MATRIX), myMechanism_3
        ],
    )

    result = z.execute([[30], [10]])

    expected_output = [
        (myMechanism.input_states[0].value, np.array([40.])),
        (myMechanism.output_states[0].value, np.array([10.])),
        (myMechanism_2.input_states[0].value, np.array([10.])),
        (myMechanism_2.output_states[0].value, np.array([20.])),
        (myMechanism_3.input_states[0].value, np.array([20.])),
        (myMechanism_3.output_states[0].value, np.array([30.])),
        (result, np.array([30.])),
    ]

    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))
Example #11
0
    def test_kwta_average_k_1_ratio_0_8(self):
        K = KWTA(
            name='K',
            size=4,
            k_value=1,
            threshold=0,
            ratio=0.8,
            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.value, [[-1.4, -0.3999999999999999, 0.6000000000000001, 1.6]])

# class TestClip:
#     def test_clip_float(self):
#         K = KWTA(clip=[-2.0, 2.0],
#                  integrator_mode=False)
#         assert np.allclose(K.execute(3.0), 2.0)
#         assert np.allclose(K.execute(-3.0), -2.0)
#
#     def test_clip_array(self):
#         K = KWTA(default_variable=[[0.0, 0.0, 0.0]],
#                  clip=[-2.0, 2.0],
#                  integrator_mode=False)
#         assert np.allclose(K.execute([3.0, 0.0, -3.0]), [2.0, 0.0, -2.0])
#
#     def test_clip_2d_array(self):
#         K = KWTA(default_variable=[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]],
#                  clip=[-2.0, 2.0],
#                  integrator_mode=False)
#         assert np.allclose(K.execute([[-5.0, -1.0, 5.0], [5.0, -5.0, 1.0], [1.0, 5.0, 5.0]]),
#                            [[-2.0, -1.0, 2.0], [2.0, -2.0, 1.0], [1.0, 2.0, 2.0]])
Example #12
0
    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.value, [[5.0, 6.0]])
        assert np.allclose(Origin2.value, [[3.0, 4.0]])
        assert np.allclose(run_result, [[np.array([18.0])]])
    def test_two_ABB(self):
        A = TransferMechanism(
            name='A',
            default_variable=[0],
            function=Linear(slope=2.0),
        )

        B = IntegratorMechanism(name='B',
                                default_variable=[0],
                                function=SimpleIntegrator(rate=.5))

        p = Process(default_variable=[0], pathway=[A, B], name='p')

        s = System(processes=[p], name='s')

        term_conds = {TimeScale.TRIAL: AfterNCalls(B, 2)}
        stim_list = {A: [[1]]}

        sched = Scheduler(system=s)
        sched.add_condition(A, Any(AtPass(0), AfterNCalls(B, 2)))
        sched.add_condition(B, Any(JustRan(A), JustRan(B)))
        s.scheduler_processing = sched

        s.run(inputs=stim_list, termination_processing=term_conds)

        terminal_mech = B
        expected_output = [
            numpy.array([2.]),
        ]

        for i in range(len(expected_output)):
            numpy.testing.assert_allclose(expected_output[i],
                                          terminal_mech.output_values[i])
Example #14
0
    def test_system_run_with_combined_condition(self):

        # Construction
        T = TransferMechanism()
        P = Process(pathway=[T])
        S = System(processes=[P])

        # Runtime param used for noise
        # ONLY mechanism value should reflect runtime param -- attr should be changed back by the time we inspect it
        S.run(inputs={T: 2.0},
              runtime_params={
                  T: {
                      "noise": (10.0, Any(AtTrial(1), AfterTrial(2)))
                  }
              },
              num_trials=5)

        # Runtime param NOT used for noise
        S.run(inputs={T: 2.0})

        assert np.allclose(
            S.results,
            [
                [np.array([2.])],  # Trial 0 - NOT condition 0, NOT condition 1
                [np.array([12.])],  # Trial 1 - condition 0, NOT condition 1
                [np.array([2.])],  # Trial 2 - NOT condition 0, NOT condition 1
                [np.array([12.])],  # Trial 3 - NOT condition 0, condition 1
                [np.array([12.])],  # Trial 4 - NOT condition 0, condition 1
                [np.array([2.])]
            ])  # New run (runtime param no longer applies)
Example #15
0
    def test_3_input_states_2_label_dicts(self):
        input_labels_dict = {
            0: {
                "red": [1, 0],
                "green": [0, 1]
            },
            2: {
                "red": [0, 1],
                "green": [1, 0]
            }
        }

        M = TransferMechanism(default_variable=[[0, 0], [0, 0], [0, 0]],
                              params={INPUT_LABELS_DICT: input_labels_dict})
        P = Process(pathway=[M])
        S = System(processes=[P])

        S.run(inputs=[['red', [0, 0], 'green'], ['green', [1, 1], 'red'],
                      ['green', [2, 2], 'green']])
        assert np.allclose(S.results,
                           [[[1, 0], [0, 0], [1, 0]], [[0, 1], [1, 1], [0, 1]],
                            [[0, 1], [2, 2], [1, 0]]])

        S.run(inputs=[['red', [0, 0], [1, 0]], ['green', [1, 1], 'red'],
                      [[0, 1], [2, 2], 'green']])
        assert np.allclose(
            S.results, [[[1, 0], [0, 0], [1, 0]], [[0, 1], [1, 1], [0, 1]],
                        [[0, 1], [2, 2], [1, 0]], [[1, 0], [0, 0], [1, 0]],
                        [[0, 1], [1, 1], [0, 1]], [[0, 1], [2, 2], [1, 0]]])
Example #16
0
    def test_LCA_length_2(self):

        T = TransferMechanism(function=Linear(slope=1.0), size=2)
        L = LCA(function=Linear(slope=2.0),
                size=2,
                self_excitation=3.0,
                leak=0.5,
                competition=1.0,
                time_step_size=0.1)
        P = Process(pathway=[T, L])
        S = System(processes=[P])

        #  - - - - - - - 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.value[0])

        S.run(inputs={T: [1.0, 2.0]},
              num_trials=3,
              call_after_trial=record_execution)

        # - - - - - - - TRIAL 1 - - - - - - -

        # new_transfer_input_1 = 0.0 + ( 0.5 * 0.0 + 3.0 * 0.0 - 1.0*0.0 + 1.0)*0.1 + 0.0    =    0.1
        # f(new_transfer_input_1) = 0.1 * 2.0 = 0.2

        # new_transfer_input_2 = 0.0 + ( 0.5 * 0.0 + 3.0 * 0.0 - 1.0*0.0 + 2.0)*0.1 + 0.0    =    0.2
        # f(new_transfer_input_2) = 0.2 * 2.0 = 0.4

        # - - - - - - - TRIAL 2 - - - - - - -

        # new_transfer_input = 0.1 + ( 0.5 * 0.1 + 3.0 * 0.2 - 1.0*0.4 + 1.0)*0.1 + 0.0    =    0.225
        # f(new_transfer_input) = 0.265 * 2.0 = 0.45

        # new_transfer_input_2 = 0.2 + ( 0.5 * 0.2 + 3.0 * 0.4 - 1.0*0.2 + 2.0)*0.1 + 0.0    =    0.51
        # f(new_transfer_input_2) = 0.1 * 2.0 = 1.02

        # - - - - - - - TRIAL 3 - - - - - - -

        # new_transfer_input = 0.225 + ( 0.5 * 0.225 + 3.0 * 0.45 - 1.0*1.02 + 1.0)*0.1 + 0.0    =    0.36925
        # f(new_transfer_input) = 0.36925 * 2.0 = 0.7385

        # new_transfer_input_2 = 0.51 + ( 0.5 * 0.51 + 3.0 * 1.02 - 1.0*0.45 + 2.0)*0.1 + 0.0    =    0.9965
        # f(new_transfer_input_2) = 0.9965 * 2.0 = 1.463

        assert np.allclose(results,
                           [[0.2, 0.4], [0.45, 1.02], [0.7385, 1.993]])
    def test_learning_of_orthognal_inputs(self):
        size = 4
        R = RecurrentTransferMechanism(size=size,
                                       function=Linear,
                                       enable_learning=True,
                                       auto=0,
                                       hetero=np.full((size, size), 0.0))
        P = Process(pathway=[R])
        S = System(processes=[P])

        inputs_dict = {R: [1, 0, 1, 0]}
        S.run(num_trials=4, inputs=inputs_dict)
        np.testing.assert_allclose(
            R.recurrent_projection.mod_matrix,
            [[0.0, 0.0, 0.23700501, 0.0], [0.0, 0.0, 0.0, 0.0],
             [0.23700501, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]])
        np.testing.assert_allclose(R.output_state.value,
                                   [1.18518086, 0.0, 1.18518086, 0.0])

        # Reset state so learning of new pattern is "uncontaminated" by activity from previous one
        R.output_state.value = [0, 0, 0, 0]
        inputs_dict = {R: [0, 1, 0, 1]}
        S.run(num_trials=4, inputs=inputs_dict)
        np.testing.assert_allclose(
            R.recurrent_projection.mod_matrix,
            [[0.0, 0.0, 0.23700501, 0.0], [0.0, 0.0, 0.0, 0.23700501],
             [0.23700501, 0.0, 0.0, 0.], [0.0, 0.23700501, 0.0, 0.]])
        np.testing.assert_allclose(R.output_state.value,
                                   [0.0, 1.18518086, 0.0, 1.18518086])
    def test_previous_value_persistence_run(self):
        T = TransferMechanism(name="T",
                              initial_value=0.5,
                              integrator_mode=True,
                              integration_rate=0.1,
                              noise=0.0)
        P = Process(name="P",
                    pathway=[T])
        S = System(name="S",
                   processes=[P])
        T.reinitialize_when = Never()

        assert np.allclose(T.integrator_function.previous_value, 0.5)

        S.run(inputs={T: 1.0}, num_trials=2)
        # Trial 1
        # 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
        # integration: 0.9*0.55 + 0.1*1.0 + 0.0 = 0.595  --->  previous value = 0.595
        # linear fn: 0.595*1.0 = 0.595
        assert np.allclose(T.integrator_function.previous_value, 0.595)

        S.run(inputs={T: 2.0}, num_trials=2)
        # Trial 3
        # integration: 0.9*0.595 + 0.1*2.0 + 0.0 = 0.7355  --->  previous value = 0.7355
        # linear fn: 0.7355*1.0 = 0.7355
        # Trial 4
        # integration: 0.9*0.7355 + 0.1*2.0 + 0.0 = 0.86195  --->  previous value = 0.86195
        # linear fn: 0.86195*1.0 = 0.86195

        assert np.allclose(T.integrator_function.previous_value, 0.86195)
    def test_initial_values_softmax(self):
        T = TransferMechanism(default_variable=[[0.0, 0.0], [0.0, 0.0]],
                              function=SoftMax(),
                              integrator_mode=True,
                              integration_rate=0.5,
                              initial_value=[[1.0, 2.0], [3.0, 4.0]])
        T2 = TransferMechanism()
        P = Process(pathway=[T, T2])
        S = System(processes=[P])

        S.run(inputs={T: [[1.5, 2.5], [3.5, 4.5]]})

        result = T.value
        # Expected results
        # integrator function:
        # input = [[1.5, 2.5], [3.5, 4.5]]  |  output = [[1.25, 2.25]], [3.25, 4.25]]
        integrator_fn = AdaptiveIntegrator(rate=0.5,
                                           default_variable=[[0.0, 0.0], [0.0, 0.0]],
                                           initializer=[[1.0, 2.0], [3.0, 4.0]])
        expected_result_integrator = integrator_fn.function([[1.5, 2.5], [3.5, 4.5]])

        S1 = SoftMax()
        expected_result_s1 = S1.function([[1.25, 2.25]])

        S2 = SoftMax()
        expected_result_s2 = S2.function([[3.25, 4.25]])

        assert np.allclose(expected_result_integrator, T.integrator_function_value)
        assert np.allclose(expected_result_s1, result[0])
        assert np.allclose(expected_result_s2, result[1])
    def test_switch_mode(self):
        T = TransferMechanism(integrator_mode=True)
        P = Process(pathway=[T])
        S = System(processes=[P])
        integrator_function = T.integrator_function
        T.reinitialize_when = Never()
        # T starts with integrator_mode = True; confirm that T behaves correctly
        S.run({T: [[1.0], [1.0], [1.0]]})
        assert np.allclose(T.value, [[0.875]])

        assert T.integrator_mode is True
        assert T.integrator_function is integrator_function

        # Switch integrator_mode to False; confirm that T behaves correctly
        T.integrator_mode = False

        assert T.integrator_mode is False
        assert T.integrator_function is None

        S.run({T: [[1.0], [1.0], [1.0]]})
        assert np.allclose(T.value, [[1.0]])

        # Switch integrator_mode BACK to True; confirm that T picks up where it left off
        T.integrator_mode = True

        assert T.integrator_mode is True
        assert T.integrator_function is integrator_function

        S.run({T: [[1.0], [1.0], [1.0]]})
        assert np.allclose(T.value, [[0.984375]])
Example #21
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].mod_matrix)
            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]])
        ])
Example #22
0
    def test_system_run_with_sticky_condition(self):

        # Construction
        T = TransferMechanism()
        P = Process(pathway=[T])
        S = System(processes=[P])
        assert T.noise == 0.0
        assert T.parameter_states['noise'].value == 0.0

        # Runtime param used for noise
        # ONLY mechanism value should reflect runtime param -- attr should be changed back by the time we inspect it
        S.run(inputs={T: 2.0},
              runtime_params={T: {
                  "noise": (10.0, AfterTrial(1))
              }},
              num_trials=4)

        # Runtime param NOT used for noise
        S.run(inputs={T: 2.0})

        assert np.allclose(
            S.results,
            [
                [np.array([2.])],  # Trial 0 - condition not satisfied yet
                [np.array([2.])],  # Trial 1 - condition not satisfied yet
                [np.array([12.])],  # Trial 2 - condition satisfied
                [np.array([12.])],  # Trial 3 - condition satisfied (sticky)
                [np.array([2.])]
            ])  # New run (runtime param no longer applies)
Example #23
0
    def test_initialize_mechanisms(self):
        A = TransferMechanism(name='A')
        B = TransferMechanism(name='B')
        C = RecurrentTransferMechanism(name='C',
                                       auto=1.0)

        abc_process = Process(pathway=[A, B, C])

        abc_system = System(processes=[abc_process])

        C.log.set_log_conditions('value')

        abc_system.run(inputs={A: [1.0, 2.0, 3.0]},
                       initial_values={A: 1.0,
                                       B: 1.5,
                                       C: 2.0},
                       initialize=True)

        abc_system.run(inputs={A: [1.0, 2.0, 3.0]},
                       initial_values={A: 1.0,
                                       B: 1.5,
                                       C: 2.0},
                       initialize=False)

        # Run 1 --> Execution 1: 1 + 2 = 3    |    Execution 2: 3 + 2 = 5    |    Execution 3: 5 + 3 = 8
        # Run 2 --> Execution 1: 8 + 1 = 9    |    Execution 2: 9 + 2 = 11    |    Execution 3: 11 + 3 = 14
        assert np.allclose(C.log.nparray_dictionary('value')['value'], [[[3]], [[5]], [[8]], [[9]], [[11]], [[14]]])
Example #24
0
    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.output_labels)

        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']]
Example #25
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.value)

        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])
Example #26
0
    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.input_labels)

        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]]])
Example #27
0
    def test_not_all_output_state_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.output_labels)

        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]])
Example #28
0
    def test_previous_value_stored(self):
        G = LCA(integrator_mode=True,
                leak=-1.0,
                noise=0.0,
                time_step_size=0.02,
                function=Linear(slope=2.0),
                self_excitation=1.0,
                competition=-1.0,
                initial_value=np.array([[1.0]]))

        P = Process(pathway=[G])
        S = System(processes=[P])
        G.output_state.value = [0.0]

        # - - - - - LCA integrator functions - - - - -
        # X = previous_value + (rate * previous_value + variable) * self.time_step_size + noise
        # f(X) = 2.0*X + 0

        # - - - - - starting values - - - - -
        # variable = G.output_state.value + stimulus = 0.0 + 1.0 = 1.0
        # previous_value = initial_value = 1.0
        # single_run = S.execute([[1.0]])
        # np.testing.assert_allclose(single_run, np.array([[2.0]]))
        np.testing.assert_allclose(S.execute([[1.0]]), np.array([[2.0]]))
        # X = 1.0 + (-1.0 + 1.0)*0.02 + 0.0
        # X = 1.0 + 0.0 + 0.0 = 1.0 <--- previous value 1.0
        # f(X) = 2.0*1.0  <--- return 2.0, recurrent projection 2.0

        np.testing.assert_allclose(S.execute([[1.0]]), np.array([[2.08]]))
        # X = 1.0 + (-1.0 + 3.0)*0.02 + 0.0
        # X = 1.0 + 0.04 = 1.04 <--- previous value 1.04
        # f(X) = 2.0*1.04  <--- return 2.08

        np.testing.assert_allclose(S.execute([[1.0]]), np.array([[2.1616]]))
Example #29
0
    def test_kwta_size_10_k_3_threshold_1(self):
        K = KWTA(
            name='K',
            size=10,
            k_value=3,
            threshold=1,
            time_scale=TimeScale.TIME_STEP
        )
        p = Process(pathway=[K], prefs=TestKWTALongTerm.simple_prefs)
        s = System(processes=[p], prefs=TestKWTALongTerm.simple_prefs)
        kwta_input = {K: [[-1, -.5, 0, 0, 0, 1, 1, 2, 3, 3]]}
        print("")
        for i in range(20):
            s.run(inputs=kwta_input)
            print('\ntrial number', i)
            print('K.value: ', K.value)
        assert np.allclose(K.value, [[0.012938850123312412, 0.022127587008877226, 0.039010157367582114,
                                     0.039010157367582114, 0.039010157367582114, 0.19055156271846602,

                                     0.19055156271846602, 0.969124504436019, 0.9895271824560731, 0.9895271824560731]])
        kwta_input2 = {K: [0] * 10}

        print('\n\nturning to zero-inputs now:')
        for i in range(20):
            s.run(inputs=kwta_input2)
            print('\ntrial number', i)
            print('K.value: ', K.value)
        assert np.allclose(K.value, [[0.13127237999481228, 0.13130057846907178, 0.1313653354768465, 0.1313653354768465,
                                     0.1313653354768465, 0.5863768938723602, 0.5863768938723602, 0.8390251365605804,
                                     0.8390251603214743, 0.8390251603214743]])
Example #30
0
    def test_recurrent_transfer_origin(self):
        R = RecurrentTransferMechanism(has_recurrent_input_state=True)
        P = Process(pathway=[R])
        S = System(processes=[P])

        S.run(inputs={R: [[1.0], [2.0], [3.0]]})
        print(S.results)