-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathddpg.py
166 lines (140 loc) · 6.14 KB
/
ddpg.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
import sys, os
from time import gmtime, strftime
import numpy as np
import tensorflow as tf
import tensorflow.contrib.slim as slim
import gym
import imageio
class Memory:
def __init__(self, env, capacity=10**6):
self.capacity = capacity
self.obs_sz = env.observation_space.shape[0]
self.ac_sz = env.action_space.shape[0]
self.obss = np.zeros((capacity, self.obs_sz))
self.actions = np.zeros((capacity, self.ac_sz))
self.rewards = np.zeros((capacity, 1))
self.j = 0
self.full = False
def add(self, obs, action, reward):
self.obss[self.j] = obs
self.actions[self.j] = action
self.rewards[self.j] = reward
self.j = (self.j + 1) % self.capacity
if self.j == 0:
self.full = True
def get_batch(self, mbsz=64):
if self.full:
idx = np.random.randint(self.capacity-1, size=(mbsz,))
else:
idx = np.random.randint(self.j-1, size=(mbsz,))
mb_obs = self.obss[idx]
mb_nobs = self.obss[idx+1]
mb_actions = self.actions[idx]
mb_rewards = self.rewards[idx]
return mb_obs, mb_nobs, mb_actions, mb_rewards
class Policy:
def __init__(self, env):
self.g = 0.99
self.decay = 0.999
self.env = env
assert np.all(self.env.action_space.high == -self.env.action_space.low)
self.sess = tf.Session()
self.create_train_op()
self.sess.run(tf.global_variables_initializer())
def build_actor(self, s, scope):
with tf.variable_scope(scope):
with tf.variable_scope('actor'):
h = slim.fully_connected(s, 300, scope='fc/1')
h = slim.fully_connected(h, 300, scope='fc/2')
h = slim.fully_connected(h, self.env.action_space.shape[0],
activation_fn=tf.nn.tanh,
weights_initializer=tf.random_uniform_initializer(-3e-3, 3e-3),
scope='fc/3')
return h * self.env.action_space.high
def build_critic(self, s, a, scope):
with tf.variable_scope(scope):
with tf.variable_scope('critic'):
ho = slim.fully_connected(s, 300, scope='obs/fc/1')
ho = slim.fully_connected(ho, 300, scope='obs/fc/2')
ha = slim.fully_connected(a, 300, scope='act/fc/1')
ha = slim.fully_connected(ha, 300, scope='act/fc/2')
h = slim.fully_connected(ha + ho, 300, scope='both/fc/1')
return slim.fully_connected(h, 1, activation_fn=None,
weights_initializer=tf.random_uniform_initializer(-3e-3, 3e-3),
scope='both/fc/2')
def create_train_op(self):
self.s = tf.placeholder(tf.float32, (None, ) + env.observation_space.shape)
self.ns = tf.placeholder(tf.float32, (None, ) + env.observation_space.shape)
self.a = tf.placeholder(tf.float32, (None, ) + env.action_space.shape)
self.r = tf.placeholder(tf.float32, (None, 1))
self.u = self.build_actor(self.s, scope='main')
q_main = self.build_critic(self.s, self.a, scope='main')
u_target = self.build_actor(self.ns, scope='target')
q_target = self.build_critic(self.ns, u_target, scope='target') * self.g + self.r
self.q_loss = tf.reduce_mean(0.5 * (q_main - q_target)**2)
self.a_loss = tf.reduce_mean(- self.build_critic(self.s, self.u, scope='main'))
self.train_actor = tf.train.AdamOptimizer(1e-4).minimize(self.a_loss,
var_list=slim.get_variables(scope="main/actor"))
self.train_critic = tf.train.AdamOptimizer(1e-3).minimize(self.q_loss,
var_list=slim.get_variables(scope="main/critic"))
updates = []
for f in ['actor', 'critic']:
for s, t in zip(
slim.get_variables(scope="main/{}".format(f)),
slim.get_variables(scope="target/{}".format(f))):
new_t = t * self.decay + s * (1 - self.decay)
updates.append(tf.assign(t, new_t))
ops = [self.train_actor, self.train_critic] + updates
self.train_op = tf.group(*ops)
def act(self, s):
return self.sess.run(self.u, {self.s: s[None, :]})[0]
def update(self, s, ns, a, r):
_, q_loss, a_loss = self.sess.run(
[self.train_op, self.q_loss, self.a_loss],
{self.s: s, self.ns: ns, self.a: a, self.r: r})
return q_loss, a_loss
class Agent:
def __init__(self, expname, policy, memory, eplen=100, iters=50000, mbsz=64, save_every=50):
self.expname = expname
self.policy = policy
self.memory = memory
self.eplen = eplen
self.iters = iters
self.mbsz = mbsz
self.save_every = save_every
def loop(self):
for e in xrange(self.iters):
obs = env.reset()
done = False
rtrn = 0.
q_loss = 0.
a_loss = 0.
frames = []
for j in xrange(self.eplen):
action = policy.act(obs)
next_obs, reward, done, info = env.step(action)
if e % self.save_every == 0:
frames.append(env.render(mode='rgb_array'))
rtrn += reward
self.memory.add(obs, action, reward)
obs = next_obs
if self.memory.j > self.mbsz:
batch = self.memory.get_batch(self.mbsz)
ql, al = policy.update(*batch)
q_loss += ql
a_loss += al
if e % self.save_every == 0:
imageio.mimwrite("{}/gifs/{}.gif".format(self.expname, e), frames, fps=36)
print "Episode {}, Return: {}, (Q, A)-loss: {}, {}".format(e, rtrn,
q_loss/self.eplen, a_loss/self.eplen)
if __name__ == '__main__':
expname = strftime("%Y.%m.%d|%H.%M.%S", gmtime())
if not os.path.exists(expname):
os.makedirs('{}/gifs'.format(expname))
envname = sys.argv[1]
eplen = int(sys.argv[2])
env = gym.make(envname)
memory = Memory(env, capacity=10**6)
policy = Policy(env)
agent = Agent(expname, policy, memory, eplen=eplen)
agent.loop()