Esempio n. 1
0
    def test_recurrent_mech_change_learning_rate(self):
        R = RecurrentTransferMechanism(size=4,
                                       function=Linear,
                                       enable_learning=True,
                                       learning_rate=0.1
                                       )

        p = Process(pathway=[R])
        s = System(processes=[p])
        assert R.learning_rate == 0.1
        assert R.learning_mechanism.learning_rate == 0.1
        # assert R.learning_mechanism.function.learning_rate == 0.1

        s.run(inputs=[[1.0, 1.0, 1.0, 1.0]])
        matrix_1 = [[0., 1.1, 1.1, 1.1],
                    [1.1, 0., 1.1, 1.1],
                    [1.1, 1.1, 0., 1.1],
                    [1.1, 1.1, 1.1, 0.]]
        assert np.allclose(R.recurrent_projection.mod_matrix, matrix_1)
        print(R.recurrent_projection.mod_matrix)
        R.learning_rate = 0.9

        assert R.learning_rate == 0.9
        assert R.learning_mechanism.learning_rate == 0.9
        # assert R.learning_mechanism.function.learning_rate == 0.9
        s.run(inputs=[[1.0, 1.0, 1.0, 1.0]])
        matrix_2 = [[0., 1.911125, 1.911125, 1.911125],
                    [1.911125, 0., 1.911125, 1.911125],
                    [1.911125, 1.911125, 0., 1.911125],
                    [1.911125, 1.911125, 1.911125, 0.]]
        # assert np.allclose(R.recurrent_projection.mod_matrix, matrix_2)
        print(R.recurrent_projection.mod_matrix)
Esempio n. 2
0
 def test_rt_without_custom_comb_fct(self):
     R1 = RecurrentTransferMechanism(
             has_recurrent_input_state=True,
             size=2,
     )
     result = R1.execute([1,2])
     np.testing.assert_allclose(result, [[1,2]])
Esempio n. 3
0
 def test_recurrent_mech_inputs_mismatched_with_default_shorter(self):
     with pytest.raises(MechanismError) as error_text:
         R = RecurrentTransferMechanism(
             name='R',
             size=6
         )
         R.execute([1, 2, 3, 4, 5])
     assert "does not match required length" in str(error_text.value)
Esempio n. 4
0
    def test_recurrent_mech_function_logistic(self):

        R = RecurrentTransferMechanism(
            name='R',
            size=10,
            function=Logistic(gain=2, offset=1)
        )
        val = R.execute(np.ones(10))
        np.testing.assert_allclose(val, [np.full(10, 0.7310585786300049)])
Esempio n. 5
0
 def test_recurrent_mech_inputs_list_of_strings(self):
     with pytest.raises(UtilitiesError) as error_text:
         R = RecurrentTransferMechanism(
             name='R',
             default_variable=[0, 0, 0, 0],
             integrator_mode=True
         )
         R.execute(["one", "two", "three", "four"])
     assert "has non-numeric entries" in str(error_text.value)
Esempio n. 6
0
 def test_rt_with_custom_comb_fct(self):
     def my_fct(x):
         return x[0] * x[1] if len(x) == 2 else x[0]
     R2 = RecurrentTransferMechanism(
             has_recurrent_input_state=True,
             size=2,
             combination_function=my_fct
     )
     result = R2.execute([1,2])
     np.testing.assert_allclose(result, [[0,0]])
Esempio n. 7
0
 def test_recurrent_mech_auto_array_matrix_spec(self):
     R = RecurrentTransferMechanism(
         name='R',
         size=4,
         auto=[1.1, 2.2, 3.3, 4.4],
         matrix=[[1, 2, 3, 4]] * 4
     )
     val = R.execute([10, 11, 12, 13])
     np.testing.assert_allclose(val, [[10., 11., 12., 13.]])
     np.testing.assert_allclose(R.matrix, [[1.1, 2, 3, 4], [1, 2.2, 3, 4], [1, 2, 3.3, 4], [1, 2, 3, 4.4]])
Esempio n. 8
0
 def test_recurrent_mech_matrix_auto_hetero_spec_size_1(self):
     R = RecurrentTransferMechanism(
         name='R',
         size=1,
         auto=-2,
         hetero=4.4
     )
     val = R.execute([10])
     np.testing.assert_allclose(val, [[10.]])
     assert isinstance(R.matrix, np.ndarray)
     np.testing.assert_allclose(R.matrix, [[-2]])
Esempio n. 9
0
    def test_recurrent_mech_function_psyneulink(self):

        a = Logistic(gain=2, offset=1)

        R = RecurrentTransferMechanism(
            name='R',
            size=7,
            function=a
        )
        val = R.execute(np.zeros(7))
        np.testing.assert_allclose(val, [np.full(7, 0.2689414213699951)])
