custom_layers
IndependentNonlinearitiesLayer(in_features, nonlin, bias, weight=True) ¶
Bases: Module
Neural network layer to add a bias, multiply the result with a scaling factor, and then apply the given nonlinearity. If a list of nonlinearities is provided, every dimension will be processed separately. The scaling and the bias are learnable parameters.
nonlin: A callable applied to all features, or one callable per feature. Module activations are
registered so their parameters, device, and training mode follow this layer.
bias: If `True`, a learnable bias is subtracted, else no bias is used.
weight: If `True`, the input is multiplied with a learnable scaling factor, else no weighting is used.
Source code in neuralfields/custom_layers.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
forward(inp) ¶
Apply a bias, scaling, and a nonliterary to each input separately.
\(y = f_{nlin}( w * (x + b) )\)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inp | Tensor | Arbitrary input tensor. | required |
Returns:
| Type | Description |
|---|---|
Tensor | Output tensor. |
Source code in neuralfields/custom_layers.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
MirroredConv1d(in_channels, out_channels, kernel_size, stride=1, padding='same', dilation=1, groups=1, bias=False, padding_mode='zeros', device=None, dtype=None) ¶
Bases: _ConvNd
A variant of the Conv1d module that re-uses parts of the convolution weights by mirroring the first half of the kernel (along the columns). This way we can save almost half of the parameters, under the assumption that we have a kernel that obeys this kind of symmetry. The biases are left unchanged.
Source code in neuralfields/custom_layers.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | |
forward(inp) ¶
Computes the 1-dim convolution just like Conv1d, however, the kernel has mirrored weights, i.e., it is symmetric around its middle element, or in case of an even kernel size around an imaginary middle element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inp | Tensor | 3-dim input tensor just like for Conv1d. | required |
Returns:
| Type | Description |
|---|---|
Tensor | 3-dim output tensor just like for Conv1d. |
Source code in neuralfields/custom_layers.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | |
apply_bell_shaped_weights_conv_(m, w, ks) ¶
Helper function to set the weights of a convolution layer according to a squared exponential.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
m | Module | Module containing the weights to be set. | required |
w | Tensor | Linearly spaced weights. | required |
ks | int | Size of the convolution kernel. | required |
Source code in neuralfields/custom_layers.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
init_param_(m, **kwargs) ¶
Initialize the parameters of the PyTorch Module / layer / network / cell according to its type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
m | Module | Module containing the weights to be set. | required |
kwargs | Any | Optional keyword arguments, e.g. | {} |
Source code in neuralfields/custom_layers.py
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 91 92 93 94 | |