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
@@ -0,0 +1,153 @@
1
+ from __future__ import absolute_import
2
+ import theano
3
+ import theano.tensor as T
4
+
5
+ from .. import activations, initializations, regularizers, constraints
6
+ from ..layers.core import Layer, MaskedLayer
7
+ from ..utils.theano_utils import sharedX
8
+
9
+ from ..constraints import unitnorm
10
+
11
+
12
+ class Embedding(Layer):
13
+ '''
14
+ Turn positive integers (indexes) into denses vectors of fixed size.
15
+ eg. [[4], [20]] -> [[0.25, 0.1], [0.6, -0.2]]
16
+
17
+ @input_dim: size of vocabulary (highest input integer + 1)
18
+ @out_dim: size of dense representation
19
+ '''
20
+ input_ndim = 2
21
+
22
+ def __init__(self, input_dim, output_dim, init='uniform', input_length=None,
23
+ W_regularizer=None, activity_regularizer=None, W_constraint=None,
24
+ mask_zero=False, weights=None, **kwargs):
25
+ self.input_dim = input_dim
26
+ self.output_dim = output_dim
27
+ self.init = initializations.get(init)
28
+ self.input_length = input_length
29
+ self.mask_zero = mask_zero
30
+
31
+ self.W_constraint = constraints.get(W_constraint)
32
+ self.constraints = [self.W_constraint]
33
+
34
+ self.W_regularizer = regularizers.get(W_regularizer)
35
+ self.activity_regularizer = regularizers.get(activity_regularizer)
36
+
37
+ self.initial_weights = weights
38
+ kwargs['input_shape'] = (self.input_dim,)
39
+ super(Embedding, self).__init__(**kwargs)
40
+
41
+ def build(self):
42
+ self.input = T.imatrix()
43
+ self.W = self.init((self.input_dim, self.output_dim))
44
+ self.params = [self.W]
45
+ self.regularizers = []
46
+ if self.W_regularizer:
47
+ self.W_regularizer.set_param(self.W)
48
+ self.regularizers.append(self.W_regularizer)
49
+
50
+ if self.activity_regularizer:
51
+ self.activity_regularizer.set_layer(self)
52
+ self.regularizers.append(self.activity_regularizer)
53
+
54
+ if self.initial_weights is not None:
55
+ self.set_weights(self.initial_weights)
56
+
57
+ def get_output_mask(self, train=None):
58
+ X = self.get_input(train)
59
+ if not self.mask_zero:
60
+ return None
61
+ else:
62
+ return T.ones_like(X) * (1 - T.eq(X, 0))
63
+
64
+ @property
65
+ def output_shape(self):
66
+ return (self.input_shape[0], self.input_length, self.output_dim)
67
+
68
+ def get_output(self, train=False):
69
+ X = self.get_input(train)
70
+ out = self.W[X]
71
+ return out
72
+
73
+ def get_config(self):
74
+ config = {"name": self.__class__.__name__,
75
+ "input_dim": self.input_dim,
76
+ "output_dim": self.output_dim,
77
+ "init": self.init.__name__,
78
+ "input_length": self.input_length,
79
+ "mask_zero": self.mask_zero,
80
+ "activity_regularizer": self.activity_regularizer.get_config() if self.activity_regularizer else None,
81
+ "W_regularizer": self.W_regularizer.get_config() if self.W_regularizer else None,
82
+ "W_constraint": self.W_constraint.get_config() if self.W_constraint else None}
83
+ base_config = super(Embedding, self).get_config()
84
+ return dict(list(base_config.items()) + list(config.items()))
85
+
86
+
87
+ class WordContextProduct(Layer):
88
+ '''
89
+ This layer turns a pair of words (a pivot word + a context word,
90
+ ie. a word from the same context, or a random, out-of-context word),
91
+ identified by their index in a vocabulary, into two dense representations
92
+ (word representation and context representation).
93
+
94
+ Then it returns activation(dot(pivot_embedding, context_embedding)),
95
+ which can be trained to encode the probability
96
+ of finding the context word in the context of the pivot word
97
+ (or reciprocally depending on your training procedure).
98
+
99
+ The layer ingests integer tensors of shape:
100
+ (nb_samples, 2)
101
+ and outputs a float tensor of shape
102
+ (nb_samples, 1)
103
+
104
+ The 2nd dimension encodes (pivot, context).
105
+ input_dim is the size of the vocabulary.
106
+
107
+ For more context, see Mikolov et al.:
108
+ Efficient Estimation of Word representations in Vector Space
109
+ http://arxiv.org/pdf/1301.3781v3.pdf
110
+ '''
111
+ input_ndim = 2
112
+
113
+ def __init__(self, input_dim, proj_dim=128,
114
+ init='uniform', activation='sigmoid', weights=None, **kwargs):
115
+
116
+ super(WordContextProduct, self).__init__(**kwargs)
117
+ self.input_dim = input_dim
118
+ self.proj_dim = proj_dim
119
+ self.init = initializations.get(init)
120
+ self.activation = activations.get(activation)
121
+
122
+ self.input = T.imatrix()
123
+ # two different embeddings for pivot word and its context
124
+ # because p(w|c) != p(c|w)
125
+ self.W_w = self.init((input_dim, proj_dim))
126
+ self.W_c = self.init((input_dim, proj_dim))
127
+
128
+ self.params = [self.W_w, self.W_c]
129
+
130
+ if weights is not None:
131
+ self.set_weights(weights)
132
+
133
+ @property
134
+ def output_shape(self):
135
+ return (self.input_shape[0], 1)
136
+
137
+ def get_output(self, train=False):
138
+ X = self.get_input(train)
139
+ w = self.W_w[X[:, 0]] # nb_samples, proj_dim
140
+ c = self.W_c[X[:, 1]] # nb_samples, proj_dim
141
+
142
+ dot = T.sum(w * c, axis=1)
143
+ dot = theano.tensor.reshape(dot, (X.shape[0], 1))
144
+ return self.activation(dot)
145
+
146
+ def get_config(self):
147
+ config = {"name": self.__class__.__name__,
148
+ "input_dim": self.input_dim,
149
+ "proj_dim": self.proj_dim,
150
+ "init": self.init.__name__,
151
+ "activation": self.activation.__name__}
152
+ base_config = super(WordContextProduct, self).get_config()
153
+ return dict(list(base_config.items()) + list(config.items()))
keras/layers/noise.py ADDED
@@ -0,0 +1,57 @@
1
+ from __future__ import absolute_import
2
+ import numpy as np
3
+ from .core import MaskedLayer
4
+ import theano
5
+ import theano.tensor as T
6
+ from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams
7
+
8
+
9
+ class GaussianNoise(MaskedLayer):
10
+ '''
11
+ Corruption process with GaussianNoise
12
+ '''
13
+ def __init__(self, sigma, **kwargs):
14
+ super(GaussianNoise, self).__init__(**kwargs)
15
+ self.sigma = sigma
16
+ self.srng = RandomStreams(seed=np.random.randint(10e6))
17
+
18
+ def get_output(self, train=False):
19
+ X = self.get_input(train)
20
+ if not train or self.sigma == 0:
21
+ return X
22
+ else:
23
+ return X + self.srng.normal(size=X.shape, avg=0.0, std=self.sigma,
24
+ dtype=theano.config.floatX)
25
+
26
+ def get_config(self):
27
+ config = {"name": self.__class__.__name__,
28
+ "sigma": self.sigma}
29
+ base_config = super(GaussianNoise, self).get_config()
30
+ return dict(list(base_config.items()) + list(config.items()))
31
+
32
+
33
+ class GaussianDropout(MaskedLayer):
34
+ '''
35
+ Multiplicative Gaussian Noise
36
+ Reference:
37
+ Dropout: A Simple Way to Prevent Neural Networks from Overfitting
38
+ Srivastava, Hinton, et al. 2014
39
+ http://www.cs.toronto.edu/~rsalakhu/papers/srivastava14a.pdf
40
+ '''
41
+ def __init__(self, p, **kwargs):
42
+ super(GaussianDropout, self).__init__(**kwargs)
43
+ self.p = p
44
+ self.srng = RandomStreams(seed=np.random.randint(10e6))
45
+
46
+ def get_output(self, train):
47
+ X = self.get_input(train)
48
+ if train:
49
+ # self.p refers to drop probability rather than retain probability (as in paper) to match Dropout layer syntax
50
+ X *= self.srng.normal(size=X.shape, avg=1.0, std=T.sqrt(self.p / (1.0 - self.p)), dtype=theano.config.floatX)
51
+ return X
52
+
53
+ def get_config(self):
54
+ config = {"name": self.__class__.__name__,
55
+ "p": self.p}
56
+ base_config = super(GaussianDropout, self).get_config()
57
+ return dict(list(base_config.items()) + list(config.items()))
@@ -0,0 +1,117 @@
1
+ from ..layers.core import Layer
2
+ from ..utils.theano_utils import shared_zeros, shared_ones, ndim_tensor, floatX
3
+ from .. import initializations
4
+
5
+ import theano.tensor as T
6
+
7
+
8
+ class BatchNormalization(Layer):
9
+ '''
10
+ Reference:
11
+ Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift
12
+ http://arxiv.org/pdf/1502.03167v3.pdf
13
+
14
+ mode: 0 -> featurewise normalization
15
+ 1 -> samplewise normalization (may sometimes outperform featurewise mode)
16
+
17
+ momentum: momentum term in the computation of a running estimate of the mean and std of the data
18
+ '''
19
+ def __init__(self, epsilon=1e-6, mode=0, momentum=0.9, weights=None, **kwargs):
20
+ self.init = initializations.get("uniform")
21
+ self.epsilon = epsilon
22
+ self.mode = mode
23
+ self.momentum = momentum
24
+ self.initial_weights = weights
25
+ super(BatchNormalization, self).__init__(**kwargs)
26
+
27
+ def build(self):
28
+ input_shape = self.input_shape # starts with samples axis
29
+ input_shape = input_shape[1:]
30
+ self.input = ndim_tensor(len(input_shape) + 1)
31
+
32
+ self.gamma = self.init((input_shape))
33
+ self.beta = shared_zeros(input_shape)
34
+
35
+ self.params = [self.gamma, self.beta]
36
+ self.running_mean = shared_zeros(input_shape)
37
+ self.running_std = shared_ones((input_shape))
38
+
39
+ # initialize self.updates: batch mean/std computation
40
+ X = self.get_input(train=True)
41
+ m = X.mean(axis=0)
42
+ std = T.mean((X - m) ** 2 + self.epsilon, axis=0) ** 0.5
43
+ mean_update = self.momentum * self.running_mean + (1-self.momentum) * m
44
+ std_update = self.momentum * self.running_std + (1-self.momentum) * std
45
+ self.updates = [(self.running_mean, mean_update), (self.running_std, std_update)]
46
+
47
+ if self.initial_weights is not None:
48
+ self.set_weights(self.initial_weights)
49
+ del self.initial_weights
50
+
51
+ def get_weights(self):
52
+ return super(BatchNormalization, self).get_weights() + [self.running_mean.get_value(), self.running_std.get_value()]
53
+
54
+ def set_weights(self, weights):
55
+ self.running_mean.set_value(floatX(weights[-2]))
56
+ self.running_std.set_value(floatX(weights[-1]))
57
+ super(BatchNormalization, self).set_weights(weights[:-2])
58
+
59
+ def get_output(self, train):
60
+ X = self.get_input(train)
61
+
62
+ if self.mode == 0:
63
+ X_normed = (X - self.running_mean) / (self.running_std + self.epsilon)
64
+
65
+ elif self.mode == 1:
66
+ m = X.mean(axis=-1, keepdims=True)
67
+ std = X.std(axis=-1, keepdims=True)
68
+ X_normed = (X - m) / (std + self.epsilon)
69
+
70
+ out = self.gamma * X_normed + self.beta
71
+ return out
72
+
73
+ def get_config(self):
74
+ config = {"name": self.__class__.__name__,
75
+ "epsilon": self.epsilon,
76
+ "mode": self.mode,
77
+ "momentum": self.momentum}
78
+ base_config = super(BatchNormalization, self).get_config()
79
+ return dict(list(base_config.items()) + list(config.items()))
80
+
81
+
82
+ class LRN2D(Layer):
83
+ """
84
+ This code is adapted from pylearn2.
85
+ License at: https://github.com/lisa-lab/pylearn2/blob/master/LICENSE.txt
86
+ """
87
+
88
+ def __init__(self, alpha=1e-4, k=2, beta=0.75, n=5, **kwargs):
89
+ if n % 2 == 0:
90
+ raise NotImplementedError("LRN2D only works with odd n. n provided: " + str(n))
91
+ super(LRN2D, self).__init__(**kwargs)
92
+ self.alpha = alpha
93
+ self.k = k
94
+ self.beta = beta
95
+ self.n = n
96
+
97
+ def get_output(self, train):
98
+ X = self.get_input(train)
99
+ b, ch, r, c = X.shape
100
+ half_n = self.n // 2
101
+ input_sqr = T.sqr(X)
102
+ extra_channels = T.alloc(0., b, ch + 2*half_n, r, c)
103
+ input_sqr = T.set_subtensor(extra_channels[:, half_n:half_n+ch, :, :], input_sqr)
104
+ scale = self.k
105
+ for i in range(self.n):
106
+ scale += self.alpha * input_sqr[:, i:i+ch, :, :]
107
+ scale = scale ** self.beta
108
+ return X / scale
109
+
110
+ def get_config(self):
111
+ config = {"name": self.__class__.__name__,
112
+ "alpha": self.alpha,
113
+ "k": self.k,
114
+ "beta": self.beta,
115
+ "n": self.n}
116
+ base_config = super(LRN2D, self).get_config()
117
+ return dict(list(base_config.items()) + list(config.items()))