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.
Files changed (106) hide show
  1. EGG-INFO/PKG-INFO +11 -0
  2. EGG-INFO/SOURCES.txt +57 -0
  3. EGG-INFO/dependency_links.txt +1 -0
  4. EGG-INFO/requires.txt +6 -0
  5. EGG-INFO/top_level.txt +2 -0
  6. EGG-INFO/zip-safe +1 -0
  7. keras/__init__.py +15 -0
  8. keras/__pycache__/__init__.cpython-35.pyc +0 -0
  9. keras/__pycache__/activations.cpython-35.pyc +0 -0
  10. keras/__pycache__/callbacks.cpython-35.pyc +0 -0
  11. keras/__pycache__/constraints.cpython-35.pyc +0 -0
  12. keras/__pycache__/initializations.cpython-35.pyc +0 -0
  13. keras/__pycache__/models.cpython-35.pyc +0 -0
  14. keras/__pycache__/objectives.cpython-35.pyc +0 -0
  15. keras/__pycache__/optimizers.cpython-35.pyc +0 -0
  16. keras/__pycache__/regularizers.cpython-35.pyc +0 -0
  17. keras/activations.py +44 -0
  18. keras/callbacks.py +293 -0
  19. keras/constraints.py +48 -0
  20. keras/datasets/__init__.py +0 -0
  21. keras/datasets/__pycache__/__init__.cpython-35.pyc +0 -0
  22. keras/datasets/__pycache__/cifar.cpython-35.pyc +0 -0
  23. keras/datasets/__pycache__/cifar10.cpython-35.pyc +0 -0
  24. keras/datasets/__pycache__/cifar100.cpython-35.pyc +0 -0
  25. keras/datasets/__pycache__/data_utils.cpython-35.pyc +0 -0
  26. keras/datasets/__pycache__/imdb.cpython-35.pyc +0 -0
  27. keras/datasets/__pycache__/mnist.cpython-35.pyc +0 -0
  28. keras/datasets/__pycache__/reuters.cpython-35.pyc +0 -0
  29. keras/datasets/cifar.py +22 -0
  30. keras/datasets/cifar10.py +31 -0
  31. keras/datasets/cifar100.py +28 -0
  32. keras/datasets/data_utils.py +50 -0
  33. keras/datasets/imdb.py +66 -0
  34. keras/datasets/mnist.py +23 -0
  35. keras/datasets/reuters.py +148 -0
  36. keras/initializations.py +89 -0
  37. keras/layers/__init__.py +0 -0
  38. keras/layers/__pycache__/__init__.cpython-35.pyc +0 -0
  39. keras/layers/__pycache__/advanced_activations.cpython-35.pyc +0 -0
  40. keras/layers/__pycache__/containers.cpython-35.pyc +0 -0
  41. keras/layers/__pycache__/convolutional.cpython-35.pyc +0 -0
  42. keras/layers/__pycache__/core.cpython-35.pyc +0 -0
  43. keras/layers/__pycache__/embeddings.cpython-35.pyc +0 -0
  44. keras/layers/__pycache__/noise.cpython-35.pyc +0 -0
  45. keras/layers/__pycache__/normalization.cpython-35.pyc +0 -0
  46. keras/layers/__pycache__/recurrent.cpython-35.pyc +0 -0
  47. keras/layers/advanced_activations.py +137 -0
  48. keras/layers/containers.py +318 -0
  49. keras/layers/convolutional.py +504 -0
  50. keras/layers/core.py +963 -0
  51. keras/layers/embeddings.py +153 -0
  52. keras/layers/noise.py +57 -0
  53. keras/layers/normalization.py +117 -0
  54. keras/layers/recurrent.py +837 -0
  55. keras/models.py +733 -0
  56. keras/objectives.py +67 -0
  57. keras/optimizers.py +228 -0
  58. keras/preprocessing/__init__.py +0 -0
  59. keras/preprocessing/__pycache__/__init__.cpython-35.pyc +0 -0
  60. keras/preprocessing/__pycache__/image.cpython-35.pyc +0 -0
  61. keras/preprocessing/__pycache__/sequence.cpython-35.pyc +0 -0
  62. keras/preprocessing/__pycache__/text.cpython-35.pyc +0 -0
  63. keras/preprocessing/image.py +251 -0
  64. keras/preprocessing/sequence.py +124 -0
  65. keras/preprocessing/text.py +176 -0
  66. keras/regularizers.py +84 -0
  67. keras/utils/__init__.py +0 -0
  68. keras/utils/__pycache__/__init__.cpython-35.pyc +0 -0
  69. keras/utils/__pycache__/generic_utils.cpython-35.pyc +0 -0
  70. keras/utils/__pycache__/io_utils.cpython-35.pyc +0 -0
  71. keras/utils/__pycache__/layer_utils.cpython-35.pyc +0 -0
  72. keras/utils/__pycache__/np_utils.cpython-35.pyc +0 -0
  73. keras/utils/__pycache__/test_utils.cpython-35.pyc +0 -0
  74. keras/utils/__pycache__/theano_utils.cpython-35.pyc +0 -0
  75. keras/utils/__pycache__/visualize_util.cpython-35.pyc +0 -0
  76. keras/utils/generic_utils.py +133 -0
  77. keras/utils/io_utils.py +71 -0
  78. keras/utils/layer_utils.py +129 -0
  79. keras/utils/np_utils.py +53 -0
  80. keras/utils/test_utils.py +27 -0
  81. keras/utils/theano_utils.py +44 -0
  82. keras/utils/visualize_util.py +41 -0
  83. keras/wrappers/__init__.py +0 -0
  84. keras/wrappers/__pycache__/__init__.cpython-35.pyc +0 -0
  85. keras/wrappers/__pycache__/scikit_learn.cpython-35.pyc +0 -0
  86. keras/wrappers/scikit_learn.py +266 -0
  87. tests/__init__.py +0 -0
  88. tests/__pycache__/__init__.cpython-35.pyc +0 -0
  89. tests/manual/__init__.py +0 -0
  90. tests/manual/__pycache__/__init__.cpython-35.pyc +0 -0
  91. tests/manual/__pycache__/check_autoencoder.cpython-35.pyc +0 -0
  92. tests/manual/__pycache__/check_callbacks.cpython-35.pyc +0 -0
  93. tests/manual/__pycache__/check_constraints.cpython-35.pyc +0 -0
  94. tests/manual/__pycache__/check_masked_recurrent.cpython-35.pyc +0 -0
  95. tests/manual/__pycache__/check_model_utils.cpython-35.pyc +0 -0
  96. tests/manual/__pycache__/check_save_weights.cpython-35.pyc +0 -0
  97. tests/manual/__pycache__/check_wrappers.cpython-35.pyc +0 -0
  98. tests/manual/__pycache__/check_yaml.cpython-35.pyc +0 -0
  99. tests/manual/check_autoencoder.py +134 -0
  100. tests/manual/check_callbacks.py +236 -0
  101. tests/manual/check_constraints.py +100 -0
  102. tests/manual/check_masked_recurrent.py +130 -0
  103. tests/manual/check_model_utils.py +44 -0
  104. tests/manual/check_save_weights.py +40 -0
  105. tests/manual/check_wrappers.py +126 -0
  106. tests/manual/check_yaml.py +101 -0
