-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy.py
40 lines (29 loc) · 1.35 KB
/
policy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import torch
import torch.nn as nn
from torch.distributions import MultivariateNormal, Normal, Independent
from dilo_utils import mlp
LOG_STD_MIN = -5.0
LOG_STD_MAX = 2.0
class GaussianPolicy(nn.Module):
def __init__(self, obs_dim, act_dim, hidden_dim=256, n_hidden=2):
super().__init__()
self.net = mlp([obs_dim, *([hidden_dim] * n_hidden), act_dim])
self.log_std = nn.Parameter(torch.zeros(act_dim, dtype=torch.float32))
def forward(self, obs):
mean = self.net(obs)
std = torch.exp(self.log_std.clamp(LOG_STD_MIN, LOG_STD_MAX))
return Independent(Normal(mean, std), 1)
def act(self, obs, deterministic=False, enable_grad=False):
with torch.set_grad_enabled(enable_grad):
dist = self(obs)
return torch.clip(dist.mean, min=-1.0, max=1.0) if deterministic else torch.clip(dist.sample(), min=-1.0, max=1.0)
class DeterministicPolicy(nn.Module):
def __init__(self, obs_dim, act_dim, hidden_dim=256, n_hidden=2, dropout_rate=0.0):
super().__init__()
self.net = mlp([obs_dim, *([hidden_dim] * n_hidden), act_dim],
output_activation=nn.Tanh)
def forward(self, obs):
return self.net(obs)
def act(self, obs, deterministic=False, enable_grad=False):
with torch.set_grad_enabled(enable_grad):
return self(obs)