Skip to content

Type Aliases

Types and type aliases used by NPFL138.

npfl138.AnyArray module-attribute

AnyArray: TypeAlias = Tensor | ndarray | list | tuple

A type alias for any array-like structure.

PyTorch tensors, NumPy arrays, lists, and tuples are supported.

npfl138.DataFormat module-attribute

DataFormat: TypeAlias = Literal['HWC', 'CHW']

A type alias for image data format description.

npfl138.HasCompute

Bases: Protocol

A protocol for objects that have a compute method of the following type:

def compute(self) -> float | torch.Tensor | np.ndarray:
    ...

compute

compute() -> float | Tensor | ndarray

Compute the value of the object.

npfl138.Logs module-attribute

A dictionary of logs, with keys being the log names and values being the log values.

When the logs are returned by a npfl138.TrainableModule or passed to a npfl138.Callback, they are always evaluated to just float values.

npfl138.Reduction module-attribute

Reduction: TypeAlias = Literal['mean', 'sum', 'none']

A type alias for reduction methods used in losses and metrics.

npfl138.Tensor module-attribute

A type alias for a single tensor or a packed sequence of tensors.

npfl138.TensorOrTensors module-attribute

TensorOrTensors: TypeAlias = (
    Tensor | tuple[Tensor, ...] | list[Tensor] | dict[str, Tensor] | Any
)

A type alias for a single tensor or a tensor structure.

While a tensor or a sequence of them is the most common, any type is allowed here to accommodate nested or completely custom data structures.