linear¶
Fully connected layer: a learned affine map on the last axis.
Category: core · Identity: linear@1
Shape¶
| Port | Direction | Pattern | dtype |
|---|---|---|---|
x |
input | x[B, ..., D_in] |
compute |
out |
output | out[B, ..., D_out] |
compute |
Arguments¶
| Name | Type | Default | Constraints | Description |
|---|---|---|---|---|
out_features (positional) |
int | inferred | >= 1; <= 2147483647; binds D_out |
Output width. Omit it to infer the width from what follows. |
in_features |
int | inferred | >= 1; <= 2147483647; binds D_in |
Input width. Normally inferred from the incoming tensor. |
bias |
bool | True |
— | Add a learned bias vector. |
spectral_norm |
bool | False |
— | Divide the weight by its largest singular value, estimated by power iteration. |
equalized |
bool | False |
— | Initialize raw weights N(0,1), zero bias, and scale weights by 1/sqrt(fan_in) at runtime. |
Description¶
Computes out = x @ weight.T + bias with weight of shape
[out_features, in_features], applied to the last axis of [B, D]
or [B, T, D] inputs. No activation is applied; add one explicitly.
Parameters are weight and, when enabled, bias.
Equalized learning rate¶
equalized=True initializes raw weights from N(0,1) and biases at zero.
Every forward uses weight / sqrt(in_features); bias remains unscaled.
Gain and learning-rate multiplier are both one; activations remain separate.
Parameter names and shapes are unchanged, but checkpoints and init=
contain raw weights. Loading ordinary linear weights directly therefore
changes their effective scale. This option cannot combine with spectral norm.
Spectral normalization¶
With spectral_norm=True the weight is reparametrized as weight /
sigma(weight), where sigma is the largest singular value estimated
by one power iteration per forward pass
(torch.nn.utils.parametrizations.spectral_norm). Each layer is then
1-Lipschitz, which is the standard constraint for a GAN discriminator.
The parametrization renames the registered state: the learned tensor
becomes parametrizations.weight.original and weight turns into a
computed attribute, with persistent buffers
parametrizations.weight.0._u and parametrizations.weight.0._v
holding the power-iteration vectors. init and trainable
overrides must therefore target parametrizations.weight.original
instead of weight; bias is unaffected. The power iteration
refreshes the buffers in training mode only, so evaluation is a pure
function of the stored state.
Examples¶
Example 1¶
The final width is inferred from the output contract.
Input ['B', 128] → output ['B', 10].
Network: [B, 128] -> [B, 10] dtype=float32
index name operation input shape output shape
0 n0 linear [B, 128] [B, 64]
1 n1 relu [B, 64] [B, 64]
2 n2 linear [B, 64] [B, 10]
Parameters: 8,906
Example 2¶
Input ['B', 16] → output ['B', 32].
Network: [B, 16] -> [B, 32] dtype=float32
index name operation input shape output shape
0 n0 linear [B, 16] [B, 32]
Parameters: 512
Example 3¶
Equalized learning rate with unit gain and unchanged parameter shapes.
Input ['B', 16] → output ['B', 32].
Network: [B, 16] -> [B, 32] dtype=float32
index name operation input shape output shape
0 n0 linear [B, 16] [B, 32]
Parameters: 544
Example 4¶
On a [B, T, D] sequence the map applies to every position.
Input ['B', 16, 32] → output ['B', 16, 8].
Network: [B, 16, 32] -> [B, 16, 8] dtype=float32
index name operation input shape output shape
0 n0 linear [B, 16, 32] [B, 16, 64]
1 n1 relu [B, 16, 64] [B, 16, 64]
2 n2 linear [B, 16, 64] [B, 16, 8]
Parameters: 2,632
Example 5¶
A spectrally normalized GAN critic head: every layer is 1-Lipschitz by construction.
Input ['B', 256] → output ['B', 1].
Network: [B, 256] -> [B, 1] dtype=float32
index name operation input shape output shape
0 n0 linear [B, 256] [B, 128]
1 n1 leaky_relu [B, 128] [B, 128]
2 n2 linear [B, 128] [B, 1]
Parameters: 33,025