stable-baselines3/stable_baselines3/td3/td3.py

211 lines
10 KiB
Python
Raw Normal View History

2019-09-05 15:29:41 +00:00
import torch as th
import torch.nn.functional as F
2020-03-12 10:12:10 +00:00
from typing import List, Tuple, Type, Union, Callable, Optional, Dict, Any
2019-09-05 15:29:41 +00:00
2020-05-05 13:02:35 +00:00
from stable_baselines3.common import logger
from stable_baselines3.common.base_class import OffPolicyRLModel
from stable_baselines3.common.noise import ActionNoise
from stable_baselines3.common.type_aliases import GymEnv, MaybeCallback
from stable_baselines3.td3.policies import TD3Policy
2019-09-05 15:29:41 +00:00
2020-02-03 17:18:41 +00:00
class TD3(OffPolicyRLModel):
2019-09-05 15:29:41 +00:00
"""
2019-09-24 13:30:58 +00:00
Twin Delayed DDPG (TD3)
Addressing Function Approximation Error in Actor-Critic Methods.
Original implementation: https://github.com/sfujim/TD3
2019-09-05 15:29:41 +00:00
Paper: https://arxiv.org/abs/1802.09477
2019-09-24 13:30:58 +00:00
Introduction to TD3: https://spinningup.openai.com/en/latest/algorithms/td3.html
:param policy: (TD3Policy or str) The policy model to use (MlpPolicy, CnnPolicy, ...)
2020-03-11 11:45:21 +00:00
:param env: (GymEnv or str) The environment to learn from (if registered in Gym, can be str)
2019-09-24 13:30:58 +00:00
:param learning_rate: (float or callable) learning rate for adam optimizer,
2020-03-11 11:45:21 +00:00
the same learning rate will be used for all networks (Q-Values, Actor and Value function)
2019-09-24 13:30:58 +00:00
it can be a function of the current progress (from 1 to 0)
2020-03-11 11:45:21 +00:00
:param buffer_size: (int) size of the replay buffer
2019-09-24 13:30:58 +00:00
:param learning_starts: (int) how many steps of the model to collect transitions for before learning starts
:param batch_size: (int) Minibatch size for each gradient update
2020-05-08 10:28:41 +00:00
:param tau: (float) the soft update coefficient ("Polyak update", between 0 and 1)
2020-03-11 11:45:21 +00:00
:param gamma: (float) the discount factor
:param train_freq: (int) Update the model every ``train_freq`` steps.
2019-09-24 13:30:58 +00:00
:param gradient_steps: (int) How many gradient update after each step
2020-03-11 11:45:21 +00:00
:param n_episodes_rollout: (int) Update the model every ``n_episodes_rollout`` episodes.
Note that this cannot be used at the same time as ``train_freq``
:param action_noise: (ActionNoise) the action noise type (None by default), this can help
for hard exploration problem. Cf common.noise for the different action noise type.
:param policy_delay: (int) Policy and target networks will only be updated once every policy_delay steps
per training steps. The Q values will be updated policy_delay more often (update every training step).
2020-01-20 15:19:35 +00:00
:param target_policy_noise: (float) Standard deviation of Gaussian noise added to target policy
2019-09-24 13:30:58 +00:00
(smoothing noise)
:param target_noise_clip: (float) Limit for absolute value of target policy smoothing noise.
:param create_eval_env: (bool) Whether to create a second environment that will be
used for evaluating the agent periodically. (Only available when passing string for the environment)
:param policy_kwargs: (dict) additional arguments to be passed to the policy on creation
2020-03-12 14:34:35 +00:00
:param verbose: (int) the verbosity level: 0 no output, 1 info, 2 debug
2019-09-24 13:30:58 +00:00
:param seed: (int) Seed for the pseudo random generators
2019-09-26 09:46:40 +00:00
:param device: (str or th.device) Device (cpu, cuda, ...) on which the code should be run.
Setting it to auto, the code will be run on the GPU if possible.
2019-09-24 13:30:58 +00:00
:param _init_setup_model: (bool) Whether or not to build the network at the creation of the instance
2019-09-05 15:29:41 +00:00
"""
2020-03-11 11:45:21 +00:00
def __init__(self, policy: Union[str, Type[TD3Policy]],
env: Union[GymEnv, str],
learning_rate: Union[float, Callable] = 1e-3,
buffer_size: int = int(1e6),
learning_starts: int = 100,
batch_size: int = 100,
tau: float = 0.005,
gamma: float = 0.99,
train_freq: int = -1,
gradient_steps: int = -1,
n_episodes_rollout: int = 1,
action_noise: Optional[ActionNoise] = None,
policy_delay: int = 2,
target_policy_noise: float = 0.2,
target_noise_clip: float = 0.5,
tensorboard_log: Optional[str] = None,
create_eval_env: bool = False,
policy_kwargs: Dict[str, Any] = None,
verbose: int = 0,
seed: Optional[int] = None,
device: Union[th.device, str] = 'auto',
_init_setup_model: bool = True):
2019-09-05 15:29:41 +00:00
super(TD3, self).__init__(policy, env, TD3Policy, learning_rate,
buffer_size, learning_starts, batch_size,
policy_kwargs, tensorboard_log, verbose, device,
2020-01-20 10:17:55 +00:00
create_eval_env=create_eval_env, seed=seed,
2020-05-08 13:00:34 +00:00
sde_support=False)
2019-09-24 13:30:58 +00:00
2019-09-25 11:20:06 +00:00
self.train_freq = train_freq
self.gradient_steps = gradient_steps
self.n_episodes_rollout = n_episodes_rollout
self.tau = tau
2019-09-24 13:30:58 +00:00
self.gamma = gamma
2019-10-07 14:26:03 +00:00
self.action_noise = action_noise
2019-09-24 13:30:58 +00:00
self.policy_delay = policy_delay
self.target_noise_clip = target_noise_clip
self.target_policy_noise = target_policy_noise
2019-11-12 17:37:13 +00:00
2019-09-05 15:29:41 +00:00
if _init_setup_model:
self._setup_model()
2020-03-11 11:45:21 +00:00
def _setup_model(self) -> None:
super(TD3, self)._setup_model()
2019-09-05 15:29:41 +00:00
self._create_aliases()
2020-03-11 11:45:21 +00:00
def _create_aliases(self) -> None:
2019-09-05 15:29:41 +00:00
self.actor = self.policy.actor
self.actor_target = self.policy.actor_target
self.critic = self.policy.critic
self.critic_target = self.policy.critic_target
2020-03-23 13:48:38 +00:00
def train(self, gradient_steps: int, batch_size: int = 100, policy_delay: int = 2) -> None:
# Update learning rate according to lr schedule
self._update_learning_rate([self.actor.optimizer, self.critic.optimizer])
2019-09-05 15:29:41 +00:00
2019-09-25 11:20:06 +00:00
for gradient_step in range(gradient_steps):
2020-03-23 13:48:38 +00:00
2019-09-05 15:29:41 +00:00
# Sample replay buffer
2020-03-23 13:48:38 +00:00
replay_data = self.replay_buffer.sample(batch_size, env=self._vec_normalize_env)
2019-09-05 15:29:41 +00:00
2020-03-23 13:48:38 +00:00
with th.no_grad():
# Select action according to policy and add clipped noise
noise = replay_data.actions.clone().data.normal_(0, self.target_policy_noise)
noise = noise.clamp(-self.target_noise_clip, self.target_noise_clip)
next_actions = (self.actor_target(replay_data.next_observations) + noise).clamp(-1, 1)
2019-09-05 15:29:41 +00:00
2020-03-23 13:48:38 +00:00
# Compute the target Q value
target_q1, target_q2 = self.critic_target(replay_data.next_observations, next_actions)
target_q = th.min(target_q1, target_q2)
target_q = replay_data.rewards + (1 - replay_data.dones) * self.gamma * target_q
2019-09-05 15:29:41 +00:00
# Get current Q estimates
2020-03-10 15:43:10 +00:00
current_q1, current_q2 = self.critic(replay_data.observations, replay_data.actions)
2019-09-05 15:29:41 +00:00
# Compute critic loss
critic_loss = F.mse_loss(current_q1, target_q) + F.mse_loss(current_q2, target_q)
# Optimize the critic
self.critic.optimizer.zero_grad()
critic_loss.backward()
self.critic.optimizer.step()
2020-03-23 13:48:38 +00:00
# Delayed policy updates
if gradient_step % policy_delay == 0:
# Compute actor loss
2020-03-23 16:15:30 +00:00
actor_loss = -self.critic.q1_forward(replay_data.observations,
self.actor(replay_data.observations)).mean()
2019-09-05 15:29:41 +00:00
2020-03-23 13:48:38 +00:00
# Optimize the actor
self.actor.optimizer.zero_grad()
actor_loss.backward()
self.actor.optimizer.step()
2019-09-05 15:29:41 +00:00
2020-03-23 13:48:38 +00:00
# Update the frozen target networks
for param, target_param in zip(self.critic.parameters(), self.critic_target.parameters()):
2020-03-23 13:48:38 +00:00
target_param.data.copy_(self.tau * param.data + (1 - self.tau) * target_param.data)
2020-03-23 13:48:38 +00:00
for param, target_param in zip(self.actor.parameters(), self.actor_target.parameters()):
target_param.data.copy_(self.tau * param.data + (1 - self.tau) * target_param.data)
2019-09-05 15:29:41 +00:00
2020-03-13 10:43:12 +00:00
self._n_updates += gradient_steps
logger.record("train/n_updates", self._n_updates, exclude='tensorboard')
2020-03-13 10:43:12 +00:00
2020-03-11 11:45:21 +00:00
def learn(self,
total_timesteps: int,
2020-03-12 11:34:25 +00:00
callback: MaybeCallback = None,
2020-03-11 11:45:21 +00:00
log_interval: int = 4,
eval_env: Optional[GymEnv] = None,
eval_freq: int = -1,
n_eval_episodes: int = 5,
tb_log_name: str = "TD3",
eval_log_path: Optional[str] = None,
reset_num_timesteps: bool = True) -> OffPolicyRLModel:
2019-09-06 08:44:55 +00:00
total_timesteps, callback = self._setup_learn(total_timesteps, eval_env, callback, eval_freq,
n_eval_episodes, eval_log_path, reset_num_timesteps,
tb_log_name)
2020-01-27 13:32:31 +00:00
callback.on_training_start(locals(), globals())
2019-09-05 15:29:41 +00:00
2020-01-27 13:32:31 +00:00
while self.num_timesteps < total_timesteps:
2019-09-05 15:29:41 +00:00
2019-09-25 11:20:06 +00:00
rollout = self.collect_rollouts(self.env, n_episodes=self.n_episodes_rollout,
2019-10-07 14:26:03 +00:00
n_steps=self.train_freq, action_noise=self.action_noise,
2020-02-03 17:31:13 +00:00
callback=callback,
2019-09-25 11:20:06 +00:00
learning_starts=self.learning_starts,
replay_buffer=self.replay_buffer,
2019-10-10 11:47:13 +00:00
log_interval=log_interval)
2020-01-27 13:32:31 +00:00
if rollout.continue_training is False:
2020-01-27 13:32:31 +00:00
break
2019-09-25 11:20:06 +00:00
2019-10-28 15:47:13 +00:00
self._update_current_progress(self.num_timesteps, total_timesteps)
2019-09-12 12:00:55 +00:00
2019-10-01 19:56:37 +00:00
if self.num_timesteps > 0 and self.num_timesteps > self.learning_starts:
gradient_steps = self.gradient_steps if self.gradient_steps > 0 else rollout.episode_timesteps
self.train(gradient_steps, batch_size=self.batch_size, policy_delay=self.policy_delay)
2020-01-27 13:32:31 +00:00
callback.on_training_end()
2019-09-06 08:44:55 +00:00
return self
2019-09-05 15:29:41 +00:00
def excluded_save_params(self) -> List[str]:
2019-11-21 15:46:53 +00:00
"""
Returns the names of the parameters that should be excluded by default
when saving the model.
2019-11-21 15:46:53 +00:00
:return: (List[str]) List of parameters that should be excluded from save
2019-11-21 15:46:53 +00:00
"""
# Exclude aliases
2020-05-08 13:00:34 +00:00
return super(TD3, self).excluded_save_params() + ["actor", "critic", "actor_target", "critic_target"]
2019-11-21 15:46:53 +00:00
def get_torch_variables(self) -> Tuple[List[str], List[str]]:
2019-11-21 15:46:53 +00:00
"""
cf base class
2019-11-21 15:46:53 +00:00
"""
state_dicts = ["policy", "actor.optimizer", "critic.optimizer"]
return state_dicts, []