continuous#
Source code: tianshou/utils/net/continuous.py
- class Actor(preprocess_net: Module | Net, action_shape: Sequence[int] | int | int64, hidden_sizes: Sequence[int] = (), max_action: float = 1.0, device: str | int | device = 'cpu', preprocess_net_output_dim: int | None = None)[source]#
Bases:
BaseActorSimple actor network that directly outputs actions for continuous action space. Used primarily in DDPG and its variants. For probabilistic policies, see
ActorProb.It will create an actor operated in continuous action space with structure of preprocess_net —> action_shape.
- Parameters:
preprocess_net – a self-defined preprocess_net, see usage. Typically, an instance of
Net.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.
preprocess_net_output_dim – the output dimension of preprocess_net. Only used when preprocess_net does not have the attribute output_dim.
For advanced usage (how to customize the network), please refer to Build the Network.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(obs: ndarray | Tensor, state: Any = None, info: dict[str, Any] | None = None) tuple[Tensor, Any][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 CriticBase(*args, **kwargs)[source]#
Bases:
Module,ABCInitializes internal Module state, shared by both nn.Module and ScriptModule.
- class Critic(preprocess_net: ~torch.nn.modules.module.Module | ~tianshou.utils.net.common.Net, hidden_sizes: ~collections.abc.Sequence[int] = (), device: str | int | ~torch.device = 'cpu', preprocess_net_output_dim: int | None = None, 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:
CriticBaseSimple critic network.
It will create an actor operated in continuous action space with structure of preprocess_net —> 1(q value).
- Parameters:
preprocess_net – a self-defined preprocess_net, see usage. Typically, an instance of
Net.hidden_sizes – a sequence of int for constructing the MLP after preprocess_net.
preprocess_net_output_dim – the output dimension of preprocess_net. Only used when preprocess_net does not have the attribute output_dim.
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.
For advanced usage (how to customize the network), please refer to Build the Network.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- class ActorProb(preprocess_net: Module | Net, action_shape: Sequence[int] | int | int64, hidden_sizes: Sequence[int] = (), max_action: float = 1.0, device: str | int | device = 'cpu', unbounded: bool = False, conditioned_sigma: bool = False, preprocess_net_output_dim: int | None = None)[source]#
Bases:
BaseActorSimple 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 – a self-defined preprocess_net, see usage. Typically, an instance of
Net.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.
preprocess_net_output_dim – the output dimension of preprocess_net. Only used when preprocess_net does not have the attribute output_dim.
For advanced usage (how to customize the network), please refer to Build the Network.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- class RecurrentActorProb(layer_num: int, state_shape: Sequence[int], action_shape: Sequence[int], hidden_layer_size: int = 128, max_action: float = 1.0, device: str | int | device = 'cpu', unbounded: bool = False, conditioned_sigma: bool = False)[source]#
Bases:
ModuleRecurrent version of ActorProb.
For advanced usage (how to customize the network), please refer to Build the Network.
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], device: str | int | device = 'cpu', hidden_layer_size: int = 128)[source]#
Bases:
ModuleRecurrent version of Critic.
For advanced usage (how to customize the network), please refer to Build the Network.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- class Perturbation(preprocess_net: Module, max_action: float, device: str | int | device = 'cpu', 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.
For advanced usage (how to customize the network), please refer to Build the Network.
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, device: str | device = 'cpu')[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.
For advanced usage (how to customize the network), please refer to Build the Network.
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.