returnn.tf.frontend_low_level._backend

Backend for exposing TensorFlow-specific functionality.

class returnn.tf.frontend_low_level._backend.TFBackend[source]

TensorFlow low-level backend, operating on tf.Tensor

name: str | None = 'tf'[source]
RawTensorType[source]

alias of Tensor

is_tensorflow: bool = True[source]
static executing_eagerly() bool[source]
Returns:

whether we are in eager execution mode

static get_dtype_name_raw(raw_tensor: Tensor) str[source]
Returns:

dtype of raw tensor, as string

static as_dtype_raw(dtype_name: str) DType[source]
Parameters:

dtype_name – e.g. “float32”

Returns:

dtype object

static get_ndim_raw(raw_tensor: Tensor) int[source]
Returns:

ndim of raw tensor. assumes it is known

static get_shape_raw(raw_tensor: Tensor) Tensor[source]
Returns:

shape of raw tensor

static get_shape_tuple_raw(raw_tensor: Tensor) Tuple[int | Tensor][source]
Returns:

shape of raw tensor. assumes that ndim is known

static get_known_shape_raw(raw_tensor: Tensor) Tuple[int | None][source]
Returns:

shape of raw tensor, int for static known, None otherwise. assumes that ndim is known.

static set_known_shape_raw(raw_tensor: Tensor, shape: Tuple[int | None]) None[source]

wrap tf.Tensor.set_shape

static fill_raw(shape: Sequence[int | Tensor] | Tensor, value: Any | Tensor) Tensor[source]
Parameters:
  • shape – shape

  • value – value to fill

Returns:

raw tensor filled with value everywhere

static compare_raw(a: Tensor, kind: str, b: Tensor) Tensor[source]
Parameters:
  • a

  • kind – “equal”, “less”, “less_equal”, “greater”, “greater_equal”, “not_equal”

  • b

Returns:

a kind b

static combine_raw(a: Tensor, kind: str, b: Tensor) Tensor[source]
Parameters:
  • a

  • kind – “add”, “sub”, “mul”, “truediv”, “floordiv”, “mod”, “pow”, “maximum”, “minimum”, “logical_and”, “logical_or”, “squared_difference”

  • b

Returns:

a kind b

static where(cond: Tensor, true_: Tensor | int | float | complex | number | ndarray | bool | str, false_: Tensor | int | float | complex | number | ndarray | bool | str, *, allow_broadcast_all_sources: bool = False) Tensor[source]
static reshape_raw(raw_tensor: Tensor, shape: Sequence[int | Tensor] | Tensor) Tensor[source]
Parameters:
  • raw_tensor – raw tensor

  • shape – new shape

Returns:

reshaped raw tensor

classmethod squeeze_raw(raw_tensor: Tensor, axes: Sequence[int]) Tensor[source]
Parameters:
  • raw_tensor – raw tensor

  • axes – axes to squeeze

Returns:

squeezed raw tensor

static transpose_raw(raw_tensor: Tensor, perm: Sequence[int]) Tensor[source]
Parameters:
  • raw_tensor

  • perm – e.g. [0, 2, 1]

Returns:

permuted (transposed) raw tensor; wraps tf.transpose

static expand_dims_raw(raw_tensor: Tensor, axis: int) Tensor[source]
Parameters:
  • raw_tensor

  • axis – e.g. 1

Returns:

raw tensor with new axis; wraps tf.expand_dims

static expand_raw(raw_tensor: Tensor, axis: int, dim: int | Tensor) Tensor[source]
Parameters:
  • raw_tensor

  • axis – shape[axis] must be 1

  • dim – the new dim for shape[axis]

Returns:

shape[axis] expands to dim

static copy(tensor: Tensor) Tensor[source]
static cast_raw(raw_tensor: Tensor, dtype: str) Tensor[source]

cast

static activation_raw(raw_tensor: Tensor, func: str) Tensor[source]
Parameters:
  • raw_tensor

  • func – e.g. “tanh”

Returns:

raw tensor after activation

static have_sequence_mask_raw() bool[source]
Returns:

whether we have sequence_mask

static sequence_mask_raw(lengths: Tensor, *, batch_major: bool = True) Tensor[source]

Wraps around tf.sequence_mask(). It will cache the value inside the passed object so that we don’t recompute it multiple times.

Parameters:
  • lengths – shape (batch,)

  • batch_major

Returns:

tensor mask of shape (batch,maxlen) if batch_major else (maxlen,batch) of type bool

static name_scope_raw(name: str) Any[source]
Parameters:

name – name of scope

Returns:

context manager

static control_dependencies_raw(dependencies: Sequence[Tensor | Operation]) Any[source]
Parameters:

dependencies – list of tensors or operations

Returns:

context manager

static identity_with_control_dependencies_raw(raw_tensor: Tensor, dependencies: Sequence[Any]) Tensor[source]
Parameters:
  • raw_tensor

  • dependencies – list of tensors or operations

Returns:

identity of tensor with control dependencies

static create_placeholder_raw(tensor: Tensor[Tensor]) Tensor[source]
Returns:

tf.placeholder in TF

static runtime_sanity_checks(tensor: Tensor[Tensor]) Operation[source]

Runtime checks

static is_valid_in_current_graph(tensor: Tensor[Tensor]) bool[source]
Returns:

whether the tensor is valid in the current graph

static format_graph_output(raw_tensor: Tensor, *, max_depth: int | None = None) str[source]
Parameters:
  • raw_tensor

  • max_depth

static convert_to_tensor(value: Tensor[Tensor] | Tensor | int | float | complex | number | ndarray | bool | str, *, dims: Sequence[Dim], dtype: str, sparse_dim: Dim | None = None, device: str | None = None, name: str | None = None) Tensor[Tensor][source]
Parameters:
  • value

  • dims

  • dtype

  • sparse_dim

  • device

  • name

Returns:

tensor

static range_over_dim(dim: Dim, *, dtype: str | None = None, device: str | None = None) Tensor[Tensor][source]
Parameters:
  • dim

  • dtype

  • device

Returns:

range over dim

static reduce(source: Tensor[Tensor], *, mode: str, axis: Dim | Sequence[Dim], use_mask: bool = True) Tensor[Tensor][source]

Reduce