Skip to content

Commit 785458a

Browse files
implement interface + base class structure
1 parent 4ad48a0 commit 785458a

16 files changed

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

0 commit comments

Comments
 (0)