-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel_based_RL.py
More file actions
240 lines (194 loc) · 9.29 KB
/
Model_based_RL.py
File metadata and controls
240 lines (194 loc) · 9.29 KB
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
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import gym
import sys
if sys.version_info.major > 2:
print('True')
xrange = range
del sys
env = gym.make('CartPole-v0')
H, learning_rate, gamma, decay_rate = 8, 1e-2, 0.99, 0.99
resume, model_bs, real_bs, D, mH = False, 3, 3, 4, 256
def resetGradBuffer(gradBuffer):
for ix, grad in enumerate(gradBuffer):
gradBuffer[ix] = grad * 0
return gradBuffer
def discount_rewards(r):
""" take 1D float array of rewards and compute discounted reward """
discounted_r = np.zeros_like(r)
running_add = 0
for t in reversed(xrange(0, r.size)):
running_add = running_add * gamma + r[t]
discounted_r[t] = running_add
return discounted_r
# This function uses our model to produce a new state when given a previous state and action
def stepModel(sess, xs, action):
toFeed = np.reshape(np.hstack([xs[-1][0], np.array(action)]), [1, 5])
myPredict = sess.run([predicted_state], feed_dict={previous_state: toFeed})
reward = myPredict[0][:, 4]
observation = myPredict[0][:, 0:4]
observation[:, 0] = np.clip(observation[:, 0], -2.4, 2.4)
observation[:, 2] = np.clip(observation[:, 2], -0.4, 0.4)
doneP = np.clip(myPredict[0][:, 5], 0, 1)
if doneP > 0.1 or len(xs) >= 300:
done = True
else:
done = False
return observation, reward, done
tf.reset_default_graph()
observations = tf.placeholder(tf.float32, [None, 4], name="input_x")
W1 = tf.get_variable("W1", shape=[4, H],
initializer=tf.contrib.layers.xavier_initializer())
layer1 = tf.nn.relu(tf.matmul(observations, W1))
W2 = tf.get_variable("W2", shape=[H, 1],
initializer=tf.contrib.layers.xavier_initializer())
score = tf.matmul(layer1, W2)
probability = tf.nn.sigmoid(score)
tvars = tf.trainable_variables()
input_y = tf.placeholder(tf.float32, [None, 1], name="input_y")
advantages = tf.placeholder(tf.float32, name="reward_signal")
adam = tf.train.AdamOptimizer(learning_rate=learning_rate)
W1Grad = tf.placeholder(tf.float32, name="batch_grad1")
W2Grad = tf.placeholder(tf.float32, name="batch_grad2")
batchGrad = [W1Grad, W2Grad]
loglik = tf.log(input_y*(input_y - probability) + (1 - input_y)*(input_y + probability))
loss = -tf.reduce_mean(loglik * advantages)
newGrads = tf.gradients(loss, tvars)
updateGrads = adam.apply_gradients(zip(batchGrad, tvars))
input_data = tf.placeholder(tf.float32, [None, 5])
with tf.variable_scope('rnnlm'):
softmax_w = tf.get_variable("softmax_w", [mH, 50])
softmax_b = tf.get_variable("softmax_b", [50])
previous_state = tf.placeholder(tf.float32, [None, 5], name="previous_state")
W1M = tf.get_variable("W1M", shape=[5, mH],
initializer=tf.contrib.layers.xavier_initializer())
B1M = tf.Variable(tf.zeros([mH]), name="B1M")
layer1M = tf.nn.relu(tf.matmul(previous_state, W1M) + B1M)
W2M = tf.get_variable("W2M", shape=[mH, mH],
initializer=tf.contrib.layers.xavier_initializer())
B2M = tf.Variable(tf.zeros([mH]), name="B2M")
layer2M = tf.nn.relu(tf.matmul(layer1M, W2M) + B2M)
wO = tf.get_variable("wO", shape=[mH, 4],
initializer=tf.contrib.layers.xavier_initializer())
wR = tf.get_variable("wR", shape=[mH, 1],
initializer=tf.contrib.layers.xavier_initializer())
wD = tf.get_variable("wD", shape=[mH, 1],
initializer=tf.contrib.layers.xavier_initializer())
bO = tf.Variable(tf.zeros([4]), name="bO")
bR = tf.Variable(tf.zeros([1]), name="bR")
bD = tf.Variable(tf.ones([1]), name="bD")
predicted_observation = tf.matmul(layer2M, wO, name="predicted_observation") + bO
predicted_reward = tf.matmul(layer2M, wR, name="predicted_reward") + bR
predicted_done = tf.sigmoid(tf.matmul(layer2M, wD, name="predicted_done") + bD)
true_observation = tf.placeholder(tf.float32, [None, 4], name="true_observation")
true_reward = tf.placeholder(tf.float32, [None, 1], name="true_reward")
true_done = tf.placeholder(tf.float32, [None, 1], name="true_done")
predicted_state = tf.concat([predicted_observation, predicted_reward, predicted_done], 1)
observation_loss = tf.square(true_observation - predicted_observation)
reward_loss = tf.square(true_reward - predicted_reward)
done_loss = tf.multiply(predicted_done, true_done) + tf.multiply(1-predicted_done, 1-true_done)
done_loss = -tf.log(done_loss)
model_loss = tf.reduce_mean(observation_loss + done_loss + reward_loss)
modelAdam = tf.train.AdamOptimizer(learning_rate=learning_rate)
updateModel = modelAdam.minimize(model_loss)
xs, drs, ys, ds = [], [], [], []
running_reward = None
reward_sum = 0
episode_number = 1
real_episodes = 1
init = tf.global_variables_initializer()
batch_size = real_bs
drawFromModel = False # When set to True, will use model for observations
trainTheModel = True # Whether to train the model
trainThePolicy = False # Whether to train the policy
switch_point = 1
Epochs, Avg_reward = [], []
# Launch the graph
with tf.Session() as sess:
rendering = False
sess.run(init)
observation = env.reset()
x = observation
gradBuffer = sess.run(tvars)
gradBuffer = resetGradBuffer(gradBuffer)
while episode_number <= 5000:
# Start displaying environment once performance is acceptably high.
if (reward_sum / batch_size > 150 and not drawFromModel) or rendering:
env.render()
rendering = True
x = np.reshape(observation, [1, 4])
tfprob = sess.run(probability, feed_dict={observations: x})
action = 1 if np.random.uniform() < tfprob else 0
# record various intermediates (needed later for backprop)
xs.append(x)
y = 1 if action == 0 else 0
ys.append(y)
# step the model or real environment and get new measurements
if not drawFromModel:
observation, reward, done, info = env.step(action)
else:
observation, reward, done = stepModel(sess, xs, action)
reward_sum += reward
ds.append(done * 1)
drs.append(reward) # record reward (has to be done after we call step() to get reward for previous action)
if done:
if not drawFromModel:
real_episodes += 1
episode_number += 1
# stack together all inputs, hidden states, action gradients, and rewards for this episode
epx = np.vstack(xs)
epy = np.vstack(ys)
epr = np.vstack(drs)
epd = np.vstack(ds)
xs, drs, ys, ds = [], [], [], [] # reset array memory
if trainTheModel:
actions = np.array([np.abs(y - 1) for y in epy][:-1])
state_prevs = epx[:-1, :]
state_prevs = np.hstack([state_prevs, actions])
state_nexts = epx[1:, :]
rewards = np.array(epr[1:, :])
dones = np.array(epd[1:, :])
state_nextsAll = np.hstack([state_nexts, rewards, dones])
feed_dict = {previous_state: state_prevs, true_observation: state_nexts, true_done: dones,
true_reward: rewards}
loss, pState, _ = sess.run([model_loss, predicted_state, updateModel], feed_dict)
if trainThePolicy:
discounted_epr = discount_rewards(epr).astype('float32')
discounted_epr -= np.mean(discounted_epr)
discounted_epr /= np.std(discounted_epr)
tGrad = sess.run(newGrads, feed_dict={observations: epx, input_y: epy, advantages: discounted_epr})
# If gradients becom too large, end training process
if np.sum(tGrad[0] == tGrad[0]) == 0:
break
for ix, grad in enumerate(tGrad):
gradBuffer[ix] += grad
if switch_point + batch_size == episode_number:
switch_point = episode_number
if trainThePolicy:
sess.run(updateGrads, feed_dict={W1Grad: gradBuffer[0], W2Grad: gradBuffer[1]})
gradBuffer = resetGradBuffer(gradBuffer)
running_reward = reward_sum if running_reward is None else running_reward * 0.99 + reward_sum * 0.01
if not drawFromModel:
print('World Perf: Episode %.0f, Reward %.1f, action: %.0f, mean reward %.1f.' % (
real_episodes, reward_sum/real_bs, action, running_reward/real_bs))
Avg_reward.append(running_reward / real_bs)
Epochs.append(real_episodes)
if reward_sum / batch_size > 200:
break
reward_sum = 0
# Once the model has been trained on 100 episodes, we start alternating between training the policy
# from the model and training the model from the real environment.
if episode_number > 100:
drawFromModel = not drawFromModel
trainTheModel = not trainTheModel
trainThePolicy = not trainThePolicy
if drawFromModel:
observation = np.random.uniform(-0.1, 0.1, [4]) # Generate reasonable starting point
batch_size = model_bs
else:
observation = env.reset()
batch_size = real_bs
plt.plot(Epochs, Avg_reward)
plt.xlabel('Epochs')
plt.ylabel('Average Reward')