-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch-agent.py
320 lines (278 loc) · 13.1 KB
/
fetch-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
from collections import deque
from copy import deepcopy
import seaborn as sns
from utils import *
import os
from matplotlib import pyplot as plt
import numpy as np
import gymnasium as gym
from networks import *
from plotting import ProgressBoard
import torch
import torch.nn as nn
from buffer import *
from tqdm import tqdm
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
torch.autograd.set_detect_anomaly(True)
np.seterr(all="raise")
class FetchAgent(Parameters):
def __init__(self, name, env: gym.Env, board: ProgressBoard = None, window = 500, gamma = 0.98, prioritized = True,
polyak = 0.95, pi_lr = 0.001, q_lr = 0.001, eps = 0.3, noise_eps = 0.2, learning_rate = 2, logging_rate = 1,
action_l2 = 1., batch_size = 256, gradient_steps=50, success_threshold = 0.98, max_episodes=500):
# Hyperparameters
self.save_parameters()
# env params for networks and buffer
observation = env.reset()[0]
self.env_params = {'obs_dim': observation['observation'].shape[0],
'goal_dim': observation['desired_goal'].shape[0],
'action_dim': env.action_space.shape[0],
'action_bound': env.action_space.high[0],
'max_steps': 50}
# Networks
self.actor: Actor = Actor(self.env_params).to(device)
self.target_actor: Actor = deepcopy(self.actor).to(device)
self.critic: Critic = Critic(self.env_params).to(device)
self.target_critic: Critic = deepcopy(self.critic).to(device)
self.policy_optimizer = torch.optim.Adam(self.actor.parameters(), lr=pi_lr)
self.value_optimizer = torch.optim.Adam(self.critic.parameters(), lr=q_lr)
self.value_loss_fn = nn.MSELoss()
#These networks must be updated not through the gradients but with polyak
for param in self.target_critic.parameters():
param.requires_grad = False
for param in self.target_actor.parameters():
param.requires_grad = False
# Experience Replay Buffer
self.memory = ReplayBuffer(self.env_params, max_timesteps=self.max_episodes * self.env_params['max_steps'], reward_function=env.unwrapped.compute_reward)
self.cache = ReplayCache(env_params = self.env_params)
def train(self, plot = False):
#Life stats
self.success_rate = []
self.ep = 0
self.timestep = 0
ep_successes = deque(maxlen = self.window)
successful_windows = 0
#Plotting
board = ProgressBoard(self.max_episodes, plot_rate=10)
mean_success = 0
for self.ep in tqdm(range(1, self.max_episodes)):
#Storing
self.store_episode()
#Learning
value_loss, policy_loss = 0, 0
if self.ep % self.learning_rate == 0: #how many episodes are stored before learning
for _ in range(self.gradient_steps):
value_loss, policy_loss = self.learning_step()
self.update_target_networks()
#Logging
ep_successes.append(self.evaluate())
if self.ep % self.logging_rate == 0:
mean_success = np.mean(ep_successes)
self.success_rate.append(mean_success)
print(f"Episode {self.ep+1} SUCCESS RATE {mean_success:.2f} VL {value_loss:.2f} PL {policy_loss:.2f}\n")
if plot: board.draw(self.ep, mean_success, "success")
if self.ep > self.window:
if mean_success >= self.success_threshold: successful_windows += 1
if successful_windows >= 10: break
self.success_rate = np.array(self.success_rate)
def store_episode(self):
# empty episode temporary memory
self.cache.reset()
# starting point
obs_dict = self.env.reset()[0]
# Episode playing
for t in range(self.env_params['max_steps']):
action = self.select_action(obs_dict['observation'],obs_dict['desired_goal'])
new_obs_dict, _, _, _, _ = self.env.step(action)
#Storing in the temporary memory
self.cache.store_transition(t, obs_dict, action, new_obs_dict)
#Preparing for next step
obs_dict = new_obs_dict
self.timestep += 1
# storing in the memory the entire episode
self.memory.store(self.cache)
def select_action(self,obs,goal,explore = True):
with torch.no_grad():
obs = torch.as_tensor(obs, dtype = torch.float32)
goal = torch.as_tensor(goal, dtype = torch.float32)
action = self.actor(obs,goal).detach().numpy()
if explore:
action += self.env_params['action_bound'] * self.noise_eps * np.random.randn(self.env_params['action_dim'])
action = np.clip(action, -self.env_params['action_bound'], self.env_params['action_bound'])
return action
def learning_step(self):
#Sampling of the minibatch
observations, goals, new_observations, actions, rewards, weights = self.sample_batch()
#Value Optimization
self.critic.train()
with torch.no_grad():
best_actions = self.target_actor(new_observations, goals).clamp(-1,1) #(batch_size, 1)
target_values = self.target_critic(new_observations,goals,best_actions)
targets = (rewards + self.gamma * target_values).detach()
clip_return = 1 / (1 - self.gamma)
targets = torch.clamp(targets, -clip_return, 0).detach()
estimations = self.critic(observations,goals,actions)
value_loss = self.value_loss_fn(estimations,targets)
value_loss = torch.mean(value_loss * weights)
self.value_optimizer.zero_grad()
value_loss.backward()
self.value_optimizer.step()
#For the priorities
if self.prioritized:
priorities = torch.abs(targets - estimations).detach().numpy() + 1e-5
self.memory.update_priorities(priorities)
#Don't waste computational effort
for param in self.critic.parameters():
param.requires_grad = False
#Policy Optimization
self.actor.train()
estimated_actions = self.actor(observations,goals)
policy_loss = -self.critic(observations,goals,estimated_actions).mean()
policy_loss += self.action_l2 * (estimated_actions / self.env_params['action_bound']).pow(2).mean()
self.policy_optimizer.zero_grad()
policy_loss.backward()
self.policy_optimizer.step()
#Reactivate computational graph for critic
for param in self.critic.parameters():
param.requires_grad = True
return value_loss, policy_loss
def sample_batch(self):
# Sample replay buffer
if self.prioritized:
transitions, weights = self.memory.sample(timestep = self.timestep)
weights = self.to_torch(weights)
else:
transitions = self.memory.sample_uniformly()
weights = 1
# preprocess
observations = self.to_torch(transitions['observation'])
goals = self.to_torch(transitions['goal'])
new_observations = self.to_torch(transitions['new_observation'])
actions = self.to_torch(transitions['action'])
rewards = self.to_torch(transitions['reward']).reshape(-1,1)
return observations, goals, new_observations, actions, rewards, weights
#Polyak averaging of target networks
def update_target_networks(self, polyak=None):
polyak = self.polyak if polyak is None else polyak
with torch.no_grad():
for target, online in zip(self.target_critic.parameters(), self.critic.parameters()):
target.data.mul_(polyak)
target.data.add_((1 - polyak) * online.data)
for target, online in zip(self.target_actor.parameters(), self.actor.parameters()):
target.data.mul_(polyak)
target.data.add_((1 - polyak) * online.data)
def evaluate(self, render = False):
# starting point
obs_dict = self.env.reset()[0]
observation = obs_dict['observation']
goal = obs_dict['desired_goal'] #never changes in an episode
info = None
self.imgs = []
# Episode playing
for _ in range(self.env_params['max_steps']):
action = self.select_action(observation,goal,explore = False)
new_obs_dict, _, _, _, info = self.env.step(action)
new_observation = new_obs_dict['observation']
if render:
self.env.render()
#Preparing for next step
observation = new_observation
success = 1 if info['is_success'] else 0
if render: print(f"SUCCESS: {success}")
return success
def save(self):
path = os.path.join("models",self.name)
if not os.path.exists(path): os.mkdir(path)
torch.save(self.actor.state_dict(), open(os.path.join(path,"actor.pt"), "wb"))
torch.save(self.critic.state_dict(), open(os.path.join(path,"critic.pt"), "wb"))
torch.save(self.success_rate, open(os.path.join(path,"success.pt"), "wb"))
print("MODELS SAVED!")
def load(self):
path = os.path.join("models",self.name)
self.actor.load_state_dict(torch.load(open(os.path.join(path,"actor.pt"),"rb")))
self.critic.load_state_dict(torch.load(open(os.path.join(path,"critic.pt"),"rb")))
print("MODELS LOADED!")
def plot_success(self):
path = os.path.join("models",self.name)
success_rate = torch.load(open(os.path.join(path,"success.pt"),"rb"))
xaxis = np.arange(start=1,stop=success_rate.size+1) * 100 #* self.env_params['max_steps']
plt.plot(xaxis, success_rate)
plt.show(block = True)
def to(self, device):
ret = super().to(device)
ret.device = device
return ret
def to_torch(self, array, copy=False):
if copy:
return torch.tensor(array, dtype = torch.float32).to(device)
return torch.as_tensor(array, dtype = torch.float32).to(device)
SEEDS = [123]
def launch(env_name = 'FetchReach-v2', prioritized = True):
for seed in SEEDS:
set_global_seeds(seed)
env = gym.make(env_name)
if prioritized:
agent = FetchAgent(f"HGR_{env_name}_{seed}", env, max_episodes = 20000, window = 1000)
else:
agent = FetchAgent(f"HER_{env_name}_{seed}", env, max_episodes = 40000, window = 1000)
agent.prioritized = prioritized
agent.train()
agent.save() #Done training and saving the model
from gymnasium.wrappers import RecordVideo
def test(env_name = 'FetchReach-v2', prioritized = True, record = False):
for seed in SEEDS:
set_global_seeds(seed)
env = gym.make(env_name, render_mode = "rgb_array")
if record:
path = os.path.join("videos",env_name)
env = RecordVideo(env, video_folder=path,disable_logger=True)
if prioritized:
agent = FetchAgent(f"HGR_{env_name}_{seed}", env)
else:
agent = FetchAgent(f"HER_{env_name}_{seed}", env)
agent.plot_success()
agent.load()
for _ in range(5):
agent.evaluate(render = True)
from enum import Enum
class Type(Enum):
HER = 1
HGR = 2
def meanplot(env_name = 'FetchReach-v2', type = Type.HGR):
success_rates = []
maxlen = 0
for seed in SEEDS:
env = gym.make(env_name)
if type == Type.HGR:
agent = FetchAgent(f"HGR_{env_name}_{seed}", env)
elif type == Type.HER:
agent = FetchAgent(f"HER_{env_name}_{seed}", env)
path = os.path.join("models",agent.name)
success_rate = torch.load(open(os.path.join(path,"success.pt"),"rb"))
if len(success_rate) > maxlen: maxlen = len(success_rate)
if not isinstance(success_rate, list): success_rate = success_rate.tolist()
success_rates.append(success_rate)
lens = [len(sr) for sr in success_rates]
srs = np.ma.empty((np.max(lens),len(success_rates)))
srs.mask = True
for idx, l in enumerate(success_rates):
srs[:len(l),idx] = l
toplot, error = srs.mean(axis=-1, dtype = np.float16), srs.std(axis = -1, dtype = np.float128)
return toplot
def plot_tasks(task):
sns.set()
toplot = meanplot(task, Type.HER)
xaxis = (np.arange(len(toplot))+1) * 100
plt.plot(xaxis, toplot, color='red')
toplot = meanplot(task, Type.HGR)
xaxis = (np.arange(len(toplot))+1) * 100
plt.plot(xaxis, toplot, color='orange')
plt.xlabel("timesteps")
plt.ylabel("mean success rate")
plt.legend(["HER", "HGR"])
plt.show(block = True)
if __name__ == "__main__":
reach = 'FetchReach-v2'
push = 'FetchPush-v2'
pickandplace = 'FetchPickAndPlace-v2'
launch(reach, Type.HER)
plot_tasks(reach)