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.

  • The Model base class constructor must be invoked before the mixins constructor.

Note

For models in JAX/Flax it is imperative to define all parameters (except observation_space, state_space, action_space and device) with default values to avoid errors during initialization (TypeError: __init__() missing N required positional argument).

In addition, it is necessary to initialize the model’s state_dict (via the init_state_dict method) after its instantiation to avoid errors during its use (AttributeError: object has no attribute "state_dict". If "state_dict" is defined in '.setup()', remember these fields are only accessible from inside 'init' or 'apply').

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

Concept

Deterministic model Deterministic model

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-light.svg ../../_images/model_deterministic_mlp-dark.svg

import torch
import torch.nn as nn

from skrl.models.torch import Model, DeterministicMixin


# define the model
class MLP(DeterministicMixin, Model):
    def __init__(self, observation_space, state_space, action_space, device, clip_actions=False):
        Model.__init__(
            self,
            observation_space=observation_space,
            state_space=state_space,
            action_space=action_space,
            device=device,
        )
        DeterministicMixin.__init__(self, clip_actions=clip_actions)

        self.net = nn.Sequential(
            nn.Linear(self.num_observations + self.num_actions, 64),
            nn.ReLU(),
            nn.Linear(64, 32),
            nn.ReLU(),
            nn.Linear(32, 1),
        )

    def compute(self, inputs, role):
        return self.net(torch.cat([inputs["observations"], inputs["taken_actions"]], dim=1)), {}


# instantiate the model (given a wrapped environment: `env`)
critic = MLP(
    observation_space=env.observation_space,
    state_space=env.state_space,
    action_space=env.action_space,
    device=env.device,
    clip_actions=False,
)

API


PyTorch

DeterministicMixin

Deterministic mixin model (deterministic model).

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

Bases: object

Deterministic mixin model (deterministic model).

Parameters:
  • clip_actions – Flag to indicate whether the actions should be clipped to the action space.

  • role – Role played by the model.

Methods:

act(inputs, *[, role])

Act deterministically in response to the observations/states of the environment.

act(inputs: dict[str, Any], *, role: str = '') tuple[torch.Tensor, dict[str, Any]][source]

Act deterministically in response to the observations/states of the environment.

Parameters:
  • inputs

    Model inputs. The most common keys are:

    • "observations": observation of the environment used to make the decision.

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

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

  • role – Role played by the model.

Returns:

Model output. The first component is the expected action/value returned by the model. The second component is a dictionary containing extra output values according to the model.


JAX

DeterministicMixin

Deterministic mixin model (deterministic model).

class skrl.models.jax.deterministic.DeterministicMixin(*, clip_actions: bool = False, role: str = '')[source]

Bases: object

Deterministic mixin model (deterministic model).

Parameters:
  • clip_actions – Flag to indicate whether the actions should be clipped to the action space.

  • role – Role played by the model.

Methods:

act(inputs, *[, role, params])

Act deterministically in response to the observations/states of the environment.

act(inputs: dict[str, Any], *, role: str = '', params: jax.Array | None = None) tuple[jax.Array, dict[str, Any]][source]

Act deterministically in response to the observations/states of the environment.

Parameters:
  • inputs

    Model inputs. The most common keys are:

    • "observations": observation of the environment used to make the decision.

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

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

  • role – Role played by the model.

  • params – Parameters used to compute the output. If not provided, internal parameters will be used.

Returns:

Model output. The first component is the expected action/value returned by the model. The second component is a dictionary containing extra output values according to the model.


Warp

DeterministicMixin

Deterministic mixin model (deterministic model).