| 667 | |
| 668 | |
| 669 | class SoftPlus(ActivationBase): |
| 670 | def __init__(self): |
| 671 | """ |
| 672 | A softplus activation function. |
| 673 | |
| 674 | Notes |
| 675 | ----- |
| 676 | In contrast to :class:`ReLU`, the softplus activation is differentiable |
| 677 | everywhere (including 0). It is, however, less computationally efficient to |
| 678 | compute. |
| 679 | |
| 680 | The derivative of the softplus activation is the logistic sigmoid. |
| 681 | """ |
| 682 | super().__init__() |
| 683 | |
| 684 | def __str__(self): |
| 685 | """Return a string representation of the activation function""" |
| 686 | return "SoftPlus" |
| 687 | |
| 688 | def fn(self, z): |
| 689 | r""" |
| 690 | Evaluate the softplus activation on the elements of input `z`. |
| 691 | |
| 692 | .. math:: |
| 693 | |
| 694 | \text{SoftPlus}(z_i) = \log(1 + e^{z_i}) |
| 695 | """ |
| 696 | return np.log(np.exp(z) + 1) |
| 697 | |
| 698 | def grad(self, x): |
| 699 | r""" |
| 700 | Evaluate the first derivative of the softplus activation on the elements |
| 701 | of input `x`. |
| 702 | |
| 703 | .. math:: |
| 704 | |
| 705 | \frac{\partial \text{SoftPlus}}{\partial x_i} = \frac{e^{x_i}}{1 + e^{x_i}} |
| 706 | """ |
| 707 | exp_x = np.exp(x) |
| 708 | return exp_x / (exp_x + 1) |
| 709 | |
| 710 | def grad2(self, x): |
| 711 | r""" |
| 712 | Evaluate the second derivative of the softplus activation on the elements |
| 713 | of input `x`. |
| 714 | |
| 715 | .. math:: |
| 716 | |
| 717 | \frac{\partial^2 \text{SoftPlus}}{\partial x_i^2} = |
| 718 | \frac{e^{x_i}}{(1 + e^{x_i})^2} |
| 719 | """ |
| 720 | exp_x = np.exp(x) |
| 721 | return exp_x / ((exp_x + 1) ** 2) |
no outgoing calls