pytensor_ml.model.Model#

class pytensor_ml.model.Model(y, compile_kwargs=None)#

A network’s input and output, with conveniences to initialize its weights, train, and run inference.

Examples

Build the network, wrap it, and the model carries the graph through initialization, a training step, and inference:

import numpy as np

from pytensor_ml.activations import ReLU
from pytensor_ml.layers import Input, Linear, Sequential
from pytensor_ml.loss import SquaredError
from pytensor_ml.model import Model
from pytensor_ml.optim import adam

X = Input("X", shape=(None, 4))
network = Sequential(
    Linear("fc1", n_in=4, n_out=8),
    ReLU(),
    Linear("fc2", n_in=8, n_out=1),
)

model = Model(network(X)).initialize(seed=0)
step = model.compile_train(adam(1e-2), SquaredError())

batch = np.zeros((16, 4))
loss_value = step(batch, np.zeros((16, 1)))
predictions = model.predict(batch)

Methods

Model.__init__(y[, compile_kwargs])

Model.compile_train(rule[, loss_fn, ...])

Compile a one-step training function, either against a supervised target or a prebuilt loss.

Model.initialize([seed, initializers])

Redraw every trainable weight from its own initializer, in place, and return self.

Model.predict(*inputs, **named_inputs)

Run the inference pass, dropping dropout and reading batch norm's running statistics.

Attributes

weights

The trainable parameters, in graph-input order rather than construction order.