| 28 | |
| 29 | |
| 30 | class Sigmoid(ActivationBase): |
| 31 | def __init__(self): |
| 32 | """A logistic sigmoid activation function.""" |
| 33 | super().__init__() |
| 34 | |
| 35 | def __str__(self): |
| 36 | """Return a string representation of the activation function""" |
| 37 | return "Sigmoid" |
| 38 | |
| 39 | def fn(self, z): |
| 40 | r""" |
| 41 | Evaluate the logistic sigmoid, :math:`\sigma`, on the elements of input `z`. |
| 42 | |
| 43 | .. math:: |
| 44 | |
| 45 | \sigma(x_i) = \frac{1}{1 + e^{-x_i}} |
| 46 | """ |
| 47 | return 1 / (1 + np.exp(-z)) |
| 48 | |
| 49 | def grad(self, x): |
| 50 | r""" |
| 51 | Evaluate the first derivative of the logistic sigmoid on the elements of `x`. |
| 52 | |
| 53 | .. math:: |
| 54 | |
| 55 | \frac{\partial \sigma}{\partial x_i} = \sigma(x_i) (1 - \sigma(x_i)) |
| 56 | """ |
| 57 | fn_x = self.fn(x) |
| 58 | return fn_x * (1 - fn_x) |
| 59 | |
| 60 | def grad2(self, x): |
| 61 | r""" |
| 62 | Evaluate the second derivative of the logistic sigmoid on the elements of `x`. |
| 63 | |
| 64 | .. math:: |
| 65 | |
| 66 | \frac{\partial^2 \sigma}{\partial x_i^2} = |
| 67 | \frac{\partial \sigma}{\partial x_i} (1 - 2 \sigma(x_i)) |
| 68 | """ |
| 69 | fn_x = self.fn(x) |
| 70 | return fn_x * (1 - fn_x) * (1 - 2 * fn_x) |
| 71 | |
| 72 | |
| 73 | class ReLU(ActivationBase): |
no outgoing calls