-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactivations.py
56 lines (41 loc) · 1.23 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
"""
Activation functions used in neural networks
"""
from typing import Callable, Any
import tensorflow.keras.backend as kb
from tensorflow.keras.activations import deserialize, serialize # noqa
from tensorflow.keras.activations import get as keras_get
from megnet.utils.typing import OptStrOrCallable
def softplus2(x):
"""
out = log(exp(x)+1) - log(2)
softplus function that is 0 at x=0, the implementation aims at avoiding overflow
Args:
x: (Tensor) input tensor
Returns:
(Tensor) output tensor
"""
return kb.relu(x) + kb.log(0.5 * kb.exp(-kb.abs(x)) + 0.5)
def swish(x):
"""
out = x * sigmoid(x)
Args:
x: (Tensor) input tensor
Returns:
(Tensor) output tensor
"""
return x * kb.sigmoid(x)
def get(identifier: OptStrOrCallable = None) -> Callable[..., Any]:
"""
Get activations by identifier
Args:
identifier (str or callable): the identifier of activations
Returns:
callable activation
"""
try:
return keras_get(identifier)
except ValueError:
if isinstance(identifier, str):
return deserialize(identifier, custom_objects=globals())
raise ValueError("Could not interpret:", identifier)