Esempio n. 10
0
 def test_recurrent_mech_reduce_fun(self):
     with pytest.raises(TransferError) as error_text:
         R = RecurrentTransferMechanism(
             name='R',
             default_variable=[0, 0, 0, 0],
             function=Reduce(),
             integration_rate=1.0,
             integrator_mode=True
         )
         R.execute([0, 0, 0, 0])
     assert "must be a TRANSFER FUNCTION TYPE" in str(error_text.value)
Esempio n. 11
0
 def test_recurrent_mech_matrix_auto_hetero_spec_size_4(self):
     R = RecurrentTransferMechanism(
         name='R',
         size=4,
         auto=2.2,
         hetero=-3
     )
     val = R.execute([10, 10, 10, 10])
     np.testing.assert_allclose(val, [[10., 10., 10., 10.]])
     np.testing.assert_allclose(R.matrix, [[2.2, -3, -3, -3], [-3, 2.2, -3, -3], [-3, -3, 2.2, -3], [-3, -3, -3, 2.2]])
     assert isinstance(R.matrix, np.ndarray)
Esempio n. 12
0
 def test_recurrent_mech_integration_rate_0_8_initial_1_2(self):
     R = RecurrentTransferMechanism(
         name='R',
         default_variable=[0, 0, 0, 0],
         function=Linear(),
         integration_rate=0.8,
         initial_value=np.array([[-1, 1, -2, 2]]),
         integrator_mode=True
     )
     val = R.execute([3, 2, 1, 0])
     np.testing.assert_allclose(val, [[2.2, 1.8, .40000000000000013, .3999999999999999]])
Esempio n. 13
0
 def test_recurrent_mech_auto_matrix_spec(self):
     # auto should override the diagonal only
     R = RecurrentTransferMechanism(
         name='R',
         size=4,
         auto=2.2,
         matrix=[[1, 2, 3, 4]] * 4
     )
     val = R.execute([10, 11, 12, 13])
     np.testing.assert_allclose(val, [[10., 11., 12., 13.]])
     np.testing.assert_allclose(R.matrix, [[2.2, 2, 3, 4], [1, 2.2, 3, 4], [1, 2, 2.2, 4], [1, 2, 3, 2.2]])
Esempio n. 14
0
    def test_recurrent_mech_matrix_keyword_spec(self, matrix):

        if matrix == RANDOM_CONNECTIVITY_MATRIX:
            pytest.skip("Random test")
        R = RecurrentTransferMechanism(
            name='R',
            size=4,
            matrix=matrix
        )
        val = R.execute([10, 10, 10, 10])
        np.testing.assert_allclose(val, [[10., 10., 10., 10.]])
        np.testing.assert_allclose(R.recurrent_projection.matrix, get_matrix(matrix, R.size[0], R.size[0]))
Esempio n. 15
0
 def test_recurrent_mech_integration_rate_0_8(self):
     R = RecurrentTransferMechanism(
         name='R',
         default_variable=[0, 0, 0, 0],
         function=Linear(),
         integration_rate=0.8,
         integrator_mode=True
     )
     val = R.execute([1, 1, 1, 1])
     np.testing.assert_allclose(val, [[0.8, 0.8, 0.8, 0.8]])
     val = R.execute([1, 1, 1, 1])
     np.testing.assert_allclose(val, [[.96, .96, .96, .96]])
Esempio n. 16
0
 def test_recurrent_mech_hetero_matrix_matrix_spec(self):
     R = RecurrentTransferMechanism(
         name='R',
         size=4,
         hetero=np.array([[-4, -3, -2, -1]] * 4),
         matrix=[[1, 2, 3, 4]] * 4
     )
     val = R.execute([1, 2, 3, 4])
     np.testing.assert_allclose(val, [[1., 2., 3., 4.]])
     np.testing.assert_allclose(
         R.matrix,
         [[1, -3, -2, -1], [-4, 2, -2, -1], [-4, -3, 3, -1], [-4, -3, -2, 4]]
     )
Esempio n. 17
0
 def test_recurrent_mech_matrix_auto_hetero_matrix_spec(self):
     # when auto, hetero, and matrix are all specified, auto and hetero should take precedence
     R = RecurrentTransferMechanism(
         name='R',
         size=4,
         auto=2.2,
         hetero=-3,
         matrix=[[1, 2, 3, 4]] * 4
     )
     val = R.execute([10, 10, 10, 10])
     np.testing.assert_allclose(val, [[10., 10., 10., 10.]])
     np.testing.assert_allclose(R.matrix, [[2.2, -3, -3, -3], [-3, 2.2, -3, -3], [-3, -3, 2.2, -3], [-3, -3, -3, 2.2]])
     assert isinstance(R.matrix, np.ndarray)
