-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.py
160 lines (128 loc) · 4.36 KB
/
net.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import torch
import torch.nn as nn
from torch.distributions import Normal
from typing import Tuple
def soft_clamp(
x: torch.Tensor, bound: tuple
) -> torch.Tensor:
low, high = bound
#x = torch.tanh(x)
x = low + 0.5 * (high - low) * (x + 1)
return x
def MLP(
input_dim: int,
hidden_dim: int,
depth: int,
output_dim: int,
activation: str = 'relu',
final_activation: str = None
) -> torch.nn.modules.container.Sequential:
if activation == 'tanh':
act_f = nn.Tanh()
elif activation == 'relu':
act_f = nn.ReLU()
layers = [nn.Linear(input_dim, hidden_dim), act_f]
for _ in range(depth -1):
layers.append(nn.Linear(hidden_dim, hidden_dim))
layers.append(act_f)
layers.append(nn.Linear(hidden_dim, output_dim))
if final_activation == 'relu':
layers.append(nn.ReLU())
elif final_activation == 'tanh':
layers.append(nn.Tanh())
else:
layers = layers
return nn.Sequential(*layers)
# def MLP(
# input_dim: int,
# hidden_dim: int,
# depth: int,
# output_dim: int,
# final_activation: str = None
# ) -> torch.nn.modules.container.Sequential:
# layers = [nn.Linear(input_dim, hidden_dim), nn.ReLU()]
# for _ in range(depth -1):
# layers.append(nn.Linear(hidden_dim, hidden_dim))
# layers.append(nn.ReLU())
# layers.append(nn.Linear(hidden_dim, output_dim))
# if final_activation == 'relu':
# layers.append(nn.ReLU())
# elif final_activation == 'tanh':
# layers.append(nn.Tanh())
# else:
# layers = layers
# return nn.Sequential(*layers)
class ValueMLP(nn.Module):
_net: torch.nn.modules.container.Sequential
def __init__(
self, state_dim: int, hidden_dim: int, depth: int
) -> None:
super().__init__()
self._net = MLP(state_dim, hidden_dim, depth, 1)
def forward(
self, s: torch.Tensor
) -> torch.Tensor:
return self._net(s)
class QMLP(nn.Module):
_net: torch.nn.modules.container.Sequential
def __init__(
self,
state_dim: int, action_dim: int, hidden_dim: int, depth:int
) -> None:
super().__init__()
self._net = MLP((state_dim + action_dim), hidden_dim, depth, 1)
def forward(
self, s: torch.Tensor, a: torch.Tensor
) -> Tuple[torch.Tensor, torch.Tensor]:
sa = torch.cat([s, a], dim=1)
return self._net(sa)
class DoubleQMLP(nn.Module):
_net: torch.nn.modules.container.Sequential
def __init__(
self,
state_dim: int, action_dim: int, hidden_dim: int, depth:int
) -> None:
super().__init__()
self._net1 = MLP((state_dim + action_dim), hidden_dim, depth, 1)
self._net2 = MLP((state_dim + action_dim), hidden_dim, depth, 1)
def forward(
self, s: torch.Tensor, a: torch.Tensor
) -> Tuple[torch.Tensor, torch.Tensor]:
sa = torch.cat([s, a], dim=1)
return self._net1(sa), self._net2(sa)
class GaussPolicyMLP(nn.Module):
_net: torch.nn.modules.container.Sequential
_log_std_bound: tuple
def __init__(
self,
state_dim: int, hidden_dim: int, depth: int, action_dim: int, pi_activation_f = 'relu'
) -> None:
super().__init__()
if pi_activation_f == 'relu':
print('using relu as activation function!!!')
elif pi_activation_f == 'tanh':
print('using tanh as activation function!!!')
self._net = MLP(state_dim, hidden_dim, depth, (2 * action_dim), pi_activation_f, 'tanh')
self._log_std_bound = (-5., 0.)
for name, p in self.named_parameters():
if 'weight' in name:
if len(p.size()) >= 2:
nn.init.orthogonal_(p, gain=1)
elif 'bias' in name:
nn.init.constant_(p, 0)
def forward(
self, s: torch.Tensor
) -> torch.distributions:
mu, log_std = self._net(s).chunk(2, dim=-1)
log_std = soft_clamp(log_std, self._log_std_bound)
std = log_std.exp()
dist = Normal(mu, std)
return dist
def predict(
self, s: torch.Tensor
) -> torch.distributions:
mu, log_std = self._net(s).chunk(2, dim=-1)
log_std = soft_clamp(log_std, self._log_std_bound)
std = log_std.exp()
dist = Normal(mu, std)
return dist, mu, std