|
| 1 | +import torch |
| 2 | +import torch.nn as nn |
| 3 | + |
| 4 | +__all__ = ["StarReLU"] |
| 5 | + |
| 6 | + |
| 7 | +class StarReLU(nn.Module): |
| 8 | + def __init__( |
| 9 | + self, |
| 10 | + scale_value: float = 1.0, |
| 11 | + bias_value: float = 0.0, |
| 12 | + scale_learnable: bool = True, |
| 13 | + bias_learnable: bool = True, |
| 14 | + inplace: bool = False, |
| 15 | + ) -> None: |
| 16 | + """Apply StarReLU activation. |
| 17 | +
|
| 18 | + Adapted from: |
| 19 | + https://github.com/sail-sg/metaformer/blob/main/metaformer_baselines.py |
| 20 | +
|
| 21 | + See MetaFormer: https://arxiv.org/abs/2210.13452 |
| 22 | +
|
| 23 | + StarReLU: s * relu(x) ** 2 + b |
| 24 | +
|
| 25 | + Parameters |
| 26 | + ---------- |
| 27 | + scale_value : float, default=1.0 |
| 28 | + Learnable scaling factor for relu activation. |
| 29 | + bias_value : float, default=0.0 |
| 30 | + Learnable bias term for relu activation. |
| 31 | + scale_learnable : bool, default=True |
| 32 | + Flag, whether to keep the scale factor learnable. |
| 33 | + bias_learnable : bool, default=True |
| 34 | + Flag, whether to keep the bias term learnable. |
| 35 | + inplace : bool, default=False |
| 36 | + Flag whether to apply inplace-relu. |
| 37 | + """ |
| 38 | + super().__init__() |
| 39 | + self.inplace = inplace |
| 40 | + self.relu = nn.ReLU(inplace=inplace) |
| 41 | + self.scale = nn.Parameter( |
| 42 | + scale_value * torch.ones(1), requires_grad=scale_learnable |
| 43 | + ) |
| 44 | + self.bias = nn.Parameter( |
| 45 | + bias_value * torch.ones(1), requires_grad=bias_learnable |
| 46 | + ) |
| 47 | + |
| 48 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 49 | + """Forward pass of the StarReLU.""" |
| 50 | + return self.scale * self.relu(x) ** 2 + self.bias |
0 commit comments