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
keras/datasets/imdb.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
from __future__ import absolute_import
|
|
2
|
+
from six.moves import cPickle
|
|
3
|
+
import gzip
|
|
4
|
+
from .data_utils import get_file
|
|
5
|
+
import random
|
|
6
|
+
from six.moves import zip
|
|
7
|
+
import numpy as np
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def load_data(path="imdb.pkl", nb_words=None, skip_top=0, maxlen=None, test_split=0.2, seed=113,
|
|
11
|
+
start_char=1, oov_char=2, index_from=3):
|
|
12
|
+
|
|
13
|
+
path = get_file(path, origin="https://s3.amazonaws.com/text-datasets/imdb.pkl")
|
|
14
|
+
|
|
15
|
+
if path.endswith(".gz"):
|
|
16
|
+
f = gzip.open(path, 'rb')
|
|
17
|
+
else:
|
|
18
|
+
f = open(path, 'rb')
|
|
19
|
+
|
|
20
|
+
X, labels = cPickle.load(f)
|
|
21
|
+
f.close()
|
|
22
|
+
|
|
23
|
+
np.random.seed(seed)
|
|
24
|
+
np.random.shuffle(X)
|
|
25
|
+
np.random.seed(seed)
|
|
26
|
+
np.random.shuffle(labels)
|
|
27
|
+
|
|
28
|
+
if start_char is not None:
|
|
29
|
+
X = [[start_char] + [w + index_from for w in x] for x in X]
|
|
30
|
+
elif index_from:
|
|
31
|
+
X = [[w + index_from for w in x] for x in X]
|
|
32
|
+
|
|
33
|
+
if maxlen:
|
|
34
|
+
new_X = []
|
|
35
|
+
new_labels = []
|
|
36
|
+
for x, y in zip(X, labels):
|
|
37
|
+
if len(x) < maxlen:
|
|
38
|
+
new_X.append(x)
|
|
39
|
+
new_labels.append(y)
|
|
40
|
+
X = new_X
|
|
41
|
+
labels = new_labels
|
|
42
|
+
|
|
43
|
+
if not nb_words:
|
|
44
|
+
nb_words = max([max(x) for x in X])
|
|
45
|
+
|
|
46
|
+
# by convention, use 2 as OOV word
|
|
47
|
+
# reserve 'index_from' (=3 by default) characters: 0 (padding), 1 (start), 2 (OOV)
|
|
48
|
+
if oov_char is not None:
|
|
49
|
+
X = [[oov_char if (w >= nb_words or w < skip_top) else w for w in x] for x in X]
|
|
50
|
+
else:
|
|
51
|
+
nX = []
|
|
52
|
+
for x in X:
|
|
53
|
+
nx = []
|
|
54
|
+
for w in x:
|
|
55
|
+
if (w >= nb_words or w < skip_top):
|
|
56
|
+
nx.append(w)
|
|
57
|
+
nX.append(nx)
|
|
58
|
+
X = nX
|
|
59
|
+
|
|
60
|
+
X_train = X[:int(len(X)*(1-test_split))]
|
|
61
|
+
y_train = labels[:int(len(X)*(1-test_split))]
|
|
62
|
+
|
|
63
|
+
X_test = X[int(len(X)*(1-test_split)):]
|
|
64
|
+
y_test = labels[int(len(X)*(1-test_split)):]
|
|
65
|
+
|
|
66
|
+
return (X_train, y_train), (X_test, y_test)
|
keras/datasets/mnist.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
import gzip
|
|
3
|
+
from .data_utils import get_file
|
|
4
|
+
from six.moves import cPickle
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def load_data(path="mnist.pkl.gz"):
|
|
9
|
+
path = get_file(path, origin="https://s3.amazonaws.com/img-datasets/mnist.pkl.gz")
|
|
10
|
+
|
|
11
|
+
if path.endswith(".gz"):
|
|
12
|
+
f = gzip.open(path, 'rb')
|
|
13
|
+
else:
|
|
14
|
+
f = open(path, 'rb')
|
|
15
|
+
|
|
16
|
+
if sys.version_info < (3,):
|
|
17
|
+
data = cPickle.load(f)
|
|
18
|
+
else:
|
|
19
|
+
data = cPickle.load(f, encoding="bytes")
|
|
20
|
+
|
|
21
|
+
f.close()
|
|
22
|
+
|
|
23
|
+
return data # (X_train, y_train), (X_test, y_test)
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
from __future__ import absolute_import
|
|
3
|
+
from __future__ import print_function
|
|
4
|
+
from .data_utils import get_file
|
|
5
|
+
import string
|
|
6
|
+
import random
|
|
7
|
+
import os
|
|
8
|
+
from six.moves import cPickle
|
|
9
|
+
from six.moves import zip
|
|
10
|
+
import numpy as np
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def make_reuters_dataset(path=os.path.join('datasets', 'temp', 'reuters21578'), min_samples_per_topic=15):
|
|
14
|
+
import re
|
|
15
|
+
from ..preprocessing.text import Tokenizer
|
|
16
|
+
|
|
17
|
+
wire_topics = []
|
|
18
|
+
topic_counts = {}
|
|
19
|
+
wire_bodies = []
|
|
20
|
+
|
|
21
|
+
for fname in os.listdir(path):
|
|
22
|
+
if 'sgm' in fname:
|
|
23
|
+
s = open(os.path.join(path, fname)).read()
|
|
24
|
+
tag = '<TOPICS>'
|
|
25
|
+
while tag in s:
|
|
26
|
+
s = s[s.find(tag)+len(tag):]
|
|
27
|
+
topics = s[:s.find('</')]
|
|
28
|
+
if topics and '</D><D>' not in topics:
|
|
29
|
+
topic = topics.replace('<D>', '').replace('</D>', '')
|
|
30
|
+
wire_topics.append(topic)
|
|
31
|
+
topic_counts[topic] = topic_counts.get(topic, 0) + 1
|
|
32
|
+
else:
|
|
33
|
+
continue
|
|
34
|
+
|
|
35
|
+
bodytag = '<BODY>'
|
|
36
|
+
body = s[s.find(bodytag)+len(bodytag):]
|
|
37
|
+
body = body[:body.find('</')]
|
|
38
|
+
wire_bodies.append(body)
|
|
39
|
+
|
|
40
|
+
# only keep most common topics
|
|
41
|
+
items = list(topic_counts.items())
|
|
42
|
+
items.sort(key=lambda x: x[1])
|
|
43
|
+
kept_topics = set()
|
|
44
|
+
for x in items:
|
|
45
|
+
print(x[0] + ': ' + str(x[1]))
|
|
46
|
+
if x[1] >= min_samples_per_topic:
|
|
47
|
+
kept_topics.add(x[0])
|
|
48
|
+
print('-')
|
|
49
|
+
print('Kept topics:', len(kept_topics))
|
|
50
|
+
|
|
51
|
+
# filter wires with rare topics
|
|
52
|
+
kept_wires = []
|
|
53
|
+
labels = []
|
|
54
|
+
topic_indexes = {}
|
|
55
|
+
for t, b in zip(wire_topics, wire_bodies):
|
|
56
|
+
if t in kept_topics:
|
|
57
|
+
if t not in topic_indexes:
|
|
58
|
+
topic_index = len(topic_indexes)
|
|
59
|
+
topic_indexes[t] = topic_index
|
|
60
|
+
else:
|
|
61
|
+
topic_index = topic_indexes[t]
|
|
62
|
+
|
|
63
|
+
labels.append(topic_index)
|
|
64
|
+
kept_wires.append(b)
|
|
65
|
+
|
|
66
|
+
# vectorize wires
|
|
67
|
+
tokenizer = Tokenizer()
|
|
68
|
+
tokenizer.fit_on_texts(kept_wires)
|
|
69
|
+
X = tokenizer.texts_to_sequences(kept_wires)
|
|
70
|
+
|
|
71
|
+
print('Sanity check:')
|
|
72
|
+
for w in ["banana", "oil", "chocolate", "the", "dsft"]:
|
|
73
|
+
print('...index of', w, ':', tokenizer.word_index.get(w))
|
|
74
|
+
print('text reconstruction:')
|
|
75
|
+
reverse_word_index = dict([(v, k) for k, v in tokenizer.word_index.items()])
|
|
76
|
+
print(' '.join(reverse_word_index[i] for i in X[10]))
|
|
77
|
+
|
|
78
|
+
dataset = (X, labels)
|
|
79
|
+
print('-')
|
|
80
|
+
print('Saving...')
|
|
81
|
+
cPickle.dump(dataset, open(os.path.join('datasets', 'data', 'reuters.pkl'), 'w'))
|
|
82
|
+
cPickle.dump(tokenizer.word_index, open(os.path.join('datasets', 'data', 'reuters_word_index.pkl'), 'w'))
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def load_data(path="reuters.pkl", nb_words=None, skip_top=0, maxlen=None, test_split=0.2, seed=113,
|
|
86
|
+
start_char=1, oov_char=2, index_from=3):
|
|
87
|
+
|
|
88
|
+
path = get_file(path, origin="https://s3.amazonaws.com/text-datasets/reuters.pkl")
|
|
89
|
+
f = open(path, 'rb')
|
|
90
|
+
|
|
91
|
+
X, labels = cPickle.load(f)
|
|
92
|
+
f.close()
|
|
93
|
+
|
|
94
|
+
np.random.seed(seed)
|
|
95
|
+
np.random.shuffle(X)
|
|
96
|
+
np.random.seed(seed)
|
|
97
|
+
np.random.shuffle(labels)
|
|
98
|
+
|
|
99
|
+
if start_char is not None:
|
|
100
|
+
X = [[start_char] + [w + index_from for w in x] for x in X]
|
|
101
|
+
elif index_from:
|
|
102
|
+
X = [[w + index_from for w in x] for x in X]
|
|
103
|
+
|
|
104
|
+
if maxlen:
|
|
105
|
+
new_X = []
|
|
106
|
+
new_labels = []
|
|
107
|
+
for x, y in zip(X, labels):
|
|
108
|
+
if len(x) < maxlen:
|
|
109
|
+
new_X.append(x)
|
|
110
|
+
new_labels.append(y)
|
|
111
|
+
X = new_X
|
|
112
|
+
labels = new_labels
|
|
113
|
+
|
|
114
|
+
if not nb_words:
|
|
115
|
+
nb_words = max([max(x) for x in X])
|
|
116
|
+
|
|
117
|
+
# by convention, use 2 as OOV word
|
|
118
|
+
# reserve 'index_from' (=3 by default) characters: 0 (padding), 1 (start), 2 (OOV)
|
|
119
|
+
if oov_char is not None:
|
|
120
|
+
X = [[oov_char if (w >= nb_words or w < skip_top) else w for w in x] for x in X]
|
|
121
|
+
else:
|
|
122
|
+
nX = []
|
|
123
|
+
for x in X:
|
|
124
|
+
nx = []
|
|
125
|
+
for w in x:
|
|
126
|
+
if (w >= nb_words or w < skip_top):
|
|
127
|
+
nx.append(w)
|
|
128
|
+
nX.append(nx)
|
|
129
|
+
X = nX
|
|
130
|
+
|
|
131
|
+
X_train = X[:int(len(X)*(1-test_split))]
|
|
132
|
+
y_train = labels[:int(len(X)*(1-test_split))]
|
|
133
|
+
|
|
134
|
+
X_test = X[int(len(X)*(1-test_split)):]
|
|
135
|
+
y_test = labels[int(len(X)*(1-test_split)):]
|
|
136
|
+
|
|
137
|
+
return (X_train, y_train), (X_test, y_test)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def get_word_index(path="reuters_word_index.pkl"):
|
|
141
|
+
path = get_file(path, origin="https://s3.amazonaws.com/text-datasets/reuters_word_index.pkl")
|
|
142
|
+
f = open(path, 'rb')
|
|
143
|
+
return cPickle.load(f)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
if __name__ == "__main__":
|
|
147
|
+
make_reuters_dataset()
|
|
148
|
+
(X_train, y_train), (X_test, y_test) = load_data()
|
keras/initializations.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
from __future__ import absolute_import
|
|
2
|
+
import theano
|
|
3
|
+
import theano.tensor as T
|
|
4
|
+
import numpy as np
|
|
5
|
+
|
|
6
|
+
from .utils.theano_utils import sharedX, shared_zeros, shared_ones
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def get_fans(shape):
|
|
10
|
+
fan_in = shape[0] if len(shape) == 2 else np.prod(shape[1:])
|
|
11
|
+
fan_out = shape[1] if len(shape) == 2 else shape[0]
|
|
12
|
+
return fan_in, fan_out
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def uniform(shape, scale=0.05):
|
|
16
|
+
return sharedX(np.random.uniform(low=-scale, high=scale, size=shape))
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def normal(shape, scale=0.05):
|
|
20
|
+
return sharedX(np.random.randn(*shape) * scale)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def lecun_uniform(shape):
|
|
24
|
+
''' Reference: LeCun 98, Efficient Backprop
|
|
25
|
+
http://yann.lecun.com/exdb/publis/pdf/lecun-98b.pdf
|
|
26
|
+
'''
|
|
27
|
+
fan_in, fan_out = get_fans(shape)
|
|
28
|
+
scale = np.sqrt(3. / fan_in)
|
|
29
|
+
return uniform(shape, scale)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def glorot_normal(shape):
|
|
33
|
+
''' Reference: Glorot & Bengio, AISTATS 2010
|
|
34
|
+
'''
|
|
35
|
+
fan_in, fan_out = get_fans(shape)
|
|
36
|
+
s = np.sqrt(2. / (fan_in + fan_out))
|
|
37
|
+
return normal(shape, s)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def glorot_uniform(shape):
|
|
41
|
+
fan_in, fan_out = get_fans(shape)
|
|
42
|
+
s = np.sqrt(6. / (fan_in + fan_out))
|
|
43
|
+
return uniform(shape, s)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def he_normal(shape):
|
|
47
|
+
''' Reference: He et al., http://arxiv.org/abs/1502.01852
|
|
48
|
+
'''
|
|
49
|
+
fan_in, fan_out = get_fans(shape)
|
|
50
|
+
s = np.sqrt(2. / fan_in)
|
|
51
|
+
return normal(shape, s)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def he_uniform(shape):
|
|
55
|
+
fan_in, fan_out = get_fans(shape)
|
|
56
|
+
s = np.sqrt(6. / fan_in)
|
|
57
|
+
return uniform(shape, s)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def orthogonal(shape, scale=1.1):
|
|
61
|
+
''' From Lasagne. Reference: Saxe et al., http://arxiv.org/abs/1312.6120
|
|
62
|
+
'''
|
|
63
|
+
flat_shape = (shape[0], np.prod(shape[1:]))
|
|
64
|
+
a = np.random.normal(0.0, 1.0, flat_shape)
|
|
65
|
+
u, _, v = np.linalg.svd(a, full_matrices=False)
|
|
66
|
+
# pick the one with the correct shape
|
|
67
|
+
q = u if u.shape == flat_shape else v
|
|
68
|
+
q = q.reshape(shape)
|
|
69
|
+
return sharedX(scale * q[:shape[0], :shape[1]])
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def identity(shape, scale=1):
|
|
73
|
+
if len(shape) != 2 or shape[0] != shape[1]:
|
|
74
|
+
raise Exception("Identity matrix initialization can only be used for 2D square matrices")
|
|
75
|
+
else:
|
|
76
|
+
return sharedX(scale * np.identity(shape[0]))
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def zero(shape):
|
|
80
|
+
return shared_zeros(shape)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def one(shape):
|
|
84
|
+
return shared_ones(shape)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
from .utils.generic_utils import get_from_module
|
|
88
|
+
def get(identifier):
|
|
89
|
+
return get_from_module(identifier, globals(), 'initialization')
|
keras/layers/__init__.py
ADDED
|
File without changes
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
from .. import initializations
|
|
2
|
+
from ..layers.core import Layer, MaskedLayer
|
|
3
|
+
from ..utils.theano_utils import shared_zeros, shared_ones, sharedX
|
|
4
|
+
import theano.tensor as T
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class LeakyReLU(MaskedLayer):
|
|
9
|
+
def __init__(self, alpha=0.3, **kwargs):
|
|
10
|
+
super(LeakyReLU, self).__init__(**kwargs)
|
|
11
|
+
self.alpha = alpha
|
|
12
|
+
|
|
13
|
+
def get_output(self, train):
|
|
14
|
+
X = self.get_input(train)
|
|
15
|
+
return T.nnet.relu(X, self.alpha)
|
|
16
|
+
|
|
17
|
+
def get_config(self):
|
|
18
|
+
config = {"name": self.__class__.__name__,
|
|
19
|
+
"alpha": self.alpha}
|
|
20
|
+
base_config = super(LeakyReLU, self).get_config()
|
|
21
|
+
return dict(list(base_config.items()) + list(config.items()))
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class PReLU(MaskedLayer):
|
|
25
|
+
'''
|
|
26
|
+
Reference:
|
|
27
|
+
Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification
|
|
28
|
+
http://arxiv.org/pdf/1502.01852v1.pdf
|
|
29
|
+
'''
|
|
30
|
+
def __init__(self, init='zero', weights=None, **kwargs):
|
|
31
|
+
self.init = initializations.get(init)
|
|
32
|
+
self.initial_weights = weights
|
|
33
|
+
super(PReLU, self).__init__(**kwargs)
|
|
34
|
+
|
|
35
|
+
def build(self):
|
|
36
|
+
input_shape = self.input_shape[1:]
|
|
37
|
+
self.alphas = self.init(input_shape)
|
|
38
|
+
self.params = [self.alphas]
|
|
39
|
+
|
|
40
|
+
if self.initial_weights is not None:
|
|
41
|
+
self.set_weights(self.initial_weights)
|
|
42
|
+
del self.initial_weights
|
|
43
|
+
|
|
44
|
+
def get_output(self, train):
|
|
45
|
+
X = self.get_input(train)
|
|
46
|
+
pos = T.nnet.relu(X)
|
|
47
|
+
neg = self.alphas * (X - abs(X)) * 0.5
|
|
48
|
+
return pos + neg
|
|
49
|
+
|
|
50
|
+
def get_config(self):
|
|
51
|
+
config = {"name": self.__class__.__name__,
|
|
52
|
+
"init": self.init.__name__}
|
|
53
|
+
base_config = super(PReLU, self).get_config()
|
|
54
|
+
return dict(list(base_config.items()) + list(config.items()))
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class ParametricSoftplus(MaskedLayer):
|
|
58
|
+
'''
|
|
59
|
+
Parametric Softplus of the form: alpha * log(1 + exp(beta * X))
|
|
60
|
+
|
|
61
|
+
Reference:
|
|
62
|
+
Inferring Nonlinear Neuronal Computation Based on Physiologically Plausible Inputs
|
|
63
|
+
http://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1003143
|
|
64
|
+
'''
|
|
65
|
+
def __init__(self, alpha_init=0.2, beta_init=5.0,
|
|
66
|
+
weights=None, **kwargs):
|
|
67
|
+
self.alpha_init = alpha_init
|
|
68
|
+
self.beta_init = beta_init
|
|
69
|
+
self.initial_weights = weights
|
|
70
|
+
super(ParametricSoftplus, self).__init__(**kwargs)
|
|
71
|
+
|
|
72
|
+
def build(self):
|
|
73
|
+
input_shape = self.input_shape[1:]
|
|
74
|
+
self.alphas = sharedX(self.alpha_init * np.ones(input_shape))
|
|
75
|
+
self.betas = sharedX(self.beta_init * np.ones(input_shape))
|
|
76
|
+
self.params = [self.alphas, self.betas]
|
|
77
|
+
|
|
78
|
+
if self.initial_weights is not None:
|
|
79
|
+
self.set_weights(self.initial_weights)
|
|
80
|
+
del self.initial_weights
|
|
81
|
+
|
|
82
|
+
def get_output(self, train):
|
|
83
|
+
X = self.get_input(train)
|
|
84
|
+
return T.nnet.softplus(self.betas * X) * self.alphas
|
|
85
|
+
|
|
86
|
+
def get_config(self):
|
|
87
|
+
config = {"name": self.__class__.__name__,
|
|
88
|
+
"alpha_init": self.alpha_init,
|
|
89
|
+
"beta_init": self.beta_init}
|
|
90
|
+
base_config = super(ParametricSoftplus, self).get_config()
|
|
91
|
+
return dict(list(base_config.items()) + list(config.items()))
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
class ThresholdedLinear(MaskedLayer):
|
|
95
|
+
'''
|
|
96
|
+
Thresholded Linear Activation
|
|
97
|
+
|
|
98
|
+
Reference:
|
|
99
|
+
Zero-Bias Autoencoders and the Benefits of Co-Adapting Features
|
|
100
|
+
http://arxiv.org/pdf/1402.3337.pdf
|
|
101
|
+
'''
|
|
102
|
+
def __init__(self, theta=1.0, **kwargs):
|
|
103
|
+
super(ThresholdedLinear, self).__init__(**kwargs)
|
|
104
|
+
self.theta = theta
|
|
105
|
+
|
|
106
|
+
def get_output(self, train):
|
|
107
|
+
X = self.get_input(train)
|
|
108
|
+
return T.switch(abs(X) < self.theta, 0, X)
|
|
109
|
+
|
|
110
|
+
def get_config(self):
|
|
111
|
+
config = {"name": self.__class__.__name__,
|
|
112
|
+
"theta": self.theta}
|
|
113
|
+
base_config = super(ThresholdedLinear, self).get_config()
|
|
114
|
+
return dict(list(base_config.items()) + list(config.items()))
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
class ThresholdedReLU(MaskedLayer):
|
|
118
|
+
'''
|
|
119
|
+
Thresholded Rectified Activation
|
|
120
|
+
|
|
121
|
+
Reference:
|
|
122
|
+
Zero-Bias Autoencoders and the Benefits of Co-Adapting Features
|
|
123
|
+
http://arxiv.org/pdf/1402.3337.pdf
|
|
124
|
+
'''
|
|
125
|
+
def __init__(self, theta=1.0, **kwargs):
|
|
126
|
+
super(ThresholdedReLU, self).__init__(**kwargs)
|
|
127
|
+
self.theta = theta
|
|
128
|
+
|
|
129
|
+
def get_output(self, train):
|
|
130
|
+
X = self.get_input(train)
|
|
131
|
+
return T.switch(X > self.theta, X, 0)
|
|
132
|
+
|
|
133
|
+
def get_config(self):
|
|
134
|
+
config = {"name": self.__class__.__name__,
|
|
135
|
+
"theta": self.theta}
|
|
136
|
+
base_config = super(ThresholdedReLU, self).get_config()
|
|
137
|
+
return dict(list(base_config.items()) + list(config.items()))
|