-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayers.py
More file actions
381 lines (327 loc) · 17.2 KB
/
layers.py
File metadata and controls
381 lines (327 loc) · 17.2 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
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
import numpy as np
import h5py as h5
from collections import OrderedDict
from initializer import *
from activation import *
# ネットワークのサイズを指定されれば自動でweightとbiasを生成するモデルに変える
# model weights are easily stored using HDF5 format and that the network structure can be saved in either JSON or YAML format.
class Layer2D:
'''Model for fully conected layers
'''
def __init__(self, layer_size, activation=ReLU()):
# List all the activation functions to validate the input
self.list_LU = []
self.list_S = []
# Initialize with He=====================================
self.list_LU.append(type(Identity()))
self.list_LU.append(type(ReLU()))
self.list_LU.append(type(LReLU()))
self.list_LU.append(type(PReLU()))
self.list_LU.append(type(ELU()))
self.list_LU.append(type(SELU()))
self.list_LU.append(type(SoftPlus()))
# Initialize with Xavier=================================
self.list_S.append(type(Sigmoid()))
self.list_S.append(type(Tanh()))
self.list_S.append(type(ArcTan()))
self.list_S.append(type(SoftSign()))
# No initializer is needed===============================
if not isinstance(activation, (tuple(self.list_LU), tuple(self.list_S))):
raise TypeError('The activation function '+ str(activation) + ' is not defined in the activation.py.')
else:
self.act = activation
self.X = {'input':None, 'output':None, 'shape':None, 'delta':None, 'batch':None, 'width':None}
self.W = {'weight':None, 'delta':None, 'shape':None, 'hight':None, 'width':layer_size}
# この場合hightが入力ノード数、widthが出力ノード数となる
self.B = {'bias':None, 'delta':None, 'shape':(1, layer_size), 'hight':1, 'width':layer_size}
def forward(self, X):
self.X['input'] = X
self.X['shape'] = X.shape
self.X['batch'], self.X['width'] = X.shape
self.W['hight'] = self.X['width']
self.W['shape'] = (self.W['hight'], self.W['width'])
#初めてXが渡されたときにのみ重みを初期化する
if self.W['weight'] is None:
init = WeightInitializer(self.W['shape'])
if isinstance(self.act, tuple(self.list_LU)):
self.W['weight'] = init.He_normal()
# He_simple に変えれば少しの精度を犠牲に処理速度の向上が見込める
else:
self.W['weight'] = init.Xavier_normal()
if self.B['bias'] is None:
init = WeightInitializer(self.B['shape'])
self.B['bias'] = init.ones()
class Layer3D:
'''Model for 3D layer
'''
def __init__(self, patch_size=None, kernel_size=(None,None), activation=ReLU()):
# List all the activation functions to validate the input
self.list_LU = []
self.list_S = []
# Initialize with He=====================================
self.list_LU.append(type(Identity()))
self.list_LU.append(type(ReLU()))
self.list_LU.append(type(LReLU()))
self.list_LU.append(type(PReLU()))
self.list_LU.append(type(ELU()))
self.list_LU.append(type(SELU()))
self.list_LU.append(type(SoftPlus()))
# Initialize with Xavier=================================
self.list_S.append(type(Sigmoid()))
self.list_S.append(type(Tanh()))
self.list_S.append(type(ArcTan()))
self.list_S.append(type(SoftSign()))
if not isinstance(activation, (tuple(self.list_LU), tuple(self.list_S))):
raise TypeError('The activation function '+ str(activation) + ' is not defined in the activation.py.')
else:
self.act = activation
self.X = {'input':None, 'output':None, 'shape':None, 'delta':None, 'batch':None, 'channel':None, 'hight':None, 'width':None}
self.W = {'weight':None, 'delta':None, 'shape':None, 'patch':patch_size, 'channel':None, 'hight':kernel_size[0], 'width':kernel_size[1]}
self.B = {'bias':None, 'delta':None, 'shape':(patch_size,1,1), 'patch':patch_size, 'hight':1, 'width':1}
def forward(self, X):
self.X['input'] = X
self.X['shape'] = X.shape
self.X['batch'], self.X['channel'], self.X['hight'], self.X['width'] = X.shape
self.W['channel'] = self.X['channel']
self.W['shape'] = (self.W['patch'], self.W['channel'], self.W['hight'], self.W['width'])
#初めてXが渡されたときにのみ重みを初期化する
if self.W['weight'] is None:
init = FilterInitializer(self.W['shape'])
self.W['weight'] = init.normal()
if self.B['bias'] is None:
self.B['bias'] = np.ones(self.B['shape'])
class Affine(Layer2D):
'''Affaine Layer (compatible with tensor)
## Arguments
layer_size: Integer, the number of nodes to use
activation: Activation functions to use
## Input shape
4D tensor with shape:
(batch_size, channels, hight, width)\n
or\n
2D tensor with shape:
(batch_size, nodes)
## Output shape
2D tensor with shape:
(batch_size, nodes)
'''
def __init__(self, layer_size, activation=ReLU()):
super().__init__(layer_size, activation)
self.X_shape = None
def forward(self, X):
self.X_shape = X.shape
X = X.reshape(X.shape[0], -1)
super().forward(X)
self.X['output'] = X
return self.act.forward(np.dot(self.X['output'], self.W['weight']) + self.B['bias'])
def backward(self, dY):
dY = self.act.backward(dY)
self.W['delta'] = np.dot(self.X['output'].T, dY)
self.B['delta'] = np.sum(dY, axis=0)
return np.dot(dY, self.W['weight'].T).reshape(self.X_shape)
def has_params(self):
return True
def get_params(self):
params = {'weight':self.W['weight'], 'bias':self.B['bias']}
return params
def get_grads(self):
grads = {'weight':self.W['delta'], 'bias':self.B['delta']}
return grads
class Convolution(Layer3D):
'''Convolution Layer
## Arguments
patch_size: Integer, the number of filters to use
kernel_size: Tuple of two intengers, determins the size of the filter (hight, width)
strides: Tuple of two intengers, (vertical stride, horizontal stride)
padding: One of 'null', 'same'
'null': no padding\n
'adj' : adjust padding so that all of the input data will be convoluted\n
'same': zero-padding the input such that the output has the same length as the input\n
'half': zero-padding the input such that the output has the half length of the input
activation: Activation functions to use
## Input shape
4D tensor with shape:
(batch_size, channels, hight, width)
## Output shape
4D tensor with shape:
(batch_size, patch_size, nwe_hight, new_width)
'''
def __init__(self, patch_size=None, kernel_size=(None,None), strides=(1,1), activation=ReLU(), padding='null', **kwargs):
super().__init__(patch_size, kernel_size, activation)
self.Y = {'hight':None, 'width':None}
self.pad = {'hight':None, 'width':None}
self.padding_option = padding
self.strides = strides
self.x = None
def forward(self, X):
super().forward(X)
if self.padding_option == 'same':
self.pad['hight'] = ((self.strides[0]-1)*self.X['hight']-self.strides[0]+self.W['hight'])
self.pad['width'] = ((self.strides[1]-1)*self.X['width']-self.strides[1]+self.W['width'])
elif self.padding_option == 'half':
self.pad['hight'] = ((self.strides[0]-2)*self.X['hight']//2-self.strides[0]+self.W['hight'])
self.pad['width'] = ((self.strides[1]-2)*self.X['width']//2-self.strides[1]+self.W['width'])
elif self.padding_option == 'adj':
self.pad['hight'] = (self.X['hight']-self.W['hight'])%self.strides[0]
self.pad['width'] = (self.X['width']-self.W['width'])%self.strides[1]
elif self.padding_option == 'null':
self.pad['hight'] = 0
self.pad['width'] = 0
self.X['output'] = np.pad(self.X['input'], [(0,0), (0,0), (self.pad['hight']//2, self.pad['hight']-self.pad['hight']//2), (self.pad['width']//2, self.pad['width']-self.pad['width']//2)], 'constant', constant_values=0)
self.Y['hight'] = (self.X['hight'] - self.W['hight'] + self.pad['hight'])//self.strides[0] + 1
self.Y['width'] = (self.X['width'] - self.W['width'] + self.pad['width'])//self.strides[1] + 1
self.x = np.zeros((self.X['batch'], self.Y['hight']*self.Y['width'], self.X['channel'], self.W['hight'], self.W['width']))
for i in range(self.Y['hight']):
for j in range(self.Y['width']):
self.x[:,self.Y['width']*i + j,:,:,:] = self.X['output'][:,:,i*self.strides[0]:i*self.strides[0] + self.W['hight'],j*self.strides[1]:j*self.strides[1] + self.W['width']]
self.x = self.x.reshape(self.X['batch'], self.Y['hight'], self.Y['width'], self.X['channel'], self.W['hight'], self.W['width'])
return self.act.forward(np.tensordot(self.x, self.W['weight'].transpose(1,2,3,0), axes=3).transpose(0,3,1,2) + self.B['bias'])
def backward(self, dY):
dY = self.act.backward(dY)
self.B['delta'] = np.sum(dY, axis=0)
self.W['delta'] = np.tensordot(dY.transpose(1,0,2,3), self.x.reshape(self.X['batch'], self.Y['hight'], self.Y['width'], self.X['channel'], self.W['hight'], self.W['width']), axes=3)
dx = np.tensordot(dY.transpose(0,2,3,1), self.W['weight'], axes=1).reshape(self.X['batch'], self.Y['hight']*self.Y['width'], self.X['channel'], self.W['hight'], self.W['width'])
self.X['delta'] = np.zeros(self.X['output'].shape)
for i in range(self.Y['hight']):
for j in range(self.Y['width']):
self.X['delta'][:,:,i*self.strides[0]:i*self.strides[0] + self.W['hight'],j*self.strides[1]:j*self.strides[1] + self.W['width']] += dx[:,self.Y['width']*i + j,:,:,:]
self.X['delta'] = self.X['delta'][:,:,self.pad['hight']//2:self.X['hight']+self.pad['hight']//2,self.pad['width']//2:self.X['width']+self.pad['width']//2]
return self.X['delta']
def has_params(self):
return True
def get_params(self):
params = {'weight':self.W['weight'], 'bias':self.B['bias']}
return params
def get_grads(self):
grads = {'weight':self.W['delta'], 'bias':self.B['delta']}
return grads
class Padding:
'''Padding Layer
## Arguments
pad_size: Tuple of two integers, (padding hight, padding width)
pad_value: sets a padding value (default value is zero)
## Input shape
4D tensor with shape:
(batch_size, channels, hight, width)
## Output shape
4D tensor with shape:
(batch_size, channels, padded_hight, padded_width)
'''
def __init__(self, pad_size=(None,None), pad_value=0):
self.pad = {'hight':pad_size[0], 'width':pad_size[1]}
self.pad_val = pad_value
self.option = option
self.X_shape = None
def forward(self, X):
self.X_shape = X.shape
return np.pad(X, [(0,0),(0,0),(self.pad['hight'], self.pad['hight']),(self.pad['width'], self.pad['width'])], 'constant', constant_values=self.pad_val)
def backward(self, dY):
dX = np.zeros(self.X_shape)
dX = dY[:,:,self.pad['hight']:self.X_shape[2]-self.pad['hight'],self.pad['width']:self.X_shape[3]-self.pad['width']]
return dX
def has_params(self):
return False
def get_params(self):
pass
def get_grads(self):
pass
class Pooling:
'''Pooling Layer
## Arguments
pool: Tuple of two integers, that determines the hight and width of pooling window
strides: Tuple of two integers, (vertical stride, horizontal stride)
option: One of 'max' and 'ave'
'max': max pooling\n
'ave': average pooling
padding: One of 'null', 'same'
'null': no padding\n
'adj' : adjust padding so that all of the input data will be convoluted\n
'same': zero-padding the input such that the output has the same length as the input\n
'half': zero-padding the input such that the output has the half length of the input
'''
def __init__(self, pool=(None,None), strides=(None,None), option='max', padding='null', **kwargs):
self.X = {'input':None, 'output':None, 'shape':None, 'delta':None, 'batch':None, 'channel':None, 'hight':None, 'width':None}
self.Y = {'hight':None, 'width':None}
self.pool = {'hight':pool[0], 'width':pool[1]}
self.strides = strides
self.option = option
self.pad = {'hight':None, 'width':None}
self.padding_option = padding
self.x = None
def forward(self, X):
self.X['input'] = X
self.X['shape'] = X.shape
self.X['batch'], self.X['channel'], self.X['hight'], self.X['width'] = X.shape
if self.padding_option == 'same':
self.pad['hight'] = ((self.strides[0]-1)*self.X['hight']-self.strides[0]+self.pool['hight'])
self.pad['width'] = ((self.strides[1]-1)*self.X['width']-self.strides[1]+self.pool['width'])
elif self.padding_option == 'half':
self.pad['hight'] = ((self.strides[0]-2)*self.X['hight']//2-self.strides[0]+self.pool['hight'])
self.pad['width'] = ((self.strides[1]-2)*self.X['width']//2-self.strides[1]+self.pool['width'])
elif self.padding_option == 'adj':
self.pad['hight'] = (self.X['hight']-self.pool['hight'])%self.strides[0]
self.pad['width'] = (self.X['width']-self.pool['width'])%self.strides[1]
elif self.padding_option == 'null':
self.pad['hight'] = 0
self.pad['width'] = 0
self.X['input'] = np.pad(self.X['input'], [(0,0), (0,0), (self.pad['hight']//2, self.pad['hight']-self.pad['hight']//2), (self.pad['width']//2, self.pad['width']-self.pad['width']//2)], 'constant', constant_values=0)
self.Y['hight'] = (self.X['hight'] - self.pool['hight'] + self.pad['hight'])//self.strides[0] + 1
self.Y['width'] = (self.X['width'] - self.pool['width'] + self.pad['width'])//self.strides[1] + 1
self.x = np.zeros((self.X['batch'], self.Y['hight']*self.Y['width'], self.X['channel'], self.pool['hight'], self.pool['width']))
for i in range(self.Y['hight']):
for j in range(self.Y['width']):
self.x[:,self.Y['width']*i + j,:,:,:] = self.X['input'][:,:,i*self.strides[0]:i*self.strides[0] + self.pool['hight'],j*self.strides[1]:j*self.strides[1] + self.pool['width']]
self.X['output'] = self.x.reshape(self.X['batch'], self.Y['hight']*self.Y['width'], self.X['channel'], self.pool['hight']*self.pool['width'])
if self.option == 'max': # max pooloing
self.x = np.max(self.X['output'], axis=3).transpose(0,2,1)
elif self.option == 'ave': # average pooling
self.x = np.average(self.X['output'], axis=3).transpose(0,2,1)
return self.x.reshape(self.X['batch'], self.X['channel'], self.Y['hight'], self.Y['width'])
def backward(self, dY):
dY = dY.reshape(self.X['batch'], self.X['channel'],-1).transpose(0,2,1)
dx = np.zeros((self.X['batch'], self.Y['hight']*self.Y['width'], self.X['channel'], self.pool['hight']*self.pool['width']))
if self.option == 'max':# max pooloing
index = np.argmax(self.X['output'], axis=3).reshape(1,-1)[0]
dY = dY.reshape(1,-1)[0]
dx = dx.reshape(-1, self.pool['hight']*self.pool['width'])
for i in range(len(index)):
dx[i,index[i]] = dY[i]
elif self.option == 'ave':# average pooling
dY = dY.reshape(self.X['batch'], self.Y['hight']*self.Y['width'], self.X['channel'],1)
dx = dx + dY
dx = dx.reshape(self.X['batch'], self.Y['hight']*self.Y['width'], self.X['channel'], self.pool['hight'], self.pool['width'])
self.X['delta'] = np.zeros(self.X['input'].shape)
for i in range(self.Y['hight']):
for j in range(self.Y['width']):
self.X['delta'][:,:,i*self.strides[0]:i*self.strides[0] + self.pool['hight'],j*self.strides[1]:j*self.strides[1] + self.pool['width']] += dx[:,self.Y['width']*i + j,:,:,:]
self.X['delta'] = self.X['delta'][:,:,self.pad['hight']//2:self.X['hight']+self.pad['hight']//2,self.pad['width']//2:self.X['width']+self.pad['width']//2]
return self.X['delta']
def has_params(self):
return False
def get_params(self):
pass
def get_grads(self):
pass
class Dropout:
"""Dropout Layer
## Arguments
dropout_rate: set the dropout rate
"""
def __init__(self, dropout_rate=0.5):
self.rate = dropout_rate
self.mask = None
def __call__(self, dropout_rate=0.5):
self.rate = dropout_rate
def forward(self, X):
self.mask = np.random.rand(*X.shape) < self.rate
return X * self.mask
def predict(self, X):
return X * self.rate
def backward(self, dY):
return dY * self.mask
def has_params(self):
return False
def get_params(self):
pass
def get_grads(self):
pass