-
Notifications
You must be signed in to change notification settings - Fork 29
/
test_recurrent_transfer_mechanism.py
1454 lines (1271 loc) · 62.3 KB
/
test_recurrent_transfer_mechanism.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import pytest
import psyneulink as pnl
import psyneulink.core.llvm as pnlvm
from psyneulink.core.components.functions.combinationfunctions import Reduce
from psyneulink.core.components.functions.distributionfunctions import NormalDist
from psyneulink.core.components.functions.function import FunctionError
from psyneulink.core.components.functions.learningfunctions import Reinforcement
from psyneulink.core.components.functions.statefulfunctions.integratorfunctions import AccumulatorIntegrator
from psyneulink.core.components.functions.transferfunctions import Linear, Logistic, get_matrix
from psyneulink.core.components.mechanisms.mechanism import MechanismError
from psyneulink.core.components.mechanisms.processing.transfermechanism import TransferError, TransferMechanism
from psyneulink.core.components.process import Process
from psyneulink.core.components.system import System
from psyneulink.core.globals.keywords import MATRIX_KEYWORD_VALUES, RANDOM_CONNECTIVITY_MATRIX, RESULT
from psyneulink.core.globals.preferences.basepreferenceset import REPORT_OUTPUT_PREF, VERBOSE_PREF
from psyneulink.core.globals.utilities import UtilitiesError
from psyneulink.core.scheduling.condition import Never
from psyneulink.library.components.mechanisms.processing.transfer.recurrenttransfermechanism import \
RecurrentTransferError, RecurrentTransferMechanism
from psyneulink.library.components.projections.pathway.autoassociativeprojection import AutoAssociativeProjection
class TestMatrixSpec:
def test_recurrent_mech_matrix(self):
T = TransferMechanism(default_variable=[[0.0, 0.0, 0.0]])
recurrent_mech = 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]])
p = Process(pathway=[T, recurrent_mech])
s = System(processes=[p])
results = []
def record_trial():
results.append(recurrent_mech.parameters.value.get(s))
s.run(inputs=[[1.0, 1.0, 1.0], [2.0, 2.0, 2.0]],
call_after_trial=record_trial)
assert True
def test_recurrent_mech_auto_associative_projection(self):
T = TransferMechanism(default_variable=[[0.0, 0.0, 0.0]])
recurrent_mech = RecurrentTransferMechanism(default_variable=[[0.0, 0.0, 0.0]],
matrix=AutoAssociativeProjection)
p = Process(pathway=[T, recurrent_mech])
s = System(processes=[p])
results = []
def record_trial():
results.append(recurrent_mech.parameters.value.get(s))
s.run(inputs=[[1.0, 1.0, 1.0], [2.0, 2.0, 2.0]],
call_after_trial=record_trial)
def test_recurrent_mech_auto_auto_hetero(self):
T = TransferMechanism(default_variable=[[0.0, 0.0, 0.0]])
recurrent_mech = RecurrentTransferMechanism(default_variable=[[0.0, 0.0, 0.0]],
auto=3.0,
hetero=-7.0)
p = Process(pathway=[T, recurrent_mech])
s = System(processes=[p])
results = []
def record_trial():
results.append(recurrent_mech.parameters.value.get(s))
s.run(inputs=[[1.0, 1.0, 1.0], [2.0, 2.0, 2.0]],
call_after_trial=record_trial)
class TestRecurrentTransferMechanismInputs:
def test_recurrent_mech_empty_spec(self):
R = RecurrentTransferMechanism(auto=1.0)
np.testing.assert_allclose(R.value, R.defaults.value)
np.testing.assert_allclose(R.defaults.variable, [[0]])
np.testing.assert_allclose(R.matrix, [[1]])
def test_recurrent_mech_check_attrs(self):
R = RecurrentTransferMechanism(
name='R',
size=3,
auto=1.0
)
print("matrix = ", R.matrix)
print("auto = ", R.auto)
print("hetero = ", R.hetero)
# np.testing.assert_allclose(R.value, R.defaults.value)
# np.testing.assert_allclose(R.defaults.variable, [[0., 0., 0.]])
# np.testing.assert_allclose(R.matrix, [[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]])
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_port
assert R.recurrent_projection.receiver is R.input_port
@pytest.mark.mechanism
@pytest.mark.recurrent_transfer_mechanism
@pytest.mark.benchmark(group="RecurrentTransferMechanism")
@pytest.mark.parametrize('mode', ['Python',
pytest.param('LLVM', marks=pytest.mark.llvm),
pytest.param('PTX', marks=[pytest.mark.llvm, pytest.mark.cuda])])
def test_recurrent_mech_inputs_list_of_ints(self, benchmark, mode):
R = RecurrentTransferMechanism(
name='R',
default_variable=[0, 0, 0, 0]
)
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([10, 12, 0, -1])
val2 = EX([1, 2, 3, 0])
benchmark(EX, [1, 2, 3, 0])
np.testing.assert_allclose(val1, [[10.0, 12.0, 0, -1]])
np.testing.assert_allclose(val2, [[1, 2, 3, 0]]) # because recurrent projection is not used when executing: mech is reset each time
@pytest.mark.mechanism
@pytest.mark.recurrent_transfer_mechanism
@pytest.mark.benchmark(group="RecurrentTransferMechanism")
@pytest.mark.parametrize('mode', ['Python',
pytest.param('LLVM', marks=pytest.mark.llvm),
pytest.param('PTX', marks=[pytest.mark.llvm, pytest.mark.cuda])])
def test_recurrent_mech_inputs_list_of_floats(self, benchmark, mode):
R = RecurrentTransferMechanism(
name='R',
size=4
)
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
val = benchmark(EX, [10.0, 10.0, 10.0, 10.0])
np.testing.assert_allclose(val, [[10.0, 10.0, 10.0, 10.0]])
@pytest.mark.mechanism
@pytest.mark.recurrent_transfer_mechanism
@pytest.mark.benchmark(group="RecurrentTransferMechanism")
@pytest.mark.parametrize('mode', ['Python',
pytest.param('LLVM', marks=pytest.mark.llvm),
pytest.param('PTX', marks=[pytest.mark.llvm, pytest.mark.cuda])])
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_ports = [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]])
# def test_recurrent_mech_inputs_list_of_fns(self):
# R = RecurrentTransferMechanism(
# name='R',
# size=4,
# integrator_mode=True
# )
# val = R.execute([Linear().execute(), NormalDist().execute(), Exponential().execute(), ExponentialDist().execute()])
# expected = [[np.array([0.]), 0.4001572083672233, np.array([1.]), 0.7872011523172707]]
# assert len(val) == len(expected) == 1
# assert len(val[0]) == len(expected[0])
# for i in range(len(val[0])):
# np.testing.assert_allclose(val[0][i], expected[0][i])
@pytest.mark.mechanism
@pytest.mark.recurrent_transfer_mechanism
@pytest.mark.benchmark(group="RecurrentTransferMechanism")
@pytest.mark.parametrize('mode', ['Python',
pytest.param('LLVM', marks=pytest.mark.llvm),
pytest.param('PTX', marks=[pytest.mark.llvm, pytest.mark.cuda])])
def test_recurrent_mech_no_inputs(self, benchmark, mode):
R = RecurrentTransferMechanism(
name='R'
)
np.testing.assert_allclose(R.defaults.variable, [[0]])
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
val = EX([10])
benchmark(EX, [1])
np.testing.assert_allclose(val, [[10.]])
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)
def test_recurrent_mech_var_list_of_strings(self):
with pytest.raises(UtilitiesError) as error_text:
R = RecurrentTransferMechanism(
name='R',
default_variable=['a', 'b', 'c', 'd'],
integrator_mode=True
)
assert "has non-numeric entries" in str(error_text.value)
def test_recurrent_mech_inputs_mismatched_with_default_longer(self):
with pytest.raises(MechanismError) as error_text:
R = RecurrentTransferMechanism(
name='R',
size=4
)
R.execute([1, 2, 3, 4, 5])
assert "does not match required length" in str(error_text.value)
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)
class TestRecurrentTransferMechanismMatrix:
@pytest.mark.parametrize("matrix", MATRIX_KEYWORD_VALUES)
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]))
@pytest.mark.parametrize("matrix", [np.matrix('1 2; 3 4'), np.array([[1, 2], [3, 4]]), [[1, 2], [3, 4]], '1 2; 3 4'])
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)
def test_recurrent_mech_matrix_auto_spec(self):
R = RecurrentTransferMechanism(
name='R',
size=3,
auto=2
)
assert isinstance(R.matrix, np.ndarray)
np.testing.assert_allclose(R.matrix, [[2, 1, 1], [1, 2, 1], [1, 1, 2]])
np.testing.assert_allclose(run_twice_in_system(R, [1, 2, 3], [10, 11, 12]), [17, 19, 21])
def test_recurrent_mech_matrix_hetero_spec(self):
R = RecurrentTransferMechanism(
name='R',
size=3,
hetero=-1
)
# (7/28/17 CW) these numbers assume that execute() leaves its value in the outputPort of the mechanism: if
# the behavior of execute() changes, feel free to change these numbers
val = R.execute([-1, -2, -3])
np.testing.assert_allclose(val, [[-1, -2, -3]])
assert isinstance(R.matrix, np.ndarray)
np.testing.assert_allclose(R.matrix, [[0, -1, -1], [-1, 0, -1], [-1, -1, 0]])
# Execution 1:
# Recurrent input = [5, 4, 3] | New input = [1, 2, 3] | Total input = [6, 6, 6]
# Output 1 = [6, 6, 6]
# Execution 2:
# Recurrent input =[-12, -12, -12] | New input = [10, 11, 12] | Total input = [-2, -1, 0]
# Output 2 = [-2, -1, 0]
np.testing.assert_allclose(run_twice_in_system(R, [1, 2, 3], [10, 11, 12]), [-2., -1., 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]])
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)
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)
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]])
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]])
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]]
)
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]]
)
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]]
)
def test_recurrent_mech_auto_hetero_matrix_spec_v2(self):
R = RecurrentTransferMechanism(
name='R',
size=4,
auto=[3],
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,
[[3, -3, -2, -1], [-4, 3, -2, -1], [-4, -3, 3, -1], [-4, -3, -2, 3]]
)
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]]
)
def test_recurrent_mech_matrix_too_large(self):
with pytest.raises(RecurrentTransferError) as error_text:
R = RecurrentTransferMechanism(
name='R',
size=3,
matrix=[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
)
assert "must be the same as its variable" in str(error_text.value)
def test_recurrent_mech_matrix_too_small(self):
with pytest.raises(RecurrentTransferError) as error_text:
R = RecurrentTransferMechanism(
name='R',
size=5,
matrix=[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
)
assert "must be the same as its variable" in str(error_text.value)
def test_recurrent_mech_matrix_strings(self):
with pytest.raises(UtilitiesError) as error_text:
R = RecurrentTransferMechanism(
name='R',
size=4,
matrix=[['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd']]
)
assert "has non-numeric entries" in str(error_text.value)
def test_recurrent_mech_matrix_nonsquare(self):
with pytest.raises(RecurrentTransferError) as error_text:
R = RecurrentTransferMechanism(
name='R',
size=4,
matrix=[[1, 3]]
)
assert "must be square" in str(error_text.value)
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)
class TestRecurrentTransferMechanismFunction:
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)])
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)])
def test_recurrent_mech_function_custom(self):
# I don't know how to do this at the moment but it seems highly important.
pass
def test_recurrent_mech_normal_fun(self):
with pytest.raises(TransferError) as error_text:
R = RecurrentTransferMechanism(
name='R',
default_variable=[0, 0, 0, 0],
function=NormalDist(),
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)
def test_recurrent_mech_reinforcement_fun(self):
with pytest.raises(TransferError) as error_text:
R = RecurrentTransferMechanism(
name='R',
default_variable=[0, 0, 0, 0],
function=Reinforcement(),
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)
def test_recurrent_mech_integrator_fun(self):
with pytest.raises(TransferError) as error_text:
R = RecurrentTransferMechanism(
name='R',
default_variable=[0, 0, 0, 0],
function=AccumulatorIntegrator(),
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)
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)
class TestRecurrentTransferMechanismTimeConstant:
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]])
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
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]])
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]])
# (7/28/17 CW): the below are used because it's good to test System and Process anyways, and because the recurrent
# projection won't get executed if we only use the execute() method of Mechanism: thus, to test it we must use a System
def run_twice_in_system(mech, input1, input2=None):
if input2 is None:
input2 = input1
simple_prefs = {REPORT_OUTPUT_PREF: False, VERBOSE_PREF: False}
simple_process = Process(size=mech.size[0], pathway=[mech], name='simple_process')
simple_system = System(processes=[simple_process], name='simple_system', prefs=simple_prefs)
first_output = simple_system.run(inputs={mech: [input1]})
second_output = simple_system.run(inputs={mech: [input2]})
return second_output[1][0]
class TestRecurrentTransferMechanismInProcess:
simple_prefs = {REPORT_OUTPUT_PREF: False, VERBOSE_PREF: False}
def test_recurrent_mech_transfer_mech_process_three_runs(self):
# this test ASSUMES that the ParameterPort for auto and hetero is updated one run-cycle AFTER they are set by
# lines by `R.auto = 0`. If this (potentially buggy) behavior is changed, then change these values
R = RecurrentTransferMechanism(
size=4,
auto=0,
hetero=-1
)
T = TransferMechanism(
size=3,
function=Linear
)
p = Process(size=4, pathway=[R, T], prefs=TestRecurrentTransferMechanismInSystem.simple_prefs)
p.run(inputs={R: [[1, 2, 3, 4]]})
np.testing.assert_allclose(R.parameters.value.get(p), [[1., 2., 3., 4.]])
np.testing.assert_allclose(T.parameters.value.get(p), [[10., 10., 10.]])
p.run(inputs={R: [[5, 6, 7, 8]]})
np.testing.assert_allclose(R.parameters.value.get(p), [[-4, -2, 0, 2]])
np.testing.assert_allclose(T.parameters.value.get(p), [[-4, -4, -4]])
p.run(inputs={R: [[-1, 2, -2, 5.5]]})
np.testing.assert_allclose(R.parameters.value.get(p), [[-1.0, 4.0, 2.0, 11.5]])
np.testing.assert_allclose(T.parameters.value.get(p), [[16.5, 16.5, 16.5]])
def test_transfer_mech_process_matrix_change(self):
from psyneulink.core.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()
# removed this assert, because before the changes of most_recent_execution_id -> most_recent_context
# proj.matrix referred to the 'Process-0' execution_id, even though it was last executed with None
# 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_recurrent_mech_process_matrix_change(self):
R = RecurrentTransferMechanism(
size=4,
auto=1,
hetero=-1)
T = TransferMechanism(
size=4,
function=Linear)
p = Process(size=4, pathway=[T, R], prefs=TestRecurrentTransferMechanismInSystem.simple_prefs)
R.matrix = [[2, 0, 1, 3]] * 4
p.run(inputs={T: [[1, 2, 3, 4]]})
np.testing.assert_allclose(T.parameters.value.get(p), [[1, 2, 3, 4]])
np.testing.assert_allclose(R.parameters.value.get(p), [[1, 2, 3, 4]])
p.run(inputs={T: [[1, 3, 2, 5]]})
np.testing.assert_allclose(R.recurrent_projection.matrix, [[2, 0, 1, 3]] * 4)
np.testing.assert_allclose(T.parameters.value.get(p), [[1, 3, 2, 5]])
np.testing.assert_allclose(R.parameters.value.get(p), [[21, 3, 12, 35]])
# this test must wait until we create a property such that R.recurrent_projection.matrix sets R.auto and R.hetero
def test_recurrent_mech_process_proj_matrix_change(self):
R = RecurrentTransferMechanism(
size=4,
auto=1,
hetero=-1)
T = TransferMechanism(
size=4,
function=Linear)
p = Process(size=4, pathway=[T, R], prefs=TestRecurrentTransferMechanismInSystem.simple_prefs)
R.recurrent_projection.matrix = [[2, 0, 1, 3]] * 4
p.run(inputs={T: [[1, 2, 3, 4]]})
np.testing.assert_allclose(T.parameters.value.get(p), [[1, 2, 3, 4]])
np.testing.assert_allclose(R.parameters.value.get(p), [[1, 2, 3, 4]])
p.run(inputs={T: [[1, 3, 2, 5]]})
np.testing.assert_allclose(R.recurrent_projection.matrix, [[2, 0, 1, 3]] * 4)
np.testing.assert_allclose(T.parameters.value.get(p), [[1, 3, 2, 5]])
np.testing.assert_allclose(R.parameters.value.get(p), [[21, 3, 12, 35]])
class TestRecurrentTransferMechanismInSystem:
simple_prefs = {REPORT_OUTPUT_PREF: False, VERBOSE_PREF: False}
def test_recurrent_mech_transfer_mech_system_three_runs(self):
# this test ASSUMES that the ParameterPort for auto and hetero is updated one run-cycle AFTER they are set by
# lines by `R.auto = 0`. If this (potentially buggy) behavior is changed, then change these values
R = RecurrentTransferMechanism(
size=4,
auto=0,
hetero=-1)
T = TransferMechanism(
size=3,
function=Linear)
p = Process(size=4, pathway=[R, T], prefs=TestRecurrentTransferMechanismInSystem.simple_prefs)
s = System(processes=[p], prefs=TestRecurrentTransferMechanismInSystem.simple_prefs)
s.run(inputs={R: [[1, 2, 3, 4]]})
np.testing.assert_allclose(R.parameters.value.get(s), [[1., 2., 3., 4.]])
np.testing.assert_allclose(T.parameters.value.get(s), [[10., 10., 10.]])
s.run(inputs={R: [[5, 6, 7, 8]]})
np.testing.assert_allclose(R.parameters.value.get(s), [[-4, -2, 0, 2]])
np.testing.assert_allclose(T.parameters.value.get(s), [[-4, -4, -4]])
s.run(inputs={R: [[-1, 2, -2, 5.5]]})
np.testing.assert_allclose(R.parameters.value.get(s), [[-1.0, 4.0, 2.0, 11.5]])
np.testing.assert_allclose(T.parameters.value.get(s), [[16.5, 16.5, 16.5]])
@pytest.mark.xfail(reason='Unsure if this is correct behavior - see note for _recurrent_transfer_mechanism_matrix_setter')
def test_recurrent_mech_system_auto_change(self):
R = RecurrentTransferMechanism(
size=4,
auto=[1, 2, 3, 4],
hetero=-1)
T = TransferMechanism(
size=3,
function=Linear)
p = Process(size=4, pathway=[R, T], prefs=TestRecurrentTransferMechanismInSystem.simple_prefs)
s = System(processes=[p], prefs=TestRecurrentTransferMechanismInSystem.simple_prefs)
s.run(inputs={R: [[1, 2, 3, 4]]})
np.testing.assert_allclose(R.parameters.value.get(s), [[1., 2., 3., 4.]])
np.testing.assert_allclose(T.parameters.value.get(s), [[10., 10., 10.]])
R.parameters.auto.set(0, s)
s.run(inputs={R: [[5, 6, 7, 8]]})
np.testing.assert_allclose(R.parameters.value.get(s), [[-4, -2, 0, 2]])
np.testing.assert_allclose(T.parameters.value.get(s), [[-4, -4, -4]])
R.recurrent_projection.parameters.auto.set([1, 1, 2, 4], s)
s.run(inputs={R: [[12, 11, 10, 9]]})
np.testing.assert_allclose(R.parameters.value.get(s), [[8, 11, 14, 23]])
np.testing.assert_allclose(T.parameters.value.get(s), [[56, 56, 56]])
@pytest.mark.xfail(reason='Unsure if this is correct behavior - see note for _recurrent_transfer_mechanism_matrix_setter')
def test_recurrent_mech_system_hetero_change(self):
R = RecurrentTransferMechanism(
size=4,
auto=[1, 2, 3, 4],
hetero=[[-1, -2, -3, -4]] * 4)
T = TransferMechanism(
size=5,
function=Linear)
p = Process(size=4, pathway=[R, T], prefs=TestRecurrentTransferMechanismInSystem.simple_prefs)
s = System(processes=[p], prefs=TestRecurrentTransferMechanismInSystem.simple_prefs)
s.run(inputs={R: [[1, 2, 3, -0.5]]})
np.testing.assert_allclose(R.parameters.value.get(s), [[1., 2., 3., -0.5]])
np.testing.assert_allclose(T.parameters.value.get(s), [[5.5, 5.5, 5.5, 5.5, 5.5]])
R.parameters.hetero.set(0, s)
s.run(inputs={R: [[-1.5, 0, 1, 2]]})
np.testing.assert_allclose(R.parameters.value.get(s), [[-.5, 4, 10, 0]])
np.testing.assert_allclose(T.parameters.value.get(s), [[13.5, 13.5, 13.5, 13.5, 13.5]])
R.parameters.hetero.set(np.array([[-1, 2, 3, 1.5]] * 4), s)
s.run(inputs={R: [[12, 11, 10, 9]]})
np.testing.assert_allclose(R.parameters.value.get(s), [[-2.5, 38, 50.5, 29.25]])
np.testing.assert_allclose(T.parameters.value.get(s), [[115.25, 115.25, 115.25, 115.25, 115.25]])
@pytest.mark.xfail(reason='Unsure if this is correct behavior - see note for _recurrent_transfer_mechanism_matrix_setter')
def test_recurrent_mech_system_auto_and_hetero_change(self):
R = RecurrentTransferMechanism(
size=4,
auto=[1, 2, 3, 4],
hetero=[[-1, -2, -3, -4]] * 4)
T = TransferMechanism(
size=5,
function=Linear)
p = Process(size=4, pathway=[R, T], prefs=TestRecurrentTransferMechanismInSystem.simple_prefs)
s = System(processes=[p], prefs=TestRecurrentTransferMechanismInSystem.simple_prefs)
s.run(inputs={R: [[1, 2, 3, -0.5]]})
np.testing.assert_allclose(R.parameters.value.get(s), [[1., 2., 3., -0.5]])
np.testing.assert_allclose(T.parameters.value.get(s), [[5.5, 5.5, 5.5, 5.5, 5.5]])
R.parameters.hetero.set(0, s)
s.run(inputs={R: [[-1.5, 0, 1, 2]]})
np.testing.assert_allclose(R.parameters.value.get(s), [[-.5, 4, 10, 0]])
np.testing.assert_allclose(T.parameters.value.get(s), [[13.5, 13.5, 13.5, 13.5, 13.5]])
R.parameters.auto.set([0, 0, 0, 0], s)
s.run(inputs={R: [[12, 11, 10, 9]]})
np.testing.assert_allclose(R.parameters.value.get(s), [[12, 11, 10, 9]])
np.testing.assert_allclose(T.parameters.value.get(s), [[42, 42, 42, 42, 42]])
@pytest.mark.xfail(reason='Unsure if this is correct behavior - see note for _recurrent_transfer_mechanism_matrix_setter')
def test_recurrent_mech_system_matrix_change(self):
R = RecurrentTransferMechanism(
size=4,
auto=1,
hetero=-1)
T = TransferMechanism(
size=4,
function=Linear)
p = Process(size=4, pathway=[T, R], prefs=TestRecurrentTransferMechanismInSystem.simple_prefs)
s = System(processes=[p], prefs=TestRecurrentTransferMechanismInSystem.simple_prefs)
R.parameters.matrix.set([[2, 0, 1, 3]] * 4, s)
s.run(inputs={T: [[1, 2, 3, 4]]})
np.testing.assert_allclose(T.parameters.value.get(s), [[1, 2, 3, 4]])
np.testing.assert_allclose(R.parameters.value.get(s), [[1, 2, 3, 4]])
s.run(inputs={T: [[1, 3, 2, 5]]})
np.testing.assert_allclose(R.recurrent_projection.parameters.matrix.get(s), [[2, 0, 1, 3]] * 4)
np.testing.assert_allclose(T.parameters.value.get(s), [[1, 3, 2, 5]])
np.testing.assert_allclose(R.parameters.value.get(s), [[21, 3, 12, 35]])
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_port.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]
]
)
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)
def test_recurrent_mech_with_learning_warning(self):
R = RecurrentTransferMechanism(size=2,
function=Linear,
matrix=np.full((2, 2), 0.1),
enable_learning=True)
P = Process(pathway=[R])
with pytest.warns(UserWarning) as record:
S = System(processes=[P],
prefs={VERBOSE_PREF: True})
# hack to find a specific warning (12 warnings are generated by the System construction)
correct_message_found = False
for warning in record:
if "This is okay if the learning (e.g. Hebbian learning) does not need a target." in str(warning.message):
correct_message_found = True
break
assert correct_message_found
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.get_mod_matrix(S),
[
[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_port.parameters.value.get(S), [1.18518086, 0.0, 1.18518086, 0.0])
# Reset state so learning of new pattern is "uncontaminated" by activity from previous one
R.output_port.parameters.value.set([0, 0, 0, 0], S, override=True)
inputs_dict = {R:[0,1,0,1]}
S.run(num_trials=4,
inputs=inputs_dict)
np.testing.assert_allclose(
R.recurrent_projection.get_mod_matrix(S),
[
[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. ]
]