continuous#
Source code: tianshou/utils/net/continuous.py
- class AbstractContinuousActorDeterministic(output_dim: int)[source]#
Bases:
Actor,ABCMarker interface for continuous deterministic actors (DDPG like).
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- class ContinuousActorDeterministic(*, preprocess_net: ModuleWithVectorOutput, action_shape: Sequence[int] | int | int64, hidden_sizes: Sequence[int] = (), max_action: float = 1.0)[source]#
Bases:
AbstractContinuousActorDeterministicActor network that directly outputs actions for continuous action space. Used primarily in DDPG and its variants.
It will create an actor operated in continuous action space with structure of preprocess_net —> action_shape.
- Parameters:
preprocess_net – first part of input processing.
action_shape – a sequence of int for the shape of action.
hidden_sizes – a sequence of int for constructing the MLP after preprocess_net.
max_action – the scale for the final action.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- get_preprocess_net() ModuleWithVectorOutput[source]#
Returns the network component that is used for pre-processing, i.e. the component which produces a latent representation, which then is transformed into the final output. This is, therefore, the first part of the network which processes the input. For example, a CNN is often used in Atari examples.
We need this method to be able to share latent representation computations with other networks (e.g. critics) within an algorithm.
Actors that do not have a pre-processing stage can return nn.Identity() (see
RandomActorfor an example).
- forward(obs: Tensor | ndarray | BatchProtocol, state: T | None = None, info: dict[str, Any] | None = None) tuple[Tensor, T | None][source]#
Mapping: s_B -> action_values_BA, hidden_state_BH | None.
Returns a tensor representing the actions directly, i.e, of shape (n_actions, ), and a hidden state (which may be None). The hidden state is only not None if a recurrent net is used as part of the learning algorithm (support for RNNs is currently experimental).
- class AbstractContinuousCritic(output_dim: int)[source]#
Bases:
ModuleWithVectorOutput,ABC- Parameters:
output_dim – the dimension of the output vector.
- class ContinuousCritic(*, preprocess_net: ~tianshou.utils.net.common.ModuleWithVectorOutput, hidden_sizes: ~collections.abc.Sequence[int] = (), linear_layer: ~collections.abc.Callable[[int, int], ~torch.nn.modules.module.Module] = <class 'torch.nn.modules.linear.Linear'>, flatten_input: bool = True, apply_preprocess_net_to_obs_only: bool = False)[source]#
Bases:
AbstractContinuousCriticSimple critic network.
It will create an actor operated in continuous action space with structure of preprocess_net —> 1(q value).
- Parameters:
preprocess_net – the pre-processing network, which returns a vector of a known dimension. Typically, an instance of
Net.hidden_sizes – a sequence of int for constructing the MLP after preprocess_net.
linear_layer – use this module as linear layer.
flatten_input – whether to flatten input data for the last layer.
apply_preprocess_net_to_obs_only – whether to apply preprocess_net to the observations only (before concatenating with the action) - and without the observations being modified in any way beforehand. This allows the actor’s preprocessing network to be reused for the critic.
output_dim – the dimension of the output vector.
- class ContinuousActorProbabilistic(*, preprocess_net: ModuleWithVectorOutput, action_shape: Sequence[int] | int | int64, hidden_sizes: Sequence[int] = (), max_action: float = 1.0, unbounded: bool = False, conditioned_sigma: bool = False)[source]#
Bases:
AbstractContinuousActorProbabilisticSimple actor network that outputs mu and sigma to be used as input for a dist_fn (typically, a Gaussian).
Used primarily in SAC, PPO and variants thereof. For deterministic policies, see
Actor.- Parameters:
preprocess_net – the pre-processing network, which returns a vector of a known dimension.
action_shape – a sequence of int for the shape of action.
hidden_sizes – a sequence of int for constructing the MLP after preprocess_net.
max_action – the scale for the final action logits.
unbounded – whether to apply tanh activation on final logits.
conditioned_sigma – True when sigma is calculated from the input, False when sigma is an independent parameter.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- get_preprocess_net() ModuleWithVectorOutput[source]#
Returns the network component that is used for pre-processing, i.e. the component which produces a latent representation, which then is transformed into the final output. This is, therefore, the first part of the network which processes the input. For example, a CNN is often used in Atari examples.
We need this method to be able to share latent representation computations with other networks (e.g. critics) within an algorithm.
Actors that do not have a pre-processing stage can return nn.Identity() (see
RandomActorfor an example).
- forward(obs: Tensor | ndarray | BatchProtocol, state: T | None = None, info: dict[str, Any] | None = None) tuple[tuple[Tensor, Tensor], T | None][source]#
The main method for tianshou to compute action representations (such as actions, inputs of distributions, Q-values, etc) from env observations. Implementations will always make use of the preprocess_net as the first processing step.
- Parameters:
obs – the observations from the environment as retrieved from ObsBatchProtocol.obs. If the environment is a dict env, this will be an instance of Batch, otherwise it will be an array (or tensor if your env returns tensors).
state – the hidden state of the RNN, if applicable
info – the info object from the environment step
- Returns:
a tuple (action_repr, hidden_state), where action_repr is either an actual action for the environment or a representation from which it can be retrieved/sampled (e.g., mean and std for a Gaussian distribution), and hidden_state is the new hidden state of the RNN, if applicable.
- class RecurrentActorProb(*, layer_num: int, state_shape: Sequence[int], action_shape: Sequence[int], hidden_layer_size: int = 128, max_action: float = 1.0, unbounded: bool = False, conditioned_sigma: bool = False)[source]#
Bases:
ModuleRecurrent version of ActorProb.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- class RecurrentCritic(layer_num: int, state_shape: Sequence[int], action_shape: Sequence[int] = (0,), hidden_layer_size: int = 128)[source]#
Bases:
ModuleRecurrent version of Critic.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- class Perturbation(*, preprocess_net: Module, max_action: float, phi: float = 0.05)[source]#
Bases:
ModuleImplementation of perturbation network in BCQ algorithm.
Given a state and action, it can generate perturbed action.
- Parameters:
preprocess_net – a self-defined preprocess_net which output a flattened hidden state.
max_action – the maximum value of each dimension of action.
device – which device to create this model on.
phi – max perturbation parameter for BCQ.
See also
You can refer to examples/offline/offline_bcq.py to see how to use it.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(state: Tensor, action: Tensor) Tensor[source]#
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class VAE(*, encoder: Module, decoder: Module, hidden_dim: int, latent_dim: int, max_action: float)[source]#
Bases:
ModuleImplementation of VAE.
It models the distribution of action. Given a state, it can generate actions similar to those in batch. It is used in BCQ algorithm.
- Parameters:
encoder – the encoder in VAE. Its input_dim must be state_dim + action_dim, and output_dim must be hidden_dim.
decoder – the decoder in VAE. Its input_dim must be state_dim + latent_dim, and output_dim must be action_dim.
hidden_dim – the size of the last linear-layer in encoder.
latent_dim – the size of latent layer.
max_action – the maximum value of each dimension of action.
device – which device to create this model on.
See also
You can refer to examples/offline/offline_bcq.py to see how to use it.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(state: Tensor, action: Tensor) tuple[Tensor, Tensor, Tensor][source]#
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.