41 lines
1.0 KiB
Python
Raw Normal View History

2023-05-15 01:24:10 +05:30
"""
Implements the Mish activation functions.
The function takes a vector of K real numbers input and then
2023-05-16 21:01:16 +05:30
applies the mish function, x*tanh(softplus(x) to each element of the vector.
2023-05-15 01:24:10 +05:30
Script inspired from its corresponding Wikipedia article
https://en.wikipedia.org/wiki/Rectifier_(neural_networks)
2023-05-20 22:54:24 +05:30
The proposed paper link is provided below.
2023-05-15 01:24:10 +05:30
https://arxiv.org/abs/1908.08681
"""
import numpy as np
2023-06-20 19:38:29 +05:30
from maths.tanh import tangent_hyperbolic as tanh
2023-05-15 01:24:10 +05:30
def mish_activation(vector: np.ndarray) -> np.ndarray:
"""
Implements the Mish function
Parameters:
vector: np.array
Returns:
Mish (np.array): The input numpy array after applying tanh.
mathematically, mish = x * tanh(softplus(x) where
softplus = ln(1+e^(x)) and tanh = (e^x - e^(-x))/(e^x + e^(-x))
so, mish can be written as x * (2/(1+e^(-2 * softplus))-1
"""
soft_plus = np.log(np.exp(vector) + 1)
2023-06-20 19:38:29 +05:30
return vector * tanh(soft_plus)
2023-05-15 01:24:10 +05:30
if __name__ == "__main__":
import doctest
doctest.testmod()