TheAlgorithms-Python
37 строк · 966.0 Байт
1"""
2Softplus Activation Function
3
4Use Case: The Softplus function is a smooth approximation of the ReLU function.
5For more detailed information, you can refer to the following link:
6https://en.wikipedia.org/wiki/Rectifier_(neural_networks)#Softplus
7"""
8
9import numpy as np
10
11
12def softplus(vector: np.ndarray) -> np.ndarray:
13"""
14Implements the Softplus activation function.
15
16Parameters:
17vector (np.ndarray): The input array for the Softplus activation.
18
19Returns:
20np.ndarray: The input array after applying the Softplus activation.
21
22Formula: f(x) = ln(1 + e^x)
23
24Examples:
25>>> softplus(np.array([2.3, 0.6, -2, -3.8]))
26array([2.39554546, 1.03748795, 0.12692801, 0.02212422])
27
28>>> softplus(np.array([-9.2, -0.3, 0.45, -4.56]))
29array([1.01034298e-04, 5.54355244e-01, 9.43248946e-01, 1.04077103e-02])
30"""
31return np.log(1 + np.exp(vector))
32
33
34if __name__ == "__main__":
35import doctest
36
37doctest.testmod()
38