This repository has been archived by the owner on Nov 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
agent.py
322 lines (252 loc) · 11.6 KB
/
agent.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import utils
import copy
import pickle
import os
torch.set_default_dtype(torch.float32)
class ConcatenationEncoder(nn.Module):
def __init__(self, observation_space):
super().__init__()
observation_dim = observation_space['observation'].shape[0]
desired_goal_dim = observation_space['desired_goal'].shape[0]
self.output_dim = observation_dim + desired_goal_dim
def forward(self, observation, desired_goal, detach=False):
observation = torch.cat([observation, desired_goal], -1)
return observation
class ConvolutionalEncoder(nn.Module):
def __init__(self, observation_space, feature_dim):
"""
Assumes that the observation and goal images have the same dimensions.
"""
super().__init__()
assert observation_space['image_observation'].shape == (3, 84, 84)
self.conv = nn.Sequential(
nn.Conv2d(3, 32, 3, stride=2),
nn.ReLU(),
nn.Conv2d(32, 32, 3, stride=1),
nn.ReLU(),
nn.Conv2d(32, 32, 3, stride=1),
nn.ReLU(),
nn.Conv2d(32, 32, 3, stride=1),
nn.ReLU(),
nn.Flatten(),
)
# compute output of flattened conv
with torch.no_grad():
n_flatten = self.conv(
torch.as_tensor(observation_space['image_observation'].sample()).float().unsqueeze(0)
).shape[-1]
self.head = nn.Sequential(
nn.Linear(n_flatten, feature_dim),
nn.LayerNorm(feature_dim))
self.output_logits = False
self.output_dim = 2 * feature_dim # double since outputs get concatenated
def forward_single_observation(self, observation, detach=False):
observation = observation / 255.
observation = self.conv(observation)
if detach:
observation = observation.detach()
observation = self.head(observation)
return observation
def forward(self, observation, desired_goal, detach=False):
observation = self.forward_single_observation(observation, detach=detach)
desired_goal = self.forward_single_observation(desired_goal, detach=detach)
observation = torch.cat([observation, desired_goal], -1)
return observation
def copy_conv_weights_from(self, source):
self.conv.load_state_dict(source.conv.state_dict())
class Actor(nn.Module):
"""torch.distributions implementation of an diagonal Gaussian policy."""
def __init__(
self,
from_images,
observation_space,
action_space,
feature_dim, # ignored for state-based
hidden_sizes,
log_std_bounds):
super().__init__()
if from_images:
self.encoder = ConvolutionalEncoder(observation_space, feature_dim)
else:
self.encoder = ConcatenationEncoder(observation_space)
self.log_std_bounds = log_std_bounds
self.trunk = utils.mlp(self.encoder.output_dim, 2 * action_space.shape[0], hidden_sizes) # twice the dimensions for outputs to split into mu and log_sigma
# self.apply(utils.weight_init)
def forward(self, observation, desired_goal, detach_encoder=False):
observation = self.encoder(observation, desired_goal, detach=detach_encoder)
mu, log_std = self.trunk(observation).chunk(2, dim=-1)
# constrain log_std inside [log_std_min, log_std_max]
log_std = torch.tanh(log_std)
log_std_min, log_std_max = self.log_std_bounds
log_std = log_std_min + 0.5 * (log_std_max - log_std_min) * (log_std + 1)
std = log_std.exp()
dist = utils.SquashedNormal(mu, std)
return dist
class Critic(nn.Module):
"""Critic network, employes double Q-learning."""
def __init__(
self,
from_images,
observation_space,
action_space,
feature_dim,
hidden_sizes):
super().__init__()
if from_images:
# images
self.encoder = ConvolutionalEncoder(observation_space, feature_dim)
else:
# states
self.encoder = ConcatenationEncoder(observation_space)
self.Q1 = utils.mlp(self.encoder.output_dim + action_space.shape[0],
1, hidden_sizes)
self.Q2 = utils.mlp(self.encoder.output_dim + action_space.shape[0],
1, hidden_sizes)
# self.apply(utils.weight_init)
def forward(self, observation, desired_goal, action, detach_encoder=False):
assert observation.size(0) == action.size(0) # batch sizes match. critic isn't used on non-batches.
observation = self.encoder(observation, desired_goal, detach=detach_encoder)
observation = torch.cat([observation, action], dim=-1)
q1 = self.Q1(observation)
q2 = self.Q2(observation)
return q1, q2
class Agent(object):
"""Data regularized Q: actor-critic method for learning from pixels."""
def __init__(self,
from_images,
observation_space,
action_space,
feature_dim=32,
hidden_sizes=[256, 256],
log_std_bounds=[-10, 2],
discount=0.95,
init_temperature=0.1,
lr=1e-3,
actor_update_frequency=2,
critic_tau=0.005,
critic_target_update_frequency=2,
batch_size=256,
device='cuda',
):
self.action_range = (
float(action_space.low.min()),
float(action_space.high.max())
)
self.device = device
self.discount = discount
self.critic_tau = critic_tau
self.actor_update_frequency = actor_update_frequency
self.critic_target_update_frequency = critic_target_update_frequency
self.batch_size = batch_size
self.actor = Actor(
from_images,
observation_space,
action_space,
feature_dim,
hidden_sizes,
log_std_bounds).to(
self.device)
self.critic = Critic(
from_images,
observation_space,
action_space,
feature_dim,
hidden_sizes).to(
self.device)
self.critic_target = copy.deepcopy(self.critic)
for parameter in self.critic_target.parameters():
parameter.requires_grad = False
# tie conv layers between actor and critic
if from_images:
self.actor.encoder.copy_conv_weights_from(self.critic.encoder)
self.log_alpha = torch.tensor(np.log(init_temperature)).to(device)
self.log_alpha.requires_grad = True
# set target entropy to -|A|
self.target_entropy = -action_space.shape[0]
# optimizers
self.actor_optimizer = torch.optim.Adam(self.actor.parameters(), lr=lr)
self.critic_optimizer = torch.optim.Adam(self.critic.parameters(),
lr=lr)
self.log_alpha_optimizer = torch.optim.Adam([self.log_alpha], lr=lr)
self.info = dict()
@property
def alpha(self):
return self.log_alpha.exp()
def act(self, observation, desired_goal, sample=False):
observation = torch.FloatTensor(observation).to(self.device).unsqueeze(0)
desired_goal = torch.FloatTensor(desired_goal).to(self.device).unsqueeze(0)
dist = self.actor(observation, desired_goal)
action = dist.sample() if sample else dist.mean
action = action.clamp(*self.action_range)
assert action.ndim == 2 and action.shape[0] == 1
return utils.to_np(action[0])
def update_critic(self, observation, desired_goal, action, reward, next_observation, not_done, step):
# batch
with torch.no_grad():
dist = self.actor(next_observation, desired_goal)
next_action = dist.rsample()
log_prob = dist.log_prob(next_action).sum(-1, keepdim=True) # log prob for diagonal gaussian
target_Q1, target_Q2 = self.critic_target(next_observation, desired_goal, next_action)
target_V = torch.min(target_Q1, target_Q2) - self.alpha.detach() * log_prob
target_Q = reward + (not_done * self.discount * target_V)
target_Q = torch.clamp(target_Q, -float('inf'), 0)
# target_Q = torch.clamp(target_Q, -2./(1. - self.discount), 0) # from HER paper, modified for our reward function
current_Q1, current_Q2 = self.critic(observation, desired_goal, action)
critic_loss = F.mse_loss(current_Q1, target_Q) + F.mse_loss(current_Q2, target_Q)
# Optimize the critic
self.critic_optimizer.zero_grad()
critic_loss.backward()
self.critic_optimizer.step()
self.info['critic_loss'] = critic_loss.item()
def update_actor_and_alpha(self, observation, desired_goal, step):
# detach conv filters, so we don't update them with the actor loss
dist = self.actor(observation, desired_goal, detach_encoder=True)
action = dist.rsample()
log_prob = dist.log_prob(action).sum(-1, keepdim=True) # diagonal gaussian
# We dont need grad wrt critic theta since we aren't optimizing them anyways.
# This saves some computational effort.
for parameter in self.critic.parameters():
parameter.requires_grad = False
# detach conv filters, so we don't update them with the actor loss
actor_Q1, actor_Q2 = self.critic(observation, desired_goal, action, detach_encoder=True)
actor_Q = torch.min(actor_Q1, actor_Q2)
actor_loss = (self.alpha.detach() * log_prob - actor_Q).mean()
# optimize the actor
self.actor_optimizer.zero_grad()
actor_loss.backward()
self.actor_optimizer.step()
self.log_alpha_optimizer.zero_grad()
alpha_loss = (self.alpha * (-log_prob - self.target_entropy).detach()).mean()
alpha_loss.backward()
self.log_alpha_optimizer.step()
for parameter in self.critic.parameters():
parameter.requires_grad = True
self.info['actor_loss'] = actor_loss.item()
self.info['entropy'] = -log_prob.mean().item()
self.info['alpha_loss'] = alpha_loss.item()
self.info['alpha'] = self.alpha.item()
self.info['actor_Q'] = actor_Q.mean().item()
def update(self, replay_buffer, step):
observation, desired_goal, action, reward, next_observation, not_done = replay_buffer.sample(self.batch_size)
self.update_critic(observation, desired_goal, action, reward, next_observation, not_done, step)
if step % self.actor_update_frequency == 0:
self.update_actor_and_alpha(observation, desired_goal, step)
if step % self.critic_target_update_frequency == 0:
utils.soft_update_params(self.critic, self.critic_target, self.critic_tau)
def save(self, filename, folder='checkpoints'):
pass
# if not os.path.exists(folder):
# os.mkdir(folder)
# with open(os.path.join(folder, filename), 'wb+') as f:
# pickle.dump(self, f)
def load(self, filename, folder='checkpoints'):
pass
# if not os.path.exists(folder):
# os.mkdir(folder)
# with open(os.path.join(folder, filename), 'rb') as f:
# agent = pickle.load(f)
# self.__dict__.update(agent.__dict__)