-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
52 lines (43 loc) · 1.47 KB
/
models.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
41
42
43
44
45
46
47
48
49
50
51
52
import torch
import torch.nn as nn
import torch.nn.functional as F
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
dtype = torch.double
class MlpPolicy(nn.Module):
def __init__(self, obs_dim: int, action_dim: int, hidden_dim: int):
super(MlpPolicy, self).__init__()
self.model = (
nn.Sequential(
nn.Linear(obs_dim, hidden_dim),
nn.Dropout(p=0.8),
nn.ReLU(),
nn.Linear(hidden_dim, action_dim),
)
.to(device)
.to(dtype)
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
logits = self.model(x)
return logits
def select_action(self, obs: torch.Tensor, deterministic: bool = False):
logits = self.forward(obs)
if deterministic:
action = torch.argmax(logits)
else:
action = torch.multinomial(F.softmax(logits, dim=-1), num_samples=1)
return action, logits
class MlpValueFn(nn.Module):
def __init__(self, obs_dim: int, hidden_dim: int):
super(MlpValueFn, self).__init__()
self.model = (
nn.Sequential(
nn.Linear(obs_dim, hidden_dim),
nn.Dropout(p=0.8),
nn.ReLU(),
nn.Linear(hidden_dim, 1),
)
.to(device)
.to(dtype)
)
def forward(self, observation: torch.Tensor) -> torch.Tensor:
return self.model(observation)