-
Notifications
You must be signed in to change notification settings - Fork 29
/
Botvinick Model Composition.py
212 lines (176 loc) · 9.18 KB
/
Botvinick Model Composition.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
import psyneulink as pnl
import numpy as np
colors_input_layer = pnl.TransferMechanism(size=3,
function=pnl.Linear,
name='COLORS_INPUT')
words_input_layer = pnl.TransferMechanism(size=3,
function=pnl.Linear,
name='WORDS_INPUT')
task_input_layer = pnl.TransferMechanism(size=2,
function=pnl.Linear,
name='TASK_INPUT')
# Task layer, tasks: ('name the color', 'read the word')
task_layer = pnl.RecurrentTransferMechanism(size=2,
function=pnl.Logistic(),
hetero=-2,
integrator_mode=True,
integration_rate=0.01,
name='TASK_LAYER')
# Hidden layer
# colors: ('red','green', 'neutral') words: ('RED','GREEN', 'NEUTRAL')
colors_hidden_layer = pnl.RecurrentTransferMechanism(size=3,
function=pnl.Logistic(x_0=4.0), # bias 4.0 is -4.0 in the paper see Docs for description
integrator_mode=True,
hetero=-2,
integration_rate=0.01, # cohen-huston text says 0.01
name='COLORS_HIDDEN')
words_hidden_layer = pnl.RecurrentTransferMechanism(size=3,
function=pnl.Logistic(x_0=4.0),
integrator_mode=True,
hetero=-2,
integration_rate=0.01,
name='WORDS_HIDDEN')
# Response layer, responses: ('red', 'green')
response_layer = pnl.RecurrentTransferMechanism(size=2,
function=pnl.Logistic(),
hetero=-2.0,
integrator_mode=True,
integration_rate=0.01,
output_ports = [pnl.RESULT,
{pnl.NAME: 'DECISION_ENERGY',
pnl.VARIABLE: (pnl.OWNER_VALUE,0),
pnl.FUNCTION: pnl.Stability(
default_variable = np.array([0.0, 0.0]),
metric = pnl.ENERGY,
matrix = np.array([[0.0, -4.0],
[-4.0, 0.0]]))}],
name='RESPONSE', )
# Mapping projections---------------------------------------------------------------------------------------------------
color_input_weights = pnl.MappingProjection(matrix=np.array([[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0]]))
word_input_weights = pnl.MappingProjection(matrix=np.array([[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0]]))
task_input_weights = pnl.MappingProjection(matrix=np.array([[1.0, 0.0],
[0.0, 1.0]]))
color_task_weights = pnl.MappingProjection(matrix=np.array([[4.0, 0.0],
[4.0, 0.0],
[4.0, 0.0]]))
task_color_weights = pnl.MappingProjection(matrix=np.array([[4.0, 4.0, 4.0],
[0.0, 0.0, 0.0]]))
response_color_weights = pnl.MappingProjection(matrix=np.array([[1.5, 0.0, 0.0],
[0.0, 1.5, 0.0]]))
response_word_weights = pnl.MappingProjection(matrix=np.array([[2.5, 0.0, 0.0],
[0.0, 2.5, 0.0]]))
color_response_weights = pnl.MappingProjection(matrix=np.array([[1.5, 0.0],
[0.0, 1.5],
[0.0, 0.0]]))
word_response_weights = pnl.MappingProjection(matrix=np.array([[2.5, 0.0],
[0.0, 2.5],
[0.0, 0.0]]))
word_task_weights = pnl.MappingProjection(matrix=np.array([[0.0, 4.0],
[0.0, 4.0],
[0.0, 4.0]]))
task_word_weights = pnl.MappingProjection(matrix=np.array([[0.0, 0.0, 0.0],
[4.0, 4.0, 4.0]]))
# CREATE Composition
comp = pnl.Composition()
# Add mechanisms
comp.add_node(colors_input_layer)
comp.add_node(colors_hidden_layer)
comp.add_node(words_input_layer)
comp.add_node(words_hidden_layer)
comp.add_node(task_input_layer)
comp.add_node(task_layer)
comp.add_node(response_layer)
# Add projections
comp.add_projection(task_input_weights, task_input_layer, task_layer)
# Color process
comp.add_projection(color_input_weights, colors_input_layer, colors_hidden_layer)
comp.add_projection(color_response_weights, colors_hidden_layer, response_layer)
comp.add_projection(response_color_weights, response_layer, colors_hidden_layer)
# Word process
comp.add_projection(word_input_weights, words_input_layer, words_hidden_layer)
comp.add_projection(word_response_weights, words_hidden_layer, response_layer)
comp.add_projection(response_word_weights, response_layer, words_hidden_layer)
# Color task process
comp.add_projection(task_color_weights, task_layer, colors_hidden_layer)
comp.add_projection(color_task_weights, colors_hidden_layer, task_layer)
# Word task process
comp.add_projection(task_word_weights, task_layer, words_hidden_layer)
comp.add_projection(word_task_weights, words_hidden_layer, task_layer)
def trial_dict(red_color, green_color, neutral_color, red_word, green_word, neutral_word, CN, WR):
trialdict = {
colors_input_layer: [red_color, green_color, neutral_color],
words_input_layer: [red_word, green_word, neutral_word],
task_input_layer: [CN, WR]
}
return trialdict
# Define initialization trials separately
CN_trial_initialize_input = trial_dict(0, 0, 0, 0, 0, 0, 1, 0)
#red_color, green color, red_word, green word, CN, WR
CN_incongruent_trial_input = trial_dict(1, 0, 0, 0, 1, 0, 1, 0)
#red_color, green color, red_word, green word, CN, WR
CN_congruent_trial_input = trial_dict(1, 0, 0, 1, 0, 0, 1, 0)
#red_color, green color, red_word, green word, CN, WR
CN_control_trial_input = trial_dict(1, 0, 0, 0, 0, 1, 1, 0)
#red_color, green color, red_word, green word, CN, WR
Stimulus = [[CN_trial_initialize_input, CN_congruent_trial_input],
[CN_trial_initialize_input, CN_incongruent_trial_input],
[CN_trial_initialize_input, CN_control_trial_input]]
# should be 500 and 1000
ntrials0 = 5
ntrials = 10
comp._analyze_graph()
comp.show_graph()
def run(bin_execute):
results = []
for stim in Stimulus:
# RUN the SYSTEM to initialize ---------------------------------------
comp.run(inputs=stim[0], num_trials=ntrials0, bin_execute=bin_execute)
comp.run(inputs=stim[1], num_trials=ntrials, bin_execute=bin_execute)
# reset after condition was run
colors_hidden_layer.reset([[0, 0, 0]], context=comp)
words_hidden_layer.reset([[0, 0, 0]], context=comp)
response_layer.reset([[0, 0]], context=comp)
task_layer.reset([[0, 0]], context=comp)
# Comp results include concatenation of both the above runs
results.append(comp.results.copy())
comp.reset()
comp.results = []
return results
pnlv_graphics_spec = {
"components": {
"nodes": {
"COLORS_HIDDEN": {
"x": 399,
"y": 145
},
"WORDS_INPUT": {
"x": 887,
"y": 398
},
"TASK_INPUT": {
"x": 670,
"y": 435
},
"WORDS_HIDDEN": {
"x": 877,
"y": 141
},
"RESPONSE": {
"x": 674,
"y": 50
},
"COLORS_INPUT": {
"x": 412,
"y": 403
},
"TASK_LAYER": {
"x": 674,
"y": 278
}
}
}
}