-
Notifications
You must be signed in to change notification settings - Fork 0
/
bipedal-reinforce.py
152 lines (119 loc) · 4.06 KB
/
bipedal-reinforce.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
#!/usr/bin/env python3
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser
import gym
import numpy as np
import tensorflow as tf
from tensorflow.contrib.layers import *
import sys
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('-l', '--load-model', metavar='NPZ',
help='NPZ file containing model weights/biases')
args = parser.parse_args()
env = gym.make('BipedalWalker-v2')
RNG_SEED=1
tf.set_random_seed(RNG_SEED)
env.seed(RNG_SEED)
hidden_size = 64
alpha = 0.01
TINY = 1e-8
gamma = 0.98
weights_init = xavier_initializer(uniform=False)
relu_init = tf.constant_initializer(0.1)
if args.load_model:
model = np.load(args.load_model)
hw_init = tf.constant_initializer(model['hidden/weights'])
hb_init = tf.constant_initializer(model['hidden/biases'])
mw_init = tf.constant_initializer(model['mus/weights'])
mb_init = tf.constant_initializer(model['mus/biases'])
sw_init = tf.constant_initializer(model['sigmas/weights'])
sb_init = tf.constant_initializer(model['sigmas/biases'])
else:
hw_init = weights_init
hb_init = relu_init
mw_init = weights_init
mb_init = relu_init
sw_init = weights_init
sb_init = relu_init
try:
output_units = env.action_space.shape[0]
except AttributeError:
output_units = env.action_space.n
input_shape = env.observation_space.shape[0]
NUM_INPUT_FEATURES = 24
x = tf.placeholder(tf.float32, shape=(None, NUM_INPUT_FEATURES), name='x')
y = tf.placeholder(tf.float32, shape=(None, output_units), name='y')
hidden = fully_connected(
inputs=x,
num_outputs=hidden_size,
activation_fn=tf.nn.relu,
weights_initializer=hw_init,
weights_regularizer=None,
biases_initializer=hb_init,
scope='hidden')
mus = fully_connected(
inputs=hidden,
num_outputs=output_units,
activation_fn=tf.tanh,
weights_initializer=mw_init,
weights_regularizer=None,
biases_initializer=mb_init,
scope='mus')
sigmas = tf.clip_by_value(fully_connected(
inputs=hidden,
num_outputs=output_units,
activation_fn=tf.nn.softplus,
weights_initializer=sw_init,
weights_regularizer=None,
biases_initializer=sb_init,
scope='sigmas'),
TINY, 5)
all_vars = tf.global_variables()
pi = tf.contrib.distributions.Normal(mus, sigmas, name='pi')
pi_sample = tf.tanh(pi.sample(), name='pi_sample')
log_pi = pi.log_prob(y, name='log_pi')
print (log_pi)
Returns = tf.placeholder(tf.float32, name='Returns')
optimizer = tf.train.GradientDescentOptimizer(alpha)
train_op = optimizer.minimize(-1.0 * Returns * log_pi)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
MEMORY=25
MAX_STEPS = env.spec.tags.get('wrapper_config.TimeLimit.max_episode_steps')
track_returns = []
for ep in range(16384):
obs = env.reset()
G = 0
ep_states = []
ep_actions = []
ep_rewards = [0]
done = False
t = 0
I = 1
while not done:
ep_states.append(obs)
env.render()
action = sess.run([pi_sample], feed_dict={x:[obs]})[0][0]
ep_actions.append(action)
obs, reward, done, info = env.step(action)
ep_rewards.append(reward * I)
G += reward * I
I *= gamma
t += 1
if t >= MAX_STEPS:
break
if not args.load_model:
returns = np.array([G - np.cumsum(ep_rewards[:-1])]).T
index = ep % MEMORY
_ = sess.run([train_op],
feed_dict={x:np.array(ep_states),
y:np.array(ep_actions),
Returns:returns })
track_returns.append(G)
track_returns = track_returns[-MEMORY:]
mean_return = np.mean(track_returns)
print("Episode {} finished after {} steps with return {}".format(ep, t, G))
print("Mean return over the last {} episodes is {}".format(MEMORY,
mean_return))
with tf.variable_scope("mus", reuse=True):
print("incoming weights for the mu's from the first hidden unit:", sess.run(tf.get_variable("weights"))[0,:])
sess.close()