Deterministic model

Deterministic models run continuous-domain deterministic policies.

skrl provides a Python mixin (DeterministicMixin) to assist in the creation of these types of models, allowing users to have full control over the function approximator definitions and architectures. Note that the use of this mixin must comply with the following rules:

  • The definition of multiple inheritance must always include the Model base class at the end.

    class DeterministicModel(DeterministicMixin, Model):
        def __init__(self, observation_space, action_space, device="cuda:0", clip_actions=False):
            Model.__init__(self, observation_space, action_space, device)
            DeterministicMixin.__init__(self, clip_actions)
    
  • The Model base class constructor must be invoked before the mixins constructor.

    class DeterministicModel(DeterministicMixin, Model):
        def __init__(self, observation_space, action_space, device="cuda:0", clip_actions=False):
            Model.__init__(self, observation_space, action_space, device)
            DeterministicMixin.__init__(self, clip_actions)
    

Concept

Deterministic model

Basic usage

  • Multi-Layer Perceptron (MLP)

  • Convolutional Neural Network (CNN)

  • Recurrent Neural Network (RNN)

  • Gated Recurrent Unit RNN (GRU)

  • Long Short-Term Memory RNN (LSTM)

../_images/model_deterministic_mlp.svg
 1import torch
 2import torch.nn as nn
 3
 4from skrl.models.torch import Model, DeterministicMixin
 5
 6
 7# define the model
 8class MLP(DeterministicMixin, Model):
 9    def __init__(self, observation_space, action_space, device, clip_actions=False):
10        Model.__init__(self, observation_space, action_space, device)
11        DeterministicMixin.__init__(self, clip_actions)
12
13        self.net = nn.Sequential(nn.Linear(self.num_observations + self.num_actions, 64),
14                                 nn.ReLU(),
15                                 nn.Linear(64, 32),
16                                 nn.ReLU(),
17                                 nn.Linear(32, 1))
18
19    def compute(self, inputs, role):
20        return self.net(torch.cat([inputs["states"], inputs["taken_actions"]], dim=1)), {}
21
22
23# instantiate the model (assumes there is a wrapped environment: env)
24critic = MLP(observation_space=env.observation_space,
25             action_space=env.action_space,
26             device=env.device,
27             clip_actions=False)

API

class skrl.models.torch.deterministic.DeterministicMixin(clip_actions: bool = False, role: str = '')

Bases: object

__init__(clip_actions: bool = False, role: str = '') None

Deterministic mixin model (deterministic model)

Parameters
  • clip_actions (bool, optional) – Flag to indicate whether the actions should be clipped to the action space (default: False)

  • role (str, optional) – Role play by the model (default: "")

Example:

# define the model
>>> import torch
>>> import torch.nn as nn
>>> from skrl.models.torch import Model, DeterministicMixin
>>>
>>> class Value(DeterministicMixin, Model):
...     def __init__(self, observation_space, action_space, device="cuda:0", clip_actions=False):
...         Model.__init__(self, observation_space, action_space, device)
...         DeterministicMixin.__init__(self, clip_actions)
...
...         self.net = nn.Sequential(nn.Linear(self.num_observations, 32),
...                                  nn.ELU(),
...                                  nn.Linear(32, 32),
...                                  nn.ELU(),
...                                  nn.Linear(32, 1))
...
...     def compute(self, inputs, role):
...         return self.net(inputs["states"]), {}
...
>>> # given an observation_space: gym.spaces.Box with shape (60,)
>>> # and an action_space: gym.spaces.Box with shape (8,)
>>> model = Value(observation_space, action_space)
>>>
>>> print(model)
Value(
  (net): Sequential(
    (0): Linear(in_features=60, out_features=32, bias=True)
    (1): ELU(alpha=1.0)
    (2): Linear(in_features=32, out_features=32, bias=True)
    (3): ELU(alpha=1.0)
    (4): Linear(in_features=32, out_features=1, bias=True)
  )
)
act(inputs: Mapping[str, Union[torch.Tensor, Any]], role: str = '') Tuple[torch.Tensor, Optional[torch.Tensor], Mapping[str, Union[torch.Tensor, Any]]]

Act deterministically in response to the state of the environment

Parameters
  • inputs (dict where the values are typically torch.Tensor) –

    Model inputs. The most common keys are:

    • "states": state of the environment used to make the decision

    • "taken_actions": actions taken by the policy for the given states

  • role (str, optional) – Role play by the model (default: "")

Returns

Model output. The first component is the action to be taken by the agent. The second component is None. The third component is a dictionary containing extra output values

Return type

tuple of torch.Tensor, torch.Tensor or None, and dictionary

Example:

>>> # given a batch of sample states with shape (4096, 60)
>>> actions, _, outputs = model.act({"states": states})
>>> print(actions.shape, outputs)
torch.Size([4096, 1]) {}