-
Notifications
You must be signed in to change notification settings - Fork 0
/
reinforce.py
202 lines (159 loc) · 7.49 KB
/
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
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
import sys
import argparse
import numpy as np
import tensorflow as tf
import keras
import gym
import matplotlib
# matplotlib.use('Agg')
import matplotlib.pyplot as plt
class Reinforce(object):
# Implementation of the policy gradient method REINFORCE.
def __init__(self, model, lr):
self.model = model
self.lr = lr
self.total_reward_test = []
self.action_space_size = 4
self.k_train_period = 200
self.eval_episodes_length = 100
self.total_reward_test_mean = []
self.total_reward_test_std = []
def train(self, env, gamma=1.0, num_episodes=10000):
# Trains the model on a single episode using REINFORCE.
for episode in range(num_episodes):
states, actions, rewards = self.generate_episode(env)
G = np.zeros_like(rewards)
G[-1] = rewards[-1]
for t in range(len(rewards)-2, -1, -1):
G[t] = rewards[t] + gamma * G[t+1]
#end_for
history = self.model.train_on_batch(states,actions*G[:, np.newaxis])
if episode % self.k_train_period == 0:
test_rewards_curr = self.test(env)
print('Test-Reward at Episode %d: \t %.2f' % (episode, float(np.mean(test_rewards_curr))))
self.total_reward_test_mean.append(np.mean(test_rewards_curr))
self.total_reward_test_std.append(np.std(test_rewards_curr))
plt.figure()
reward_mean_tmp = np.array(self.total_reward_test_mean)
reward_std_tmp = np.array(self.total_reward_test_std)
plt.plot(range(len(reward_mean_tmp)), reward_mean_tmp)
plt.fill_between(range(len(reward_mean_tmp)),
reward_mean_tmp-reward_std_tmp,
reward_mean_tmp+reward_std_tmp, alpha=0.5)
plt.xlabel('Iterations')
plt.ylabel('Average Test Reward')
plt.savefig('REINFORCE-Reward_tmp.png')
np.save('Total_MeanTest_Reward_tmp', reward_mean_tmp)
np.save('Total_StdTest_Reward_tmp', reward_std_tmp)
# end_if
# end_for
return
def test(self, env):
rewards_total = []
for episode in range(self.eval_episodes_length):
reward_net = 0
state = env.reset()
is_done = False
time_step = 0
while not is_done:
action_prob = self.model.predict(np.array([state])).squeeze()
action = np.random.choice(self.action_space_size, 1, p=action_prob)[0]
next_state, reward, is_done, info = env.step(action)
reward_net += reward
if is_done:
break
state = next_state
# end_while
rewards_total.append(reward_net)
# end_for
return rewards_total
def generate_episode(self, env, render=False):
# Generates an episode by executing the current policy in the given env.
# Returns:
# - a list of states, indexed by time step
# - a list of actions, indexed by time step
# - a list of rewards, indexed by time step
states = []
actions = []
rewards = []
done = False
state = env.reset()
while not done:
action_prob = self.model.predict(np.array([state])).squeeze()
action = np.random.choice(self.action_space_size, 1, p=action_prob)[0]
states.append(np.array(state))
actions.append(keras.utils.to_categorical(action, self.action_space_size))
next_state, reward, done, info = env.step(action)
rewards.append(reward)
if done:
break
state = next_state
# end_while
return np.array(states), np.array(actions), np.array(rewards)
def parse_arguments():
# Command-line flags are defined here.
parser = argparse.ArgumentParser()
parser.add_argument('--num-episodes', dest='num_episodes', type=int,
default=50000, help="Number of episodes to train on.")
parser.add_argument('--lr', dest='lr', type=float,
default=5e-4, help="The learning rate.")
parser_group = parser.add_mutually_exclusive_group(required=False)
parser_group.add_argument('--render', dest='render',
action='store_true',
help="Whether to render the environment.")
parser_group.add_argument('--no-render', dest='render',
action='store_false',
help="Whether to render the environment.")
parser.set_defaults(render=False)
return parser.parse_args()
def main(args):
# Parse command-line arguments.
# args = parse_arguments()
# num_episodes = args.num_episodes
# lr = args.lr
# render = args.render
num_episodes = 50000
lr = 5e-4
render = False
# gpu_ops = tf.GPUOptions(allow_growth=True)
# config = tf.ConfigProto(gpu_options=gpu_ops, intra_op_parallelism_threads=4, inter_op_parallelism_threads=4)
# sess = tf.Session(config=config)
# keras.backend.tensorflow_backend.set_session(sess)
# Create the environment.
env = gym.make('LunarLander-v2')
# Create the model.
model = keras.models.Sequential()
model.add(keras.layers.Dense(16, activation='relu', input_shape=env.observation_space.shape,
bias_initializer='zeros', kernel_initializer=keras.initializers.VarianceScaling(scale=1.0,
distribution='uniform')))
model.add(keras.layers.Dense(16, activation='relu', bias_initializer='zeros',
kernel_initializer=keras.initializers.VarianceScaling(scale=1.0,
distribution='uniform')))
model.add(keras.layers.Dense(16, activation='relu', bias_initializer='zeros',
kernel_initializer=keras.initializers.VarianceScaling(scale=1.0,
distribution='uniform')))
model.add(keras.layers.Dense(4, activation='softmax', bias_initializer='zeros',
kernel_initializer=keras.initializers.VarianceScaling(scale=1.0,
distribution='uniform')))
model.compile(loss='categorical_crossentropy',
optimizer=tf.train.AdamOptimizer(),
metrics=['accuracy'])
# Train the model using REINFORCE and plot the learning curve.
reinforce = Reinforce(model, lr)
reinforce.train(env, gamma=0.99, num_episodes=num_episodes)
reward_mean_tmp = np.array(reinforce.total_reward_test_mean)
reward_std_tmp = np.array(reinforce.total_reward_test_std)
np.save('Total_MeanTest_Reward', reward_mean_tmp)
np.save('Total_StdTest_Reward', reward_std_tmp)
# Final Plot
plt.figure(1)
plt.plot(range(len(reward_mean_tmp)), reward_mean_tmp)
plt.fill_between(range(len(reward_mean_tmp)),
reward_mean_tmp - reward_std_tmp,
reward_mean_tmp + reward_std_tmp, alpha=0.5)
plt.xlabel('Iterations')
plt.ylabel('Average Test Reward')
plt.savefig('REINFORCE-Reward.png')
print('Shahriar')
if __name__ == '__main__':
main(sys.argv)