Esempio n. 18
0
    def test_recurrent_mech_matrix_other_spec(self, matrix):

        R = RecurrentTransferMechanism(
            name='R',
            size=2,
            matrix=matrix
        )
        val = R.execute([10, 10])
        np.testing.assert_allclose(val, [[10., 10.]])
        assert isinstance(R.matrix, np.ndarray)
        np.testing.assert_allclose(R.matrix, [[1, 2], [3, 4]])
        np.testing.assert_allclose(R.recurrent_projection.matrix, [[1, 2], [3, 4]])
        assert isinstance(R.recurrent_projection.matrix, np.ndarray)
Esempio n. 19
0
 def test_ris_simple(self):
     R2 = RecurrentTransferMechanism(default_variable=[[0.0, 0.0, 0.0]],
                                         matrix=[[1.0, 2.0, 3.0],
                                                 [2.0, 1.0, 2.0],
                                                 [3.0, 2.0, 1.0]],
                                         has_recurrent_input_state=True)
     R2.execute(input=[1, 3, 2])
     p2 = Process(pathway=[R2])
     s2 = System(processes=[p2])
     s2.run(inputs=[[1, 3, 2]])
     np.testing.assert_allclose(R2.parameters.value.get(s2), [[14., 12., 13.]])
     assert len(R2.input_states) == 2
     assert "Recurrent Input State" not in R2.input_state.name  # make sure recurrent input state isn't primary
Esempio n. 20
0
 def test_recurrent_mech_integration_rate_0_8_initial_0_5(self):
     R = RecurrentTransferMechanism(
         name='R',
         default_variable=[0, 0, 0, 0],
         function=Linear(),
         integration_rate=0.8,
         initial_value=np.array([[0.5, 0.5, 0.5, 0.5]]),
         integrator_mode=True
     )
     val = R.execute([1, 1, 1, 1])
     np.testing.assert_allclose(val, [[0.9, 0.9, 0.9, 0.9]])
     val = R.execute([1, 2, 3, 4])
     np.testing.assert_allclose(val, [[.98, 1.78, 2.5800000000000005, 3.3800000000000003]])  # due to inevitable floating point errors
Esempio n. 21
0
 def test_recurrent_mech_auto_hetero_matrix_spec_v3(self):
     R = RecurrentTransferMechanism(
         name='R',
         size=4,
         auto=[3],
         hetero=2,
         matrix=[[1, 2, 3, 4]] * 4
     )
     val = R.execute([1, 2, 3, 4])
     np.testing.assert_allclose(val, [[1., 2., 3., 4.]])
     np.testing.assert_allclose(
         R.matrix,
         [[3, 2, 2, 2], [2, 3, 2, 2], [2, 2, 3, 2], [2, 2, 2, 3]]
     )
Esempio n. 22
0
 def test_recurrent_mech_hetero_float_matrix_spec(self):
     # hetero should override off-diagonal only
     R = RecurrentTransferMechanism(
         name='R',
         size=4,
         hetero=-2.2,
         matrix=[[1, 2, 3, 4]] * 4
     )
     val = R.execute([1, 2, 3, 4])
     np.testing.assert_allclose(val, [[1., 2., 3., 4.]])
     np.testing.assert_allclose(
         R.matrix,
         [[1, -2.2, -2.2, -2.2], [-2.2, 2, -2.2, -2.2], [-2.2, -2.2, 3, -2.2], [-2.2, -2.2, -2.2, 4]]
     )
Esempio n. 23
0
    def test_recurrent_mech_with_learning(self):
        R = RecurrentTransferMechanism(size=4,
                                       function=Linear,
                                       matrix=np.full((4, 4), 0.1),
                                       enable_learning=True
                                       )
        # Test that all of these are the same:
        np.testing.assert_allclose(
            R.recurrent_projection.mod_matrix,
            [
                [0.1,  0.1, 0.1, 0.1],
                [0.1, 0.1, 0.1, 0.1],
                [0.1, 0.1, 0.1, 0.1],
                [0.1, 0.1, 0.1, 0.1]
            ]
        )
        np.testing.assert_allclose(R.recurrent_projection.matrix, R.matrix)
        np.testing.assert_allclose(R.input_state.path_afferents[0].matrix, R.matrix)

        # Test that activity is properly computed prior to learning
        p = Process(pathway=[R])
        R.learning_enabled = False
        p.execute([1, 1, 0, 0])
        p.execute([1, 1, 0, 0])
        np.testing.assert_allclose(R.parameters.value.get(p), [[1.2, 1.2, 0.2, 0.2]])

        # Test that activity and weight changes are properly computed with learning
        R.learning_enabled = True
        p.execute([1, 1, 0, 0])
        np.testing.assert_allclose(R.parameters.value.get(p), [[1.28, 1.28, 0.28, 0.28]])
        np.testing.assert_allclose(
            R.recurrent_projection.get_mod_matrix(p),
            [
                [0.1, 0.18192000000000003, 0.11792000000000001, 0.11792000000000001],
                [0.18192000000000003, 0.1, 0.11792000000000001, 0.11792000000000001],
                [0.11792000000000001, 0.11792000000000001, 0.1, 0.10392000000000001],
                [0.11792000000000001, 0.11792000000000001, 0.10392000000000001, 0.1]
            ]
        )
        p.execute([1, 1, 0, 0])
        np.testing.assert_allclose(R.parameters.value.get(p), [[1.4268928, 1.4268928, 0.3589728, 0.3589728]])
        np.testing.assert_allclose(
            R.recurrent_projection.get_mod_matrix(p),
            [
                [0.1, 0.28372115, 0.14353079, 0.14353079],
                [0.28372115, 0.1, 0.14353079, 0.14353079],
                [0.14353079, 0.14353079, 0.1, 0.11036307],
                [0.14353079, 0.14353079, 0.11036307, 0.1]
            ]
        )
