pytensor_ml.state.Initializer#

class pytensor_ml.state.Initializer#

Base class for parameter initializers.

Subclasses implement sample(). Calling an instance assigns a freshly sampled value to a parameter in place, while initialize_params() calls sample() directly and leaves the assignment to its caller.

__props__ names the constructor arguments that define the draw, borrowing pytensor’s Op convention so the same encoder serves both. A subclass taking arguments must list them, or a saved network rebuilds it with the defaults rather than the values it was built with.

Examples

Subclass it to describe a draw of your own, or reach for the initializer() decorator, which builds the class from a sampling function. __props__ is what a saved config records, so a parameter that is reloaded is drawn the same way:

import numpy as np

from pytensor_ml.layers import Input, Linear
from pytensor_ml.state import Initializer

class ConstantInitializer(Initializer):
    __props__ = ("value",)

    def __init__(self, value=0.0):
        self.value = value

    def sample(self, shape, dtype, rng):
        return np.full(shape, self.value, dtype=dtype)

layer = Linear("fc", n_in=64, n_out=32, weight_initializer=ConstantInitializer(0.5))
activations = layer(Input("X", shape=(None, 64)))

Methods

Initializer.initial_value(shape)

Draw the value a parameter of shape is born holding, at the current floatX.

Initializer.sample(shape, dtype, rng)