-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathppo2.py
193 lines (155 loc) · 7.21 KB
/
ppo2.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
import tensorflow as tf
import numpy as np
import core as cr
import gym
from tensorboardX import SummaryWriter
class PPO:
def __init__(self):
self.sess = tf.Session()
self.gamma = 0.99
self.lamda = 0.95
self.state_size = 33
self.output_size = 4
self.hidden = [400, 300]
self.batch_size = 32
self.pi_lr = 0.00025
self.v_lr = 0.00025
self.ppo_eps = 0.2
self.epoch = 10
self.x_ph, self.a_ph, self.adv_ph, self.target_ph, self.logp_old_ph, self.old_value = \
cr.placeholders(self.state_size, self.output_size, None, None, None, None)
self.pi, self.logp, self.logp_pi, self.v = cr.ppo_mlp_actor_critic(
x=self.x_ph,
a=self.a_ph,
hidden=self.hidden,
activation=tf.nn.relu,
output_activation=None,
output_size=self.output_size
)
self.all_phs = [self.x_ph, self.a_ph, self.adv_ph, self.target_ph, self.logp_old_ph, self.old_value]
self.get_action_ops = [self.pi, self.v, self.logp_pi]
self.ratio = tf.exp(self.logp - self.logp_old_ph)
self.min_adv = tf.where(self.adv_ph > 0, (1.0 + self.ppo_eps)*self.adv_ph, (1.0 - self.ppo_eps)*self.adv_ph)
self.pi_loss = -tf.reduce_mean(tf.minimum(self.ratio * self.adv_ph, self.min_adv))
self.clipped_value_loss = self.old_value + tf.clip_by_value(self.v - self.old_value, -self.ppo_eps, self.ppo_eps)
self.v_loss1 = (self.target_ph - self.clipped_value_loss) ** 2
self.v_loss2 = (self.target_ph - self.v) ** 2
self.v_loss = 0.5 * tf.reduce_mean(tf.maximum(self.v_loss1, self.v_loss2))
self.train_pi = tf.train.AdamOptimizer(self.pi_lr).minimize(self.pi_loss)
self.train_v = tf.train.AdamOptimizer(self.v_lr).minimize(self.v_loss)
self.approx_kl = tf.reduce_mean(self.logp_old_ph - self.logp)
self.approx_ent = tf.reduce_mean(-self.logp)
self.sess.run(tf.global_variables_initializer())
def update(self, state, action, target, adv, logp_old, value):
zip_ph = [state, action, adv, target, logp_old, value]
inputs = {k:v for k,v in zip(self.all_phs, zip_ph)}
value_loss, kl, ent = 0, 0, 0
for i in range(self.epoch):
_, _, v_loss, approxkl, approxent = self.sess.run([self.train_pi, self.train_v, self.v_loss, self.approx_kl, self.approx_ent], feed_dict=inputs)
value_loss += v_loss
kl += approxkl
ent += approxent
return value_loss, kl, ent
def get_action(self, state):
a, v, logp_t = self.sess.run(self.get_action_ops, feed_dict={
self.x_ph: [state]})
return a[0], v[0], logp_t[0]
def test(self):
from mlagents.envs import UnityEnvironment
num_worker = 20
state_size = 33
output_size = 4
n_step = 128
ep = 0
score = 0
saver = tf.train.Saver()
saver.restore(self.sess, 'model/model')
env = UnityEnvironment(file_name='env/walker', worker_id=2)
default_brain = env.brain_names[0]
brain = env.brains[default_brain]
initial_observation = env.reset()
env_info = env.reset()
states = np.zeros([num_worker, state_size])
while True:
inference = [self.get_action(s) for s in states]
actions = [inf[0] for inf in inference]
env_info = env.step(actions)[default_brain]
states = env_info.vector_observations
def run(self):
from mlagents.envs import UnityEnvironment
writer = SummaryWriter('runs/ppo2')
num_worker = 20
state_size = 33
output_size = 4
n_step = 128
ep = 0
score = 0
saver = tf.train.Saver()
env = UnityEnvironment(file_name='env/training', worker_id=0)
default_brain = env.brain_names[0]
brain = env.brains[default_brain]
initial_observation = env.reset()
env_info = env.reset()
states = np.zeros([num_worker, state_size])
while True:
values_list, states_list, actions_list, dones_list, logp_ts_list, rewards_list = \
[], [], [], [], [], []
for _ in range(n_step):
inference = [self.get_action(s) for s in states]
actions = [inf[0] for inf in inference]
values = [inf[1] for inf in inference]
logp_ts = [inf[2] for inf in inference]
env_info = env.step(actions)[default_brain]
next_states = env_info.vector_observations
rewards = env_info.rewards
dones = env_info.local_done
score += sum(rewards)
states_list.append(states)
values_list.append(values)
actions_list.append(actions)
dones_list.append(dones)
logp_ts_list.append(logp_ts)
rewards_list.append(rewards)
states = next_states
if dones[0]:
ep += 1
if ep < 1000:
writer.add_scalar('data/reward', score, ep)
print(ep, score)
saver.save(self.sess, 'model/model')
score = 0
inference = [self.get_action(s) for s in states]
values = [inf[1] for inf in inference]
values_list.append(values)
values_list = np.stack(values_list).transpose([1, 0])
current_value_list = values_list[:, :-1]
next_value_list = values_list[:, 1:]
states_list = np.stack(states_list).transpose([1, 0, 2]).reshape([-1, state_size])
actions_list = np.stack(actions_list).transpose([1, 0, 2]).reshape([-1, output_size])
dones_list = np.stack(dones_list).transpose([1, 0]).reshape([-1])
logp_ts_list = np.stack(logp_ts_list).transpose([1, 0]).reshape([-1])
rewards_list = np.stack(rewards_list).transpose([1, 0]).reshape([-1])
current_value_list = np.stack(current_value_list).reshape([-1])
next_value_list = np.stack(next_value_list).reshape([-1])
adv_list, target_list = [], []
for idx in range(num_worker):
start_idx = idx * n_step
end_idx = (idx + 1) * n_step
adv, target = cr.get_gaes(
rewards_list[start_idx : end_idx],
dones_list[start_idx : end_idx],
current_value_list[start_idx : end_idx],
next_value_list[start_idx : end_idx],
self.gamma,
self.lamda,
True
)
adv_list.append(adv)
target_list.append(target)
adv_list = np.stack(adv_list).reshape([-1])
target_list = np.stack(target_list).reshape([-1])
value_loss, kl, ent = self.update(states_list, actions_list, target_list,
adv_list, logp_ts_list, current_value_list)
if __name__ == '__main__':
agent = PPO()
agent.run()