Multivariate Gaussian model

Multivariate Gaussian models run continuous-domain stochastic policies.



skrl provides a Python mixin (MultivariateGaussianMixin) 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.

class MultivariateGaussianModel(MultivariateGaussianMixin, Model):
    def __init__(
        self,
        observation_space,
        state_space,
        action_space,
        device,
        clip_actions=False,
        clip_mean_actions=False,
        clip_log_std=True,
        min_log_std=-20,
        max_log_std=2,
    ):
        Model.__init__(
            self,
            observation_space=observation_space,
            state_space=state_space,
            action_space=action_space,
            device=device,
        )
        MultivariateGaussianMixin.__init__(
            self,
            clip_actions=clip_actions,
            clip_mean_actions=clip_mean_actions,
            clip_log_std=clip_log_std,
            min_log_std=min_log_std,
            max_log_std=max_log_std,
        )

Concept

Multivariate Gaussian model Multivariate Gaussian 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_gaussian_mlp-light.svg ../../_images/model_gaussian_mlp-dark.svg

import torch
import torch.nn as nn

from skrl.models.torch import Model, MultivariateGaussianMixin


# define the model
class MLP(MultivariateGaussianMixin, Model):
    def __init__(
        self,
        observation_space,
        state_space,
        action_space,
        device,
        clip_actions=False,
        clip_mean_actions=False,
        clip_log_std=True,
        min_log_std=-20,
        max_log_std=2,
    ):
        Model.__init__(
            self,
            observation_space=observation_space,
            state_space=state_space,
            action_space=action_space,
            device=device,
        )
        MultivariateGaussianMixin.__init__(
            self,
            clip_actions=clip_actions,
            clip_mean_actions=clip_mean_actions,
            clip_log_std=clip_log_std,
            min_log_std=min_log_std,
            max_log_std=max_log_std,
        )

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

        self.log_std_parameter = nn.Parameter(torch.zeros(self.num_actions))

    def compute(self, inputs, role):
        return self.net(inputs["observations"]), {"log_std": self.log_std_parameter}


# instantiate the model (given a wrapped environment: `env`)
policy = MLP(
    observation_space=env.observation_space,
    state_space=env.state_space,
    action_space=env.action_space,
    device=env.device,
    clip_actions=True,
    clip_mean_actions=True,
    clip_log_std=True,
    min_log_std=-20,
    max_log_std=2,
)

API


PyTorch

MultivariateGaussianMixin

Multivariate Gaussian mixin model (stochastic model).

class skrl.models.torch.multivariate_gaussian.MultivariateGaussianMixin(*, clip_actions: bool = False, clip_mean_actions: bool = False, clip_log_std: bool = True, min_log_std: float = -20, max_log_std: float = 2, role: str = '')[source]

Bases: object

Multivariate Gaussian mixin model (stochastic model).

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

  • clip_mean_actions – Flag to indicate whether the mean actions should be clipped to the action space. If True, the mean actions will be clipped before sampling the actions.

  • clip_log_std – Flag to indicate whether the log standard deviations should be clipped.

  • min_log_std – Minimum value of the log standard deviation if clip_log_std is True.

  • max_log_std – Maximum value of the log standard deviation if clip_log_std is True.

  • role – Role played by the model.

Methods:

act(inputs, *[, role])

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

distribution(*[, role])

Get the current distribution of the model.

get_entropy(*[, role])

Compute and return the entropy of the model.

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

Act stochastically 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 the following extra output values:

  • "log_std": log of the standard deviation.

  • "log_prob": log of the probability density function.

  • "mean_actions": mean actions (network output after optional clipping).

distribution(*, role: str = '') torch.distributions.MultivariateNormal[source]

Get the current distribution of the model.

Parameters:

role – Role played by the model.

Returns:

Distribution of the model.

get_entropy(*, role: str = '') torch.Tensor[source]

Compute and return the entropy of the model.

Parameters:

role – Role played by the model.

Returns:

Entropy of the model.