chuchichaestli.models.smc
Self-modulated convolutions for 1, 2, and 3D inputs.
Classes:
| Name | Description |
|---|---|
SMConvND |
Convolution that normalizes and rescales its own weight. |
SMConvND
SMConvND(
dimensions: int,
in_channels: int,
out_channels: int | None = None,
kernel_size: int = 3,
stride: int = 1,
padding: int | str = "same",
bias: bool = True,
eps: float = 1e-08,
**kwargs,
)
Convolution that normalizes and rescales its own weight.
A learnable scale per input channel modulates the weight, which is then demodulated back to unit norm per output channel:
w'[o, i, k] = s[i] w[o, i, k] / sqrt(sum_{i, k} (s[i] w[o, i, k])**2 + eps)
Rescaling the weight rather than the activations removes the need for a normalization layer in front of the convolution, which is what makes it a drop-in replacement for a normalization and convolution pair.
Attributes:
| Name | Type | Description |
|---|---|---|
weight |
Convolution weight, shaped |
|
bias |
Convolution bias, or |
|
scales |
Learnable modulation, one scale per input channel. |
|
gain |
Learnable scalar applied to the demodulated weight. |
Constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimensions
|
int
|
Number of spatial dimensions. |
required |
in_channels
|
int
|
Number of input channels. |
required |
out_channels
|
int | None
|
Number of output channels; as many as the input if omitted. |
None
|
kernel_size
|
int
|
Kernel size of the convolution. |
3
|
stride
|
int
|
Stride of the convolution. |
1
|
padding
|
int | str
|
Padding of the convolution. |
'same'
|
bias
|
bool
|
Whether the convolution learns a bias. |
True
|
eps
|
float
|
Small constant keeping the demodulation finite. |
1e-08
|
kwargs
|
Additional keyword arguments for the convolution. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If a keyword argument the forward pass cannot honour is given. |
Methods:
| Name | Description |
|---|---|
extra_repr |
Report the channel counts and the kernel size. |
forward |
Forward pass through the self-modulated convolution. |
modulated_weight |
Return the weight after modulation, demodulation and the gain. |
Source code in src/chuchichaestli/models/smc.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
extra_repr
extra_repr() -> str
Report the channel counts and the kernel size.
Source code in src/chuchichaestli/models/smc.py
117 118 119 120 121 122 | |
forward
forward(x: torch.Tensor, *args) -> torch.Tensor
Forward pass through the self-modulated convolution.
Source code in src/chuchichaestli/models/smc.py
104 105 106 107 108 109 110 111 112 113 114 115 | |
modulated_weight
modulated_weight() -> torch.Tensor
Return the weight after modulation, demodulation and the gain.
Source code in src/chuchichaestli/models/smc.py
92 93 94 95 96 97 98 99 100 101 102 | |