stable-baselines3/torchy_baselines/ppo/policies.py

95 lines
3.7 KiB
Python
Raw Normal View History

2019-09-18 11:10:27 +00:00
import torch as th
import torch.nn as nn
from torch.distributions import Normal
2019-09-18 13:35:17 +00:00
from torchy_baselines.common.policies import BasePolicy, register_policy, create_mlp
2019-09-18 11:10:27 +00:00
class PPOPolicy(BasePolicy):
def __init__(self, observation_space, action_space,
learning_rate=1e-3, net_arch=None, device='cpu',
2019-09-19 15:18:41 +00:00
activation_fn=nn.Tanh, adam_epsilon=1e-5):
2019-09-18 11:10:27 +00:00
super(PPOPolicy, self).__init__(observation_space, action_space, device)
self.state_dim = self.observation_space.shape[0]
self.action_dim = self.action_space.shape[0]
if net_arch is None:
net_arch = [64, 64]
self.net_arch = net_arch
self.activation_fn = activation_fn
2019-09-19 15:18:41 +00:00
self.adam_epsilon = adam_epsilon
2019-09-18 11:10:27 +00:00
self.net_args = {
'input_dim': self.state_dim,
'output_dim': -1,
'net_arch': self.net_arch,
'activation_fn': self.activation_fn
}
self.shared_net = None
self._build(learning_rate)
2019-09-19 09:43:15 +00:00
@staticmethod
def init_weights(module):
if type(module) == nn.Linear:
nn.init.orthogonal_(module.weight, gain=1)
module.bias.data.fill_(0.0)
2019-09-18 11:10:27 +00:00
def _build(self, learning_rate):
2019-09-18 13:35:17 +00:00
shared_net = create_mlp(self.state_dim, output_dim=-1, net_arch=self.net_arch, activation_fn=self.activation_fn)
2019-09-18 11:10:27 +00:00
self.shared_net = nn.Sequential(*shared_net).to(self.device)
self.actor_net = nn.Linear(self.net_arch[-1], self.action_dim)
self.value_net = nn.Linear(self.net_arch[-1], 1)
2019-09-18 20:12:32 +00:00
self.log_std = nn.Parameter(th.zeros(self.action_dim))
2019-09-19 09:43:15 +00:00
# Init weights:
for module in [self.shared_net, self.actor_net, self.value_net]:
module.apply(self.init_weights)
2019-09-19 15:18:41 +00:00
self.optimizer = th.optim.Adam(self.parameters(), lr=learning_rate, eps=self.adam_epsilon)
2019-09-18 11:10:27 +00:00
2019-09-19 09:43:15 +00:00
def forward(self, state, deterministic=False):
2019-09-18 13:35:17 +00:00
state = th.FloatTensor(state).to(self.device)
2019-09-18 11:10:27 +00:00
latent = self.shared_net(state)
value = self.value_net(latent)
2019-09-19 09:43:15 +00:00
action, action_distribution = self._get_action_dist_from_latent(latent, deterministic=deterministic)
log_prob = self._get_log_prob(action_distribution, action)
2019-09-18 11:10:27 +00:00
return action, value, log_prob
2019-09-19 09:43:15 +00:00
def _get_action_dist_from_latent(self, latent, deterministic=False):
2019-09-18 11:10:27 +00:00
mean_actions = self.actor_net(latent)
2019-09-18 21:55:41 +00:00
action_std = th.ones(mean_actions.size()) * self.log_std.exp()
action_distribution = Normal(mean_actions, action_std)
2019-09-18 11:10:27 +00:00
# Sample from the gaussian
2019-09-18 21:55:41 +00:00
if deterministic:
action = mean_actions
else:
action = action_distribution.rsample()
2019-09-19 09:43:15 +00:00
return action, action_distribution
2019-09-18 13:35:17 +00:00
2019-09-19 09:43:15 +00:00
@staticmethod
def _get_log_prob(action_distribution, action):
2019-09-18 21:48:47 +00:00
log_prob = action_distribution.log_prob(action)
if len(log_prob.shape) > 1:
log_prob = log_prob.sum(axis=1)
else:
log_prob = log_prob.sum()
2019-09-19 09:43:15 +00:00
return log_prob
def actor_forward(self, state, deterministic=False):
state = th.FloatTensor(state).to(self.device)
latent = self.shared_net(state)
action, _ = self._get_action_dist_from_latent(latent, deterministic=deterministic)
return action.detach().cpu().numpy()
def get_policy_stats(self, state, action):
state = th.FloatTensor(state).to(self.device)
latent = self.shared_net(state)
_, action_distribution = self._get_action_dist_from_latent(latent)
log_prob = self._get_log_prob(action_distribution, action)
2019-09-18 21:48:47 +00:00
value = self.value_net(latent)
2019-09-19 09:43:15 +00:00
return value, log_prob, action_distribution.entropy()
2019-09-18 21:48:47 +00:00
2019-09-18 11:10:27 +00:00
def value_forward(self):
pass
MlpPolicy = PPOPolicy
register_policy("MlpPolicy", MlpPolicy)