/
activations.py
350 lines (256 loc) · 7.63 KB
/
activations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# ------------------------------------------------------------------------
# Copyright (c) 2017-present, SeetaTech. All Rights Reserved.
#
# Licensed under the BSD 2-Clause License,
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://opensource.org/licenses/BSD-2-Clause
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------------------
"""Activation functions."""
from dragon.core.ops import activation_ops
from dragon.core.ops import math_ops
from dragon.vm.keras.core.utils import generic_utils
def elu(x, alpha=1.0, **kwargs):
r"""Apply exponential linear unit to input.
`[Clevert et.al, 2015] <https://arxiv.org/abs/1511.07289>`_.
The **ELU** function is defined as:
.. math::
\text{ELU}(x) =
\begin{cases}
x, & \text{ if } x \geq 0 \\
\alpha * (\exp(x) - 1), & \text{ otherwise }
\end{cases}
Examples:
```python
x = tf.constant([-1, 0, 1], 'float32')
print(tf.keras.activations.elu(x, inplace=False))
```
Parameters
----------
x : dragon.Tensor
The input tensor.
alpha : float, optional, default=1.
The value to :math:`\alpha`.
Returns
-------
dragon.Tensor
The output tensor.
"""
return activation_ops.elu(x, alpha=alpha, **kwargs)
def exponential(x):
r"""Apply exponential activation to input.
The **Exponential** function is defined as:
.. math:: \text{Exp}(x) = \exp(x)
Examples:
```python
x = tf.constant([1, 2, 3], 'float32')
print(tf.keras.activations.exponential(x))
```
Parameters
----------
x : dragon.Tensor
The input tensor.
Returns
-------
dragon.Tensor
The output tensor.
"""
return math_ops.exp(x)
def hard_sigmoid(x, **kwargs):
r"""Apply hard sigmoid function to input.
The **HardSigmoid** function is defined as:
.. math:: \text{HardSigmoid}(x) = \max(0, \min(1, 0.2 * x + 0.5))
Examples:
```python
x = tf.constant([-2.5, -1.0, 0.0, 1.0, 2.5])
print(tf.keras.activations.hard_sigmoid(x, inplace=False))
```
Parameters
----------
x : dragon.Tensor
The input tensor.
Returns
-------
dragon.Tensor
The output tensor.
"""
return activation_ops.hardsigmoid(x, **kwargs)
def linear(x):
r"""Apply linear activation to input.
The **Linear** function is defined as:
.. math:: \text{Linear}(x) = x
Examples:
```python
x = tf.constant([1, 2, 3], 'float32')
print(tf.keras.activations.linear(x))
```
Parameters
----------
x : dragon.Tensor
The input tensor.
Returns
-------
dragon.Tensor
The output tensor.
"""
return x
def relu(x, alpha=0, max_value=None, **kwargs):
r"""Apply rectified linear unit to input.
`[Nair & Hinton, 2010] <http://www.csri.utoronto.ca/~hinton/absps/reluICML.pdf>`_.
The **ReLU** function is defined as:
.. math::
\text{ReLU}(x) =
\begin{cases}
\min(x, v_{max}), & \text{ if } x \geq 0 \\
\alpha * x, & \text{ otherwise }
\end{cases}
Examples:
```python
x = tf.constant([-1, 0, 1], 'float32')
print(tf.keras.activations.relu(x, inplace=False))
```
Parameters
----------
x : dragon.Tensor
The input tensor.
alpha : number, optional, default=0
The value to :math:`\alpha`.
max_value : number, optional
The value to :math:`v_{max}`.
"""
if max_value is not None:
if alpha != 0:
raise ValueError("Set either <alpha> or <max_value>.")
if max_value != 6:
raise ValueError("<max_value> can only be set to 6.")
return activation_ops.relu6(x, **kwargs)
return activation_ops.leaky_relu(x, alpha=alpha, **kwargs)
def selu(x, **kwargs):
r"""Apply scaled exponential linear unit to input.
`[Klambauer et.al, 2017] <https://arxiv.org/abs/1706.02515>`_.
.. math::
\text{SELU}(x) = 1.0507 *
\begin{cases}
x, & \text{ if } x \geq 0 \\
1.67326 * (\exp(x) - 1), & \text{ otherwise }
\end{cases}
Examples:
```python
x = tf.constant([-1, 0, 1], 'float32')
print(tf.keras.activations.selu(x, inplace=False))
```
Parameters
----------
x : dragon.Tensor
The input tensor.
Returns
-------
dragon.Tensor
The output tensor.
"""
return activation_ops.selu(x, **kwargs)
def sigmoid(x, **kwargs):
r"""Apply sigmoid function to input.
The **Sigmoid** function is defined as:
.. math:: \text{Sigmoid}(x) = \frac{1}{1 + \exp(-x)}
Examples:
```python
x = tf.constant([0.2, 0.4, 0.6, 0.8, 1.0], 'float32')
print(tf.keras.activations.sigmoid(x, inplace=False))
```
Parameters
----------
x : dragon.Tensor
The input tensor.
Returns
-------
dragon.Tensor
The output tensor
"""
return activation_ops.sigmoid(x, **kwargs)
def softmax(x, axis=-1, **kwargs):
r"""Apply softmax function to input.
The **Softmax** function is defined as:
.. math:: \text{Softmax}(x_{i}) = \frac{\exp(x_{i})}{\sum_{j} \exp(x_{j})}
Examples:
```python
x = tf.constant([-1, 0, 1], 'float32')
print(tf.keras.activations.softmax(x, inplace=False))
```
Parameters
----------
x : dragon.Tensor
The input tensor.
axis : int, optional, default=-1
The axis to reduce.
Returns
-------
dragon.Tensor
The output tensor.
"""
return activation_ops.softmax(x, axis=axis, **kwargs)
def swish(x):
r"""Apply swish function to input.
`[Ramachandran et.al, 2017] <https://arxiv.org/abs/1710.05941>`_.
The **Swish** function is defined as:
.. math:: \text{Swish}(x) = x \cdot \frac{1}{1 + \exp(-x)}
Examples:
```python
x = tf.constant([-2.5, -1.0, 0.0, 1.0, 2.5])
print(tf.keras.activations.swish(x))
```
Parameters
----------
x : dragon.Tensor
The input tensor.
Returns
-------
dragon.Tensor
The output tensor.
"""
return activation_ops.silu(x)
def tanh(x, **kwargs):
r"""Apply tanh function to input.
The **Tanh** function is defined as:
.. math:: \text{Tanh}(x) = \frac{\exp(x) - \exp(-x)}{\exp(x) + \exp(-x)}
Examples:
```python
x = tf.constant([0.2, 0.4, 0.6, 0.8, 1.0], 'float32')
print(tf.keras.activations.tanh(x, inplace=False))
```
Parameters
----------
x : dragon.Tensor
The input tensor.
Returns
-------
dragon.Tensor
The output tensor.
"""
return activation_ops.tanh(x, **kwargs)
def get(identifier):
"""Return the activation function by identifier.
Parameters
----------
identifier : Union[callable, str]
The identifier.
Returns
-------
callable
The activation function.
"""
if identifier is None:
return linear
elif callable(identifier):
return identifier
elif isinstance(identifier, str):
return generic_utils.deserialize_keras_object(identifier, globals(), "activation")
else:
raise TypeError("Could not interpret the activation identifier: {}.".format(identifier))