keras 0.2.0__py3.5.egg
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- EGG-INFO/PKG-INFO +11 -0
- EGG-INFO/SOURCES.txt +57 -0
- EGG-INFO/dependency_links.txt +1 -0
- EGG-INFO/requires.txt +6 -0
- EGG-INFO/top_level.txt +2 -0
- EGG-INFO/zip-safe +1 -0
- keras/__init__.py +15 -0
- keras/__pycache__/__init__.cpython-35.pyc +0 -0
- keras/__pycache__/activations.cpython-35.pyc +0 -0
- keras/__pycache__/callbacks.cpython-35.pyc +0 -0
- keras/__pycache__/constraints.cpython-35.pyc +0 -0
- keras/__pycache__/initializations.cpython-35.pyc +0 -0
- keras/__pycache__/models.cpython-35.pyc +0 -0
- keras/__pycache__/objectives.cpython-35.pyc +0 -0
- keras/__pycache__/optimizers.cpython-35.pyc +0 -0
- keras/__pycache__/regularizers.cpython-35.pyc +0 -0
- keras/activations.py +44 -0
- keras/callbacks.py +293 -0
- keras/constraints.py +48 -0
- keras/datasets/__init__.py +0 -0
- keras/datasets/__pycache__/__init__.cpython-35.pyc +0 -0
- keras/datasets/__pycache__/cifar.cpython-35.pyc +0 -0
- keras/datasets/__pycache__/cifar10.cpython-35.pyc +0 -0
- keras/datasets/__pycache__/cifar100.cpython-35.pyc +0 -0
- keras/datasets/__pycache__/data_utils.cpython-35.pyc +0 -0
- keras/datasets/__pycache__/imdb.cpython-35.pyc +0 -0
- keras/datasets/__pycache__/mnist.cpython-35.pyc +0 -0
- keras/datasets/__pycache__/reuters.cpython-35.pyc +0 -0
- keras/datasets/cifar.py +22 -0
- keras/datasets/cifar10.py +31 -0
- keras/datasets/cifar100.py +28 -0
- keras/datasets/data_utils.py +50 -0
- keras/datasets/imdb.py +66 -0
- keras/datasets/mnist.py +23 -0
- keras/datasets/reuters.py +148 -0
- keras/initializations.py +89 -0
- keras/layers/__init__.py +0 -0
- keras/layers/__pycache__/__init__.cpython-35.pyc +0 -0
- keras/layers/__pycache__/advanced_activations.cpython-35.pyc +0 -0
- keras/layers/__pycache__/containers.cpython-35.pyc +0 -0
- keras/layers/__pycache__/convolutional.cpython-35.pyc +0 -0
- keras/layers/__pycache__/core.cpython-35.pyc +0 -0
- keras/layers/__pycache__/embeddings.cpython-35.pyc +0 -0
- keras/layers/__pycache__/noise.cpython-35.pyc +0 -0
- keras/layers/__pycache__/normalization.cpython-35.pyc +0 -0
- keras/layers/__pycache__/recurrent.cpython-35.pyc +0 -0
- keras/layers/advanced_activations.py +137 -0
- keras/layers/containers.py +318 -0
- keras/layers/convolutional.py +504 -0
- keras/layers/core.py +963 -0
- keras/layers/embeddings.py +153 -0
- keras/layers/noise.py +57 -0
- keras/layers/normalization.py +117 -0
- keras/layers/recurrent.py +837 -0
- keras/models.py +733 -0
- keras/objectives.py +67 -0
- keras/optimizers.py +228 -0
- keras/preprocessing/__init__.py +0 -0
- keras/preprocessing/__pycache__/__init__.cpython-35.pyc +0 -0
- keras/preprocessing/__pycache__/image.cpython-35.pyc +0 -0
- keras/preprocessing/__pycache__/sequence.cpython-35.pyc +0 -0
- keras/preprocessing/__pycache__/text.cpython-35.pyc +0 -0
- keras/preprocessing/image.py +251 -0
- keras/preprocessing/sequence.py +124 -0
- keras/preprocessing/text.py +176 -0
- keras/regularizers.py +84 -0
- keras/utils/__init__.py +0 -0
- keras/utils/__pycache__/__init__.cpython-35.pyc +0 -0
- keras/utils/__pycache__/generic_utils.cpython-35.pyc +0 -0
- keras/utils/__pycache__/io_utils.cpython-35.pyc +0 -0
- keras/utils/__pycache__/layer_utils.cpython-35.pyc +0 -0
- keras/utils/__pycache__/np_utils.cpython-35.pyc +0 -0
- keras/utils/__pycache__/test_utils.cpython-35.pyc +0 -0
- keras/utils/__pycache__/theano_utils.cpython-35.pyc +0 -0
- keras/utils/__pycache__/visualize_util.cpython-35.pyc +0 -0
- keras/utils/generic_utils.py +133 -0
- keras/utils/io_utils.py +71 -0
- keras/utils/layer_utils.py +129 -0
- keras/utils/np_utils.py +53 -0
- keras/utils/test_utils.py +27 -0
- keras/utils/theano_utils.py +44 -0
- keras/utils/visualize_util.py +41 -0
- keras/wrappers/__init__.py +0 -0
- keras/wrappers/__pycache__/__init__.cpython-35.pyc +0 -0
- keras/wrappers/__pycache__/scikit_learn.cpython-35.pyc +0 -0
- keras/wrappers/scikit_learn.py +266 -0
- tests/__init__.py +0 -0
- tests/__pycache__/__init__.cpython-35.pyc +0 -0
- tests/manual/__init__.py +0 -0
- tests/manual/__pycache__/__init__.cpython-35.pyc +0 -0
- tests/manual/__pycache__/check_autoencoder.cpython-35.pyc +0 -0
- tests/manual/__pycache__/check_callbacks.cpython-35.pyc +0 -0
- tests/manual/__pycache__/check_constraints.cpython-35.pyc +0 -0
- tests/manual/__pycache__/check_masked_recurrent.cpython-35.pyc +0 -0
- tests/manual/__pycache__/check_model_utils.cpython-35.pyc +0 -0
- tests/manual/__pycache__/check_save_weights.cpython-35.pyc +0 -0
- tests/manual/__pycache__/check_wrappers.cpython-35.pyc +0 -0
- tests/manual/__pycache__/check_yaml.cpython-35.pyc +0 -0
- tests/manual/check_autoencoder.py +134 -0
- tests/manual/check_callbacks.py +236 -0
- tests/manual/check_constraints.py +100 -0
- tests/manual/check_masked_recurrent.py +130 -0
- tests/manual/check_model_utils.py +44 -0
- tests/manual/check_save_weights.py +40 -0
- tests/manual/check_wrappers.py +126 -0
- tests/manual/check_yaml.py +101 -0
|
@@ -0,0 +1,504 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
from __future__ import absolute_import
|
|
3
|
+
|
|
4
|
+
import theano
|
|
5
|
+
import theano.tensor as T
|
|
6
|
+
from theano.tensor.signal import downsample
|
|
7
|
+
|
|
8
|
+
from .. import activations, initializations, regularizers, constraints
|
|
9
|
+
from ..utils.theano_utils import shared_zeros, on_gpu
|
|
10
|
+
from ..layers.core import Layer
|
|
11
|
+
|
|
12
|
+
if on_gpu():
|
|
13
|
+
from theano.sandbox.cuda import dnn
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def conv_output_length(input_length, filter_size, border_mode, stride):
|
|
17
|
+
if input_length is None:
|
|
18
|
+
return None
|
|
19
|
+
assert border_mode in {'same', 'full', 'valid'}
|
|
20
|
+
if border_mode == 'same':
|
|
21
|
+
output_length = input_length
|
|
22
|
+
elif border_mode == 'full':
|
|
23
|
+
output_length = input_length + filter_size - 1
|
|
24
|
+
elif border_mode == 'valid':
|
|
25
|
+
output_length = input_length - filter_size + 1
|
|
26
|
+
return (output_length + stride - 1) // stride
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def pool_output_length(input_length, pool_size, ignore_border, stride):
|
|
30
|
+
if input_length is None:
|
|
31
|
+
return None
|
|
32
|
+
if ignore_border:
|
|
33
|
+
output_length = input_length - pool_size + 1
|
|
34
|
+
output_length = (output_length + stride - 1) // stride
|
|
35
|
+
else:
|
|
36
|
+
if pool_size == input_length:
|
|
37
|
+
output_length = min(input_length, stride - stride % 2)
|
|
38
|
+
if output_length <= 0:
|
|
39
|
+
output_length = 1
|
|
40
|
+
elif stride >= pool_size:
|
|
41
|
+
output_length = (input_length + stride - 1) // stride
|
|
42
|
+
else:
|
|
43
|
+
output_length = (input_length - pool_size + stride - 1) // stride
|
|
44
|
+
if output_length <= 0:
|
|
45
|
+
output_length = 1
|
|
46
|
+
else:
|
|
47
|
+
output_length += 1
|
|
48
|
+
return output_length
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class Convolution1D(Layer):
|
|
52
|
+
input_ndim = 3
|
|
53
|
+
|
|
54
|
+
def __init__(self, nb_filter, filter_length,
|
|
55
|
+
init='uniform', activation='linear', weights=None,
|
|
56
|
+
border_mode='valid', subsample_length=1,
|
|
57
|
+
W_regularizer=None, b_regularizer=None, activity_regularizer=None,
|
|
58
|
+
W_constraint=None, b_constraint=None, input_dim=None, input_length=None, **kwargs):
|
|
59
|
+
|
|
60
|
+
if border_mode not in {'valid', 'full', 'same'}:
|
|
61
|
+
raise Exception('Invalid border mode for Convolution1D:', border_mode)
|
|
62
|
+
self.nb_filter = nb_filter
|
|
63
|
+
self.filter_length = filter_length
|
|
64
|
+
self.init = initializations.get(init)
|
|
65
|
+
self.activation = activations.get(activation)
|
|
66
|
+
self.border_mode = border_mode
|
|
67
|
+
self.subsample_length = subsample_length
|
|
68
|
+
|
|
69
|
+
self.subsample = (subsample_length, 1)
|
|
70
|
+
|
|
71
|
+
self.W_regularizer = regularizers.get(W_regularizer)
|
|
72
|
+
self.b_regularizer = regularizers.get(b_regularizer)
|
|
73
|
+
self.activity_regularizer = regularizers.get(activity_regularizer)
|
|
74
|
+
|
|
75
|
+
self.W_constraint = constraints.get(W_constraint)
|
|
76
|
+
self.b_constraint = constraints.get(b_constraint)
|
|
77
|
+
self.constraints = [self.W_constraint, self.b_constraint]
|
|
78
|
+
|
|
79
|
+
self.initial_weights = weights
|
|
80
|
+
|
|
81
|
+
self.input_dim = input_dim
|
|
82
|
+
self.input_length = input_length
|
|
83
|
+
if self.input_dim:
|
|
84
|
+
kwargs['input_shape'] = (self.input_length, self.input_dim)
|
|
85
|
+
super(Convolution1D, self).__init__(**kwargs)
|
|
86
|
+
|
|
87
|
+
def build(self):
|
|
88
|
+
input_dim = self.input_shape[2]
|
|
89
|
+
self.input = T.tensor3()
|
|
90
|
+
self.W_shape = (self.nb_filter, input_dim, self.filter_length, 1)
|
|
91
|
+
self.W = self.init(self.W_shape)
|
|
92
|
+
self.b = shared_zeros((self.nb_filter,))
|
|
93
|
+
self.params = [self.W, self.b]
|
|
94
|
+
self.regularizers = []
|
|
95
|
+
|
|
96
|
+
if self.W_regularizer:
|
|
97
|
+
self.W_regularizer.set_param(self.W)
|
|
98
|
+
self.regularizers.append(self.W_regularizer)
|
|
99
|
+
|
|
100
|
+
if self.b_regularizer:
|
|
101
|
+
self.b_regularizer.set_param(self.b)
|
|
102
|
+
self.regularizers.append(self.b_regularizer)
|
|
103
|
+
|
|
104
|
+
if self.activity_regularizer:
|
|
105
|
+
self.activity_regularizer.set_layer(self)
|
|
106
|
+
self.regularizers.append(self.activity_regularizer)
|
|
107
|
+
|
|
108
|
+
if self.initial_weights is not None:
|
|
109
|
+
self.set_weights(self.initial_weights)
|
|
110
|
+
del self.initial_weights
|
|
111
|
+
|
|
112
|
+
@property
|
|
113
|
+
def output_shape(self):
|
|
114
|
+
length = conv_output_length(self.input_shape[1], self.filter_length, self.border_mode, self.subsample[0])
|
|
115
|
+
return (self.input_shape[0], length, self.nb_filter)
|
|
116
|
+
|
|
117
|
+
def get_output(self, train=False):
|
|
118
|
+
X = self.get_input(train)
|
|
119
|
+
X = T.reshape(X, (X.shape[0], X.shape[1], X.shape[2], 1)).dimshuffle(0, 2, 1, 3)
|
|
120
|
+
|
|
121
|
+
border_mode = self.border_mode
|
|
122
|
+
if on_gpu() and dnn.dnn_available():
|
|
123
|
+
if border_mode == 'same':
|
|
124
|
+
assert(self.subsample_length == 1)
|
|
125
|
+
pad_x = (self.filter_length - self.subsample_length) // 2
|
|
126
|
+
conv_out = dnn.dnn_conv(img=X,
|
|
127
|
+
kerns=self.W,
|
|
128
|
+
border_mode=(pad_x, 0))
|
|
129
|
+
else:
|
|
130
|
+
conv_out = dnn.dnn_conv(img=X,
|
|
131
|
+
kerns=self.W,
|
|
132
|
+
border_mode=border_mode,
|
|
133
|
+
subsample=self.subsample)
|
|
134
|
+
else:
|
|
135
|
+
if border_mode == 'same':
|
|
136
|
+
assert(self.subsample_length == 1)
|
|
137
|
+
border_mode = 'full'
|
|
138
|
+
|
|
139
|
+
input_shape = self.input_shape
|
|
140
|
+
image_shape = (input_shape[0], input_shape[2], input_shape[1], 1)
|
|
141
|
+
conv_out = T.nnet.conv.conv2d(X, self.W,
|
|
142
|
+
border_mode=border_mode,
|
|
143
|
+
subsample=self.subsample,
|
|
144
|
+
image_shape=image_shape,
|
|
145
|
+
filter_shape=self.W_shape)
|
|
146
|
+
if self.border_mode == 'same':
|
|
147
|
+
shift_x = (self.filter_length - 1) // 2
|
|
148
|
+
conv_out = conv_out[:, :, shift_x:X.shape[2] + shift_x, :]
|
|
149
|
+
|
|
150
|
+
output = self.activation(conv_out + self.b.dimshuffle('x', 0, 'x', 'x'))
|
|
151
|
+
output = T.reshape(output, (output.shape[0], output.shape[1], output.shape[2])).dimshuffle(0, 2, 1)
|
|
152
|
+
return output
|
|
153
|
+
|
|
154
|
+
def get_config(self):
|
|
155
|
+
config = {"name": self.__class__.__name__,
|
|
156
|
+
"nb_filter": self.nb_filter,
|
|
157
|
+
"filter_length": self.filter_length,
|
|
158
|
+
"init": self.init.__name__,
|
|
159
|
+
"activation": self.activation.__name__,
|
|
160
|
+
"border_mode": self.border_mode,
|
|
161
|
+
"subsample_length": self.subsample_length,
|
|
162
|
+
"W_regularizer": self.W_regularizer.get_config() if self.W_regularizer else None,
|
|
163
|
+
"b_regularizer": self.b_regularizer.get_config() if self.b_regularizer else None,
|
|
164
|
+
"activity_regularizer": self.activity_regularizer.get_config() if self.activity_regularizer else None,
|
|
165
|
+
"W_constraint": self.W_constraint.get_config() if self.W_constraint else None,
|
|
166
|
+
"b_constraint": self.b_constraint.get_config() if self.b_constraint else None,
|
|
167
|
+
"input_dim": self.input_dim,
|
|
168
|
+
"input_length": self.input_length}
|
|
169
|
+
base_config = super(Convolution1D, self).get_config()
|
|
170
|
+
return dict(list(base_config.items()) + list(config.items()))
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
class Convolution2D(Layer):
|
|
174
|
+
input_ndim = 4
|
|
175
|
+
|
|
176
|
+
def __init__(self, nb_filter, nb_row, nb_col,
|
|
177
|
+
init='glorot_uniform', activation='linear', weights=None,
|
|
178
|
+
border_mode='valid', subsample=(1, 1),
|
|
179
|
+
W_regularizer=None, b_regularizer=None, activity_regularizer=None,
|
|
180
|
+
W_constraint=None, b_constraint=None, **kwargs):
|
|
181
|
+
|
|
182
|
+
if border_mode not in {'valid', 'full', 'same'}:
|
|
183
|
+
raise Exception('Invalid border mode for Convolution2D:', border_mode)
|
|
184
|
+
self.nb_filter = nb_filter
|
|
185
|
+
self.nb_row = nb_row
|
|
186
|
+
self.nb_col = nb_col
|
|
187
|
+
self.init = initializations.get(init)
|
|
188
|
+
self.activation = activations.get(activation)
|
|
189
|
+
self.border_mode = border_mode
|
|
190
|
+
self.subsample = tuple(subsample)
|
|
191
|
+
|
|
192
|
+
self.W_regularizer = regularizers.get(W_regularizer)
|
|
193
|
+
self.b_regularizer = regularizers.get(b_regularizer)
|
|
194
|
+
self.activity_regularizer = regularizers.get(activity_regularizer)
|
|
195
|
+
|
|
196
|
+
self.W_constraint = constraints.get(W_constraint)
|
|
197
|
+
self.b_constraint = constraints.get(b_constraint)
|
|
198
|
+
self.constraints = [self.W_constraint, self.b_constraint]
|
|
199
|
+
|
|
200
|
+
self.initial_weights = weights
|
|
201
|
+
super(Convolution2D, self).__init__(**kwargs)
|
|
202
|
+
|
|
203
|
+
def build(self):
|
|
204
|
+
stack_size = self.input_shape[1]
|
|
205
|
+
self.input = T.tensor4()
|
|
206
|
+
self.W_shape = (self.nb_filter, stack_size, self.nb_row, self.nb_col)
|
|
207
|
+
self.W = self.init(self.W_shape)
|
|
208
|
+
self.b = shared_zeros((self.nb_filter,))
|
|
209
|
+
self.params = [self.W, self.b]
|
|
210
|
+
self.regularizers = []
|
|
211
|
+
|
|
212
|
+
if self.W_regularizer:
|
|
213
|
+
self.W_regularizer.set_param(self.W)
|
|
214
|
+
self.regularizers.append(self.W_regularizer)
|
|
215
|
+
|
|
216
|
+
if self.b_regularizer:
|
|
217
|
+
self.b_regularizer.set_param(self.b)
|
|
218
|
+
self.regularizers.append(self.b_regularizer)
|
|
219
|
+
|
|
220
|
+
if self.activity_regularizer:
|
|
221
|
+
self.activity_regularizer.set_layer(self)
|
|
222
|
+
self.regularizers.append(self.activity_regularizer)
|
|
223
|
+
|
|
224
|
+
if self.initial_weights is not None:
|
|
225
|
+
self.set_weights(self.initial_weights)
|
|
226
|
+
del self.initial_weights
|
|
227
|
+
|
|
228
|
+
@property
|
|
229
|
+
def output_shape(self):
|
|
230
|
+
input_shape = self.input_shape
|
|
231
|
+
rows = input_shape[2]
|
|
232
|
+
cols = input_shape[3]
|
|
233
|
+
rows = conv_output_length(rows, self.nb_row, self.border_mode, self.subsample[0])
|
|
234
|
+
cols = conv_output_length(cols, self.nb_col, self.border_mode, self.subsample[1])
|
|
235
|
+
return (input_shape[0], self.nb_filter, rows, cols)
|
|
236
|
+
|
|
237
|
+
def get_output(self, train=False):
|
|
238
|
+
X = self.get_input(train)
|
|
239
|
+
border_mode = self.border_mode
|
|
240
|
+
if on_gpu() and dnn.dnn_available():
|
|
241
|
+
if border_mode == 'same':
|
|
242
|
+
assert(self.subsample == (1, 1))
|
|
243
|
+
pad_x = (self.nb_row - self.subsample[0]) // 2
|
|
244
|
+
pad_y = (self.nb_col - self.subsample[1]) // 2
|
|
245
|
+
conv_out = dnn.dnn_conv(img=X,
|
|
246
|
+
kerns=self.W,
|
|
247
|
+
border_mode=(pad_x, pad_y))
|
|
248
|
+
else:
|
|
249
|
+
conv_out = dnn.dnn_conv(img=X,
|
|
250
|
+
kerns=self.W,
|
|
251
|
+
border_mode=border_mode,
|
|
252
|
+
subsample=self.subsample)
|
|
253
|
+
else:
|
|
254
|
+
if border_mode == 'same':
|
|
255
|
+
border_mode = 'full'
|
|
256
|
+
assert(self.subsample == (1, 1))
|
|
257
|
+
|
|
258
|
+
conv_out = T.nnet.conv.conv2d(X, self.W,
|
|
259
|
+
border_mode=border_mode,
|
|
260
|
+
subsample=self.subsample,
|
|
261
|
+
image_shape=self.input_shape,
|
|
262
|
+
filter_shape=self.W_shape)
|
|
263
|
+
if self.border_mode == 'same':
|
|
264
|
+
shift_x = (self.nb_row - 1) // 2
|
|
265
|
+
shift_y = (self.nb_col - 1) // 2
|
|
266
|
+
conv_out = conv_out[:, :, shift_x:X.shape[2] + shift_x, shift_y:X.shape[3] + shift_y]
|
|
267
|
+
|
|
268
|
+
return self.activation(conv_out + self.b.dimshuffle('x', 0, 'x', 'x'))
|
|
269
|
+
|
|
270
|
+
def get_config(self):
|
|
271
|
+
config = {"name": self.__class__.__name__,
|
|
272
|
+
"nb_filter": self.nb_filter,
|
|
273
|
+
"nb_row": self.nb_row,
|
|
274
|
+
"nb_col": self.nb_col,
|
|
275
|
+
"init": self.init.__name__,
|
|
276
|
+
"activation": self.activation.__name__,
|
|
277
|
+
"border_mode": self.border_mode,
|
|
278
|
+
"subsample": self.subsample,
|
|
279
|
+
"W_regularizer": self.W_regularizer.get_config() if self.W_regularizer else None,
|
|
280
|
+
"b_regularizer": self.b_regularizer.get_config() if self.b_regularizer else None,
|
|
281
|
+
"activity_regularizer": self.activity_regularizer.get_config() if self.activity_regularizer else None,
|
|
282
|
+
"W_constraint": self.W_constraint.get_config() if self.W_constraint else None,
|
|
283
|
+
"b_constraint": self.b_constraint.get_config() if self.b_constraint else None}
|
|
284
|
+
base_config = super(Convolution2D, self).get_config()
|
|
285
|
+
return dict(list(base_config.items()) + list(config.items()))
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
class MaxPooling1D(Layer):
|
|
289
|
+
input_ndim = 3
|
|
290
|
+
|
|
291
|
+
def __init__(self, pool_length=2, stride=None, ignore_border=True, **kwargs):
|
|
292
|
+
super(MaxPooling1D, self).__init__(**kwargs)
|
|
293
|
+
if stride is None:
|
|
294
|
+
stride = pool_length
|
|
295
|
+
self.pool_length = pool_length
|
|
296
|
+
self.stride = stride
|
|
297
|
+
self.st = (self.stride, 1)
|
|
298
|
+
|
|
299
|
+
self.input = T.tensor3()
|
|
300
|
+
self.pool_size = (pool_length, 1)
|
|
301
|
+
self.ignore_border = ignore_border
|
|
302
|
+
|
|
303
|
+
@property
|
|
304
|
+
def output_shape(self):
|
|
305
|
+
input_shape = self.input_shape
|
|
306
|
+
length = pool_output_length(input_shape[1], self.pool_length, self.ignore_border, self.stride)
|
|
307
|
+
return (input_shape[0], length, input_shape[2])
|
|
308
|
+
|
|
309
|
+
def get_output(self, train=False):
|
|
310
|
+
X = self.get_input(train)
|
|
311
|
+
X = T.reshape(X, (X.shape[0], X.shape[1], X.shape[2], 1)).dimshuffle(0, 2, 1, 3)
|
|
312
|
+
output = downsample.max_pool_2d(X, ds=self.pool_size, st=self.st, ignore_border=self.ignore_border)
|
|
313
|
+
output = output.dimshuffle(0, 2, 1, 3)
|
|
314
|
+
return T.reshape(output, (output.shape[0], output.shape[1], output.shape[2]))
|
|
315
|
+
|
|
316
|
+
def get_config(self):
|
|
317
|
+
config = {"name": self.__class__.__name__,
|
|
318
|
+
"stride": self.stride,
|
|
319
|
+
"pool_length": self.pool_length,
|
|
320
|
+
"ignore_border": self.ignore_border}
|
|
321
|
+
base_config = super(MaxPooling1D, self).get_config()
|
|
322
|
+
return dict(list(base_config.items()) + list(config.items()))
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
class MaxPooling2D(Layer):
|
|
326
|
+
input_ndim = 4
|
|
327
|
+
|
|
328
|
+
def __init__(self, pool_size=(2, 2), stride=None, ignore_border=True, **kwargs):
|
|
329
|
+
super(MaxPooling2D, self).__init__(**kwargs)
|
|
330
|
+
self.input = T.tensor4()
|
|
331
|
+
self.pool_size = tuple(pool_size)
|
|
332
|
+
if stride is None:
|
|
333
|
+
stride = self.pool_size
|
|
334
|
+
self.stride = tuple(stride)
|
|
335
|
+
self.ignore_border = ignore_border
|
|
336
|
+
|
|
337
|
+
@property
|
|
338
|
+
def output_shape(self):
|
|
339
|
+
input_shape = self.input_shape
|
|
340
|
+
rows = pool_output_length(input_shape[2], self.pool_size[0], self.ignore_border, self.stride[0])
|
|
341
|
+
cols = pool_output_length(input_shape[3], self.pool_size[1], self.ignore_border, self.stride[1])
|
|
342
|
+
return (input_shape[0], input_shape[1], rows, cols)
|
|
343
|
+
|
|
344
|
+
def get_output(self, train=False):
|
|
345
|
+
X = self.get_input(train)
|
|
346
|
+
output = downsample.max_pool_2d(X, ds=self.pool_size, st=self.stride, ignore_border=self.ignore_border)
|
|
347
|
+
return output
|
|
348
|
+
|
|
349
|
+
def get_config(self):
|
|
350
|
+
config = {"name": self.__class__.__name__,
|
|
351
|
+
"pool_size": self.pool_size,
|
|
352
|
+
"ignore_border": self.ignore_border,
|
|
353
|
+
"stride": self.stride}
|
|
354
|
+
base_config = super(MaxPooling2D, self).get_config()
|
|
355
|
+
return dict(list(base_config.items()) + list(config.items()))
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
class UpSample1D(Layer):
|
|
359
|
+
input_ndim = 3
|
|
360
|
+
|
|
361
|
+
def __init__(self, length=2, **kwargs):
|
|
362
|
+
super(UpSample1D, self).__init__(**kwargs)
|
|
363
|
+
self.length = length
|
|
364
|
+
self.input = T.tensor3()
|
|
365
|
+
|
|
366
|
+
@property
|
|
367
|
+
def output_shape(self):
|
|
368
|
+
input_shape = self.input_shape
|
|
369
|
+
return (input_shape[0], self.length * input_shape[1], input_shape[2])
|
|
370
|
+
|
|
371
|
+
def get_output(self, train=False):
|
|
372
|
+
X = self.get_input(train)
|
|
373
|
+
output = theano.tensor.extra_ops.repeat(X, self.length, axis=1)
|
|
374
|
+
return output
|
|
375
|
+
|
|
376
|
+
def get_config(self):
|
|
377
|
+
config = {"name": self.__class__.__name__,
|
|
378
|
+
"length": self.length}
|
|
379
|
+
base_config = super(UpSample1D, self).get_config()
|
|
380
|
+
return dict(list(base_config.items()) + list(config.items()))
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
class UpSample2D(Layer):
|
|
384
|
+
input_ndim = 4
|
|
385
|
+
|
|
386
|
+
def __init__(self, size=(2, 2), **kwargs):
|
|
387
|
+
super(UpSample2D, self).__init__(**kwargs)
|
|
388
|
+
self.input = T.tensor4()
|
|
389
|
+
self.size = tuple(size)
|
|
390
|
+
|
|
391
|
+
@property
|
|
392
|
+
def output_shape(self):
|
|
393
|
+
input_shape = self.input_shape
|
|
394
|
+
return (input_shape[0], input_shape[1], self.size[0] * input_shape[2], self.size[1] * input_shape[3])
|
|
395
|
+
|
|
396
|
+
def get_output(self, train=False):
|
|
397
|
+
X = self.get_input(train)
|
|
398
|
+
Y = theano.tensor.extra_ops.repeat(X, self.size[0], axis=2)
|
|
399
|
+
output = theano.tensor.extra_ops.repeat(Y, self.size[1], axis=3)
|
|
400
|
+
return output
|
|
401
|
+
|
|
402
|
+
def get_config(self):
|
|
403
|
+
config = {"name": self.__class__.__name__,
|
|
404
|
+
"size": self.size}
|
|
405
|
+
base_config = super(UpSample2D, self).get_config()
|
|
406
|
+
return dict(list(base_config.items()) + list(config.items()))
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
class ZeroPadding1D(Layer):
|
|
410
|
+
"""Zero-padding layer for 1D input (e.g. temporal sequence).
|
|
411
|
+
|
|
412
|
+
Input shape
|
|
413
|
+
-----------
|
|
414
|
+
3D tensor with shape (samples, axis_to_pad, features)
|
|
415
|
+
|
|
416
|
+
Output shape
|
|
417
|
+
------------
|
|
418
|
+
3D tensor with shape (samples, padded_axis, features)
|
|
419
|
+
|
|
420
|
+
Arguments
|
|
421
|
+
---------
|
|
422
|
+
padding: int
|
|
423
|
+
How many zeros to add at the beginning and end of
|
|
424
|
+
the padding dimension (axis 1).
|
|
425
|
+
"""
|
|
426
|
+
input_ndim = 3
|
|
427
|
+
|
|
428
|
+
def __init__(self, padding=1, **kwargs):
|
|
429
|
+
super(ZeroPadding1D, self).__init__(**kwargs)
|
|
430
|
+
self.padding = padding
|
|
431
|
+
self.input = T.tensor3()
|
|
432
|
+
|
|
433
|
+
@property
|
|
434
|
+
def output_shape(self):
|
|
435
|
+
input_shape = self.input_shape
|
|
436
|
+
return (input_shape[0], input_shape[1] + self.padding * 2, input_shape[2])
|
|
437
|
+
|
|
438
|
+
def get_output(self, train=False):
|
|
439
|
+
X = self.get_input(train)
|
|
440
|
+
input_shape = X.shape
|
|
441
|
+
output_shape = (input_shape[0],
|
|
442
|
+
input_shape[1] + 2 * self.padding,
|
|
443
|
+
input_shape[2])
|
|
444
|
+
output = T.zeros(output_shape)
|
|
445
|
+
return T.set_subtensor(output[:, self.padding:X.shape[1] + self.padding, :], X)
|
|
446
|
+
|
|
447
|
+
def get_config(self):
|
|
448
|
+
config = {"name": self.__class__.__name__,
|
|
449
|
+
"padding": self.padding}
|
|
450
|
+
base_config = super(ZeroPadding1D, self).get_config()
|
|
451
|
+
return dict(list(base_config.items()) + list(config.items()))
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
class ZeroPadding2D(Layer):
|
|
455
|
+
"""Zero-padding layer for 1D input (e.g. temporal sequence).
|
|
456
|
+
|
|
457
|
+
Input shape
|
|
458
|
+
-----------
|
|
459
|
+
4D tensor with shape (samples, depth, first_axis_to_pad, second_axis_to_pad)
|
|
460
|
+
|
|
461
|
+
Output shape
|
|
462
|
+
------------
|
|
463
|
+
4D tensor with shape (samples, depth, first_padded_axis, second_padded_axis)
|
|
464
|
+
|
|
465
|
+
Arguments
|
|
466
|
+
---------
|
|
467
|
+
padding: tuple of int (length 2)
|
|
468
|
+
How many zeros to add at the beginning and end of
|
|
469
|
+
the 2 padding dimensions (axis 3 and 4).
|
|
470
|
+
"""
|
|
471
|
+
input_ndim = 4
|
|
472
|
+
|
|
473
|
+
def __init__(self, padding=(1, 1), **kwargs):
|
|
474
|
+
super(ZeroPadding2D, self).__init__(**kwargs)
|
|
475
|
+
self.padding = tuple(padding)
|
|
476
|
+
self.input = T.tensor4()
|
|
477
|
+
|
|
478
|
+
@property
|
|
479
|
+
def output_shape(self):
|
|
480
|
+
input_shape = self.input_shape
|
|
481
|
+
return (input_shape[0],
|
|
482
|
+
input_shape[1],
|
|
483
|
+
input_shape[2] + 2 * self.padding[0],
|
|
484
|
+
input_shape[3] + 2 * self.padding[1])
|
|
485
|
+
|
|
486
|
+
def get_output(self, train=False):
|
|
487
|
+
X = self.get_input(train)
|
|
488
|
+
input_shape = X.shape
|
|
489
|
+
output_shape = (input_shape[0],
|
|
490
|
+
input_shape[1],
|
|
491
|
+
input_shape[2] + 2 * self.padding[0],
|
|
492
|
+
input_shape[3] + 2 * self.padding[1])
|
|
493
|
+
output = T.zeros(output_shape)
|
|
494
|
+
indices = (slice(None),
|
|
495
|
+
slice(None),
|
|
496
|
+
slice(self.padding[0], input_shape[2] + self.padding[0]),
|
|
497
|
+
slice(self.padding[1], input_shape[3] + self.padding[1]))
|
|
498
|
+
return T.set_subtensor(output[indices], X)
|
|
499
|
+
|
|
500
|
+
def get_config(self):
|
|
501
|
+
config = {"name": self.__class__.__name__,
|
|
502
|
+
"padding": self.padding}
|
|
503
|
+
base_config = super(ZeroPadding2D, self).get_config()
|
|
504
|
+
return dict(list(base_config.items()) + list(config.items()))
|