forked from seungeunrho/minimalRL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppo-continuous.py
161 lines (131 loc) · 5.43 KB
/
ppo-continuous.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
import gym
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.distributions import Normal
import numpy as np
#Hyperparameters
learning_rate = 0.0003
gamma = 0.9
lmbda = 0.9
eps_clip = 0.2
K_epoch = 10
rollout_len = 3
buffer_size = 30
minibatch_size = 32
class PPO(nn.Module):
def __init__(self):
super(PPO, self).__init__()
self.data = []
self.fc1 = nn.Linear(3,128)
self.fc_mu = nn.Linear(128,1)
self.fc_std = nn.Linear(128,1)
self.fc_v = nn.Linear(128,1)
self.optimizer = optim.Adam(self.parameters(), lr=learning_rate)
self.optimization_step = 0
def pi(self, x, softmax_dim = 0):
x = F.relu(self.fc1(x))
mu = 2.0*torch.tanh(self.fc_mu(x))
std = F.softplus(self.fc_std(x))
return mu, std
def v(self, x):
x = F.relu(self.fc1(x))
v = self.fc_v(x)
return v
def put_data(self, transition):
self.data.append(transition)
def make_batch(self):
s_batch, a_batch, r_batch, s_prime_batch, prob_a_batch, done_batch = [], [], [], [], [], []
data = []
for j in range(buffer_size):
for i in range(minibatch_size):
rollout = self.data.pop()
s_lst, a_lst, r_lst, s_prime_lst, prob_a_lst, done_lst = [], [], [], [], [], []
for transition in rollout:
s, a, r, s_prime, prob_a, done = transition
s_lst.append(s)
a_lst.append([a])
r_lst.append([r])
s_prime_lst.append(s_prime)
prob_a_lst.append([prob_a])
done_mask = 0 if done else 1
done_lst.append([done_mask])
s_batch.append(s_lst)
a_batch.append(a_lst)
r_batch.append(r_lst)
s_prime_batch.append(s_prime_lst)
prob_a_batch.append(prob_a_lst)
done_batch.append(done_lst)
mini_batch = torch.tensor(s_batch, dtype=torch.float), torch.tensor(a_batch, dtype=torch.float), \
torch.tensor(r_batch, dtype=torch.float), torch.tensor(s_prime_batch, dtype=torch.float), \
torch.tensor(done_batch, dtype=torch.float), torch.tensor(prob_a_batch, dtype=torch.float)
data.append(mini_batch)
return data
def calc_advantage(self, data):
data_with_adv = []
for mini_batch in data:
s, a, r, s_prime, done_mask, old_log_prob = mini_batch
with torch.no_grad():
td_target = r + gamma * self.v(s_prime) * done_mask
delta = td_target - self.v(s)
delta = delta.numpy()
advantage_lst = []
advantage = 0.0
for delta_t in delta[::-1]:
advantage = gamma * lmbda * advantage + delta_t[0]
advantage_lst.append([advantage])
advantage_lst.reverse()
advantage = torch.tensor(advantage_lst, dtype=torch.float)
data_with_adv.append((s, a, r, s_prime, done_mask, old_log_prob, td_target, advantage))
return data_with_adv
def train_net(self):
if len(self.data) == minibatch_size * buffer_size:
data = self.make_batch()
data = self.calc_advantage(data)
for i in range(K_epoch):
for mini_batch in data:
s, a, r, s_prime, done_mask, old_log_prob, td_target, advantage = mini_batch
mu, std = self.pi(s, softmax_dim=1)
dist = Normal(mu, std)
log_prob = dist.log_prob(a)
ratio = torch.exp(log_prob - old_log_prob) # a/b == exp(log(a)-log(b))
surr1 = ratio * advantage
surr2 = torch.clamp(ratio, 1-eps_clip, 1+eps_clip) * advantage
loss = -torch.min(surr1, surr2) + F.smooth_l1_loss(self.v(s) , td_target)
self.optimizer.zero_grad()
loss.mean().backward()
nn.utils.clip_grad_norm_(self.parameters(), 1.0)
self.optimizer.step()
self.optimization_step += 1
def main():
env = gym.make('Pendulum-v0')
model = PPO()
score = 0.0
print_interval = 20
rollout = []
for n_epi in range(10000):
s = env.reset()
done = False
while not done:
for t in range(rollout_len):
mu, std = model.pi(torch.from_numpy(s).float())
dist = Normal(mu, std)
a = dist.sample()
log_prob = dist.log_prob(a)
s_prime, r, done, info = env.step([a.item()])
rollout.append((s, a, r/10.0, s_prime, log_prob.item(), done))
if len(rollout) == rollout_len:
model.put_data(rollout)
rollout = []
s = s_prime
score += r
if done:
break
model.train_net()
if n_epi%print_interval==0 and n_epi!=0:
print("# of episode :{}, avg score : {:.1f}, opt step: {}".format(n_epi, score/print_interval, model.optimization_step))
score = 0.0
env.close()
if __name__ == '__main__':
main()