Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
    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.]])
Exemplo n.º 3
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]])
        ])
Exemplo n.º 4
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])
Exemplo n.º 5
0
 def test_processing_mechanism_multiple_input_states(self):
     PM1 = ProcessingMechanism(size=[4, 4], function=LinearCombination, input_states=['input_1', 'input_2'])
     PM2 = ProcessingMechanism(size=[2, 2, 2], function=LinearCombination, input_states=['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])
Exemplo n.º 6
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]])
Exemplo n.º 7
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]]])
Exemplo n.º 8
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']]
Exemplo n.º 9
0
    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)
Exemplo n.º 10
0
    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].mod_matrix)

        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]]])
Exemplo n.º 11
0
    def test_dict_of_floats(self):
        input_labels_dict = {"red": 1, "green": 0}

        M = ProcessingMechanism(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', 'green', 'red'],
              call_after_trial=call_after_trial)
        assert np.allclose(S.results, [[[1.]], [[0.]], [[0.]], [[1.]]])
        assert store_input_labels == [['red'], ['green'], ['green'], ['red']]
        S.run(inputs=[1, 'green', 0, 'red'])
        assert np.allclose(
            S.results,
            [[[1.]], [[0.]], [[0.]], [[1.]], [[1.]], [[0.]], [[0.]], [[1.]]])
Exemplo n.º 12
0
 def test_processing_mechanism_FHNIntegrator_function(self):
     PM1 = ProcessingMechanism(function=FHNIntegrator)
     PM1.execute(1.0)
Exemplo n.º 13
0
 def test_processing_mechanism_AGTUtilityIntegrator_function(self):
     PM1 = ProcessingMechanism(function=AGTUtilityIntegrator)
     PM1.execute(1.0)
Exemplo n.º 14
0
 def test_processing_mechanism_OrnsteinUhlenbeckIntegrator_function(self):
     PM1 = ProcessingMechanism(function=OrnsteinUhlenbeckIntegrator)
     PM1.execute(1.0)
Exemplo n.º 15
0
 def test_processing_mechanism_Stability_function(self):
     PM1 = ProcessingMechanism(function=Stability)
     PM1.execute(1.0)
Exemplo n.º 16
0
 def test_processing_mechanism_Reinforcement_function(self):
     PM1 = ProcessingMechanism(function=Reinforcement,
                               default_variable=[[0.0], [0.0], [0.0]])
     PM1.execute([[1.0], [2.0], [3.0]])
Exemplo n.º 17
0
 def test_processing_mechanism_DriftDiffusionIntegrator_function(self):
     PM1 = ProcessingMechanism(function=DriftDiffusionIntegrator)
     PM1.execute(1.0)
Exemplo n.º 18
0
 def test_processing_mechanism_Exponential_function(self):
     PM1 = ProcessingMechanism(function=Exponential)
     PM1.execute(1.0)
Exemplo n.º 19
0
 def test_processing_mechanism_GammaDist_function(self):
     PM1 = ProcessingMechanism(function=GammaDist)
     PM1.execute(1.0)
Exemplo n.º 20
0
 def test_processing_mechanism_Reduce_function(self):
     PM1 = ProcessingMechanism(function=Reduce)
     PM1.execute(1.0)
Exemplo n.º 21
0
 def test_processing_mechanism_CombineMeans_function(self):
     PM1 = ProcessingMechanism(function=CombineMeans)
     PM1.execute(1.0)
Exemplo n.º 22
0
 def test_processing_mechanism_Distance_function(self):
     PM1 = ProcessingMechanism(function=Distance,
                               default_variable=[[0, 0], [0, 0]])
     PM1.execute([[1, 2], [3, 4]])
Exemplo n.º 23
0
    def test_processing_mechanism_LinearCombination_function(self):

        PM1 = ProcessingMechanism(function=LinearCombination)
        PM1.execute(1.0)
Exemplo n.º 24
0
 def test_processing_mechanism_TDLearning_function(self):
     PM1 = ProcessingMechanism(function=TDLearning,
                               default_variable=[[0.0], [0.0], [0.0]])
     PM1.execute([[1.0], [2.0], [3.0]])
