tensordict.nn#
This page lists the modules of tensordict.nn, and the probabilistic
modules, distributions, and helpers that go with them. A module reads the
entries named in its in_keys from a tensordict, and writes the entries
named in its out_keys into it. For a guide with examples, see
Modules: TensorDictModule and tensordict.nn.
The page of a module class lists its methods and the methods of its
tensordict base classes. For the methods of torch.nn.Module, see
the PyTorch documentation.
Modules#
TensorDictModuleBaseis the base class of the tensordict modules.TensorDictModulewraps atorch.nn.Modulethat takes and returns tensors, and gives itin_keysandout_keys.TensorDictSequentialruns modules one after the other on the same tensordict.WrapModulewraps a function that takes and returns a tensordict.TensorDictModuleWrapperwraps a tensordict module and behaves like it, except for the methods that a subclass overrides.as_tensordict_moduleis a decorator that turns a function into aTensorDictModule.CudaGraphModulecaptures a module or a function in a CUDA graph and replays it. See torch.compile, torch.func, torch.export, and CUDA graphs.
|
Base class of the tensordict modules, which read |
|
Wraps a callable so that it reads its inputs from a tensordict and writes its outputs to it. |
|
Runs TensorDictModules one after the other on the same tensordict. |
|
Wraps a function that takes and returns a tensordict as a tensordict module. |
|
Wraps a tensordict module and behaves like it, except for the methods that a subclass overrides. |
|
Turns a function or a method into a tensordict module that reads |
|
Records a function, or a TensorDictModule, as a CUDA graph and replays it. |
Modules with tensorclass inputs and outputs#
A TensorClassModuleBase subclass declares the tensorclass types
of its input and output, so that a type checker can report a wrong field
name or type.
TensorClassModuleBase.as_td_module() returns a
TensorClassModuleWrapper, which reads and writes a tensordict, so
that you can put the module in a TensorDictSequential.
|
Base class of the modules that take and return a tensorclass. |
|
Wraps a |
Probabilistic modules#
A ProbabilisticTensorDictModule builds a distribution from
entries of the tensordict, and writes a sample, and optionally its
log-probability, to the tensordict. InteractionType and
set_interaction_type choose how the module gets its output from
the distribution: a random sample, the mode, the median, the mean, or
another deterministic value. set_composite_lp_aggregate chooses
whether a CompositeDistribution
aggregates the log-probabilities and entropies of its parts in one
tensor.
|
A module that builds a distribution from entries of a tensordict and writes a value from it. |
|
A |
|
The rule that a probabilistic module uses to get a value from a distribution. |
|
Sets the interaction type of all probabilistic modules inside a block of code, or inside a decorated function. |
|
Sets whether a |
|
Returns whether a |
Returns the interaction type set by |
Ensembles#
EnsembleModule makes num_copies copies of a module, each with
its own newly initialized parameters, and calls all of them on the input
with torch.vmap(). The output has a new leading batch dimension of
size num_copies. To keep parameters as a tensordict on a module, see
TensorDictParams on tensordict: containers and functions.
functional_vmap() calls a module once per set of parameters in a
stacked tensordict, such as one built with
from_modules(), and stacks the outputs. It runs
the sets with torch.vmap(), or one at a time for modules that
torch.vmap() cannot handle.
|
Runs |
|
Calls |
Compiling modules#
The modules of tensordict.nn work with torch.compile(). For
example, you can compile a TensorDictSequential. For the limits
and their workarounds, see torch.compile, torch.func, torch.export, and CUDA graphs. To capture a module in CUDA
graphs, use CudaGraphModule.
Distributions#
These distributions and modules build the distributions of probabilistic
modules. NormalParamExtractor and
AddStateIndependentNormalScale turn the output of a network into
the location and scale of a normal distribution.
CompositeDistribution groups several distributions, one for each
entry of a tensordict.
A module that returns its input as a location, with a learned scale that does not depend on the input. |
|
|
A distribution made of several named distributions, which samples and scores tensordicts. |
|
A distribution with all its mass on one value, |
|
A module without parameters that splits a tensor into the location and scale of a normal distribution. |
|
A categorical distribution whose values are one-hot tensors. |
|
A normal distribution of mean |
A standard normal distribution restricted to the interval |
Utilities#
dispatchlets you call a module with keyword arguments instead of a tensordict.set_skip_existingandskip_existing()make a module skip its computation when its output entries are already in the tensordict.mappings(),add_custom_mapping(), andbiased_softplusmap the output of a network to positive values, for example the scale of a distribution.inv_softplus()is the inverse of the softplus function, andexpln()is the positive mapping of the"expln"key ofmappings().rand_one_hot()samples a one-hot vector from logits.
tensordict.nn.make_tensordict is tensordict.make_tensordict().
|
Lets a function that takes a tensordict be called with tensors as arguments. |
|
Makes modules skip their computation when their outputs are already in the tensordict. |
|
A softplus shifted so that it returns |
Returns the mode set by |
|
|
Returns the function named by |
|
Registers |
|
Returns the input of the softplus function that gives |
|
Maps real numbers to positive numbers: |
|
Draws a one-hot sample along the last dimension of |
FX tracing (prototype)#
symbolic_trace() traces a tensordict module with torch.fx and
returns a TDGraphModule. Both are
prototypes. See FX tracing (prototype).
|
Traces a TensorDictModule or a TensorDictSequential into an FX graph. |
|
The module that |
See also#
Modules: TensorDictModule and tensordict.nn: how a module reads and writes a tensordict, with examples.
torch.compile, torch.func, torch.export, and CUDA graphs: compile modules and capture them in CUDA graphs.
The tutorials Building models with TensorDictModule, Functional calls and ensembles with TensorDict, and Exporting tensordict modules.