Skip to main content

Python module

sequential

A Module for a sequence of tensor transformations.

ModuleList

class max.nn.sequential.ModuleList(iterable=(), /)

A Module subclass which is locally a list container.

ModuleList instances will use the stringified integer index of their submodules as the name of the module for the purposes of qualified paths.

For example:

from max.nn import Linear, Sequential

model = Sequential(
    Linear(5, 10),
    Linear(10, 5),
)

assert dict(model.parameters).keys() == {
    "0.weight", "0.bias", "1.weight", "1.bias"
}

children

property children: Iterable[tuple[str, Module[..., Any]]]

Iterates over the direct child modules of the Module.

Yields:

(name, module) pairs, where name is the attribute name of the child on the module.

Sequential

class max.nn.sequential.Sequential(*modules)

A Module subclass which holds a sequence of unary modules.

A unary Module is one whose forward() method has the signature:

def forward(self, x: Tensor) -> Tensor: ...

Sequential is itself a unary Module. Its forward() method computes the result of applying each of its child modules in sequence to its input.

For example, this will apply a linear transformation up to a dimension of 10, apply a LayerNorm, and then apply a final linear transformation to reduce back to the input dimension of 5:

from max.tensor import Tensor
from max.nn import Linear, Sequential

model = Sequential(
    Linear(5, 10),
    Linear(10, 5),
)

result = model(Tensor.ones([5]))
assert result.shape == [5]

Parameters:

modules (T)

forward()

forward(x)

Applies the contained modules in order.

For example, this code creates a sequence of linear transformations which each increase the dimension of the input by 5.

The input tensor must have dim 5. The intermediate applications will result in intermediate tensors of dim 10 and 15 respectively, and the final result will have dim 20:

from max.tensor import Tensor
from max.nn import Linear, Sequential

hidden_dims = [5, 10, 15, 20]

model = Sequential(*(
    Linear(in_dim, out_dim) for in_dim, out_dim in
    zip(hidden_dims, hidden_dims[1:])
))

result = model(Tensor.ones([5]))
assert result.shape == [20]

Parameters:

x (Tensor) – The input tensor.

Returns:

The result of iteratively applying each contained module in sequence.

Return type:

Tensor

Was this page helpful?