pytensor_ml.layers.Linear#

class pytensor_ml.layers.Linear(name=None, *, n_in, n_out, bias=True, weight_initializer=None, bias_initializer=None)#

Affine map \(y = x W + b\).

Parameters:
namestr or None

Name prefix for the layer’s parameters. Defaults to “Linear” when None.

n_inint

Size of the input feature axis.

n_outint

Size of the output feature axis.

biasbool, optional

Add the learned shift \(b\), which starts at zero and is redrawn there. Default is True.

weight_initializerInitializer, optional

How \(W\) is drawn, at construction and on every redraw. Xavier normal when omitted.

bias_initializerInitializer, optional

How \(b\) is drawn. Zeros when omitted, following Keras and flax rather than torch, which draws the bias from \(\mathcal{U}(\pm 1/\sqrt{\text{fan\_in}})\).

Notes

Both parameters are drawn when the layer is built, so a network trains without any further call. initialize() redraws them from a single seed, which is what makes a run reproducible.

Examples

The dense layer: multiply by a learned matrix and add a learned bias. Pass bias=False where a normalization layer follows and would subtract the bias away again:

from pytensor_ml.layers import BatchNorm, Input, Linear, Sequential

X = Input("X", shape=(None, 64))
network = Sequential(
    Linear("fc", n_in=64, n_out=32, bias=False),
    BatchNorm("bn", n_in=32),
)

activations = network(X)

Methods

Linear.__init__([name, bias, ...])