returnn.tf.frontend_layers.layer
#
Layer (NameCtx earlier)
- class returnn.tf.frontend_layers.layer.Layer(*, module: ~returnn.frontend.module.Module | None = None, suggested_name: str | None = None, name: str | None = None, virtual: bool = False, can_access_children: bool = True, new_control_flow_ctx: ~returnn.tensor.control_flow_ctx.ControlFlowContext | None = None, parent: ~returnn.tf.frontend_layers.layer.Layer | None = <class 'returnn.util.basic.NotSpecified'>)[source]#
This is a helper class to keep track of the current name context when creating layers. Usually you do not need to access this directly except for creating the root name ctx and getting out the final RETURNN config or net dict.
A name ctx represents one absolute layer name in the RETURNN layer hierarchy, except for the root name ctx.
A name ctx thus can have a parent name ctx (if it is not the root), and potentially child name contexts.
See the documentation on name hierarchies for RETURNN and RETURNN-common in the module docstring at the top.
(Note: This class was previously called NameCtx in RETURNN-common.)
You are not supposed to call this directly. Use
NameCtx.new_root()
orscoped()
.- classmethod current_ctx(*, ignore_top_stack_frames: int = 0) Layer [source]#
Return the current context. This is the top from the stack with is_subnet_ctx, and additionally using the Python stack trace to automatically infer further subnets.
- Parameters:
ignore_top_stack_frames (int) –
- classmethod inner_loop() ControlFlowContext | None [source]#
- Returns:
the most inner loop in the current context, if there is one E.g. you can use it to access the outer spatial dim.
- classmethod inner_control_flow() ControlFlowContext | None [source]#
- Returns:
the most inner loop in the current context, if there is one E.g. you can use it to access the outer spatial dim.
- assign_parent(parent: Layer, suggested_name: str | None = None)[source]#
Assign or reassign parent to this name context.
- move_tensor_here(tensor: Tensor)[source]#
Moves an existing layer ref (with assigned name ctx) to another name ctx (without assigned layer or layer ref).
This assumes that there are no other references to tensor.raw_tensor because those would become invalid. References to tensor itself should be fine.
- property can_access_children_from_root[source]#
- Returns:
whether can_access_children for self and all parents
- control_flow_ctx() ControlFlowContext | None [source]#
- Returns:
control flow context of this name ctx
- get_returnn_config(*, root_module: Module) _ReturnnConfigSerializer [source]#
- Parameters:
root_module – there must be one root module such that all params have a well-defined name
- Returns:
config serializer
- make_default_output(ref: Tensor) Tensor [source]#
Assume this is a subnet, or the root net, and make a default output.
- get_abs_name(*, join_str: str = '/') str [source]#
- Returns:
absolute RETURNN layer name starting from root context.
- get_name_in_ctx(ctx: Layer, *, middle_prefix: str = '', shorten_subnet: bool = True) str [source]#
Get layer name valid in given scope.
- get_child_with_tensor(name: str, *, data: Tensor) Layer [source]#
Makes sure the child exists, including a corresponding layer ref. Creates the child together with a layer ref if it does not exist yet.
- get_child_tensor(name: str, *, data: Tensor) Tensor[Layer] [source]#
Get child layer ref. Makes sure it exists.
- get_recent_tensor(*, only_same_control_flow: bool = False) Tensor | None [source]#
Get recent tensor if it exists. Can go deeply through children.
- get_tensor_dependencies(*, _extra_layer_dict=None) List[Layer] [source]#
- Returns:
list of tensors this tensor depends on
- class returnn.tf.frontend_layers.layer.Net(*, name_ctx: Layer)[source]#
Represents a RETURNN (sub) network.
- class returnn.tf.frontend_layers.layer.ReturnnDimTagsProxy(*, reserved_names: Set[str] | None = None)[source]#
When serialized via __repr__, this represents a dict unique_name -> dim tag. All usages in the network and extern_data will also get proxies when serialized point to this dict.
- class DimRefProxy(*, dim: Dim | MarkedDim, name: str | None, path: Tuple[Any, ...], parent: ReturnnDimTagsProxy)[source]#
This will be a reference to the global dim_tags __repr__.
- dim_repr()[source]#
Dim repr, used for serialization of all registered dim tags. Any derived dims or special dims will not be registered and instead be represented with the same derivation referencing other registered dim tags. See
ReturnnDimTagsProxy.dim_ref_repr()
.
- class SetProxy(values: Sequence[Any])[source]#
This represents a set but with a predefined order. We want a deterministic order in the repr such that the generated code stays deterministic.
- copy() ReturnnDimTagsProxy [source]#
- Returns:
creates a shallow copy
- py_code_str(exclude_dims: Collection[DimRefProxy] = ())[source]#
- Parameters:
exclude_dims – dim tags to exclude from serializing
- Returns:
Python code