IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /max/get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /max/get-started.md).

Python class

Linear

Linear​

class max.experimental.nn.Linear(in_dim, out_dim, *, bias=True)

source

Bases: Module

A unary linear transformation over an input tensor.

Linear is defined as f(x) = x @ W.T + B where W is the weight tensor and B is an optional bias tensor.

If W is not square then the transformation represents a dimensionality change. By convention the weight tensor is stored transposed.

from max.experimental.nn import Linear
from max.experimental.tensor import Tensor

model = Linear(5, 10)

assert dict(model.parameters) == {
    "weight": model.weight, "bias": model.bias
}

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

Constructs a random linear transformation of the given dimensions.

Parameters:

  • in_dim (DimLike) – The dimensionality of the input to the transformation
  • out_dim (DimLike) – The dimensionality after applying the transformation to the input tensor of dim in_dim.
  • bias (Tensor | Literal[0]) – Whether to use a bias in the transformation.

bias​

bias: Tensor | Literal[0]

source

The bias Tensor for the linear transformation (or 0 if bias is disabled).

forward()​

forward(x)

source

Defines the computation performed by the module.

Users must override this method in their subclass to define the module’s computation.

Parameters:

  • *args – Positional arguments for the computation.
  • **kwargs – Keyword arguments for the computation.
  • x (Tensor)

Returns:

The result of applying the module to the input.

Raises:

NotImplementedError – If the subclass does not override this method.

Return type:

Tensor

in_dim​

property in_dim: Dim

source

The input dimension for the transformation.

out_dim​

property out_dim: Dim

source

The output dimension for the transformation.

weight​

weight: Tensor

source

The weight Tensor for the linear transformation.