Skip to content

Commit 58f4620

Browse files
Jonathan DEKHTIARzsdonghao
authored andcommitted
ReLU6 rework (#687)
* leaky_relu deprecated and leaky_relu6 and PReLU6Layer added * cleaning * doc corrections * YAPF fix * leaky_twice_relu6 function added * recenter initializer * YAPF correction * Update test_activations.py
1 parent c6cf743 commit 58f4620

File tree

8 files changed

+418
-27
lines changed

8 files changed

+418
-27
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ To release a new version, please update the changelog as followed:
7272
- API:
7373
- `tl.alphas` and `tl.alphas_like` added following the tf.ones/zeros and tf.zeros_like/ones_like (by @DEKHTIARJonathan in #580)
7474
- `tl.lazy_imports.LazyImport` to import heavy libraries only when necessary (by @DEKHTIARJonathan in #667)
75+
- `tl.act.leaky_relu6` and `tl.layers.PRelu6Layer` have been (by @DEKHTIARJonathan in #686)
76+
- `tl.act.leaky_twice_relu6` and `tl.layers.PTRelu6Layer` have been (by @DEKHTIARJonathan in #686)
7577
- CI Tool:
7678
- [Stale Probot](https://github.com/probot/stale) added to clean stale issues (by @DEKHTIARJonathan in #573)
7779
- [Changelog Probot](https://github.com/mikz/probot-changelog) Configuration added (by @DEKHTIARJonathan in #637)
@@ -107,6 +109,7 @@ To release a new version, please update the changelog as followed:
107109
- `test_optimizer_amsgrad.py` added to test `AMSGrad` optimizer (by @DEKHTIARJonathan in #636)
108110
- `test_logging.py` added to insure robustness of the logging API (by @DEKHTIARJonathan in #645)
109111
- `test_decorators.py` added (by @DEKHTIARJonathan in #660)
112+
- `test_activations.py` added (by @DEKHTIARJonathan in #686)
110113
- Tutorials:
111114
- `tutorial_tfslim` has been introduced to show how to use `SlimNetsLayer` (by @2wins in #560).
112115

@@ -135,6 +138,7 @@ To release a new version, please update the changelog as followed:
135138

136139
### Deprecated
137140
- `tl.layers.TimeDistributedLayer` argurment `args` is deprecated in favor of `layer_args` (by @DEKHTIARJonathan in #667)
141+
- `tl.act.leaky_relu` have been deprecated in favor of `tf.nn.leaky_relu` (by @DEKHTIARJonathan in #686)
138142

139143
### Removed
140144
- `assert()` calls remove and replaced by `raise AssertionError()` (by @DEKHTIARJonathan in #667)
@@ -156,6 +160,7 @@ To release a new version, please update the changelog as followed:
156160
- Tutorial:
157161
- `tutorial_word2vec_basic.py` saving issue #476 fixed (by @DEKHTIARJonathan in #635)
158162
- All tutorials tested and errors have been fixed (by @DEKHTIARJonathan in #635)
163+
159164
### Security
160165

161166
### Dependencies Update
@@ -173,6 +178,7 @@ To release a new version, please update the changelog as followed:
173178
- API:
174179
- `tl.alphas` and `tl.alphas_like` added following the tf.ones/zeros and tf.zeros_like/ones_like (by @DEKHTIARJonathan in #580)
175180
- `tl.lazy_imports.LazyImport` to import heavy libraries only when necessary (by @DEKHTIARJonathan in #667)
181+
- `tl.act.leaky_relu6` and `tl.layers.PRelu6Layer` have been (by @DEKHTIARJonathan in #686)
176182
- CI Tool:
177183
- [Stale Probot](https://github.com/probot/stale) added to clean stale issues (by @DEKHTIARJonathan in #573)
178184
- [Changelog Probot](https://github.com/mikz/probot-changelog) Configuration added (by @DEKHTIARJonathan in #637)
@@ -196,6 +202,7 @@ To release a new version, please update the changelog as followed:
196202
- Layer:
197203
- ElementwiseLambdaLayer added to use custom function to connect multiple layer inputs (by @One-sixth in #579)
198204
- AtrousDeConv2dLayer added (by @2wins in #662)
205+
- Fix bugs of using `tf.layers` in CNN (by @zsdonghao in #686)
199206
- Optimizer:
200207
- AMSGrad Optimizer added based on `On the Convergence of Adam and Beyond (ICLR 2018)` (by @DEKHTIARJonathan in #636)
201208
- Setup:
@@ -235,6 +242,7 @@ To release a new version, please update the changelog as followed:
235242

236243
### Deprecated
237244
- `tl.layers.TimeDistributedLayer` argurment `args` is deprecated in favor of `layer_args` (by @DEKHTIARJonathan in #667)
245+
- `tl.act.leaky_relu` have been deprecated in favor of `tf.nn.leaky_relu` (by @DEKHTIARJonathan in #686)
238246

239247
### Removed
240248
- `assert()` calls remove and replaced by `raise AssertionError()` (by @DEKHTIARJonathan in #667)
@@ -256,6 +264,7 @@ To release a new version, please update the changelog as followed:
256264
- Tutorial:
257265
- `tutorial_word2vec_basic.py` saving issue #476 fixed (by @DEKHTIARJonathan in #635)
258266
- All tutorials tested and errors have been fixed (by @DEKHTIARJonathan in #635)
267+
259268
### Security
260269

261270
### Dependencies Update

docs/modules/activation.rst

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ For more complex activation, TensorFlow API will be required.
2626

2727
.. autosummary::
2828

29-
ramp
3029
leaky_relu
30+
leaky_relu6
31+
leaky_twice_relu6
32+
ramp
3133
swish
3234
sign
3335
hard_tanh
@@ -37,10 +39,18 @@ Ramp
3739
------
3840
.. autofunction:: ramp
3941

40-
Leaky Relu
42+
Leaky ReLU
4143
------------
4244
.. autofunction:: leaky_relu
4345

46+
Leaky ReLU6
47+
------------
48+
.. autofunction:: leaky_relu6
49+
50+
Twice Leaky ReLU6
51+
-----------------
52+
.. autofunction:: leaky_twice_relu6
53+
4454
Swish
4555
------------
4656
.. autofunction:: swish

docs/modules/layers.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@ Layer list
345345
ScaleLayer
346346

347347
PReluLayer
348+
PRelu6Layer
349+
PTRelu6Layer
348350

349351
MultiplexerLayer
350352

@@ -886,8 +888,21 @@ Scale
886888
Parametric activation layer
887889
---------------------------
888890

891+
PReLU Layer
892+
^^^^^^^^^^^
889893
.. autoclass:: PReluLayer
890894

895+
896+
PReLU6 Layer
897+
^^^^^^^^^^^^
898+
.. autoclass:: PRelu6Layer
899+
900+
901+
PTReLU6 Layer
902+
^^^^^^^^^^^^^
903+
.. autoclass:: PTRelu6Layer
904+
905+
891906
Flow control layer
892907
----------------------
893908

tensorlayer/activation.py

Lines changed: 134 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66
from tensorflow.python.util.deprecation import deprecated
77

88
__all__ = [
9-
'ramp',
109
'leaky_relu',
10+
'leaky_relu6',
11+
'leaky_twice_relu6',
12+
'lrelu',
13+
'lrelu6',
14+
'ltrelu6',
15+
'ramp',
1116
'swish',
1217
'sign',
18+
'htanh',
19+
'hard_tanh',
1320
'pixel_wise_softmax',
14-
'linear',
15-
'lrelu',
1621
]
1722

1823

@@ -39,10 +44,16 @@ def ramp(x, v_min=0, v_max=1, name=None):
3944
return tf.clip_by_value(x, clip_value_min=v_min, clip_value_max=v_max, name=name)
4045

4146

42-
def leaky_relu(x, alpha=0.1, name="lrelu"):
43-
"""LeakyReLU, Shortcut is ``lrelu``.
47+
@deprecated("2018-09-30", "This API is deprecated. Please use as `tf.nn.leaky_relu`.")
48+
def leaky_relu(x, alpha=0.2, name="leaky_relu"):
49+
"""leaky_relu can be used through its shortcut: :func:`tl.act.lrelu`.
50+
51+
This function is a modified version of ReLU, introducing a nonzero gradient for negative input. Introduced by the paper:
52+
`Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__
4453
45-
Modified version of ReLU, introducing a nonzero gradient for negative input.
54+
The function return the following results:
55+
- When x < 0: ``f(x) = alpha_low * x``.
56+
- When x >= 0: ``f(x) = x``.
4657
4758
Parameters
4859
----------
@@ -55,6 +66,7 @@ def leaky_relu(x, alpha=0.1, name="lrelu"):
5566
5667
Examples
5768
--------
69+
>>> import tensorlayer as tl
5870
>>> net = tl.layers.DenseLayer(net, 100, act=lambda x : tl.act.lrelu(x, 0.2), name='dense')
5971
6072
Returns
@@ -64,16 +76,122 @@ def leaky_relu(x, alpha=0.1, name="lrelu"):
6476
6577
References
6678
----------
67-
- `Rectifier Nonlinearities Improve Neural Network Acoustic Models, Maas et al. (2013)`
68-
http://web.stanford.edu/~awni/papers/relu_hybrid_icml2013_final.pdf
79+
- `Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__
6980
7081
"""
71-
# with tf.name_scope(name) as scope:
72-
# x = tf.nn.relu(x)
73-
# m_x = tf.nn.relu(-x)
74-
# x -= alpha * m_x
75-
x = tf.maximum(x, alpha * x, name=name)
76-
return x
82+
83+
if not (0 < alpha <= 1):
84+
raise ValueError("`alpha` value must be in [0, 1]`")
85+
86+
with tf.name_scope(name, "leaky_relu") as name_scope:
87+
x = tf.convert_to_tensor(x, name="features")
88+
return tf.maximum(x, alpha * x, name=name_scope)
89+
90+
91+
def leaky_relu6(x, alpha=0.2, name="leaky_relu6"):
92+
""":func:`leaky_relu6` can be used through its shortcut: :func:`tl.act.lrelu6`.
93+
94+
This activation function is a modified version :func:`leaky_relu` introduced by the following paper:
95+
`Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__
96+
97+
This activation function also follows the behaviour of the activation function :func:`tf.nn.relu6` introduced by the following paper:
98+
`Convolutional Deep Belief Networks on CIFAR-10 [A. Krizhevsky, 2010] <http://www.cs.utoronto.ca/~kriz/conv-cifar10-aug2010.pdf>`__
99+
100+
The function return the following results:
101+
- When x < 0: ``f(x) = alpha_low * x``.
102+
- When x in [0, 6]: ``f(x) = x``.
103+
- When x > 6: ``f(x) = 6``.
104+
105+
Parameters
106+
----------
107+
x : Tensor
108+
Support input type ``float``, ``double``, ``int32``, ``int64``, ``uint8``, ``int16``, or ``int8``.
109+
alpha : float
110+
Slope.
111+
name : str
112+
The function name (optional).
113+
114+
Examples
115+
--------
116+
>>> import tensorlayer as tl
117+
>>> net = tl.layers.DenseLayer(net, 100, act=lambda x : tl.act.leaky_relu6(x, 0.2), name='dense')
118+
119+
Returns
120+
-------
121+
Tensor
122+
A ``Tensor`` in the same type as ``x``.
123+
124+
References
125+
----------
126+
- `Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__
127+
- `Convolutional Deep Belief Networks on CIFAR-10 [A. Krizhevsky, 2010] <http://www.cs.utoronto.ca/~kriz/conv-cifar10-aug2010.pdf>`__
128+
"""
129+
130+
if not (0 < alpha <= 1):
131+
raise ValueError("`alpha` value must be in [0, 1]`")
132+
133+
with tf.name_scope(name, "leaky_relu6") as name_scope:
134+
x = tf.convert_to_tensor(x, name="features")
135+
return tf.minimum(tf.maximum(x, alpha * x), 6, name=name_scope)
136+
137+
138+
def leaky_twice_relu6(x, alpha_low=0.2, alpha_high=0.2, name="leaky_relu6"):
139+
""":func:`leaky_twice_relu6` can be used through its shortcut: :func:`:func:`tl.act.ltrelu6`.
140+
141+
This activation function is a modified version :func:`leaky_relu` introduced by the following paper:
142+
`Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__
143+
144+
This activation function also follows the behaviour of the activation function :func:`tf.nn.relu6` introduced by the following paper:
145+
`Convolutional Deep Belief Networks on CIFAR-10 [A. Krizhevsky, 2010] <http://www.cs.utoronto.ca/~kriz/conv-cifar10-aug2010.pdf>`__
146+
147+
This function push further the logic by adding `leaky` behaviour both below zero and above six.
148+
149+
The function return the following results:
150+
- When x < 0: ``f(x) = alpha_low * x``.
151+
- When x in [0, 6]: ``f(x) = x``.
152+
- When x > 6: ``f(x) = 6 + (alpha_high * (x-6))``.
153+
154+
Parameters
155+
----------
156+
x : Tensor
157+
Support input type ``float``, ``double``, ``int32``, ``int64``, ``uint8``, ``int16``, or ``int8``.
158+
alpha_low : float
159+
Slope for x < 0: ``f(x) = alpha_low * x``.
160+
alpha_high : float
161+
Slope for x < 6: ``f(x) = 6 (alpha_high * (x-6))``.
162+
name : str
163+
The function name (optional).
164+
165+
Examples
166+
--------
167+
>>> import tensorlayer as tl
168+
>>> net = tl.layers.DenseLayer(net, 100, act=lambda x : tl.act.leaky_twice_relu6(x, 0.2, 0.2), name='dense')
169+
170+
Returns
171+
-------
172+
Tensor
173+
A ``Tensor`` in the same type as ``x``.
174+
175+
References
176+
----------
177+
- `Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__
178+
- `Convolutional Deep Belief Networks on CIFAR-10 [A. Krizhevsky, 2010] <http://www.cs.utoronto.ca/~kriz/conv-cifar10-aug2010.pdf>`__
179+
180+
"""
181+
182+
if not (0 < alpha_high <= 1):
183+
raise ValueError("`alpha_high` value must be in [0, 1]`")
184+
185+
if not (0 < alpha_low <= 1):
186+
raise ValueError("`alpha_low` value must be in [0, 1]`")
187+
188+
with tf.name_scope(name, "leaky_twice_relu6") as name_scope:
189+
x = tf.convert_to_tensor(x, name="features")
190+
191+
x_is_above_0 = tf.minimum(x, 6 * (1 - alpha_high) + alpha_high * x)
192+
x_is_below_0 = tf.minimum(alpha_low * x, 0)
193+
194+
return tf.maximum(x_is_above_0, x_is_below_0, name=name_scope)
77195

78196

79197
def swish(x, name='swish'):
@@ -219,4 +337,6 @@ def pixel_wise_softmax(x, name='pixel_wise_softmax'):
219337

220338
# Alias
221339
lrelu = leaky_relu
340+
lrelu6 = leaky_relu6
341+
ltrelu6 = leaky_twice_relu6
222342
htanh = hard_tanh

tensorlayer/cost.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131

3232

3333
def cross_entropy(output, target, name=None):
34-
"""Softmax cross-entropy operation, returns the TensorFlow expression of cross-entropy for two distributions, it implements
35-
softmax internally. See ``tf.nn.sparse_softmax_cross_entropy_with_logits``.
34+
"""Softmax cross-entropy operation, returns the TensorFlow expression of cross-entropy for two distributions,
35+
it implements softmax internally. See ``tf.nn.sparse_softmax_cross_entropy_with_logits``.
3636
3737
Parameters
3838
----------

tensorlayer/layers/padding.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ def __init__(
125125
if not isinstance(padding, (int, tuple)):
126126
raise AssertionError("Padding should be of type `int` or `tuple`")
127127

128-
self.outputs = tf.keras.layers.ZeroPadding2D(padding=padding, name=name)(self.inputs)
128+
self.outputs = tf.keras.layers.ZeroPadding2D(padding=padding, name=name)(self.inputs) # TODO: Stop using Keras
129+
129130
self._add_layers(self.outputs)
130131

131132

0 commit comments

Comments
 (0)