-
Notifications
You must be signed in to change notification settings - Fork 12
/
convolution.py
executable file
·287 lines (234 loc) · 9.85 KB
/
convolution.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
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
import numpy as np
import theano
import theano.tensor as T
from utils import create_shared, random_weights
from theano.tensor.nnet import conv
import pooling
floatX = theano.config.floatX
device = theano.config.device
class Conv1DLayer(object):
def __init__(self, nb_filters, stack_size, filter_height, wide, emb_dim, name):
"""
1D convolutional layer: 1D Row-wise convolution.
Requires to know the dimension of the embeddings.
"""
self.nb_filters = nb_filters
self.stack_size = stack_size
self.filter_height = filter_height
self.wide = wide
self.emb_dim = emb_dim
self.filter_shape = (emb_dim, nb_filters, stack_size, filter_height, 1)
# _TODO_ check initialization
# fan_in = in_fmaps * 1 * width
# fan_out = out_fmaps * 1 * width
# W_bound = numpy.sqrt(6./(fan_in+fan_out))
filters_values = np.asarray(
np.random.normal(0, 0.05, size=self.filter_shape),
dtype=theano.config.floatX
)
self.filters = create_shared(filters_values, name + '__filters')
self.bias = create_shared(np.zeros((nb_filters, emb_dim)), name + '__bias')
# parameters in the layer
self.params = [self.filters, self.bias]
def link(self, input):
self.input = input
conv_list = []
for i in range(self.emb_dim):
conv_out = conv.conv2d(
input=self.input[:, :, :, i:i + 1],
filters=self.filters[i],
border_mode=('full' if self.wide else 'valid')
)
conv_list.append(conv_out)
self.conv_out = T.concatenate(conv_list, axis=3)
# bias + squash function
self.linear_output = self.conv_out + self.bias.dimshuffle('x', 0, 'x', 1)
self.output = T.tanh(self.linear_output)
return self.output
class Conv2DLayerOld(object):
"""
2D Convolutional neural layer.
"""
def __init__(self, nb_filters, stack_size, filter_height, filter_width, wide, name):
"""
Construct a convolutional layer
`wide`:
False: only apply filter to complete patches of the image.
Generates output of shape: image_shape - filter_shape + 1
True: zero-pads image to multiple of filter shape to generate
output of shape: image_shape + filter_shape - 1
"""
self.nb_filters = nb_filters
self.stack_size = stack_size
self.filter_height = filter_height
self.filter_width = filter_width
self.wide = wide
self.name = name
self.filter_shape = (nb_filters, stack_size, filter_height, filter_width)
fan_in = stack_size * filter_height * filter_width # number of inputs to each hidden unit
fan_out = ((nb_filters * filter_height * filter_width)) # each unit in the lower layer receives a gradient from
drange = np.sqrt(6. / (fan_in + fan_out)) # initialize filters with random values
self.filters = create_shared(drange * random_weights(self.filter_shape), name + '__filters')
self.bias = create_shared(np.zeros((nb_filters,)), name + '__bias')
# parameters in the layer
self.params = [self.filters, self.bias]
def link(self, input):
"""
Convolve input feature maps with filters.
Input: Feature map of dimension (batch_size, stack_size, nb_rows, nb_cols)
Output: Feature map of dimension (batch_size, nb_filters, output_rows, output_cols)
"""
self.input = input
# convolutional layer
self.conv_out = conv.conv2d(
input=self.input,
filters=self.filters,
border_mode=('full' if self.wide else 'valid'),
filter_shape=self.filter_shape
)
# bias + squash function
self.linear_output = self.conv_out + self.bias.dimshuffle('x', 0, 'x', 'x')
self.output = T.tanh(self.linear_output)
return self.output
class Conv2DLayer(object):
"""
2D Convolutional neural layer.
"""
def __init__(self, nb_filters, stack_size, filter_height, filter_width, border_mode, stride, name):
"""
Construct a convolutional layer.
"""
self.nb_filters = nb_filters
self.stack_size = stack_size
self.filter_height = filter_height
self.filter_width = filter_width
self.border_mode = border_mode
self.filter_shape = (nb_filters, stack_size, filter_height, filter_width)
self.stride = stride
self.name = name
fan_in = stack_size * filter_height * filter_width # number of inputs to each hidden unit
fan_out = ((nb_filters * filter_height * filter_width)) # each unit in the lower layer receives a gradient from
drange = np.sqrt(6. / (fan_in + fan_out)) # initialize filters with random values
self.filters = create_shared(drange * random_weights(self.filter_shape), name + '__filters')
self.bias = create_shared(np.ones((nb_filters,)) * 0.1, name + '__bias')
# parameters in the layer
self.params = [self.filters, self.bias]
def link(self, input):
"""
Convolve input feature maps with filters.
Input: Feature map of dimension (batch_size, stack_size, nb_rows, nb_cols)
Output: Feature map of dimension (batch_size, nb_filters, output_rows, output_cols)
"""
# convolutional layer
self.conv_out = T.nnet.conv2d(
input=input,
filters=self.filters,
# input_shape=None, _TODO_ might be faster
filter_shape=self.filter_shape,
border_mode=self.border_mode,
subsample=self.stride,
filter_flip=False,
# image_shape=None
)
# bias + squash function
self.linear_output = self.conv_out + self.bias.dimshuffle('x', 0, 'x', 'x')
self.output = T.nnet.relu(self.linear_output)
return self.output
class Conv1DLayerKMaxPooling(object):
"""
1D Convolutional neural layer with k-max pooling.
"""
def __init__(self, nb_filters, stack_size, filter_height, wide, emb_dim, name):
"""
Construct a convolutional layer
`wide`:
False: only apply filter to complete patches of the image.
Generates output of shape: image_shape - filter_shape + 1
True: zero-pads image to multiple of filter shape to generate
output of shape: image_shape + filter_shape - 1
"""
self.nb_filters = nb_filters
self.stack_size = stack_size
self.filter_height = filter_height
self.wide = wide
self.emb_dim = emb_dim
self.name = name
self.k_max = None
self.conv1d_layer = Conv1DLayer(
nb_filters,
stack_size,
filter_height,
wide,
emb_dim,
name + "__conv1d_layer"
)
# parameters in the layer
self.params = [self.conv1d_layer.filters, self.conv1d_layer.bias]
def link(self, input):
"""
Convolve input feature maps with filters.
Input: Feature map of dimension (batch_size, stack_size, nb_rows, nb_cols)
Output: Feature map of dimension (batch_size, nb_filters, output_rows, output_cols)
"""
if self.k_max is None:
raise Exception("k_max has not been defined in the layer %s!" % self.name)
self.input = input
# 1D convolutional layer
self.conv1d_layer.link(self.input)
self.conv_out = self.conv1d_layer.conv_out
# k-max pooling
k_max_layer = pooling.KMaxPoolingLayer1(self.k_max)
self.pooled_out = k_max_layer.link(self.conv_out)
# bias + squash function
self.linear_output = self.pooled_out + self.conv1d_layer.bias.dimshuffle('x', 0, 'x', 1)
self.output = T.tanh(self.linear_output)
return self.output
class Conv2DLayerKMaxPooling(object):
"""
2D Convolutional neural layer with k-max pooling.
"""
def __init__(self, nb_filters, stack_size, filter_height, filter_width, wide, name):
"""
Construct a convolutional layer
`wide`:
False: only apply filter to complete patches of the image.
Generates output of shape: image_shape - filter_shape + 1
True: zero-pads image to multiple of filter shape to generate
output of shape: image_shape + filter_shape - 1
"""
self.nb_filters = nb_filters
self.stack_size = stack_size
self.filter_height = filter_height
self.filter_width = filter_width
self.wide = wide
self.name = name
self.k_max = None
self.conv2d_layer = Conv2DLayer(
nb_filters,
stack_size,
filter_height,
filter_width,
wide,
name + "__conv2d_layer"
)
# parameters in the layer
self.params = [self.conv2d_layer.filters, self.conv2d_layer.bias]
def link(self, input):
"""
Convolve input feature maps with filters.
Input: Feature map of dimension (batch_size, stack_size, nb_rows, nb_cols)
Output: Feature map of dimension (batch_size, nb_filters, output_rows, output_cols)
"""
if self.k_max is None:
raise Exception("k_max has not been defined in the layer %s!" % self.name)
self.input = input
# 2D convolutional layer
self.conv2d_layer.link(self.input)
self.conv_out = self.conv2d_layer.conv_out
# k-max pooling
k_max_layer = pooling.KMaxPoolingLayer1(self.k_max)
self.pooled_out = k_max_layer.link(self.conv_out)
# bias + squash function
self.linear_output = self.pooled_out + self.conv2d_layer.bias.dimshuffle('x', 0, 'x', 'x')
self.output = T.tanh(self.linear_output)
return self.output