-
Notifications
You must be signed in to change notification settings - Fork 129
/
ppo_gae_continuous3.py
257 lines (218 loc) · 9.55 KB
/
ppo_gae_continuous3.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
###
# Compared with ppo_gae_continuous2, using minibatch SGD, separate actor and critic networks.
###
import gym
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.distributions import Categorical
# from torch.distributions import Normal
from torch.distributions.normal import Normal
import numpy as np
import wandb
from collections import deque
from torch.utils.tensorboard import SummaryWriter
import argparse
import random
#Hyperparameters
learning_rate = 3e-4
gamma = 0.99
lmbda = 0.95
eps_clip = 0.2
batch_size = 2048
mini_batch = int(batch_size//32)
K_epoch = 10
T_horizon = 10000
n_epis = 10000
vf_coef = 0.5
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--run", type=str, default='test',
help="the name of this experiment")
parser.add_argument('--wandb_activate', type=bool, default=False, help='whether wandb')
parser.add_argument("--wandb_entity", type=str, default=None,
help="the entity (team) of wandb's project")
args = parser.parse_args()
print(args)
return args
def layer_init(layer, std=np.sqrt(2), bias_const=0.0):
torch.nn.init.orthogonal_(layer.weight, std)
torch.nn.init.constant_(layer.bias, bias_const)
return layer
class Actor(nn.Module):
def __init__(self, num_inputs, num_actions, hidden_dim):
super().__init__()
self.mean = nn.Sequential(
layer_init(nn.Linear(num_inputs, hidden_dim)),
nn.Tanh(),
layer_init(nn.Linear(hidden_dim, hidden_dim)),
nn.Tanh(),
layer_init(nn.Linear(hidden_dim, num_actions), std=0.01),
)
self.logstd = nn.Parameter(torch.zeros(1, num_actions))
def forward(self, x):
action_mean = self.mean(x)
action_logstd = self.logstd.expand_as(action_mean)
return action_mean.squeeze(), action_logstd.squeeze()
class Critic(nn.Module):
def __init__(self, num_inputs, hidden_dim):
super().__init__()
self.model = nn.Sequential(
layer_init(nn.Linear(num_inputs, hidden_dim)),
nn.Tanh(),
layer_init(nn.Linear(hidden_dim, hidden_dim)),
nn.Tanh(),
layer_init(nn.Linear(hidden_dim, 1), std=1.0),
)
def forward(self, x):
return self.model(x)
class PPO():
def __init__(self, num_inputs, num_actions, hidden_dim):
self.data = deque(maxlen=batch_size) # a ring buffer
self.max_grad_norm = 0.5
self.v_loss_clip = True
self.critic = Critic(num_inputs, hidden_dim).to(device)
self.actor = Actor(num_inputs, num_actions, hidden_dim).to(device)
self.parameters = list(self.critic.parameters()) + list(self.actor.parameters())
self.optimizer = optim.Adam(self.parameters, lr=learning_rate, eps=1e-5)
def pi(self, x):
return self.actor(x)
def v(self, x):
return self.critic(x)
def get_action_and_value(self, x, action=None):
mean, log_std = self.pi(x)
std = torch.exp(log_std)
normal = Normal(mean, std)
if action is None:
action = normal.sample()
log_prob = normal.log_prob(action).sum(-1)
value = self.v(x)
return action.cpu().numpy(), log_prob, value
def put_data(self, transition):
self.data.append(transition)
def make_batch(self,):
s, a, r, s_prime, logprob_a, v, done_mask = zip(*self.data)
s,a,r,s_prime,logprob_a,v,done_mask = torch.tensor(np.array(s), dtype=torch.float), torch.tensor(np.array(a)), \
torch.tensor(np.array(r), dtype=torch.float).unsqueeze(-1), torch.tensor(np.array(s_prime), dtype=torch.float), \
torch.tensor(logprob_a).unsqueeze(-1), torch.tensor(v).unsqueeze(-1), torch.tensor(np.array(done_mask), dtype=torch.float).unsqueeze(-1)
return s.to(device), a.to(device), r.to(device), s_prime.to(device), done_mask.to(device), logprob_a.to(device), v.to(device)
def train_net(self):
s, a, r, s_prime, done_mask, logprob_a, v = self.make_batch()
with torch.no_grad():
advantage = torch.zeros_like(r).to(device)
lastgaelam = 0
for t in reversed(range(s.shape[0])):
if done_mask[t] or t == s.shape[0]-1:
nextvalues = self.v(s_prime[t])
else:
nextvalues = v[t+1]
delta = r[t] + gamma * nextvalues * (1.0-done_mask[t]) - v[t]
advantage[t] = lastgaelam = delta + gamma * lmbda * lastgaelam * (1.0-done_mask[t])
assert advantage.shape == v.shape
td_target = advantage + v
# minibatch SGD over the entire buffer (K epochs)
b_inds = np.arange(batch_size)
for epoch in range(K_epoch):
np.random.shuffle(b_inds)
for start in range(0, batch_size, mini_batch):
end = start + mini_batch
minibatch_idx = b_inds[start:end]
bs, ba, blogprob_a, bv = s[minibatch_idx], a[minibatch_idx], logprob_a[minibatch_idx].reshape(-1), v[minibatch_idx].reshape(-1)
badvantage, btd_target = advantage[minibatch_idx].reshape(-1), td_target[minibatch_idx].reshape(-1)
if not torch.isnan(badvantage.std()):
badvantage = (badvantage - badvantage.mean()) / (badvantage.std() + 1e-8)
_, newlogprob_a, new_vs = self.get_action_and_value(bs, ba)
new_vs = new_vs.reshape(-1)
ratio = torch.exp(newlogprob_a - blogprob_a) # a/b == exp(log(a)-log(b))
surr1 = -ratio * badvantage
surr2 = -torch.clamp(ratio, 1-eps_clip, 1+eps_clip) * badvantage
policy_loss = torch.max(surr1, surr2).mean()
if self.v_loss_clip: # clipped value loss
v_clipped = bv + torch.clamp(new_vs - bv, -eps_clip, eps_clip)
value_loss_clipped = (v_clipped - btd_target) ** 2
value_loss_unclipped = (new_vs - btd_target) ** 2
value_loss_max = torch.max(value_loss_unclipped, value_loss_clipped)
value_loss = 0.5 * value_loss_max.mean()
else:
value_loss = F.smooth_l1_loss(new_vs, btd_target)
loss = policy_loss + vf_coef * value_loss
self.optimizer.zero_grad()
loss.backward()
nn.utils.clip_grad_norm_(self.parameters, self.max_grad_norm)
self.optimizer.step()
def main():
args = parse_args()
env_id = 2
seed = 1
env_name = ['HalfCheetah-v2', 'Ant-v2', 'Hopper-v2'][env_id]
env = gym.make(env_name)
env = gym.wrappers.RecordEpisodeStatistics(env) # bypass the reward normalization to record episodic return
env = gym.wrappers.ClipAction(env)
env = gym.wrappers.NormalizeObservation(env)
env = gym.wrappers.TransformObservation(env, lambda obs: np.clip(obs, -10, 10))
env = gym.wrappers.NormalizeReward(env) # this improves learning significantly
env = gym.wrappers.TransformReward(env, lambda reward: np.clip(reward, -10, 10))
env.seed(seed)
env.action_space.seed(seed)
env.observation_space.seed(seed)
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.backends.cudnn.deterministic = True
state_dim = env.observation_space.shape[0]
action_dim = env.action_space.shape[0]
hidden_dim = 64
model = PPO(state_dim, action_dim, hidden_dim)
score = 0.0
print_interval = 1
step = 0
update = 1
if args.wandb_activate:
wandb.init(
project=args.run,
entity=args.wandb_entity,
sync_tensorboard=True,
config=vars(args),
name=args.run+f'_{env_name}',
monitor_gym=True,
save_code=True,
)
writer = SummaryWriter(f"runs/{args.run}_{env_name}")
for n_epi in range(n_epis):
s = env.reset()
done = False
epi_r = 0.
## learning rate schedule
# frac = 1.0 - (n_epi - 1.0) / n_epis
# lrnow = frac * learning_rate
# model.optimizer.param_groups[0]["lr"] = lrnow
# while not done:
for t in range(T_horizon):
step += 1
with torch.no_grad():
a, logprob, v = model.get_action_and_value(torch.from_numpy(s).float().unsqueeze(0).to(device))
s_prime, r, done, info = env.step(a)
# env.render()
model.put_data((s, a, r, s_prime, logprob, v.squeeze(-1), done))
s = s_prime
score += r
if step % batch_size == 0 and step > 0:
model.train_net()
update += 1
eff_update = update
if 'episode' in info.keys():
epi_r = info['episode']['r']
print(f"Global steps: {step}, score: {epi_r}")
if done:
break
if n_epi%print_interval==0 and n_epi!=0:
# print("Global steps: {}, # of episode :{}, avg score : {:.1f}".format(step, n_epi, score/print_interval)) # this is normalized reward
writer.add_scalar("charts/episodic_return", epi_r, n_epi)
writer.add_scalar("charts/episodic_length", t, n_epi)
writer.add_scalar("charts/update", update, n_epi)
score = 0.0
env.close()
if __name__ == '__main__':
main()