-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_modules.py
199 lines (145 loc) · 6.39 KB
/
base_modules.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
import torch.distributions as dis
class UnsqueezeModule(nn.Module):
def __init__(self, dim: int):
super(UnsqueezeModule, self).__init__()
self.dim = dim
def forward(self, x):
return torch.unsqueeze(x, dim=self.dim)
def make_dcnn(feature_size, out_channels):
dcnn = nn.Sequential(
nn.ConvTranspose2d(feature_size, 64, [1, 4], 1, 0),
nn.ReLU(),
nn.ConvTranspose2d(64, 16, [2, 4], [1, 2], [0, 1]),
nn.ReLU(),
nn.ConvTranspose2d(16, 16, 4, 2, 1),
nn.ReLU(),
nn.ConvTranspose2d(16, 8, 4, 2, 1),
nn.ReLU(),
nn.ConvTranspose2d(8,
8,
kernel_size=3,
stride=2,
padding=1,
output_padding=1),
nn.ReLU(),
nn.Conv2d(8, out_channels=out_channels,
kernel_size=3, padding=1)
) # output size 16 x 64
return dcnn
def make_cnn(n_channels):
cnn_module_list = nn.ModuleList()
cnn_module_list.append(nn.Conv2d(n_channels, 8, 4, 2, 1))
cnn_module_list.append(nn.ReLU())
cnn_module_list.append(nn.Conv2d(8, 16, 4, 2, 1))
cnn_module_list.append(nn.ReLU())
cnn_module_list.append(nn.Conv2d(16, 16, 4, 2, 1))
cnn_module_list.append(nn.ReLU())
cnn_module_list.append(nn.Conv2d(16, 64, [2, 4], 2, [0, 1]))
cnn_module_list.append(nn.ReLU())
cnn_module_list.append(nn.Conv2d(64, 256, [1, 4], [1, 4], 0))
cnn_module_list.append(nn.ReLU())
cnn_module_list.append(nn.Flatten())
phi_size = 256
return nn.Sequential(*cnn_module_list), phi_size
def make_mlp(input_size, hidden_layers, output_size, act_fn, last_layer_linear=False):
mlp = nn.ModuleList()
last_layer_size = input_size
for layer_size in hidden_layers:
mlp.append(nn.Linear(last_layer_size, layer_size, bias=True))
mlp.append(act_fn())
last_layer_size = layer_size
mlp.append(nn.Linear(last_layer_size, output_size, bias=True))
if not last_layer_linear:
mlp.append(act_fn())
return nn.Sequential(*mlp)
class ContinuousActionQNetwork(nn.Module):
def __init__(self, input_size, action_size, hidden_layers=None, act_fn=nn.ReLU):
super(ContinuousActionQNetwork, self).__init__()
if hidden_layers is None:
hidden_layers = [256, 256]
self.input_size = input_size
self.action_size = action_size
self.output_size = 1
self.hidden_layers = hidden_layers
self.network_modules = nn.ModuleList()
last_layer_size = input_size + action_size
for layer_size in hidden_layers:
self.network_modules.append(nn.Linear(last_layer_size, layer_size))
self.network_modules.append(act_fn())
last_layer_size = layer_size
self.network_modules.append(nn.Linear(last_layer_size, self.output_size))
self.main_network = nn.Sequential(*self.network_modules)
def forward(self, x, a):
q = self.main_network(torch.cat((x, a), dim=-1))
return q
class ContinuousActionVNetwork(nn.Module):
def __init__(self, input_size, hidden_layers=None, act_fn=nn.ReLU):
super(ContinuousActionVNetwork, self).__init__()
if hidden_layers is None:
hidden_layers = [256, 256]
self.input_size = input_size
self.output_size = 1
self.hidden_layers = hidden_layers
self.network_modules = nn.ModuleList()
last_layer_size = input_size
for layer_size in hidden_layers:
self.network_modules.append(nn.Linear(last_layer_size, layer_size))
self.network_modules.append(act_fn())
last_layer_size = layer_size
self.network_modules.append(nn.Linear(last_layer_size, self.output_size))
self.main_network = nn.Sequential(*self.network_modules)
def forward(self, x):
q = self.main_network(x)
return q
class ContinuousActionPolicyNetwork(nn.Module):
def __init__(self, input_size, output_size, output_distribution="Gaussian", hidden_layers=None, act_fn=nn.ReLU,
logsig_clip=None):
super(ContinuousActionPolicyNetwork, self).__init__()
if logsig_clip is None:
logsig_clip = [-20, 2]
if hidden_layers is None:
hidden_layers = [256, 256]
self.input_size = input_size
self.output_size = output_size
self.hidden_layers = hidden_layers
self.logsig_clip = logsig_clip
self.output_distribution = output_distribution # Currently only support "Gaussian" or "DiracDelta"
self.mu_layers = nn.ModuleList()
self.logsig_layers = nn.ModuleList()
last_layer_size = input_size
for layer_size in hidden_layers:
self.mu_layers.append(nn.Linear(last_layer_size, layer_size))
self.mu_layers.append(act_fn())
self.logsig_layers.append(nn.Linear(last_layer_size, layer_size))
self.logsig_layers.append(act_fn())
last_layer_size = layer_size
self.mu_layers.append(nn.Linear(last_layer_size, self.output_size))
self.logsig_layers.append(nn.Linear(last_layer_size, self.output_size))
self.mu_net = nn.Sequential(*self.mu_layers)
self.logsig_net = nn.Sequential(*self.logsig_layers)
def forward(self, x):
if self.output_distribution == "Gaussian":
mu = self.mu_net(x)
logsig = self.logsig_net(x).clamp(self.logsig_clip[0], self.logsig_clip[1])
return mu, logsig
else:
raise NotImplementedError
def get_log_action_probability(self, x, a):
mu = self.mu_net(x)
logsig = self.logsig_net(x).clamp(self.logsig_clip[0], self.logsig_clip[1])
dist = torch.distributions.normal.Normal(loc=mu, scale=torch.exp(logsig))
log_action_probability = dist.log_prob(a)
return log_action_probability
def sample_action(self, x, greedy=False):
mu = self.mu_net(x)
logsig = self.logsig_net(x).clamp(self.logsig_clip[0], self.logsig_clip[1])
if greedy:
return torch.tanh(mu).detach().cpu().numpy()
else:
dist = torch.distributions.normal.Normal(loc=mu, scale=torch.exp(logsig))
sampled_u = dist.sample()
return torch.tanh(sampled_u.detach().cpu()).numpy()