Esempio n. 24
0
 def test_recurrent_mech_integration_rate_0_8_initial_1_8(self):
     R = RecurrentTransferMechanism(
         name='R',
         default_variable=[0, 0, 0, 0],
         function=Linear(),
         integration_rate=0.8,
         initial_value=np.array([[1.8, 1.8, 1.8, 1.8]]),
         integrator_mode=True
     )
     val = R.execute([1, 1, 1, 1])
     np.testing.assert_allclose(val, [[1.16, 1.16, 1.16, 1.16]])
     val = R.execute([2, 2, 2, 2])
     np.testing.assert_allclose(val, [[1.832, 1.832, 1.832, 1.832]])
     val = R.execute([-4, -3, 0, 1])
     np.testing.assert_allclose(val, [[-2.8336, -2.0336000000000003, .36639999999999995, 1.1663999999999999]])
Esempio n. 25
0
 def test_recurrent_mech_auto_hetero_matrix_spec_v1(self):
     # auto and hetero should override matrix
     R = RecurrentTransferMechanism(
         name='R',
         size=4,
         auto=[1, 3, 5, 7],
         hetero=np.array([[-4, -3, -2, -1]] * 4),
         matrix=[[1, 2, 3, 4]] * 4
     )
     val = R.execute([1, 2, 3, 4])
     np.testing.assert_allclose(val, [[1., 2., 3., 4.]])
     np.testing.assert_allclose(
         R.matrix,
         [[1, -3, -2, -1], [-4, 3, -2, -1], [-4, -3, 5, -1], [-4, -3, -2, 7]]
     )
Esempio n. 26
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')[abc_system.default_execution_id]
            ['value'], [[[3]], [[5]], [[8]], [[9]], [[11]], [[14]]])
Esempio n. 27
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)
Esempio n. 28
0
    def test_recurrent_mech_integrator(self, benchmark, mode):
        R = RecurrentTransferMechanism(size=2,
                                       function=Logistic(),
                                       hetero=-2.0,
                                       integrator_mode=True,
                                       integration_rate=0.01,
                                       output_states = [RECURRENT_OUTPUT.RESULT])
        if mode == 'Python':
            EX = R.execute
        elif mode == 'LLVM':
            e = pnlvm.execution.MechExecution(R)
            EX = e.execute
        elif mode == 'PTX':
            e = pnlvm.execution.MechExecution(R)
            EX = e.cuda_execute

        val1 = EX([[1.0, 2.0]])
        val2 = EX([[1.0, 2.0]])
        # execute 10 times
        for i in range(10):
            val = EX([[1.0, 2.0]])
        benchmark(EX, [[1.0, 2.0]])

        assert np.allclose(val1, [[0.50249998, 0.50499983]])
        assert np.allclose(val2, [[0.50497484, 0.50994869]])
        assert np.allclose(val, [[0.52837327, 0.55656439]])
Esempio n. 29
0
 def test_recurrent_mech_check_proj_attrs(self):
     R = RecurrentTransferMechanism(
         name='R',
         size=3
     )
     np.testing.assert_allclose(R.recurrent_projection.matrix, R.matrix)
     assert R.recurrent_projection.sender is R.output_state
     assert R.recurrent_projection.receiver is R.input_state
Esempio n. 30
0
 def test_recurrent_mech_matrix_3d(self):
     with pytest.raises(FunctionError) as error_text:
         R = RecurrentTransferMechanism(
             name='R',
             size=2,
             matrix=[[[1, 3], [2, 4]], [[5, 7], [6, 8]]]
         )
     assert "more than 2d" in str(error_text.value)