EGG-INFO/PKG-INFO ADDED
@@ -0,0 +1,11 @@
1
+ Metadata-Version: 1.1
2
+ Name: Keras
3
+ Version: 0.2.0
4
+ Summary: Theano-based Deep Learning library
5
+ Home-page: https://github.com/fchollet/keras
6
+ Author: Francois Chollet
7
+ Author-email: francois.chollet@gmail.com
8
+ License: MIT
9
+ Download-URL: https://github.com/fchollet/keras/tarball/0.2.0
10
+ Description: UNKNOWN
11
+ Platform: UNKNOWN
EGG-INFO/SOURCES.txt ADDED
@@ -0,0 +1,57 @@
1
+ setup.cfg
2
+ setup.py
3
+ Keras.egg-info/PKG-INFO
4
+ Keras.egg-info/SOURCES.txt
5
+ Keras.egg-info/dependency_links.txt
6
+ Keras.egg-info/requires.txt
7
+ Keras.egg-info/top_level.txt
8
+ keras/__init__.py
9
+ keras/activations.py
10
+ keras/callbacks.py
11
+ keras/constraints.py
12
+ keras/initializations.py
13
+ keras/models.py
14
+ keras/objectives.py
15
+ keras/optimizers.py
16
+ keras/regularizers.py
17
+ keras/datasets/__init__.py
18
+ keras/datasets/cifar.py
19
+ keras/datasets/cifar10.py
20
+ keras/datasets/cifar100.py
21
+ keras/datasets/data_utils.py
22
+ keras/datasets/imdb.py
23
+ keras/datasets/mnist.py
24
+ keras/datasets/reuters.py
25
+ keras/layers/__init__.py
26
+ keras/layers/advanced_activations.py
27
+ keras/layers/containers.py
28
+ keras/layers/convolutional.py
29
+ keras/layers/core.py
30
+ keras/layers/embeddings.py
31
+ keras/layers/noise.py
32
+ keras/layers/normalization.py
33
+ keras/layers/recurrent.py
34
+ keras/preprocessing/__init__.py
35
+ keras/preprocessing/image.py
36
+ keras/preprocessing/sequence.py
37
+ keras/preprocessing/text.py
38
+ keras/utils/__init__.py
39
+ keras/utils/generic_utils.py
40
+ keras/utils/io_utils.py
41
+ keras/utils/layer_utils.py
42
+ keras/utils/np_utils.py
43
+ keras/utils/test_utils.py
44
+ keras/utils/theano_utils.py
45
+ keras/utils/visualize_util.py
46
+ keras/wrappers/__init__.py
47
+ keras/wrappers/scikit_learn.py
48
+ tests/__init__.py
49
+ tests/manual/__init__.py
50
+ tests/manual/check_autoencoder.py
51
+ tests/manual/check_callbacks.py
52
+ tests/manual/check_constraints.py
53
+ tests/manual/check_masked_recurrent.py
54
+ tests/manual/check_model_utils.py
55
+ tests/manual/check_save_weights.py
56
+ tests/manual/check_wrappers.py
57
+ tests/manual/check_yaml.py
@@ -0,0 +1 @@
1
+
EGG-INFO/requires.txt ADDED
@@ -0,0 +1,6 @@
1
+ theano
2
+ pyyaml
3
+ six
4
+
5
+ [h5py]
6
+ h5py
EGG-INFO/top_level.txt ADDED
@@ -0,0 +1,2 @@
1
+ keras
2
+ tests
EGG-INFO/zip-safe ADDED
@@ -0,0 +1 @@
1
+
keras/__init__.py ADDED
@@ -0,0 +1,15 @@
1
+
2
+ """
3
+ Keras: Theano-based Deep Learning library
4
+ ==================================
5
+ Keras is a minimalist, highly modular neural network library in
6
+ the spirit of Torch, written in Python / Theano so as not to have
7
+ to deal with the dearth of ecosystem in Lua. It was developed with
8
+ a focus on enabling fast experimentation. Being able to go from
9
+ idea to result with the least possible delay is key to doing
10
+ good research.
11
+
12
+ See http://keras.io/
13
+ """
14
+
15
+ __version__ = '0.2.0'
Binary file
keras/activations.py ADDED
@@ -0,0 +1,44 @@
1
+ from __future__ import absolute_import
2
+ import theano.tensor as T
3
+
4
+
5
+ def softmax(x):
6
+ return T.nnet.softmax(x.reshape((-1, x.shape[-1]))).reshape(x.shape)
7
+
8
+
9
+ def time_distributed_softmax(x):
10
+ import warnings
11
+ warnings.warn("time_distributed_softmax is deprecated. Just use softmax!", DeprecationWarning)
12
+ return softmax(x)
13
+
14
+
15
+ def softplus(x):
16
+ return T.nnet.softplus(x)
17
+
18
+
19
+ def relu(x):
20
+ return T.nnet.relu(x)
21
+
22
+
23
+ def tanh(x):
24
+ return T.tanh(x)
25
+
26
+
27
+ def sigmoid(x):
28
+ return T.nnet.sigmoid(x)
29
+
30
+
31
+ def hard_sigmoid(x):
32
+ return T.nnet.hard_sigmoid(x)
33
+
34
+
35
+ def linear(x):
36
+ '''
37
+ The function returns the variable that is passed in, so all types work
38
+ '''
39
+ return x
40
+
41
+
42
+ from .utils.generic_utils import get_from_module
43
+ def get(identifier):
44
+ return get_from_module(identifier, globals(), 'activation function')
keras/callbacks.py ADDED
@@ -0,0 +1,293 @@
1
+ from __future__ import absolute_import
2
+ from __future__ import print_function
3
+
4
+ import numpy as np
5
+ import time, json, warnings
6
+
7
+ from collections import deque
8
+ from .utils.generic_utils import Progbar
9
+
10
+
11
+ class CallbackList(object):
12
+ def __init__(self, callbacks=[], queue_length=10):
13
+ self.callbacks = [c for c in callbacks]
14
+ self.queue_length = queue_length
15
+
16
+ def append(self, callback):
17
+ self.callbacks.append(callback)
18
+
19
+ def _set_params(self, params):
20
+ for callback in self.callbacks:
21
+ callback._set_params(params)
22
+
23
+ def _set_model(self, model):
24
+ for callback in self.callbacks:
25
+ callback._set_model(model)
26
+
27
+ def on_epoch_begin(self, epoch, logs={}):
28
+ for callback in self.callbacks:
29
+ callback.on_epoch_begin(epoch, logs)
30
+ self._delta_t_batch = 0.
31
+ self._delta_ts_batch_begin = deque([], maxlen=self.queue_length)
32
+ self._delta_ts_batch_end = deque([], maxlen=self.queue_length)
33
+
34
+ def on_epoch_end(self, epoch, logs={}):
35
+ for callback in self.callbacks:
36
+ callback.on_epoch_end(epoch, logs)
37
+
38
+ def on_batch_begin(self, batch, logs={}):
39
+ t_before_callbacks = time.time()
40
+ for callback in self.callbacks:
41
+ callback.on_batch_begin(batch, logs)
42
+ self._delta_ts_batch_begin.append(time.time() - t_before_callbacks)
43
+ delta_t_median = np.median(self._delta_ts_batch_begin)
44
+ if self._delta_t_batch > 0. and delta_t_median > 0.95 * self._delta_t_batch and delta_t_median > 0.1:
45
+ warnings.warn('Method on_batch_begin() is slow compared '
46
+ 'to the batch update (%f). Check your callbacks.' % delta_t_median)
47
+ self._t_enter_batch = time.time()
48
+
49
+ def on_batch_end(self, batch, logs={}):
50
+ self._delta_t_batch = time.time() - self._t_enter_batch
51
+ t_before_callbacks = time.time()
52
+ for callback in self.callbacks:
53
+ callback.on_batch_end(batch, logs)
54
+ self._delta_ts_batch_end.append(time.time() - t_before_callbacks)
55
+ delta_t_median = np.median(self._delta_ts_batch_end)
56
+ if self._delta_t_batch > 0. and delta_t_median > 0.95 * self._delta_t_batch and delta_t_median > 0.1:
57
+ warnings.warn('Method on_batch_end() is slow compared '
58
+ 'to the batch update (%f). Check your callbacks.' % delta_t_median)
59
+
60
+ def on_train_begin(self, logs={}):
61
+ for callback in self.callbacks:
62
+ callback.on_train_begin(logs)
63
+
64
+ def on_train_end(self, logs={}):
65
+ for callback in self.callbacks:
66
+ callback.on_train_end(logs)
67
+
68
+
69
+ class Callback(object):
70
+
71
+ def __init__(self):
72
+ pass
73
+
74
+ def _set_params(self, params):
75
+ self.params = params
76
+
77
+ def _set_model(self, model):
78
+ self.model = model
79
+
80
+ def on_epoch_begin(self, epoch, logs={}):
81
+ pass
82
+
83
+ def on_epoch_end(self, epoch, logs={}):
84
+ pass
85
+
86
+ def on_batch_begin(self, batch, logs={}):
87
+ pass
88
+
89
+ def on_batch_end(self, batch, logs={}):
90
+ pass
91
+
92
+ def on_train_begin(self, logs={}):
93
+ pass
94
+
95
+ def on_train_end(self, logs={}):
96
+ pass
97
+
98
+
99
+ class BaseLogger(Callback):
100
+ def on_train_begin(self, logs={}):
101
+ self.verbose = self.params['verbose']
102
+ self.nb_epoch = self.params['nb_epoch']
103
+
104
+ def on_epoch_begin(self, epoch, logs={}):
105
+ if self.verbose:
106
+ print('Epoch %d/%d' % (epoch + 1, self.nb_epoch))
107
+ self.progbar = Progbar(target=self.params['nb_sample'],
108
+ verbose=self.verbose)
109
+ self.seen = 0
110
+ self.totals = {}
111
+
112
+ def on_batch_begin(self, batch, logs={}):
113
+ if self.seen < self.params['nb_sample']:
114
+ self.log_values = []
115
+
116
+ def on_batch_end(self, batch, logs={}):
117
+ batch_size = logs.get('size', 0)
118
+ self.seen += batch_size
119
+
120
+ for k, v in logs.items():
121
+ if k in self.totals:
122
+ self.totals[k] += v * batch_size
123
+ else:
124
+ self.totals[k] = v * batch_size
125
+ for k in self.params['metrics']:
126
+ if k in logs:
127
+ self.log_values.append((k, logs[k]))
128
+
129
+ # skip progbar update for the last batch; will be handled by on_epoch_end
130
+ if self.verbose and self.seen < self.params['nb_sample']:
131
+ self.progbar.update(self.seen, self.log_values)
132
+
133
+ def on_epoch_end(self, epoch, logs={}):
134
+ for k in self.params['metrics']:
135
+ if k in self.totals:
136
+ self.log_values.append((k, self.totals[k] / self.seen))
137
+ if k in logs:
138
+ self.log_values.append((k, logs[k]))
139
+ if self.verbose:
140
+ self.progbar.update(self.seen, self.log_values)
141
+
142
+
143
+ class History(Callback):
144
+
145
+ def on_train_begin(self, logs={}):
146
+ self.epoch = []
147
+ self.history = {}
148
+
149
+ def on_epoch_begin(self, epoch, logs={}):
150
+ self.seen = 0
151
+ self.totals = {}
152
+
153
+ def on_batch_end(self, batch, logs={}):
154
+ batch_size = logs.get('size', 0)
155
+ self.seen += batch_size
156
+ for k, v in logs.items():
157
+ if k in self.totals:
158
+ self.totals[k] += v * batch_size
159
+ else:
160
+ self.totals[k] = v * batch_size
161
+
162
+ def on_epoch_end(self, epoch, logs={}):
163
+ self.epoch.append(epoch)
164
+ for k, v in self.totals.items():
165
+ if k not in self.history:
166
+ self.history[k] = []
167
+ self.history[k].append(v / self.seen)
168
+
169
+ for k, v in logs.items():
170
+ if k not in self.history:
171
+ self.history[k] = []
172
+ self.history[k].append(v)
173
+
174
+
175
+ class ModelCheckpoint(Callback):
176
+ def __init__(self, filepath, monitor='val_loss', verbose=0, save_best_only=False, mode='auto'):
177
+
178
+ super(Callback, self).__init__()
179
+ self.monitor = monitor
180
+ self.verbose = verbose
181
+ self.filepath = filepath
182
+ self.save_best_only = save_best_only
183
+
184
+ if mode not in ['auto', 'min', 'max']:
185
+ warnings.warn("ModelCheckpoint mode %s is unknown, fallback to auto mode" % (self.mode), RuntimeWarning)
186
+ mode = 'auto'
187
+
188
+ if mode == "min":
189
+ self.monitor_op = np.less
190
+ self.best = np.Inf
191
+ elif mode == "max":
192
+ self.monitor_op = np.greater
193
+ self.best = -np.Inf
194
+ else:
195
+ if "acc" in self.monitor:
196
+ self.monitor_op = np.greater
197
+ self.best = -np.Inf
198
+ else:
199
+ self.monitor_op = np.less
200
+ self.best = np.Inf
201
+
202
+ def on_epoch_end(self, epoch, logs={}):
203
+ filepath = self.filepath.format(epoch=epoch, **logs)
204
+ if self.save_best_only:
205
+ current = logs.get(self.monitor)
206
+ if current is None:
207
+ warnings.warn("Can save best model only with %s available, skipping." % (self.monitor), RuntimeWarning)
208
+ else:
209
+ if self.monitor_op(current, self.best):
210
+ if self.verbose > 0:
211
+ print("Epoch %05d: %s improved from %0.5f to %0.5f, saving model to %s"
212
+ % (epoch, self.monitor, self.best, current, filepath))
213
+ self.best = current
214
+ self.model.save_weights(filepath, overwrite=True)
215
+ else:
216
+ if self.verbose > 0:
217
+ print("Epoch %05d: %s did not improve" % (epoch, self.monitor))
218
+ else:
219
+ if self.verbose > 0:
220
+ print("Epoch %05d: saving model to %s" % (epoch, filepath))
221
+ self.model.save_weights(filepath, overwrite=True)
222
+
223
+
224
+ class EarlyStopping(Callback):
225
+ def __init__(self, monitor='val_loss', patience=0, verbose=0):
226
+ super(Callback, self).__init__()
227
+
228
+ self.monitor = monitor
229
+ self.patience = patience
230
+ self.verbose = verbose
231
+ self.best = np.Inf
232
+ self.wait = 0
233
+
234
+ def on_epoch_end(self, epoch, logs={}):
235
+ current = logs.get(self.monitor)
236
+ if current is None:
237
+ warnings.warn("Early stopping requires %s available!" % (self.monitor), RuntimeWarning)
238
+
239
+ if current < self.best:
240
+ self.best = current
241
+ self.wait = 0
242
+ else:
243
+ if self.wait >= self.patience:
244
+ if self.verbose > 0:
245
+ print("Epoch %05d: early stopping" % (epoch))
246
+ self.model.stop_training = True
247
+ self.wait += 1
248
+
249
+
250
+ class RemoteMonitor(Callback):
251
+ def __init__(self, root='http://localhost:9000'):
252
+ self.root = root
253
+
254
+ def on_epoch_begin(self, epoch, logs={}):
255
+ self.seen = 0
256
+ self.totals = {}
257
+
258
+ def on_batch_end(self, batch, logs={}):
259
+ batch_size = logs.get('size', 0)
260
+ self.seen += batch_size
261
+ for k, v in logs.items():
262
+ if k in self.totals:
263
+ self.totals[k] += v * batch_size
264
+ else:
265
+ self.totals[k] = v * batch_size
266
+
267
+ def on_epoch_end(self, epoch, logs={}):
268
+ import requests
269
+ send = {}
270
+ send['epoch'] = epoch
271
+
272
+ for k, v in self.totals.items():
273
+ send[k] = v / self.seen
274
+ for k, v in logs.items():
275
+ send[k] = v
276
+
277
+ try:
278
+ r = requests.post(self.root + '/publish/epoch/end/', {'data': json.dumps(send)})
279
+ except:
280
+ print('Warning: could not reach RemoteMonitor root server at ' + str(self.root))
281
+
282
+
283
+ class LearningRateScheduler(Callback):
284
+ '''LearningRateScheduler
285
+ schedule is a function that gets an epoch number as input and returns a new
286
+ learning rate as output.
287
+ '''
288
+ def __init__(self, schedule):
289
+ super(LearningRateScheduler, self).__init__()
290
+ self.schedule = schedule
291
+
292
+ def on_epoch_begin(self, epoch, logs={}):
293
+ self.model.optimizer.lr.set_value(self.schedule(epoch))
keras/constraints.py ADDED
@@ -0,0 +1,48 @@
1
+ from __future__ import absolute_import
2
+ import theano
3
+ import theano.tensor as T
4
+ import numpy as np
5
+
6
+
7
+ class Constraint(object):
8
+ def __call__(self, p):
9
+ return p
10
+
11
+ def get_config(self):
12
+ return {"name": self.__class__.__name__}
13
+
14
+
15
+ class MaxNorm(Constraint):
16
+ def __init__(self, m=2):
17
+ self.m = m
18
+
19
+ def __call__(self, p):
20
+ norms = T.sqrt(T.sum(T.sqr(p), axis=0))
21
+ desired = T.clip(norms, 0, self.m)
22
+ p = p * (desired / (1e-7 + norms))
23
+ return p
24
+
25
+ def get_config(self):
26
+ return {"name": self.__class__.__name__,
27
+ "m": self.m}
28
+
29
+
30
+ class NonNeg(Constraint):
31
+ def __call__(self, p):
32
+ p = theano.shared(p)
33
+ p *= T.ge(p, 0.)
34
+ return p
35
+
36
+
37
+ class UnitNorm(Constraint):
38
+ def __call__(self, p):
39
+ return p / T.sqrt(T.sum(p**2, axis=-1, keepdims=True))
40
+
41
+ identity = Constraint
42
+ maxnorm = MaxNorm
43
+ nonneg = NonNeg
44
+ unitnorm = UnitNorm
45
+
46
+ from .utils.generic_utils import get_from_module
47
+ def get(identifier, kwargs=None):
48
+ return get_from_module(identifier, globals(), 'constraint', instantiate=True, kwargs=kwargs)
File without changes
@@ -0,0 +1,22 @@
1
+ # -*- coding: utf-8 -*-
2
+ from __future__ import absolute_import
3
+ import sys
4
+ from six.moves import cPickle
5
+ from six.moves import range
6
+
7
+ def load_batch(fpath, label_key='labels'):
8
+ f = open(fpath, 'rb')
9
+ if sys.version_info < (3,):
10
+ d = cPickle.load(f)
11
+ else:
12
+ d = cPickle.load(f, encoding="bytes")
13
+ # decode utf8
14
+ for k, v in d.items():
15
+ del(d[k])
16
+ d[k.decode("utf8")] = v
17
+ f.close()
18
+ data = d["data"]
19
+ labels = d[label_key]
20
+
21
+ data = data.reshape(data.shape[0], 3, 32, 32)
22
+ return data, labels
@@ -0,0 +1,31 @@
1
+ from __future__ import absolute_import
2
+ from .cifar import load_batch
3
+ from .data_utils import get_file
4
+ import numpy as np
5
+ import os
6
+
7
+
8
+ def load_data():
9
+ dirname = "cifar-10-batches-py"
10
+ origin = "http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz"
11
+ path = get_file(dirname, origin=origin, untar=True)
12
+
13
+ nb_test_samples = 10000
14
+ nb_train_samples = 50000
15
+
16
+ X_train = np.zeros((nb_train_samples, 3, 32, 32), dtype="uint8")
17
+ y_train = np.zeros((nb_train_samples,), dtype="uint8")
18
+
19
+ for i in range(1, 6):
20
+ fpath = os.path.join(path, 'data_batch_' + str(i))
21
+ data, labels = load_batch(fpath)
22
+ X_train[(i-1)*10000:i*10000, :, :, :] = data
23
+ y_train[(i-1)*10000:i*10000] = labels
24
+
25
+ fpath = os.path.join(path, 'test_batch')
26
+ X_test, y_test = load_batch(fpath)
27
+
28
+ y_train = np.reshape(y_train, (len(y_train), 1))
29
+ y_test = np.reshape(y_test, (len(y_test), 1))
30
+
31
+ return (X_train, y_train), (X_test, y_test)
@@ -0,0 +1,28 @@
1
+ from __future__ import absolute_import
2
+ from .cifar import load_batch
3
+ from .data_utils import get_file
4
+ import numpy as np
5
+ import os
6
+
7
+
8
+ def load_data(label_mode='fine'):
9
+ if label_mode not in ['fine', 'coarse']:
10
+ raise Exception('label_mode must be one of "fine" "coarse".')
11
+
12
+ dirname = "cifar-100-python"
13
+ origin = "http://www.cs.toronto.edu/~kriz/cifar-100-python.tar.gz"
14
+ path = get_file(dirname, origin=origin, untar=True)
15
+
16
+ nb_test_samples = 10000
17
+ nb_train_samples = 50000
18
+
19
+ fpath = os.path.join(path, 'train')
20
+ X_train, y_train = load_batch(fpath, label_key=label_mode+'_labels')
21
+
22
+ fpath = os.path.join(path, 'test')
23
+ X_test, y_test = load_batch(fpath, label_key=label_mode+'_labels')
24
+
25
+ y_train = np.reshape(y_train, (len(y_train), 1))
26
+ y_test = np.reshape(y_test, (len(y_test), 1))
27
+
28
+ return (X_train, y_train), (X_test, y_test)
@@ -0,0 +1,50 @@
1
+ from __future__ import absolute_import
2
+ from __future__ import print_function
3
+
4
+ import tarfile, inspect, os
5
+ from six.moves.urllib.request import FancyURLopener
6
+
7
+ from ..utils.generic_utils import Progbar
8
+
9
+ class ParanoidURLopener(FancyURLopener):
10
+ def http_error_default(self, url, fp, errcode, errmsg, headers):
11
+ raise Exception('URL fetch failure on {}: {} -- {}'.format(url, errcode, errmsg))
12
+
13
+ def get_file(fname, origin, untar=False):
14
+ datadir = os.path.expanduser(os.path.join('~', '.keras', 'datasets'))
15
+ if not os.path.exists(datadir):
16
+ os.makedirs(datadir)
17
+
18
+ if untar:
19
+ untar_fpath = os.path.join(datadir, fname)
20
+ fpath = untar_fpath + '.tar.gz'
21
+ else:
22
+ fpath = os.path.join(datadir, fname)
23
+
24
+ try:
25
+ f = open(fpath)
26
+ except:
27
+ print('Downloading data from', origin)
28
+
29
+ global progbar
30
+ progbar = None
31
+
32
+ def dl_progress(count, block_size, total_size):
33
+ global progbar
34
+ if progbar is None:
35
+ progbar = Progbar(total_size)
36
+ else:
37
+ progbar.update(count*block_size)
38
+
39
+ ParanoidURLopener().retrieve(origin, fpath, dl_progress)
40
+ progbar = None
41
+
42
+ if untar:
43
+ if not os.path.exists(untar_fpath):
44
+ print('Untaring file...')
45
+ tfile = tarfile.open(fpath, 'r:gz')
46
+ tfile.extractall(path=datadir)
47
+ tfile.close()
48
+ return untar_fpath
49
+
50
+ return fpath