-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatternStructure.py
221 lines (180 loc) · 7.96 KB
/
PatternStructure.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
import random
from dataclasses import dataclass
import numpy as np
class InteractionKernel:
def __init__(self, gamma, xi):
self.gamma = gamma
self.xi = xi
def symmetric_kernel(self, x1, x2):
'''
Symmetric part of the interaction kernel
'''
d = x1-x2
return np.exp(-d**2/self.xi)
def antisymmetric_kernel(self, x1, x2):
'''
Anti-symmetric part of the interaction kernel
'''
d = x1-x2
return -(d/self.xi)*np.exp(-d**2/self.xi)
def kernel(self, x1, x2):
'''
Total interaction kernel
'''
return self.symmetric_kernel(x1, x2)-self.gamma*self.antisymmetric_kernel(x1, x2)
@dataclass
class PatternStructure:
n_cells: int
n_patterns: int
cells_per_pattern: int
n_chains: int
patterns_per_chain: int
kernel: InteractionKernel
build_structure: bool = True
patterns: list = None
chains: list = None
chain_transitions: np.array = None
pattern_matrices: list = None
pattern_matrices_symmetric: list = None
autoassociative_matrix: np.array = None
heteroassociative_matrix: np.array = None
interaction_matrix: np.array = None
def generate_patterns(self, patterns=None):
'''
If patterns are not given, randomly generates the patterns of the memory structure
by random permutations of the field centers on the line (0,1).
If patterns are given assigns the given patterns to the memory structure.
'''
if patterns == None:
self.patterns = []
field_centers = np.linspace(0, 1, self.cells_per_pattern)
for p in range(self.n_patterns):
pattern_cells = random.sample(
range(self.n_cells), self.cells_per_pattern)
pattern = {}
for i, cell in enumerate(pattern_cells):
pattern[cell] = field_centers[i]
self.patterns.append(pattern)
else:
self.patterns = patterns
def generate_chains(self, chains=None):
'''
If chains are not given, randomly generates the pattern chains of the memory structure
by randomly drawing from the patterns in the structure.
If chains are given assigns the given chains to the memory structure.
'''
if chains == None:
self.chains = []
for i in range(self.n_chains):
self.chains.append(random.sample(
range(self.n_patterns), self.patterns_per_chain))
else:
self.chains = chains
self.chain_transitions = np.zeros((self.n_patterns, self.n_patterns))
for chain in self.chains:
for i in range(1, len(chain)):
self.chain_transitions[chain[i-1], chain[i]] += 1
def build_interactions(self):
'''
Builds the total interaction matrix by summing the autoassociative
and heteroassociative components
'''
if self.autoassociative_matrix == None:
#print("Building autoassociative interactions ...")
self._build_autoassociative_matrix_()
if self.heteroassociative_matrix == None:
#print("Building heteroassociative interactions ...")
self._build_heteroassociative_matrix_()
self.interaction_matrix = self.autoassociative_matrix+self.heteroassociative_matrix
def _build_pattern_matrix_(self, pattern):
'''
Builds the interaction matrix of the specified pattern.
The pattern-specific interaction matrices are mostly used in the calculations
of the order parameters
'''
matrix = np.zeros((self.n_cells, self.n_cells))
for cell1 in pattern.keys():
for cell2 in pattern.keys():
x1 = pattern[cell1]
x2 = pattern[cell2]
if cell1 != cell2:
matrix[cell1, cell2] = self.kernel.kernel(x1, x2)
return matrix
def _build_pattern_matrix_symmetric_(self, pattern):
'''
Builds the symmetric part of interaction matrix of the specified pattern.
The pattern-specific symmetric interaction matrices are mostly used in the calculations
of the order parameters
'''
matrix = np.zeros((self.n_cells, self.n_cells))
for cell1 in pattern.keys():
for cell2 in pattern.keys():
x1 = pattern[cell1]
x2 = pattern[cell2]
if cell1 != cell2:
matrix[cell1, cell2] = self.kernel.symmetric_kernel(x1, x2)
return matrix
def _build_autoassociative_matrix_(self):
'''
Builds the autoassociative matrix by computing and summing the contribution of each pattern.
Pattern-specific matrices are also saved in `self.pattern_matrices` and `self.pattern__matrices_symmetric`.
'''
self.autoassociative_matrix = np.zeros((self.n_cells, self.n_cells))
self.pattern_matrices = []
self.pattern_matrices_symmetric = []
for pattern in self.patterns:
pattern_mat = self._build_pattern_matrix_(pattern)
pattern_symmetric_mat = self._build_pattern_matrix_symmetric_(
pattern)
self.autoassociative_matrix += pattern_mat
self.pattern_matrices.append(pattern_mat)
self.pattern_matrices_symmetric.append(pattern_symmetric_mat)
self.autoassociative_matrix = self.autoassociative_matrix / \
len(self.patterns)
def _build_heteroassociative_matrix_(self):
'''
Builds heteroassociative interaction matrix by linking together the end of
each pattern to the beginning of each pattern that follows the first in any of the chains
'''
self.heteroassociative_matrix = np.zeros((self.n_cells, self.n_cells))
for i, pattern1 in enumerate(self.patterns):
for j, pattern2 in enumerate(self.patterns):
if self.chain_transitions[i, j] > 0:
for cell1 in pattern1.keys():
for cell2 in pattern2.keys():
x1 = pattern1[cell1]
# offsets so that pattern 2 begins at end of 1
x2 = pattern2[cell2]+1
self.heteroassociative_matrix[cell1,
cell2] += self.kernel.kernel(x1, x2)
self.heteroassociative_matrix[cell2,
cell1] += self.kernel.kernel(x2, x1)
self.heteroassociative_matrix = self.heteroassociative_matrix / \
len(self.patterns)
# FUNCTIONS
def pattern_overlap_matrix(pattern_structure: PatternStructure) -> np.array:
'''
Computes the pattern-pattern overlap for all pattern pairs in the memory
structure. Overlaps are defined as the number of common cells between the patterns
over the total number of cells in the each pattern.
'''
matrix = np.zeros((pattern_structure.n_patterns,
pattern_structure.n_patterns))
for i in range(pattern_structure.n_patterns):
for j in range(i+1, pattern_structure.n_patterns):
cells1 = set([c for c in pattern_structure.patterns[i].keys()])
cells2 = set([c for c in pattern_structure.patterns[j].keys()])
intersection = len(cells1.intersection(cells2))
matrix[i, j] = matrix[j, i] = float(intersection) / \
float(pattern_structure.cells_per_pattern)
return matrix
def build_correlated_activity(pattern_struct, pattern_num, position=0.5):
'''
Produces an activity configuration correlated with the given pattern in the given position.
'''
V = np.zeros(pattern_struct.n_cells)
pattern = pattern_struct.patterns[pattern_num]
for cell in pattern.keys():
V[cell] = pattern_struct.kernel.symmetric_kernel(
position, pattern[cell])
return V