-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
269 lines (215 loc) · 7.17 KB
/
utils.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import torch
import torch.nn as nn
import math
import copy
import numpy as np
import pickle
import matplotlib.pyplot as plt
import torch.nn.functional as F
from torch import distributions as pyd
import torch.optim as optim
from torch.distributions import Categorical
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def log_density(x, mu, std, logstd):
var = std.pow(2)
log_density = -(x - mu).pow(2) / (2 * var) \
- 0.5 * math.log(2 * math.pi) - logstd
return log_density.sum(1, keepdim=True)
class PGPolicy(nn.Module):
def __init__(self, num_inputs, num_outputs, hidden_dim=64, hidden_depth=2):
super(PGPolicy, self).__init__()
self.trunk = mlp(num_inputs, hidden_dim, num_outputs*2, hidden_depth)
def forward(self, x):
outs = self.trunk(x)
mu, std, log_std = self.dist_create(outs)
std = torch.exp(log_std)
action = torch.normal(mu, std)
self.mu = mu
return action, std, log_std
def dist_create(self, logits):
min_log_std = -5
max_log_std = 5
loc, scale = torch.split(logits, logits.shape[-1] // 2, dim=-1)
loc = torch.tanh(loc)
log_std = torch.sigmoid(scale)
log_std = min_log_std + log_std * (max_log_std - min_log_std)
std = torch.exp(log_std)
return loc, std, log_std
class PGBaseline(nn.Module):
def __init__(self, num_inputs, hidden_dim=64, hidden_depth=2):
super(PGBaseline, self).__init__()
self.trunk = mlp(num_inputs, hidden_dim, 1, hidden_depth)
def forward(self, x):
v = self.trunk(x)
return v
class ACPolicy(nn.Module):
def __init__(self, num_inputs, num_outputs, hidden_dim=64, hidden_depth=2):
super(ACPolicy, self).__init__()
self.trunk = mlp(num_inputs, hidden_dim, num_outputs*2, hidden_depth)
def forward(self, x):
outs = self.trunk(x)
mu, std, log_std = self.dist_create(outs)
action = self.dist_sample_no_postprocess(mu, std)
self.std = std
self.mu = mu
return action, std, log_std
def dist_create(self, logits):
min_log_std = -5
max_log_std = 5
loc, scale = torch.split(logits, logits.shape[-1] // 2, dim=-1)
loc = torch.tanh(loc)
log_std = torch.sigmoid(scale)
log_std = min_log_std + log_std * (max_log_std - min_log_std)
std = torch.exp(log_std)
return loc, std, log_std
def dist_sample_no_postprocess(self, mu, std):
action = torch.zeros((mu.shape[0], 1)).to(device)
# TODO START
# Hint: perform the reparameterization trick - action = mean + epsilon*std, where epsilon \sim N(0, I)
# This will allow policy updates through gradient based updates via pathwise derivatives
# TODO END
return action
class QF(nn.Module):
def __init__(self, num_inputs, hidden_dim=64, hidden_depth=2):
super(QF, self).__init__()
self.trunk = mlp(num_inputs, hidden_dim, 1, hidden_depth)
def forward(self, x):
v = self.trunk(x)
return v
class TargetQF(nn.Module):
def __init__(self, num_inputs, hidden_dim=64, hidden_depth=2):
super(TargetQF, self).__init__()
self.trunk = mlp(num_inputs, hidden_dim, 1, hidden_depth)
def forward(self, x):
v = self.trunk(x)
return v
def collect_trajs(
env,
agent,
replay_buffer,
device,
episode_length=math.inf,
render=False,
):
# Collect the following data
raw_obs = []
raw_next_obs = []
actions = []
rewards = []
dones = []
images = []
path_length = 0
o = env.reset()
if render:
env.render()
while path_length < episode_length:
o_for_agent = o
action, _, _ = agent(torch.Tensor(o_for_agent).unsqueeze(0).to(device))
action= action.cpu().detach().numpy()[0]
# Step the simulation forward
next_o, r, done, env_info = env.step(copy.deepcopy(action))
replay_buffer.add(o,
action,
r,
next_o,
done)
# Render the environment
if render:
env.render()
raw_obs.append(o)
raw_next_obs.append(next_o)
actions.append(action)
rewards.append(r)
dones.append(done)
path_length += 1
if done:
break
o = next_o
# Prepare the items to be returned
observations = np.array(raw_obs)
next_observations = np.array(raw_next_obs)
actions = np.array(actions)
if len(actions.shape) == 1:
actions = np.expand_dims(actions, 1)
rewards = np.array(rewards)
if len(rewards.shape) == 1:
rewards = rewards.reshape(-1, 1)
dones = np.array(dones).reshape(-1, 1)
# Return in the following format
return dict(
observations=observations,
next_observations=next_observations,
actions=actions,
rewards=rewards,
dones=np.array(dones).reshape(-1, 1),
images=np.array(images)
)
def mlp(input_dim, hidden_dim, output_dim, hidden_depth, output_mod=None):
if hidden_depth == 0:
mods = [nn.Linear(input_dim, output_dim)]
else:
mods = [nn.Linear(input_dim, hidden_dim), nn.ReLU(inplace=True)]
for i in range(hidden_depth - 1):
mods += [nn.Linear(hidden_dim, hidden_dim), nn.ReLU(inplace=True)]
mods.append(nn.Linear(hidden_dim, output_dim))
if output_mod is not None:
mods.append(output_mod)
trunk = nn.Sequential(*mods)
return trunk
def rollout(
env,
agent,
episode_length=math.inf,
render=False,
):
# Collect the following data
raw_obs = []
raw_next_obs = []
actions = []
rewards = []
dones = []
images = []
entropy = None
log_prob = None
agent_info = None
path_length = 0
o = env.reset()
if render:
env.render()
while path_length < episode_length:
o_for_agent = o
action, _, _ = agent(torch.Tensor(o_for_agent).unsqueeze(0).to(device))
action = action.cpu().detach().numpy()[0]
# Step the simulation forward
next_o, r, done, env_info = env.step(copy.deepcopy(action))
# Render the environment
if render:
env.render()
raw_obs.append(o)
raw_next_obs.append(next_o)
actions.append(action)
rewards.append(r)
dones.append(done)
path_length += 1
if done:
break
o = next_o
# Prepare the items to be returned
observations = np.array(raw_obs)
next_observations = np.array(raw_next_obs)
actions = np.array(actions)
if len(actions.shape) == 1:
actions = np.expand_dims(actions, 1)
rewards = np.array(rewards)
if len(rewards.shape) == 1:
rewards = rewards.reshape(-1, 1)
dones = np.array(dones).reshape(-1, 1)
# Return in the following format
return dict(
observations=observations,
next_observations=next_observations,
actions=actions,
rewards=rewards,
dones=np.array(dones).reshape(-1, 1),
images=np.array(images)
)