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))
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.value, [[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.value, [[1.28, 1.28, 0.28, 0.28]]) np.testing.assert_allclose(R.recurrent_projection.mod_matrix, [[ 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.value, [[1.4268928, 1.4268928, 0.3589728, 0.3589728]]) np.testing.assert_allclose(R.recurrent_projection.mod_matrix, [[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]])
def test_stroop_model(self): process_prefs = {REPORT_OUTPUT_PREF: True, VERBOSE_PREF: False} # system_prefs = { # REPORT_OUTPUT_PREF: True, # VERBOSE_PREF: False # } colors = TransferMechanism( size=2, function=Linear, name="Colors", ) words = TransferMechanism( default_variable=[0, 0], size=2, function=Linear, name="Words", ) response = TransferMechanism( default_variable=[0, 0], function=Logistic, name="Response", ) color_naming_process = Process( default_variable=[1, 2.5], pathway=[colors, FULL_CONNECTIVITY_MATRIX, response], learning=LEARNING_PROJECTION, target=[0, 1], name='Color Naming', prefs=process_prefs, ) word_reading_process = Process( default_variable=[.5, 3], pathway=[words, FULL_CONNECTIVITY_MATRIX, response], name='Word Reading', learning=LEARNING_PROJECTION, target=[1, 0], prefs=process_prefs, ) # s = System( # processes=[color_naming_process, word_reading_process], # name='Stroop Model', # targets=[0, 0], # prefs=system_prefs, # ) # stim_dict = { # colors: [ # [1,0], # [0,1] # ], # words: [ # [0,1], # [1,0] # ] # } # target_dict = { # response: [ # [1,0], # [0,1] # ] # } # results = s.run( # num_trials=10, # inputs=stim_dict, # targets=target_dict, # ) expected_color_results = [ np.array([0.88079708, 0.88079708]), np.array([0.85997037, 0.88340023]), np.array([0.83312329, 0.88585176]), np.array([0.79839127, 0.88816536]), np.array([0.75384913, 0.89035312]), np.array([0.69835531, 0.89242571]), np.array([0.63303376, 0.89439259]), np.array([0.56245802, 0.8962622]), np.array([0.49357614, 0.89804208]), np.array([0.43230715, 0.89973899]), ] expected_word_results = [ np.array([0.88079708, 0.88079708]), np.array([0.88340023, 0.85997037]), np.array([0.88585176, 0.83312329]), np.array([0.88816536, 0.79839127]), np.array([0.89035312, 0.75384913]), np.array([0.89242571, 0.69835531]), np.array([0.89439259, 0.63303376]), np.array([0.8962622, 0.56245802]), np.array([0.89804208, 0.49357614]), np.array([0.89973899, 0.43230715]), ] for i in range(10): cr = color_naming_process.execute(input=[1, 1], target=[0, 1]) wr = word_reading_process.execute(input=[1, 1], target=[1, 0]) np.testing.assert_allclose( cr, expected_color_results[i], atol=1e-08, err_msg='Failed on expected_color_results[{0}]'.format(i)) np.testing.assert_allclose( wr, expected_word_results[i], atol=1e-08, err_msg='Failed on expected_word_results[{0}]'.format(i))
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.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.value, [[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.value, [[1.28, 1.28, 0.28, 0.28]]) np.testing.assert_allclose( R.matrix, [[ 0.18192000000000003, 0.18192000000000003, 0.11792000000000001, 0.11792000000000001 ], [ 0.18192000000000003, 0.18192000000000003, 0.11792000000000001, 0.11792000000000001 ], [ 0.11792000000000001, 0.11792000000000001, 0.10392000000000001, 0.10392000000000001 ], [ 0.11792000000000001, 0.11792000000000001, 0.10392000000000001, 0.10392000000000001 ]]) p.execute([1, 1, 0, 0]) np.testing.assert_allclose( R.value, [[1.5317504, 1.5317504, 0.3600704, 0.3600704]]) np.testing.assert_allclose( R.matrix, [[ 0.299232964395008, 0.299232964395008, 0.14549689896140802, 0.14549689896140802 ], [ 0.299232964395008, 0.299232964395008, 0.14549689896140802, 0.14549689896140802 ], [ 0.14549689896140802, 0.14549689896140802, 0.11040253464780801, 0.11040253464780801 ], [ 0.14549689896140802, 0.14549689896140802, 0.11040253464780801, 0.11040253464780801 ]])