|
| 1 | +"""Module for the Adaptive ELU activation function.""" |
| 2 | + |
| 3 | +import torch |
| 4 | +from pina._src.adaptive_function.base_adaptive_function import ( |
| 5 | + BaseAdaptiveFunction, |
| 6 | +) |
| 7 | + |
| 8 | + |
| 9 | +class AdaptiveELU(BaseAdaptiveFunction): |
| 10 | + r""" |
| 11 | + Adaptive, trainable variant of the :class:`~torch.nn.ELU` activation. |
| 12 | +
|
| 13 | + This module extends the standard ELU by introducing learnable scaling |
| 14 | + and shifting parameters applied to both the input and the output. |
| 15 | +
|
| 16 | + Given the function :math:`\text{ELU}:\mathbb{R}^n\rightarrow\mathbb{R}^n`, |
| 17 | + the corresponding adaptive activation |
| 18 | + :math:`\text{ELU}_{\text{adaptive}}:\mathbb{R}^n\rightarrow\mathbb{R}^n` is |
| 19 | + defined as: |
| 20 | +
|
| 21 | + .. math:: |
| 22 | + \text{ELU}_{\text{adaptive}}({x}) = \alpha\,\text{ELU}(\beta{x}+\gamma), |
| 23 | +
|
| 24 | + where :math:`\alpha`, :math:`\beta`, and :math:`\gamma` are trainable |
| 25 | + parameters controlling output scaling, input scaling, and input shifting, |
| 26 | + respectively. |
| 27 | +
|
| 28 | + The ELU function is defined elementwise as: |
| 29 | + |
| 30 | + .. math:: |
| 31 | + \text{ELU}(x) = \begin{cases} |
| 32 | + x, & \text{ if }x > 0\\ |
| 33 | + \exp(x) - 1, & \text{ if }x \leq 0 |
| 34 | + \end{cases} |
| 35 | +
|
| 36 | + .. seealso:: |
| 37 | +
|
| 38 | + **Original reference**: Godfrey, L. B., Gashler, M. S. (2015). |
| 39 | + *A continuum among logarithmic, linear, and exponential functions, |
| 40 | + and its potential to improve generalization in neural networks.* |
| 41 | + 7th international joint conference on knowledge discovery, knowledge |
| 42 | + engineering and knowledge management (IC3K), Vol. 1. |
| 43 | + DOI: `arXiv preprint arXiv:1602.01321. |
| 44 | + <https://arxiv.org/abs/1602.01321>`_. |
| 45 | +
|
| 46 | + **Original reference**: Jagtap, A. D., Karniadakis, G. E. (2020). |
| 47 | + *Adaptive activation functions accelerate convergence in deep and |
| 48 | + physics-informed neural networks*. |
| 49 | + Journal of Computational Physics, 404. |
| 50 | + DOI: `JCP 10.1016 <https://doi.org/10.1016/j.jcp.2019.109136>`_. |
| 51 | + """ |
| 52 | + |
| 53 | + def __init__(self, alpha=None, beta=None, gamma=None, fixed=None): |
| 54 | + """ |
| 55 | + Initialization of the :class:`AdaptiveELU` class. |
| 56 | +
|
| 57 | + :param alpha: The output scaling parameter of the adaptive function. |
| 58 | + If ``None``, it is initialized to ``1``. Default is ``None``. |
| 59 | + :type alpha: int | float | complex |
| 60 | + :param beta: The input scaling parameter of the adaptive function. |
| 61 | + If ``None``, it is initialized to ``1``. Default is ``None``. |
| 62 | + :type beta: int | float | complex |
| 63 | + :param gamma: The input shifting parameter of the adaptive function. |
| 64 | + If ``None``, it is initialized to ``0``. Default is ``None``. |
| 65 | + :type gamma: int | float | complex |
| 66 | + :param fixed: The names of parameters to keep fixed during training. |
| 67 | + These parameters will not be optimized and will have |
| 68 | + ``requires_grad=False``. Available options are ``"alpha"``, |
| 69 | + ``"beta"``, and ``"gamma"``. If ``None``, all parameters are |
| 70 | + trainable. Default is ``None``. |
| 71 | + :type fixed: str | list[str] |
| 72 | + :raises ValueError: If alpha, when provided, is not a number. |
| 73 | + :raises ValueError: If beta, when provided, is not a number. |
| 74 | + :raises ValueError: If gamma, when provided, is not a number. |
| 75 | + :raises ValueError: If fixed, when provided, is neither a string nor a |
| 76 | + list of strings. |
| 77 | + :raises ValueError: If fixed contains invalid parameter names. |
| 78 | + """ |
| 79 | + super().__init__(alpha, beta, gamma, fixed) |
| 80 | + self.func = torch.nn.ELU() |
0 commit comments