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