Rate this Page
★ ★ ★ ★ ★

InteractionType#

class tensordict.nn.InteractionType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)#

Bases: StrEnum

The rule that a probabilistic module uses to get a value from a distribution.

  • RANDOM: a sample, from rsample() if the distribution has it, else from sample().

  • MODE, MEDIAN, MEAN: the attribute of the same name. If the distribution has no mean, MEAN averages n_empirical_estimate samples (see ProbabilisticTensorDictModule).

  • DETERMINISTIC: the deterministic_sample attribute. If the distribution does not have one, the mode for distributions with enumerable support (Categorical, Bernoulli, OneHotCategorical) and the mean for the others. For a distribution class that tensordict does not know, the module chooses the mean or the mode from the support of the distribution, and warns.

Set the type for a block of code with set_interaction_type, or for one module with its default_interaction_type argument. Strings are accepted, in any case: InteractionType("RANDOM").

Examples

>>> from tensordict.nn import InteractionType
>>> InteractionType("RANDOM")
<InteractionType.RANDOM: 'random'>
>>> InteractionType.from_str("Mode") is InteractionType.MODE
True
classmethod from_str(type_str: str) → InteractionType#

Returns the interaction type whose name matches a string, in any case.

Parameters:

type_str (str) – the name, for example "random" or "MODE".

Tutorials that use InteractionType#

Building models with TensorDictModule

Building models with TensorDictModule