-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_models.py
More file actions
233 lines (172 loc) · 7.24 KB
/
new_models.py
File metadata and controls
233 lines (172 loc) · 7.24 KB
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
import numpy as np
from typing import Dict, List
from dataloader import DataLoader
import random
from tqdm import tqdm
import matplotlib.pyplot as plt
class Weight:
def __init__(self, column_count: int, cell_count: int, dendrite_count: int, input_dim: int):
self.column_count = column_count
self.cell_count = cell_count
self.dendrite_count = dendrite_count
self.input_dim = input_dim
initial_W = np.random.normal(0.45, 0.1, (column_count, cell_count, dendrite_count, input_dim)).astype(np.float16)
self.W = np.clip(initial_W, 0, 1)
self.active_dendrites = np.zeros((column_count, cell_count, dendrite_count), dtype=bool)
self.fired_cells = np.zeros((column_count, cell_count), dtype=bool)
pass
def get_connected_synapses(self, threshold: float = 0.5):
return self.W > threshold
def get_active_dendrites_and_cells(self, column_idx: int, x, threshold: int = 1):
# Step1 : calculate activation and activate dendrites
#activations = self.get_connected_synapses()[column_idx, :, :, :] @ x # activations.shape = (cell, dendrite)
activations = self.W[column_idx, :, :, :] @ x # activations.shape = (cell, dendrite)
#print(f'act of dend {activations}')
#active_dendrites = activations >= threshold # active_dendrites.shape = (cell, dendrite)
# Step2 : select best dendrite
active_dendrites = np.zeros((self.cell_count, self.dendrite_count), dtype=bool)
for i in range(self.cell_count):
max_idx = np.argmax(activations[i, :])
active_dendrites[i, :] = False
active_dendrites[i, max_idx] = True
self.active_dendrites[column_idx] = active_dendrites
#print(f'active dendrites: {self.active_dendrites}')
# この一塊は改善の余地あり
self.fired_cells.fill(False)
'''
fired_cells = np.zeros((self.column_count, self.cell_count), dtype=bool)
if np.max(activations) >= threshold:
max_act_in_cell = np.max(activations, axis=1) # shape = (cell)
winner_cell_idx = np.argmax(max_act_in_cell)
fired_cells[column_idx, winner_cell_idx] = True
self.fired_cells = fired_cells
#print(f'fired cells: {fired_cells}')
'''
winner_cell_coord = np.unravel_index(np.argmax(activations), (self.column_count, self.cell_count))
self.fired_cells[column_idx, winner_cell_coord[1]] = True
return active_dendrites, winner_cell_coord
def update_weights(self, column_idx: int, x, sigma: float = 1.0, lr = 0.05):
# Step 1 : coordinate of winner cell
fc_idx = np.where(self.fired_cells[column_idx, :] == True)[0]
wd_idx = np.where(self.active_dendrites[column_idx, fc_idx, :] == True)[1]
#print(f'idx: {fc_idx}, {wd_idx}')
# Step 2 : calculate distance and influence
location = np.arange(self.cell_count)
distance = abs(location - int(fc_idx))
influence = np.exp(-(distance**2) / (2 * (sigma**2)))
# Step 3 : update weights
self.W[column_idx, :, :, :] += lr * influence[:, np.newaxis, np.newaxis] * (x - self.W[column_idx, :, :, :])
# winnerセルはinputに近づく
'''
self.W[column_idx, fc_idx, wd_idx, :] += 2 * lr * (x - self.W[column_idx, fc_idx, wd_idx, :])
# loserセルはinputから遠ざかる
self.W[column_idx, :, :, :] -= lr * (x - self.W[column_idx, :, :, :])
self.W[column_idx, :, :, :] = np.clip(self.W[column_idx, :, :, :], 0, 1)
'''
return self.W
def fix_weights(self, column_idx, cell_idx, dest, new_weight: float = 0.51):
w = np.zeros(self.W[column_idx, cell_idx, 0, :].shape)
#print(f'w.shape : {w.shape}')
w[dest] = new_weight
self.W[column_idx, cell_idx, 0, :] = w
pass
class CorticalNeuralNetwork:
def __init__(self):
self.column_count = 25
self.cell_count = 32
self.dendrite_count = 1
self.input_dim = 128
self.lateral_W = Weight(25, 32, 1, 25*32)
self.basal_W = Weight(25, 32, 1, 128)
pass
def train_basal(self, column_idx: int, x : np.ndarray):
_, coordinate = self.basal_W.get_active_dendrites_and_cells(column_idx, x)
self.basal_W.update_weights(column_idx, x)
#print(coordinate)
return coordinate
def train_image(self, patterns: List[np.ndarray]):
act_cell_record = []
for patch_idx, patch in enumerate(patterns):
act_cell_coord = self.train_basal(patch_idx, patch)
act_cell_record.append(act_cell_coord)
self.train_lateral(act_cell_record)
return act_cell_record[8][1]
def train_lateral(self, act_cells: List[int]):
for i in range(len(act_cells)):
dest_coord = act_cells[:i] + act_cells[i+1:]
#print(dest_coord)
dest_gbidx = [x*self.cell_count + y for x, y in dest_coord]
#print(dest_gbidx)
#print(f'column_idx: {column_idx}, cell_idx: {cell_idx}, dest: {dest}')
self.lateral_W.fix_weights(act_cells[i][0], act_cells[i][1], dest_gbidx)
def eval():
pass
#print(f'W : {w.W}')
#con = w.get_connected_synapses()
#print(np.sum(con))
#print(f'diff: {w_after - w_before}')
#print(w.W)
cnn = CorticalNeuralNetwork()
loader = DataLoader('dataset', 30)
#d = [{i: 0} for i in range(10)]
d = [0 for _ in range(10)]
dist_of_class_in_cell = [d.copy() for _ in range(32)]
#print(dist_of_class_in_cell[0])
#print(dist_of_class_in_cell)
for data, label in tqdm(loader): # data: [vectors, patch_indices], label: int
vec = data[0]
#print(f'vec.shape: {vec.shape}')
act_cell = cnn.train_image(vec)
#print(f'act_cell: {act_cell}, label: {label}')
dist_of_class_in_cell[act_cell][label] += 1
map = np.array(dist_of_class_in_cell)
plt.imshow(map)
plt.colorbar(label='value')
plt.show()
"""
Sprint Backlog
Weight implementation
L dendrite
L cell
L update, fix_update
"""
"""
Design
class Weight:
def init():
# 本当はどっちかだけ
self.W_lateral = np.array((25, 32, 32, 25*32), float16)
self.W_basal = np.array((25, 32, 32, 128), float16)
def activate_dendrite()
def activate_cell(active_column)
activations = self.W_basal[active_column, :, :] @ input_vector
fired_cells = activations > threshold
return fired_cells
def get_connected():
return self.W > threshold
class CorticalNN:
def __init__(self):
self.lateral_W = Weight()
self.basal_W = Weight()
def train_image()
act_cell_record = []
for patch in image:
act = self.train()
act_cell_record.append(act)
self.train_lateral(act_cell_record)
def eval_image()
def train_basal()
act_from_basal = self.basal_W.activate_cell(column_idx)
self.basal_W.update_weights(column_idx, act_from_basal)
return act_from_basal
def train_lateral(act_cells)
self.lateral_w.update_weights(act_cells)
def eval()
class experiment()
def init():
self.model = Model()
def train()
for all_images:
model.train_image()
def eval()
"""