The Tensor class represents a tensor. This is framework-agnostic, i.e. is fine for TensorFlow, PyTorch, or any other framework. (Earlier, this was the class Data in RETURNN, and Data is now an alias for this class.)

This class is to describe a tensor, i.e. its shape and properties like whether we should consider it sparse data (i.e. it represents indices). Each dimension is described by Dim.

This is used in TFNetwork to describe the dataset external data (ExternData) as well as in every layer’s output and in many other parts of the code.

See Tensor and Dim.

This is designed in a way that is efficient for eager-mode execution such as PyTorch, but at the same time compatible with older RETURNN code.

Discussion on the move from loosely TF-specific Data to framework-agnostic Tensor:

class returnn.tensor.tensor.Tensor(name: str, dims: ~typing.Sequence[~returnn.tensor.dim.Dim] | None = None, dtype: str | None = None, *, sparse_dim: ~returnn.tensor.dim.Dim | None = None, feature_dim: ~returnn.tensor.dim.Dim | None = None, feature_dim_axis: int | ~returnn.util.basic.NotSpecified | None = <class 'returnn.util.basic.NotSpecified'>, raw_tensor: ~returnn.tensor.tensor.RawTensorType | None = None, version: int | None = None, **kwargs)[source]

Represents a tensor, in a frame-agnostic way. See the module docstring.

  • name

  • dims – the shape, where each dimension is described by a Dim.

  • dtype – e.g. “float32” or “int64”

  • sparse_dim – when the values are indices into some dimension, this is the dimension. You can also interpret the whole tensor as a sparse representation of a dense one-hot tensor, where this sparse_dim becomes the additional dense dimension.

  • raw_tensor – the raw tensor, e.g. numpy array, TF tensor, or PyTorch tensor

  • version

    behavior version just for Tensor. If not specified, and dims is None (old code), it uses version 1. - v1: the old behavior of Data. Specifically, time_dim_axis and feature_dim_axis are used

    and automatically inferred when not specified.

    • v2: time_dim_axis, feature_dim_axis are None by default.

  • kwargs – see _handle_extra_kwargs(), infer_dim_tags()

size_dtype = 'int32'[source]
name: str[source]
dtype: str[source]
sparse_dim: Dim | None[source]
version: int[source]
property dims: Tuple[Dim, ...][source]

dim tags

property dims_set: Set[Dim][source]

set of dim tags. in all high-level code, the order of dims is irrelevant. The order must not play a role (RETURNN principles: Note that we do not include any implicit dims here. Also see verify_out_shape() and

property raw_tensor: RawTensorType | None[source]

raw tensor

property feature_dim: Dim | None[source]

self.dims[self.feature_dim_axis] or None. See for some discussion.

property device: str | None[source]