Exemplo n.º 25
0
 def test_processing_mechanism_BogaczEtAl_function(self):
     PM1 = ProcessingMechanism(function=BogaczEtAl)
     PM1.execute(1.0)
Exemplo n.º 26
0
 def test_processing_mechanism_Logistic_function(self):
     PM1 = ProcessingMechanism(function=Logistic)
     PM1.execute(1.0)
Exemplo n.º 27
0
 def test_processing_mechanism_NormalDist_function(self):
     PM1 = ProcessingMechanism(function=NormalDist)
     PM1.execute(1.0)
Exemplo n.º 28
0
 def test_processing_mechanism_SoftMax_function(self):
     PM1 = ProcessingMechanism(function=SoftMax(per_item=False))
     PM1.execute(1.0)
Exemplo n.º 29
0
    def test_valid_matrix_specs(self):
        # Note: default matrix specification is None

        PM_default = ProcessingMechanism(function=LinearMatrix())
        PM_default.execute(1.0)

        assert np.allclose(PM_default.value, 1.0)

        PM_default_len_2_var = ProcessingMechanism(
            function=LinearMatrix(default_variable=[[0.0, 0.0]]),
            default_variable=[[0.0, 0.0]])
        PM_default_len_2_var.execute([[1.0, 2.0]])

        assert np.allclose(PM_default_len_2_var.value, [[1.0, 2.0]])

        PM_default_2d_var = ProcessingMechanism(function=LinearMatrix(
            default_variable=[[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]),
                                                default_variable=[[0.0, 0.0],
                                                                  [0.0, 0.0],
                                                                  [0.0, 0.0]])

        #  [1.0   0.0]           [1.0   0.0]
        #  [0.0   2.0]    *      [0.0   1.0]
        #  [3.0   0.0]
        PM_default_2d_var.execute([[1.0, 0.0], [0.0, 2.0], [3.0, 0.0]])

        assert np.allclose(PM_default_2d_var.value,
                           [[1.0, 0.0], [0.0, 2.0], [3.0, 0.0]])

        # PM_float = ProcessingMechanism(function=LinearMatrix(matrix=4.0))
        # PM_float.execute(1.0)
        #
        # assert np.allclose(PM_float.value, 4.0)

        PM_1d_list = ProcessingMechanism(function=LinearMatrix(matrix=[4.0]))
        PM_1d_list.execute(1.0)

        assert np.allclose(PM_1d_list.value, 4.0)

        PM_2d_list = ProcessingMechanism(
            function=LinearMatrix(matrix=[[4.0, 5.0], [6.0, 7.0], [8.0, 9.0],
                                          [10.0, 11.0]],
                                  default_variable=[[0.0, 0.0, 0.0, 0.0],
                                                    [0.0, 0.0, 0.0, 0.0],
                                                    [0.0, 0.0, 0.0, 0.0]]),
            default_variable=[[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0],
                              [0.0, 0.0, 0.0, 0.0]])
        PM_2d_list.execute([[1.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0],
                            [0.0, 0.0, 0.0, 1.0]])

        assert np.allclose(PM_2d_list.value,
                           [[4.0, 5.0], [8.0, 9.0], [10.0, 11.0]])

        PM_1d_array = ProcessingMechanism(function=LinearMatrix(
            matrix=np.array([4.0])))
        PM_1d_array.execute(1.0)

        assert np.allclose(PM_1d_array.value, 4.0)

        PM_2d_array = ProcessingMechanism(function=LinearMatrix(
            matrix=np.array([[4.0]])))
        PM_2d_array.execute(1.0)

        assert np.allclose(PM_2d_array.value, 4.0)

        PM_matrix = ProcessingMechanism(function=LinearMatrix(
            matrix=np.matrix([[4.0]])))
        PM_matrix.execute(1.0)

        assert np.allclose(PM_matrix.value, 4.0)
Exemplo n.º 30
0
 def test_processing_mechanism_Hebbian_function(self):
     PM1 = ProcessingMechanism(function=Hebbian,
                               default_variable=[[0.0], [0.0], [0.0]])
     PM1.execute([[1.0], [2.0], [3.0]])