-
Notifications
You must be signed in to change notification settings - Fork 29
/
dndtesting.py
210 lines (164 loc) · 5.76 KB
/
dndtesting.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
# -*- coding: utf-8 -*-
"""ContentAddressableMemorytesting.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1XjXuOsceGgDSyGsSF8OVQvQS4h-L-U4F
"""
# !pip install --upgrade psyneulink
import psyneulink as pnl
import numpy as np
print(pnl.__version__)
# network params
n_input = 2
n_hidden = 5
n_output = 1
max_entries = 7
# training params
num_epochs = 3
learning_rate = .1
wts_init_scale = .1
# layers
input = pnl.TransferMechanism(
name='input',
default_variable=np.zeros(n_input)
)
hidden = pnl.TransferMechanism(
name='hidden',
default_variable=np.zeros(n_hidden),
function=pnl.Logistic()
)
output = pnl.TransferMechanism(
name='output',
default_variable=np.zeros(n_output),
function=pnl.Logistic()
)
# weights
w_ih = pnl.MappingProjection(
name='input_to_hidden',
matrix=np.random.randn(n_input, n_hidden) * wts_init_scale,
sender=input,
receiver=hidden
)
w_ho = pnl.MappingProjection(
name='hidden_to_output',
matrix=np.random.randn(n_hidden, n_output) * wts_init_scale,
sender=hidden,
receiver=output
)
# ContentAddressableMemory
ContentAddressableMemory = pnl.EpisodicMemoryMechanism(
cue_size=n_hidden, assoc_size=n_hidden,
name='ContentAddressableMemory'
)
w_hdc = pnl.MappingProjection(
name='hidden_to_cue',
matrix=np.random.randn(n_hidden, n_hidden) * wts_init_scale,
sender=hidden,
receiver=ContentAddressableMemory.input_ports[pnl.CUE_INPUT]
)
w_hda = pnl.MappingProjection(
name='hidden_to_assoc',
matrix=np.random.randn(n_hidden, n_hidden) * wts_init_scale,
sender=hidden,
receiver=ContentAddressableMemory.input_ports[pnl.ASSOC_INPUT]
)
w_dh = pnl.MappingProjection(
name='em_to_hidden',
matrix=np.random.randn(n_hidden, n_hidden) * wts_init_scale,
sender=ContentAddressableMemory,
receiver=hidden
)
comp = pnl.Composition(name='xor')
# add all nodes
all_nodes = [input, hidden, output, ContentAddressableMemory]
for node in all_nodes:
comp.add_node(node)
# input-hidden-output pathway
comp.add_projection(sender=input, projection=w_ih, receiver=hidden)
comp.add_projection(sender=hidden, projection=w_ho, receiver=output)
# conneciton, ContentAddressableMemory
comp.add_projection(sender=ContentAddressableMemory, projection=w_dh, receiver=hidden)
comp.add_projection(
sender=hidden,
projection=w_hdc,
receiver=ContentAddressableMemory.input_ports[pnl.CUE_INPUT]
)
comp.add_projection(
sender=hidden,
projection=w_hda,
receiver=ContentAddressableMemory.input_ports[pnl.ASSOC_INPUT]
)
# show graph
comp.show_graph(show_node_structure=True)
# # comp.show()
# # the required inputs for ContentAddressableMemory
# print('ContentAddressableMemory input_ports: ', ContentAddressableMemory.input_ports.names)
#
# # currently, ContentAddressableMemory receive info from the following node
# print('ContentAddressableMemory receive: ')
# for ContentAddressableMemory_input in ContentAddressableMemory.input_ports.names:
# afferents = ContentAddressableMemory.input_ports[ContentAddressableMemory_input].path_afferents
# if len(afferents) == 0:
# print(f'- {ContentAddressableMemory_input}: NA')
# else:
# sending_node_name = afferents[0].sender.owner.name
# print(f'- {ContentAddressableMemory_input}: {sending_node_name}')
#
# print('ContentAddressableMemory cue input: ', ContentAddressableMemory.input_ports.names)
#
# print('hidden receive: ')
# for hidden_afferent in hidden.input_ports[0].path_afferents:
# print('- ', hidden_afferent.sender.owner.name)
# comp.show()
# the required inputs for ContentAddressableMemory
print('ContentAddressableMemory input_ports: ', ContentAddressableMemory.input_ports.names)
# currently, ContentAddressableMemory receive info from the following node
print('ContentAddressableMemory receive: ')
for ContentAddressableMemory_input in ContentAddressableMemory.input_ports.names:
afferents = ContentAddressableMemory.input_ports[ContentAddressableMemory_input].path_afferents
if len(afferents) == 0:
print(f'- {ContentAddressableMemory_input}: NA')
else:
sending_node_name = afferents[0].sender.owner.name
print(f'- {ContentAddressableMemory_input}: {sending_node_name}')
print('ContentAddressableMemory cue input: ', ContentAddressableMemory.input_ports.names)
print('hidden receive: ')
for hidden_afferent in hidden.input_ports[0].path_afferents:
print('- ', hidden_afferent.sender.owner.name)
print(ContentAddressableMemory.output_ports.names)
print(ContentAddressableMemory.output_ports.values)
print(input)
#comp.run([1,1])
#print(ContentAddressableMemory.values)
#ContentAddressableMemory.dict.insert_memory([1,1])
print(ContentAddressableMemory.output_ports.values)
input = [0,1]
print(ContentAddressableMemory.input_ports)
print(hidden.value)
print(output.value)
#comp.run(input)
print(comp.run([1,1]))
print(comp.run([2,2]))
print(comp.run([100,100]))
print(comp.run([10000,100000]))
#comp.output.value
input_dict = [[0,1], [1,2], [2,3], [3,4], [4,5]]
result = comp.run(inputs=input_dict, context = 5) #, do_logging=True)
print(ContentAddressableMemory.input_values)
print(hidden.value)
print(ContentAddressableMemory.output_values)
result1 = comp.run([0,1])
result2 = comp.run([0,2])
print(result1)
print(result2)
print(output.output_values)
ContentAddressableMemory.CUE_INPUT = [1,1,1,1,1]
ContentAddressableMemory.ASSOC_INPUT = [1,2,3,4,5]
print(ContentAddressableMemory.input_values)
print(ContentAddressableMemory.output_values)
# ContentAddressableMemory.function.add_to_memory([[[100,101,102,103,104],[23,24,25,26,27]]], context=5)
# assert True
# ContentAddressableMemory.dict.insert_memory({0,1})
#
# ContentAddressableMemory.get_memory(0)
"""cant figure out get&store memory functions"""