Filtered Leaky ReLU
Filtered Leaky ReLU¤
neojax.nn.filtered_lrelu.FilteredLReLU
¤
Filtered leaky ReLU block (n-dimensional).
Computes the alias-free activation layer fused with the preceding/subsequent resampling operator of the CNO (does not include the preceding convolution layer).
Which activation layer and resampling operator to compute can
be controlled via block_type:
"invariant"(I-block/R-block): \(u = 2, d = 2\).s -> 2s -> lReLU -> s"upsample"(U-block): \(u = 4, d = 2\).s -> 4s -> lReLU -> 2s"downsample"(D-block): \(u = 2, d = 4\).s -> 2s -> lReLU -> s/2
Implements the CUDA logic as in the reference (classic CNO variant), i.e. as in StyleGAN3 in JAX. The filter taps are compile-time constants.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ndim
|
int
|
Number of spatial dimensions. |
required |
channels
|
int
|
Number of channels of the input, needed for the bias. |
required |
h_up
|
KaiserFIRFilter
|
Kaiser windowed FIR filter used for upsampling. It needs
|
required |
h_down
|
KaiserFIRFilter
|
Kaiser windowed FIR filter used for downsampling. It needs
|
required |
block_type
|
Literal['invariant', 'upsample', 'downsample']
|
The type of activation layer + resampling operator
block to compute. Must be one of |
required |
use_bias
|
bool
|
Whether to add a learnable channel-wise bias
(initialized to zero) before the activation. Default is |
True
|
pad_mode
|
Literal['wrap', 'reflect', 'constant']
|
Padding mode for the up- and downsampling padding. Use |
'wrap'
|
negative_slope
|
float
|
Negative slope for the leaky ReLU
(multiplication factor for |
0.2
|
gain
|
float
|
Factor multiplying the output of the leaky ReLU. Default is \(\sqrt{2}\), as in the reference. |
1.4142135623730951
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ValueError
|
If |
ValueError
|
If the number of taps of |
Internal Attributes
These fields store the internal layer states and weights.
- bias (
Float[Array, "c"] | None): The learnable channel-wise bias. - h_up (
KaiserFIRFilter): The upsampling filter. - h_down (
KaiserFIRFilter): The downsampling filter. - resampling_factors (
dict[str, tuple[int, int]]): Dictionary mappingblock_typeto up- and downsampling factors. - block_type (
str): The type of block. - pad_mode (
str): The padding mode. - spatial_axes (
tuple[int, ...]): The (negative) spatial axes. - negative_slope (
float): The slope of the leaky ReLU. - gain (
float): The gain after the leaky ReLU.
Examples:
import jax.numpy as jnp
from neojax.nn import FilteredLReLU, KaiserFIRFilter
# Filters of an I-block, designed on a unit grid
h_up = KaiserFIRFilter(1 / 2.0001, 6, 0.8 - 1 / 2.0001, 2, 1)
h_down = KaiserFIRFilter(1 / 2.0001, 6, 0.8 - 1 / 2.0001, 2, 1)
act = FilteredLReLU(
ndim=2,
channels=3,
h_up=h_up,
h_down=h_down,
block_type="invariant",
)
x = jnp.ones((3, 32, 32))
# out.shape = (3, 32, 32)
out = act(x)
down
property
¤
Downsampling factor of the activation rate w.r.t. the output.
up
property
¤
Upsampling factor of the activation rate w.r.t the input.
__call__(x: Inexact[Array, 'c ...']) -> Inexact[Array, 'c ...']
¤
Forward pass through the fused sampling and LReLU block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Inexact[Array, 'c ...']
|
Input signal shaped (c, d1, ..., dN). |
required |
Returns:
| Type | Description |
|---|---|
Inexact[Array, 'c ...']
|
The up- or downsampled signal with applied LReLU activation,
shaped (c, d1', ..., dN') with |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |