403 lines
16 KiB
Python
403 lines
16 KiB
Python
from functools import partial
|
|
from typing import Any, Dict, List, Optional, Tuple, Type, Union
|
|
|
|
import gym
|
|
import numpy as np
|
|
import torch as th
|
|
from stable_baselines3.common.policies import BasePolicy
|
|
from stable_baselines3.common.torch_layers import (
|
|
BaseFeaturesExtractor,
|
|
CombinedExtractor,
|
|
FlattenExtractor,
|
|
MlpExtractor,
|
|
NatureCNN,
|
|
)
|
|
from stable_baselines3.common.type_aliases import Schedule
|
|
from torch import nn
|
|
|
|
from sb3_contrib.common.maskable.distributions import MaskableDistribution, make_masked_proba_distribution
|
|
|
|
|
|
class MaskableActorCriticPolicy(BasePolicy):
|
|
"""
|
|
Policy class for actor-critic algorithms (has both policy and value prediction).
|
|
Used by A2C, PPO and the likes.
|
|
|
|
:param observation_space: Observation space
|
|
:param action_space: Action space
|
|
:param lr_schedule: Learning rate schedule (could be constant)
|
|
:param net_arch: The specification of the policy and value networks.
|
|
:param activation_fn: Activation function
|
|
:param ortho_init: Whether to use or not orthogonal initialization
|
|
:param features_extractor_class: Features extractor to use.
|
|
:param features_extractor_kwargs: Keyword arguments
|
|
to pass to the features extractor.
|
|
:param normalize_images: Whether to normalize images or not,
|
|
dividing by 255.0 (True by default)
|
|
:param optimizer_class: The optimizer to use,
|
|
``th.optim.Adam`` by default
|
|
:param optimizer_kwargs: Additional keyword arguments,
|
|
excluding the learning rate, to pass to the optimizer
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
observation_space: gym.spaces.Space,
|
|
action_space: gym.spaces.Space,
|
|
lr_schedule: Schedule,
|
|
net_arch: Optional[List[Union[int, Dict[str, List[int]]]]] = None,
|
|
activation_fn: Type[nn.Module] = nn.Tanh,
|
|
ortho_init: bool = True,
|
|
features_extractor_class: Type[BaseFeaturesExtractor] = FlattenExtractor,
|
|
features_extractor_kwargs: Optional[Dict[str, Any]] = None,
|
|
normalize_images: bool = True,
|
|
optimizer_class: Type[th.optim.Optimizer] = th.optim.Adam,
|
|
optimizer_kwargs: Optional[Dict[str, Any]] = None,
|
|
):
|
|
|
|
if optimizer_kwargs is None:
|
|
optimizer_kwargs = {}
|
|
# Small values to avoid NaN in Adam optimizer
|
|
if optimizer_class == th.optim.Adam:
|
|
optimizer_kwargs["eps"] = 1e-5
|
|
|
|
super().__init__(
|
|
observation_space,
|
|
action_space,
|
|
features_extractor_class,
|
|
features_extractor_kwargs,
|
|
optimizer_class=optimizer_class,
|
|
optimizer_kwargs=optimizer_kwargs,
|
|
squash_output=False,
|
|
)
|
|
|
|
# Default network architecture, from stable-baselines
|
|
if net_arch is None:
|
|
if features_extractor_class == NatureCNN:
|
|
net_arch = []
|
|
else:
|
|
net_arch = [dict(pi=[64, 64], vf=[64, 64])]
|
|
|
|
self.net_arch = net_arch
|
|
self.activation_fn = activation_fn
|
|
self.ortho_init = ortho_init
|
|
|
|
self.features_extractor = features_extractor_class(self.observation_space, **self.features_extractor_kwargs)
|
|
self.features_dim = self.features_extractor.features_dim
|
|
|
|
self.normalize_images = normalize_images
|
|
|
|
# Action distribution
|
|
self.action_dist = make_masked_proba_distribution(action_space)
|
|
|
|
self._build(lr_schedule)
|
|
|
|
def forward(
|
|
self,
|
|
obs: th.Tensor,
|
|
deterministic: bool = False,
|
|
action_masks: Optional[np.ndarray] = None,
|
|
) -> Tuple[th.Tensor, th.Tensor, th.Tensor]:
|
|
"""
|
|
Forward pass in all the networks (actor and critic)
|
|
|
|
:param obs: Observation
|
|
:param deterministic: Whether to sample or use deterministic actions
|
|
:param action_masks: Action masks to apply to the action distribution
|
|
:return: action, value and log probability of the action
|
|
"""
|
|
latent_pi, latent_vf = self._get_latent(obs)
|
|
# Evaluate the values for the given observations
|
|
values = self.value_net(latent_vf)
|
|
distribution = self._get_action_dist_from_latent(latent_pi)
|
|
if action_masks is not None:
|
|
distribution.apply_masking(action_masks)
|
|
actions = distribution.get_actions(deterministic=deterministic)
|
|
log_prob = distribution.log_prob(actions)
|
|
return actions, values, log_prob
|
|
|
|
def _get_constructor_parameters(self) -> Dict[str, Any]:
|
|
data = super()._get_constructor_parameters()
|
|
|
|
data.update(
|
|
dict(
|
|
net_arch=self.net_arch,
|
|
activation_fn=self.activation_fn,
|
|
lr_schedule=self._dummy_schedule, # dummy lr schedule, not needed for loading policy alone
|
|
ortho_init=self.ortho_init,
|
|
optimizer_class=self.optimizer_class,
|
|
optimizer_kwargs=self.optimizer_kwargs,
|
|
features_extractor_class=self.features_extractor_class,
|
|
features_extractor_kwargs=self.features_extractor_kwargs,
|
|
)
|
|
)
|
|
return data
|
|
|
|
def _build_mlp_extractor(self) -> None:
|
|
"""
|
|
Create the policy and value networks.
|
|
Part of the layers can be shared.
|
|
"""
|
|
# Note: If net_arch is None and some features extractor is used,
|
|
# net_arch here is an empty list and mlp_extractor does not
|
|
# really contain any layers (acts like an identity module).
|
|
self.mlp_extractor = MlpExtractor(
|
|
self.features_dim,
|
|
net_arch=self.net_arch,
|
|
activation_fn=self.activation_fn,
|
|
device=self.device,
|
|
)
|
|
|
|
def _build(self, lr_schedule: Schedule) -> None:
|
|
"""
|
|
Create the networks and the optimizer.
|
|
|
|
:param lr_schedule: Learning rate schedule
|
|
lr_schedule(1) is the initial learning rate
|
|
"""
|
|
self._build_mlp_extractor()
|
|
|
|
self.action_net = self.action_dist.proba_distribution_net(latent_dim=self.mlp_extractor.latent_dim_pi)
|
|
self.value_net = nn.Linear(self.mlp_extractor.latent_dim_vf, 1)
|
|
|
|
# Init weights: use orthogonal initialization
|
|
# with small initial weight for the output
|
|
if self.ortho_init:
|
|
# TODO: check for features_extractor
|
|
# Values from stable-baselines.
|
|
# features_extractor/mlp values are
|
|
# originally from openai/baselines (default gains/init_scales).
|
|
module_gains = {
|
|
self.features_extractor: np.sqrt(2),
|
|
self.mlp_extractor: np.sqrt(2),
|
|
self.action_net: 0.01,
|
|
self.value_net: 1,
|
|
}
|
|
for module, gain in module_gains.items():
|
|
module.apply(partial(self.init_weights, gain=gain))
|
|
|
|
# Setup optimizer with initial learning rate
|
|
self.optimizer = self.optimizer_class(self.parameters(), lr=lr_schedule(1), **self.optimizer_kwargs)
|
|
|
|
def _get_latent(self, obs: th.Tensor) -> Tuple[th.Tensor, th.Tensor]:
|
|
"""
|
|
Get the latent code (i.e., activations of the last layer of each network)
|
|
for the different networks.
|
|
|
|
:param obs: Observation
|
|
:return: Latent codes
|
|
for the actor, the value function and for gSDE function
|
|
"""
|
|
# Preprocess the observation if needed
|
|
features = self.extract_features(obs)
|
|
latent_pi, latent_vf = self.mlp_extractor(features)
|
|
|
|
return latent_pi, latent_vf
|
|
|
|
def _get_action_dist_from_latent(self, latent_pi: th.Tensor) -> MaskableDistribution:
|
|
"""
|
|
Retrieve action distribution given the latent codes.
|
|
|
|
:param latent_pi: Latent code for the actor
|
|
:return: Action distribution
|
|
"""
|
|
action_logits = self.action_net(latent_pi)
|
|
return self.action_dist.proba_distribution(action_logits=action_logits)
|
|
|
|
def _predict(
|
|
self,
|
|
observation: th.Tensor,
|
|
deterministic: bool = False,
|
|
action_masks: Optional[np.ndarray] = None,
|
|
) -> th.Tensor:
|
|
"""
|
|
Get the action according to the policy for a given observation.
|
|
|
|
:param observation:
|
|
:param deterministic: Whether to use stochastic or deterministic actions
|
|
:param action_masks: Action masks to apply to the action distribution
|
|
:return: Taken action according to the policy
|
|
"""
|
|
latent_pi, _ = self._get_latent(observation)
|
|
distribution = self._get_action_dist_from_latent(latent_pi)
|
|
if action_masks is not None:
|
|
distribution.apply_masking(action_masks)
|
|
return distribution.get_actions(deterministic=deterministic)
|
|
|
|
def predict(
|
|
self,
|
|
observation: Union[np.ndarray, Dict[str, np.ndarray]],
|
|
state: Optional[np.ndarray] = None,
|
|
mask: Optional[np.ndarray] = None,
|
|
deterministic: bool = False,
|
|
action_masks: Optional[np.ndarray] = None,
|
|
) -> Tuple[np.ndarray, Optional[np.ndarray]]:
|
|
"""
|
|
Get the policy action and state from an observation (and optional state).
|
|
Includes sugar-coating to handle different observations (e.g. normalizing images).
|
|
|
|
:param observation: the input observation
|
|
:param state: The last states (can be None, used in recurrent policies)
|
|
:param mask: The last masks (can be None, used in recurrent policies)
|
|
:param deterministic: Whether or not to return deterministic actions.
|
|
:param action_masks: Action masks to apply to the action distribution
|
|
:return: the model's action and the next state
|
|
(used in recurrent policies)
|
|
"""
|
|
# TODO (GH/1): add support for RNN policies
|
|
# if state is None:
|
|
# state = self.initial_state
|
|
# if mask is None:
|
|
# mask = [False for _ in range(self.n_envs)]
|
|
|
|
# Switch to eval mode (this affects batch norm / dropout)
|
|
self.set_training_mode(False)
|
|
|
|
observation, vectorized_env = self.obs_to_tensor(observation)
|
|
|
|
with th.no_grad():
|
|
actions = self._predict(observation, deterministic=deterministic, action_masks=action_masks)
|
|
# Convert to numpy
|
|
actions = actions.cpu().numpy()
|
|
|
|
if isinstance(self.action_space, gym.spaces.Box):
|
|
if self.squash_output:
|
|
# Rescale to proper domain when using squashing
|
|
actions = self.unscale_action(actions)
|
|
else:
|
|
# Actions could be on arbitrary scale, so clip the actions to avoid
|
|
# out of bound error (e.g. if sampling from a Gaussian distribution)
|
|
actions = np.clip(actions, self.action_space.low, self.action_space.high)
|
|
|
|
if not vectorized_env:
|
|
if state is not None:
|
|
raise ValueError("Error: The environment must be vectorized when using recurrent policies.")
|
|
actions = actions[0]
|
|
|
|
return actions, state
|
|
|
|
def evaluate_actions(
|
|
self,
|
|
obs: th.Tensor,
|
|
actions: th.Tensor,
|
|
action_masks: Optional[np.ndarray] = None,
|
|
) -> Tuple[th.Tensor, th.Tensor, th.Tensor]:
|
|
"""
|
|
Evaluate actions according to the current policy,
|
|
given the observations.
|
|
|
|
:param obs:
|
|
:param actions:
|
|
:return: estimated value, log likelihood of taking those actions
|
|
and entropy of the action distribution.
|
|
"""
|
|
latent_pi, latent_vf = self._get_latent(obs)
|
|
distribution = self._get_action_dist_from_latent(latent_pi)
|
|
if action_masks is not None:
|
|
distribution.apply_masking(action_masks)
|
|
log_prob = distribution.log_prob(actions)
|
|
values = self.value_net(latent_vf)
|
|
return values, log_prob, distribution.entropy()
|
|
|
|
|
|
class MaskableActorCriticCnnPolicy(MaskableActorCriticPolicy):
|
|
"""
|
|
CNN policy class for actor-critic algorithms (has both policy and value prediction).
|
|
Used by A2C, PPO and the likes.
|
|
|
|
:param observation_space: Observation space
|
|
:param action_space: Action space
|
|
:param lr_schedule: Learning rate schedule (could be constant)
|
|
:param net_arch: The specification of the policy and value networks.
|
|
:param activation_fn: Activation function
|
|
:param ortho_init: Whether to use or not orthogonal initialization
|
|
:param features_extractor_class: Features extractor to use.
|
|
:param features_extractor_kwargs: Keyword arguments
|
|
to pass to the features extractor.
|
|
:param normalize_images: Whether to normalize images or not,
|
|
dividing by 255.0 (True by default)
|
|
:param optimizer_class: The optimizer to use,
|
|
``th.optim.Adam`` by default
|
|
:param optimizer_kwargs: Additional keyword arguments,
|
|
excluding the learning rate, to pass to the optimizer
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
observation_space: gym.spaces.Space,
|
|
action_space: gym.spaces.Space,
|
|
lr_schedule: Schedule,
|
|
net_arch: Optional[List[Union[int, Dict[str, List[int]]]]] = None,
|
|
activation_fn: Type[nn.Module] = nn.Tanh,
|
|
ortho_init: bool = True,
|
|
features_extractor_class: Type[BaseFeaturesExtractor] = NatureCNN,
|
|
features_extractor_kwargs: Optional[Dict[str, Any]] = None,
|
|
normalize_images: bool = True,
|
|
optimizer_class: Type[th.optim.Optimizer] = th.optim.Adam,
|
|
optimizer_kwargs: Optional[Dict[str, Any]] = None,
|
|
):
|
|
super(MaskableActorCriticCnnPolicy, self).__init__(
|
|
observation_space,
|
|
action_space,
|
|
lr_schedule,
|
|
net_arch,
|
|
activation_fn,
|
|
ortho_init,
|
|
features_extractor_class,
|
|
features_extractor_kwargs,
|
|
normalize_images,
|
|
optimizer_class,
|
|
optimizer_kwargs,
|
|
)
|
|
|
|
|
|
class MaskableMultiInputActorCriticPolicy(MaskableActorCriticPolicy):
|
|
"""
|
|
MultiInputActorClass policy class for actor-critic algorithms (has both policy and value prediction).
|
|
Used by A2C, PPO and the likes.
|
|
|
|
:param observation_space: Observation space (Tuple)
|
|
:param action_space: Action space
|
|
:param lr_schedule: Learning rate schedule (could be constant)
|
|
:param net_arch: The specification of the policy and value networks.
|
|
:param activation_fn: Activation function
|
|
:param ortho_init: Whether to use or not orthogonal initialization
|
|
:param features_extractor_class: Uses the CombinedExtractor
|
|
:param features_extractor_kwargs: Keyword arguments
|
|
to pass to the feature extractor.
|
|
:param normalize_images: Whether to normalize images or not,
|
|
dividing by 255.0 (True by default)
|
|
:param optimizer_class: The optimizer to use,
|
|
``th.optim.Adam`` by default
|
|
:param optimizer_kwargs: Additional keyword arguments,
|
|
excluding the learning rate, to pass to the optimizer
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
observation_space: gym.spaces.Dict,
|
|
action_space: gym.spaces.Space,
|
|
lr_schedule: Schedule,
|
|
net_arch: Optional[List[Union[int, Dict[str, List[int]]]]] = None,
|
|
activation_fn: Type[nn.Module] = nn.Tanh,
|
|
ortho_init: bool = True,
|
|
features_extractor_class: Type[BaseFeaturesExtractor] = CombinedExtractor,
|
|
features_extractor_kwargs: Optional[Dict[str, Any]] = None,
|
|
normalize_images: bool = True,
|
|
optimizer_class: Type[th.optim.Optimizer] = th.optim.Adam,
|
|
optimizer_kwargs: Optional[Dict[str, Any]] = None,
|
|
):
|
|
super(MaskableMultiInputActorCriticPolicy, self).__init__(
|
|
observation_space,
|
|
action_space,
|
|
lr_schedule,
|
|
net_arch,
|
|
activation_fn,
|
|
ortho_init,
|
|
features_extractor_class,
|
|
features_extractor_kwargs,
|
|
normalize_images,
|
|
optimizer_class,
|
|
optimizer_kwargs,
|
|
)
|