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,318 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
from __future__ import absolute_import
|
|
3
|
+
from __future__ import print_function
|
|
4
|
+
|
|
5
|
+
from collections import OrderedDict
|
|
6
|
+
import theano.tensor as T
|
|
7
|
+
from ..layers.core import Layer, Merge
|
|
8
|
+
from ..utils.theano_utils import ndim_tensor
|
|
9
|
+
from six.moves import range
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Sequential(Layer):
|
|
13
|
+
'''
|
|
14
|
+
Simple linear stack of layers.
|
|
15
|
+
|
|
16
|
+
inherited from Layer:
|
|
17
|
+
- get_params
|
|
18
|
+
- get_output_mask
|
|
19
|
+
- supports_masked_input
|
|
20
|
+
'''
|
|
21
|
+
|
|
22
|
+
def __init__(self, layers=[]):
|
|
23
|
+
self.layers = []
|
|
24
|
+
for layer in layers:
|
|
25
|
+
self.add(layer)
|
|
26
|
+
|
|
27
|
+
def set_previous(self, layer):
|
|
28
|
+
self.layers[0].previous = layer
|
|
29
|
+
|
|
30
|
+
def add(self, layer):
|
|
31
|
+
self.layers.append(layer)
|
|
32
|
+
if len(self.layers) > 1:
|
|
33
|
+
self.layers[-1].set_previous(self.layers[-2])
|
|
34
|
+
if not hasattr(self.layers[0], 'input'):
|
|
35
|
+
self.set_input()
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def params(self):
|
|
39
|
+
params = []
|
|
40
|
+
for l in self.layers:
|
|
41
|
+
if l.trainable:
|
|
42
|
+
params += l.get_params()[0]
|
|
43
|
+
return params
|
|
44
|
+
|
|
45
|
+
@property
|
|
46
|
+
def regularizers(self):
|
|
47
|
+
regularizers = []
|
|
48
|
+
for l in self.layers:
|
|
49
|
+
if l.trainable:
|
|
50
|
+
regularizers += l.get_params()[1]
|
|
51
|
+
return regularizers
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def constraints(self):
|
|
55
|
+
constraints = []
|
|
56
|
+
for l in self.layers:
|
|
57
|
+
if l.trainable:
|
|
58
|
+
constraints += l.get_params()[2]
|
|
59
|
+
return constraints
|
|
60
|
+
|
|
61
|
+
@property
|
|
62
|
+
def updates(self):
|
|
63
|
+
updates = []
|
|
64
|
+
for l in self.layers:
|
|
65
|
+
if l.trainable:
|
|
66
|
+
updates += l.get_params()[3]
|
|
67
|
+
return updates
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def output_shape(self):
|
|
71
|
+
return self.layers[-1].output_shape
|
|
72
|
+
|
|
73
|
+
def get_output(self, train=False):
|
|
74
|
+
return self.layers[-1].get_output(train)
|
|
75
|
+
|
|
76
|
+
def set_input(self):
|
|
77
|
+
for l in self.layers:
|
|
78
|
+
if hasattr(l, 'input'):
|
|
79
|
+
ndim = l.input.ndim
|
|
80
|
+
self.layers[0].input = ndim_tensor(ndim)
|
|
81
|
+
break
|
|
82
|
+
|
|
83
|
+
def get_input(self, train=False):
|
|
84
|
+
if not hasattr(self.layers[0], 'input'):
|
|
85
|
+
self.set_input()
|
|
86
|
+
return self.layers[0].get_input(train)
|
|
87
|
+
|
|
88
|
+
@property
|
|
89
|
+
def input_shape(self):
|
|
90
|
+
return self.layers[0].input_shape
|
|
91
|
+
|
|
92
|
+
@property
|
|
93
|
+
def input(self):
|
|
94
|
+
return self.get_input()
|
|
95
|
+
|
|
96
|
+
def get_weights(self):
|
|
97
|
+
weights = []
|
|
98
|
+
for layer in self.layers:
|
|
99
|
+
weights += layer.get_weights()
|
|
100
|
+
return weights
|
|
101
|
+
|
|
102
|
+
def set_weights(self, weights):
|
|
103
|
+
for i in range(len(self.layers)):
|
|
104
|
+
nb_param = len(self.layers[i].params)
|
|
105
|
+
self.layers[i].set_weights(weights[:nb_param])
|
|
106
|
+
weights = weights[nb_param:]
|
|
107
|
+
|
|
108
|
+
def get_config(self):
|
|
109
|
+
return {"name": self.__class__.__name__,
|
|
110
|
+
"layers": [layer.get_config() for layer in self.layers]}
|
|
111
|
+
|
|
112
|
+
def count_params(self):
|
|
113
|
+
return sum([layer.count_params() for layer in self.layers])
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
class Graph(Layer):
|
|
117
|
+
'''
|
|
118
|
+
Implement a NN graph with arbitrary layer connections,
|
|
119
|
+
arbitrary number of inputs and arbitrary number of outputs.
|
|
120
|
+
|
|
121
|
+
Note: Graph can only be used as a layer
|
|
122
|
+
(connect, input, get_input, get_output)
|
|
123
|
+
when it has exactly one input and one output.
|
|
124
|
+
|
|
125
|
+
inherited from Layer:
|
|
126
|
+
- get_output_mask
|
|
127
|
+
- supports_masked_input
|
|
128
|
+
- get_weights
|
|
129
|
+
- set_weights
|
|
130
|
+
'''
|
|
131
|
+
def __init__(self):
|
|
132
|
+
self.namespace = set() # strings
|
|
133
|
+
self.nodes = OrderedDict() # layer-like
|
|
134
|
+
self.inputs = {} # layer-like
|
|
135
|
+
self.input_order = [] # strings
|
|
136
|
+
self.outputs = {} # layer-like
|
|
137
|
+
self.output_order = [] # strings
|
|
138
|
+
self.input_config = [] # dicts
|
|
139
|
+
self.output_config = [] # dicts
|
|
140
|
+
self.node_config = [] # dicts
|
|
141
|
+
|
|
142
|
+
@property
|
|
143
|
+
def nb_input(self):
|
|
144
|
+
return len(self.inputs)
|
|
145
|
+
|
|
146
|
+
@property
|
|
147
|
+
def nb_output(self):
|
|
148
|
+
return len(self.outputs)
|
|
149
|
+
|
|
150
|
+
@property
|
|
151
|
+
def params(self):
|
|
152
|
+
params = []
|
|
153
|
+
for l in self.nodes.values():
|
|
154
|
+
if l.trainable:
|
|
155
|
+
params += l.get_params()[0]
|
|
156
|
+
return params
|
|
157
|
+
|
|
158
|
+
@property
|
|
159
|
+
def regularizers(self):
|
|
160
|
+
regularizers = []
|
|
161
|
+
for l in self.nodes.values():
|
|
162
|
+
if l.trainable:
|
|
163
|
+
regularizers += l.get_params()[1]
|
|
164
|
+
return regularizers
|
|
165
|
+
|
|
166
|
+
@property
|
|
167
|
+
def constraints(self):
|
|
168
|
+
constraints = []
|
|
169
|
+
for l in self.nodes.values():
|
|
170
|
+
if l.trainable:
|
|
171
|
+
constraints += l.get_params()[2]
|
|
172
|
+
return constraints
|
|
173
|
+
|
|
174
|
+
@property
|
|
175
|
+
def updates(self):
|
|
176
|
+
updates = []
|
|
177
|
+
for l in self.nodes.values():
|
|
178
|
+
if l.trainable:
|
|
179
|
+
updates += l.get_params()[3]
|
|
180
|
+
return updates
|
|
181
|
+
|
|
182
|
+
def set_previous(self, layer, connection_map={}):
|
|
183
|
+
if self.nb_input != layer.nb_output:
|
|
184
|
+
raise Exception('Cannot connect layers: input count does not match output count.')
|
|
185
|
+
if self.nb_input == 1:
|
|
186
|
+
self.inputs[self.input_order[0]].set_previous(layer)
|
|
187
|
+
else:
|
|
188
|
+
if not connection_map:
|
|
189
|
+
raise Exception('Cannot attach multi-input layer: no connection_map provided.')
|
|
190
|
+
for k, v in connection_map.items():
|
|
191
|
+
if k in self.inputs and v in layer.outputs:
|
|
192
|
+
self.inputs[k].set_previous(layer.outputs[v])
|
|
193
|
+
else:
|
|
194
|
+
raise Exception('Invalid connection map.')
|
|
195
|
+
|
|
196
|
+
def get_input(self, train=False):
|
|
197
|
+
if len(self.inputs) == len(self.outputs) == 1:
|
|
198
|
+
return self.inputs[self.input_order[0]].get_input(train)
|
|
199
|
+
else:
|
|
200
|
+
return dict([(k, v.get_input(train)) for k, v in self.inputs.items()])
|
|
201
|
+
|
|
202
|
+
@property
|
|
203
|
+
def input(self):
|
|
204
|
+
return self.get_input()
|
|
205
|
+
|
|
206
|
+
@property
|
|
207
|
+
def output_shape(self):
|
|
208
|
+
if self.nb_output == 1:
|
|
209
|
+
# return tuple
|
|
210
|
+
return self.outputs[self.output_order[0]].output_shape
|
|
211
|
+
else:
|
|
212
|
+
# return dictionary mapping output names to shape tuples
|
|
213
|
+
return dict([(k, v.output_shape) for k, v in self.outputs.items()])
|
|
214
|
+
|
|
215
|
+
def get_output(self, train=False):
|
|
216
|
+
if len(self.inputs) == len(self.outputs) == 1:
|
|
217
|
+
return self.outputs[self.output_order[0]].get_output(train)
|
|
218
|
+
else:
|
|
219
|
+
return dict([(k, v.get_output(train)) for k, v in self.outputs.items()])
|
|
220
|
+
|
|
221
|
+
def add_input(self, name, input_shape, dtype='float'):
|
|
222
|
+
if name in self.namespace:
|
|
223
|
+
raise Exception('Duplicate node identifier: ' + name)
|
|
224
|
+
self.namespace.add(name)
|
|
225
|
+
self.input_order.append(name)
|
|
226
|
+
layer = Layer() # empty layer
|
|
227
|
+
layer.set_input_shape(input_shape)
|
|
228
|
+
ndim = len(input_shape) + 1
|
|
229
|
+
if dtype == 'float':
|
|
230
|
+
layer.input = ndim_tensor(ndim)
|
|
231
|
+
else:
|
|
232
|
+
if ndim == 2:
|
|
233
|
+
layer.input = T.imatrix()
|
|
234
|
+
else:
|
|
235
|
+
raise Exception('Type "int" can only be used with ndim==2 (Embedding).')
|
|
236
|
+
layer.input.name = name
|
|
237
|
+
self.inputs[name] = layer
|
|
238
|
+
self.input_config.append({'name': name,
|
|
239
|
+
'input_shape': input_shape,
|
|
240
|
+
'dtype': dtype})
|
|
241
|
+
|
|
242
|
+
def add_node(self, layer, name, input=None, inputs=[],
|
|
243
|
+
merge_mode='concat', concat_axis=-1, dot_axes=-1, create_output=False):
|
|
244
|
+
if hasattr(layer, 'set_name'):
|
|
245
|
+
layer.set_name(name)
|
|
246
|
+
if name in self.namespace:
|
|
247
|
+
raise Exception('Duplicate node identifier: ' + name)
|
|
248
|
+
if input:
|
|
249
|
+
if input not in self.namespace:
|
|
250
|
+
raise Exception('Unknown node/input identifier: ' + input)
|
|
251
|
+
if input in self.nodes:
|
|
252
|
+
layer.set_previous(self.nodes[input])
|
|
253
|
+
elif input in self.inputs:
|
|
254
|
+
layer.set_previous(self.inputs[input])
|
|
255
|
+
if inputs:
|
|
256
|
+
to_merge = []
|
|
257
|
+
for n in inputs:
|
|
258
|
+
if n in self.nodes:
|
|
259
|
+
to_merge.append(self.nodes[n])
|
|
260
|
+
elif n in self.inputs:
|
|
261
|
+
to_merge.append(self.inputs[n])
|
|
262
|
+
else:
|
|
263
|
+
raise Exception('Unknown identifier: ' + n)
|
|
264
|
+
merge = Merge(to_merge, mode=merge_mode, concat_axis=concat_axis, dot_axes=dot_axes)
|
|
265
|
+
layer.set_previous(merge)
|
|
266
|
+
|
|
267
|
+
self.namespace.add(name)
|
|
268
|
+
self.nodes[name] = layer
|
|
269
|
+
self.node_config.append({'name': name,
|
|
270
|
+
'input': input,
|
|
271
|
+
'inputs': inputs,
|
|
272
|
+
'merge_mode': merge_mode,
|
|
273
|
+
'concat_axis': concat_axis,
|
|
274
|
+
'dot_axes': dot_axes,
|
|
275
|
+
'create_output': create_output})
|
|
276
|
+
|
|
277
|
+
if create_output:
|
|
278
|
+
self.add_output(name, input=name)
|
|
279
|
+
|
|
280
|
+
def add_output(self, name, input=None, inputs=[],
|
|
281
|
+
merge_mode='concat', concat_axis=-1, dot_axes=-1):
|
|
282
|
+
if name in self.output_order:
|
|
283
|
+
raise Exception('Duplicate output identifier: ' + name)
|
|
284
|
+
if input:
|
|
285
|
+
if input not in self.namespace:
|
|
286
|
+
raise Exception('Unknown node/input identifier: ' + input)
|
|
287
|
+
if input in self.nodes:
|
|
288
|
+
self.outputs[name] = self.nodes[input]
|
|
289
|
+
elif input in self.inputs:
|
|
290
|
+
self.outputs[name] = self.inputs[input]
|
|
291
|
+
if inputs:
|
|
292
|
+
to_merge = []
|
|
293
|
+
for n in inputs:
|
|
294
|
+
if n not in self.nodes:
|
|
295
|
+
raise Exception('Unknown identifier: ' + n)
|
|
296
|
+
to_merge.append(self.nodes[n])
|
|
297
|
+
merge = Merge(to_merge, mode=merge_mode, concat_axis=concat_axis, dot_axes=dot_axes)
|
|
298
|
+
self.outputs[name] = merge
|
|
299
|
+
|
|
300
|
+
self.output_order.append(name)
|
|
301
|
+
self.output_config.append({'name': name,
|
|
302
|
+
'input': input,
|
|
303
|
+
'inputs': inputs,
|
|
304
|
+
'merge_mode': merge_mode,
|
|
305
|
+
'concat_axis': concat_axis,
|
|
306
|
+
'dot_axes': dot_axes})
|
|
307
|
+
|
|
308
|
+
def get_config(self):
|
|
309
|
+
return {"name": self.__class__.__name__,
|
|
310
|
+
"input_config": self.input_config,
|
|
311
|
+
"node_config": self.node_config,
|
|
312
|
+
"output_config": self.output_config,
|
|
313
|
+
"input_order": self.input_order,
|
|
314
|
+
"output_order": self.output_order,
|
|
315
|
+
"nodes": dict([(c["name"], self.nodes[c["name"]].get_config()) for c in self.node_config])}
|
|
316
|
+
|
|
317
|
+
def count_params(self):
|
|
318
|
+
return sum([layer.count_params() for layer in self.nodes.values()])
|