stable-baselines3/torchy_baselines/common/policies.py

422 lines
19 KiB
Python
Raw Normal View History

2020-03-23 16:15:30 +00:00
from typing import Union, Type, Dict, List, Tuple, Optional
2020-02-12 14:25:05 +00:00
2019-11-22 12:06:41 +00:00
from itertools import zip_longest
2020-02-12 14:25:05 +00:00
import gym
2019-09-18 13:35:17 +00:00
import torch as th
2019-09-06 08:44:55 +00:00
import torch.nn as nn
2020-02-12 14:25:05 +00:00
import numpy as np
2019-09-05 15:29:41 +00:00
2020-03-23 16:15:30 +00:00
from torchy_baselines.common.preprocessing import preprocess_obs
2019-09-05 15:29:41 +00:00
2019-09-06 08:44:55 +00:00
class BasePolicy(nn.Module):
2019-09-05 15:29:41 +00:00
"""
The base policy object
2020-02-12 14:25:05 +00:00
:param observation_space: (gym.spaces.Space) The observation space of the environment
:param action_space: (gym.spaces.Space) The action space of the environment
:param device: (Union[th.device, str]) Device on which the code should run.
:param squash_output: (bool) For continuous actions, whether the output is squashed
or not using a `tanh()` function.
2020-03-23 16:15:30 +00:00
:param features_extractor: (nn.Module) Network to extract features
(a CNN when using images, a nn.Flatten() layer otherwise)
:param normalize_images: (bool) Whether to normalize images or not,
dividing by 255.0 (True by default)
2019-09-05 15:29:41 +00:00
"""
2020-02-12 14:25:05 +00:00
def __init__(self, observation_space: gym.spaces.Space,
action_space: gym.spaces.Space,
device: Union[th.device, str] = 'cpu',
2020-03-23 16:15:30 +00:00
squash_output: bool = False,
features_extractor: Optional[nn.Module] = None,
normalize_images: bool = True):
2019-09-06 08:44:55 +00:00
super(BasePolicy, self).__init__()
2019-09-05 15:29:41 +00:00
self.observation_space = observation_space
self.action_space = action_space
self.device = device
2020-03-23 16:15:30 +00:00
self.features_extractor = features_extractor
self.normalize_images = normalize_images
self._squash_output = squash_output
2020-03-23 16:15:30 +00:00
def extract_features(self, obs: th.Tensor) -> th.Tensor:
"""
Preprocess the observation if needed and extract features.
:param obs: (th.Tensor)
:return: (th.Tensor)
"""
assert self.features_extractor is not None, 'No feature extractor was set'
preprocessed_obs = preprocess_obs(obs, self.observation_space, normalize_images=self.normalize_images)
return self.features_extractor(preprocessed_obs)
@property
def squash_output(self) -> bool:
""" (bool) Getter for squash_output."""
return self._squash_output
2019-09-05 15:29:41 +00:00
2019-09-25 11:20:06 +00:00
@staticmethod
2020-02-12 14:25:05 +00:00
def init_weights(module: nn.Module, gain: float = 1):
2020-03-24 09:10:37 +00:00
if isinstance(module, nn.Linear):
2019-09-25 11:20:06 +00:00
nn.init.orthogonal_(module.weight, gain=gain)
module.bias.data.fill_(0.0)
2019-09-06 12:01:10 +00:00
def forward(self, *_args, **kwargs):
raise NotImplementedError()
def _predict(self, observation: th.Tensor, deterministic: bool = False) -> th.Tensor:
2020-02-12 14:25:05 +00:00
"""
Get the action according to the policy for a given observation.
2020-03-23 16:15:30 +00:00
:param observation: (th.Tensor)
:param deterministic: (bool) Whether to use stochastic or deterministic actions
:return: (th.Tensor) Taken action according to the policy
2020-02-12 14:25:05 +00:00
"""
raise NotImplementedError()
def predict(self, observation: np.ndarray,
state: Optional[np.ndarray] = None,
mask: Optional[np.ndarray] = None,
deterministic: bool = False) -> Tuple[np.ndarray, Optional[np.ndarray]]:
"""
Get the policy action and state from an observation (and optional state).
:param observation: (np.ndarray) the input observation
:param state: (Optional[np.ndarray]) The last states (can be None, used in recurrent policies)
:param mask: (Optional[np.ndarray]) The last masks (can be None, used in recurrent policies)
:param deterministic: (bool) Whether or not to return deterministic actions.
:return: (Tuple[np.ndarray, Optional[np.ndarray]]) the model's action and the next state
(used in recurrent policies)
"""
# if state is None:
# state = self.initial_state
# if mask is None:
# mask = [False for _ in range(self.n_envs)]
observation = np.array(observation)
vectorized_env = self._is_vectorized_observation(observation, self.observation_space)
observation = observation.reshape((-1,) + self.observation_space.shape)
observation = th.as_tensor(observation).to(self.device)
with th.no_grad():
actions = self._predict(observation, deterministic=deterministic)
# Convert to numpy
actions = actions.cpu().numpy()
# Rescale to proper domain when using squashing
if isinstance(self.action_space, gym.spaces.Box) and self.squash_output:
actions = self.unscale_action(actions)
clipped_actions = actions
# Clip the actions to avoid out of bound error when using gaussian distribution
if isinstance(self.action_space, gym.spaces.Box) and not self.squash_output:
clipped_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.")
clipped_actions = clipped_actions[0]
return clipped_actions, state
def scale_action(self, action: np.ndarray) -> np.ndarray:
"""
Rescale the action from [low, high] to [-1, 1]
(no need for symmetric action space)
:param action: (np.ndarray) Action to scale
:return: (np.ndarray) Scaled action
"""
low, high = self.action_space.low, self.action_space.high
return 2.0 * ((action - low) / (high - low)) - 1.0
def unscale_action(self, scaled_action: np.ndarray) -> np.ndarray:
"""
Rescale the action from [-1, 1] to [low, high]
(no need for symmetric action space)
:param scaled_action: Action to un-scale
"""
low, high = self.action_space.low, self.action_space.high
return low + (0.5 * (scaled_action + 1.0) * (high - low))
@staticmethod
def _is_vectorized_observation(observation: np.ndarray, observation_space: gym.spaces.Space) -> bool:
"""
For every observation type, detects and validates the shape,
then returns whether or not the observation is vectorized.
:param observation: (np.ndarray) the input observation to validate
:param observation_space: (gym.spaces) the observation space
:return: (bool) whether the given observation is vectorized or not
"""
if isinstance(observation_space, gym.spaces.Box):
if observation.shape == observation_space.shape:
return False
elif observation.shape[1:] == observation_space.shape:
return True
else:
raise ValueError("Error: Unexpected observation shape {} for ".format(observation.shape) +
"Box environment, please use {} ".format(observation_space.shape) +
"or (n_env, {}) for the observation shape."
.format(", ".join(map(str, observation_space.shape))))
elif isinstance(observation_space, gym.spaces.Discrete):
if observation.shape == (): # A numpy array of a number, has shape empty tuple '()'
return False
elif len(observation.shape) == 1:
return True
else:
raise ValueError("Error: Unexpected observation shape {} for ".format(observation.shape) +
"Discrete environment, please use (1,) or (n_env, 1) for the observation shape.")
# TODO: add support for MultiDiscrete and MultiBinary observation spaces
# elif isinstance(observation_space, gym.spaces.MultiDiscrete):
# if observation.shape == (len(observation_space.nvec),):
# return False
# elif len(observation.shape) == 2 and observation.shape[1] == len(observation_space.nvec):
# return True
# else:
# raise ValueError("Error: Unexpected observation shape {} for MultiDiscrete ".format(observation.shape) +
# "environment, please use ({},) or ".format(len(observation_space.nvec)) +
# "(n_env, {}) for the observation shape.".format(len(observation_space.nvec)))
# elif isinstance(observation_space, gym.spaces.MultiBinary):
# if observation.shape == (observation_space.n,):
# return False
# elif len(observation.shape) == 2 and observation.shape[1] == observation_space.n:
# return True
# else:
# raise ValueError("Error: Unexpected observation shape {} for MultiBinary ".format(observation.shape) +
# "environment, please use ({},) or ".format(observation_space.n) +
# "(n_env, {}) for the observation shape.".format(observation_space.n))
else:
raise ValueError("Error: Cannot determine if the observation is vectorized with the space type {}."
.format(observation_space))
2020-02-12 14:25:05 +00:00
def save(self, path: str) -> None:
2019-09-06 12:01:10 +00:00
"""
Save policy weights to a given location.
NOTE: we don't save policy parameters
2019-09-06 12:01:10 +00:00
:param path: (str)
"""
th.save(self.state_dict(), path)
2020-02-12 14:25:05 +00:00
def load(self, path: str) -> None:
2019-09-06 12:01:10 +00:00
"""
Load policy weights from path.
NOTE: we don't load policy parameters
2019-09-06 12:01:10 +00:00
:param path: (str)
"""
self.load_state_dict(th.load(path))
2020-02-12 14:25:05 +00:00
def load_from_vector(self, vector: np.ndarray):
2019-09-06 12:01:10 +00:00
"""
Load parameters from a 1D vector.
:param vector: (np.ndarray)
"""
th.nn.utils.vector_to_parameters(th.FloatTensor(vector).to(self.device), self.parameters())
2020-02-12 14:25:05 +00:00
def parameters_to_vector(self) -> np.ndarray:
2019-09-06 12:01:10 +00:00
"""
Convert the parameters to a 1D vector.
:return: (np.ndarray)
"""
2019-09-18 11:10:27 +00:00
return th.nn.utils.parameters_to_vector(self.parameters()).detach().cpu().numpy()
def create_mlp(input_dim: int,
output_dim: int,
net_arch: List[int],
2020-03-24 09:10:37 +00:00
activation_fn: Type[nn.Module] = nn.ReLU,
squash_output: bool = False) -> List[nn.Module]:
"""
Create a multi layer perceptron (MLP), which is
a collection of fully-connected layers each followed by an activation function.
:param input_dim: (int) Dimension of the input vector
:param output_dim: (int)
:param net_arch: (List[int]) Architecture of the neural net
It represents the number of units per layer.
The length of this list is the number of layers.
2020-03-24 09:10:37 +00:00
:param activation_fn: (Type[nn.Module]) The activation function
to use after each layer.
:param squash_output: (bool) Whether to squash the output using a Tanh
activation function
:return: (List[nn.Module])
"""
if len(net_arch) > 0:
modules = [nn.Linear(input_dim, net_arch[0]), activation_fn()]
else:
modules = []
2019-09-18 11:10:27 +00:00
for idx in range(len(net_arch) - 1):
modules.append(nn.Linear(net_arch[idx], net_arch[idx + 1]))
modules.append(activation_fn())
if output_dim > 0:
modules.append(nn.Linear(net_arch[-1], output_dim))
if squash_output:
2019-09-18 11:10:27 +00:00
modules.append(nn.Tanh())
return modules
2020-03-23 16:15:30 +00:00
def create_sde_features_extractor(features_dim: int,
sde_net_arch: List[int],
2020-03-24 09:10:37 +00:00
activation_fn: Type[nn.Module]) -> Tuple[nn.Sequential, int]:
"""
Create the neural network that will be used to extract features
2020-03-23 16:15:30 +00:00
for the SDE exploration function.
:param features_dim: (int)
:param sde_net_arch: ([int])
2020-03-24 09:10:37 +00:00
:param activation_fn: (Type[nn.Module])
:return: (nn.Sequential, int)
"""
# Special case: when using states as features (i.e. sde_net_arch is an empty list)
# don't use any activation function
sde_activation = activation_fn if len(sde_net_arch) > 0 else None
latent_sde_net = create_mlp(features_dim, -1, sde_net_arch, activation_fn=sde_activation, squash_output=False)
latent_sde_dim = sde_net_arch[-1] if len(sde_net_arch) > 0 else features_dim
2020-03-23 16:15:30 +00:00
sde_features_extractor = nn.Sequential(*latent_sde_net)
return sde_features_extractor, latent_sde_dim
2019-09-18 11:10:27 +00:00
_policy_registry = dict() # type: Dict[Type[BasePolicy], Dict[str, Type[BasePolicy]]]
2019-09-05 15:29:41 +00:00
def get_policy_from_name(base_policy_type: Type[BasePolicy], name: str) -> Type[BasePolicy]:
2019-09-05 15:29:41 +00:00
"""
Returns the registered policy from the base type and name
2019-09-05 15:29:41 +00:00
:param base_policy_type: (Type[BasePolicy]) the base policy class
2019-09-05 15:29:41 +00:00
:param name: (str) the policy name
:return: (Type[BasePolicy]) the policy
2019-09-05 15:29:41 +00:00
"""
if base_policy_type not in _policy_registry:
2020-01-22 15:39:25 +00:00
raise ValueError(f"Error: the policy type {base_policy_type} is not registered!")
2019-09-05 15:29:41 +00:00
if name not in _policy_registry[base_policy_type]:
2020-01-22 15:39:25 +00:00
raise ValueError(f"Error: unknown policy type {name},"
"the only registed policy type are: {list(_policy_registry[base_policy_type].keys())}!")
2019-09-05 15:29:41 +00:00
return _policy_registry[base_policy_type][name]
def register_policy(name: str, policy: Type[BasePolicy]) -> None:
2019-09-05 15:29:41 +00:00
"""
Register a policy, so it can be called using its name.
e.g. SAC('MlpPolicy', ...) instead of SAC(MlpPolicy, ...)
2019-09-05 15:29:41 +00:00
:param name: (str) the policy name
:param policy: (Type[BasePolicy]) the policy class
2019-09-05 15:29:41 +00:00
"""
sub_class = None
2019-09-26 09:46:40 +00:00
# For building the doc
try:
for cls in BasePolicy.__subclasses__():
if issubclass(policy, cls):
sub_class = cls
break
except AttributeError:
sub_class = str(th.random.randint(100))
2019-09-05 15:29:41 +00:00
if sub_class is None:
2020-01-22 15:39:25 +00:00
raise ValueError(f"Error: the policy {policy} is not of any known subclasses of BasePolicy!")
2019-09-05 15:29:41 +00:00
if sub_class not in _policy_registry:
_policy_registry[sub_class] = {}
if name in _policy_registry[sub_class]:
2020-01-22 15:39:25 +00:00
raise ValueError(f"Error: the name {name} is alreay registered for a different policy, will not override.")
2019-09-05 15:29:41 +00:00
_policy_registry[sub_class][name] = policy
2019-11-22 12:06:41 +00:00
class MlpExtractor(nn.Module):
"""
Constructs an MLP that receives observations as an input and outputs a latent representation for the policy and
a value network. The ``net_arch`` parameter allows to specify the amount and size of the hidden layers and how many
of them are shared between the policy network and the value network. It is assumed to be a list with the following
structure:
1. An arbitrary length (zero allowed) number of integers each specifying the number of units in a shared layer.
If the number of ints is zero, there will be no shared layers.
2. An optional dict, to specify the following non-shared layers for the value network and the policy network.
It is formatted like ``dict(vf=[<value layer sizes>], pi=[<policy layer sizes>])``.
If it is missing any of the keys (pi or vf), no non-shared layers (empty list) is assumed.
For example to construct a network with one shared layer of size 55 followed by two non-shared layers for the value
network of size 255 and a single non-shared layer of size 128 for the policy network, the following layers_spec
would be used: ``[55, dict(vf=[255, 255], pi=[128])]``. A simple shared network topology with two layers of size 128
would be specified as [128, 128].
Adapted from Stable Baselines.
:param feature_dim: (int) Dimension of the feature vector (can be the output of a CNN)
:param net_arch: ([int or dict]) The specification of the policy and value networks.
See above for details on its formatting.
2020-03-24 09:10:37 +00:00
:param activation_fn: (Type[nn.Module]) The activation function to use for the networks.
2019-11-22 12:06:41 +00:00
:param device: (th.device)
"""
2020-03-10 17:09:45 +00:00
def __init__(self, feature_dim: int,
net_arch: List[Union[int, Dict[str, List[int]]]],
2020-03-24 09:10:37 +00:00
activation_fn: Type[nn.Module],
2020-03-10 17:09:45 +00:00
device: Union[th.device, str] = 'cpu'):
2019-11-22 12:06:41 +00:00
super(MlpExtractor, self).__init__()
shared_net, policy_net, value_net = [], [], []
policy_only_layers = [] # Layer sizes of the network that only belongs to the policy network
value_only_layers = [] # Layer sizes of the network that only belongs to the value network
last_layer_dim_shared = feature_dim
# Iterate through the shared layers and build the shared parts of the network
for idx, layer in enumerate(net_arch):
if isinstance(layer, int): # Check that this is a shared layer
layer_size = layer
# TODO: give layer a meaningful name
shared_net.append(nn.Linear(last_layer_dim_shared, layer_size))
shared_net.append(activation_fn())
last_layer_dim_shared = layer_size
else:
assert isinstance(layer, dict), "Error: the net_arch list can only contain ints and dicts"
if 'pi' in layer:
assert isinstance(layer['pi'], list), "Error: net_arch[-1]['pi'] must contain a list of integers."
policy_only_layers = layer['pi']
if 'vf' in layer:
assert isinstance(layer['vf'], list), "Error: net_arch[-1]['vf'] must contain a list of integers."
value_only_layers = layer['vf']
break # From here on the network splits up in policy and value network
last_layer_dim_pi = last_layer_dim_shared
last_layer_dim_vf = last_layer_dim_shared
# Build the non-shared part of the network
for idx, (pi_layer_size, vf_layer_size) in enumerate(zip_longest(policy_only_layers, value_only_layers)):
if pi_layer_size is not None:
assert isinstance(pi_layer_size, int), "Error: net_arch[-1]['pi'] must only contain integers."
policy_net.append(nn.Linear(last_layer_dim_pi, pi_layer_size))
policy_net.append(activation_fn())
last_layer_dim_pi = pi_layer_size
if vf_layer_size is not None:
assert isinstance(vf_layer_size, int), "Error: net_arch[-1]['vf'] must only contain integers."
value_net.append(nn.Linear(last_layer_dim_vf, vf_layer_size))
value_net.append(activation_fn())
last_layer_dim_vf = vf_layer_size
# Save dim, used to create the distributions
self.latent_dim_pi = last_layer_dim_pi
self.latent_dim_vf = last_layer_dim_vf
# Create networks
# If the list of layers is empty, the network will just act as an Identity module
self.shared_net = nn.Sequential(*shared_net).to(device)
self.policy_net = nn.Sequential(*policy_net).to(device)
self.value_net = nn.Sequential(*value_net).to(device)
2020-03-10 17:09:45 +00:00
def forward(self, features: th.Tensor) -> Tuple[th.Tensor, th.Tensor]:
2019-11-22 12:06:41 +00:00
"""
:return: (th.Tensor, th.Tensor) latent_policy, latent_value of the specified network.
If all layers are shared, then ``latent_policy == latent_value``
"""
shared_latent = self.shared_net(features)
return self.policy_net(shared_latent), self.value_net(shared_latent)