-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreinforce.py
191 lines (164 loc) · 7.09 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
import sys
import argparse
import numpy as np
import tensorflow as tf
import keras
import keras.backend as K
import gym
import timeit
#import matplotlib
#matplotlib.use('Agg')
import matplotlib.pyplot as plt
np.random.seed(1234)
tf.set_random_seed(1234)
class Reinforce(object):
# Implementation of the policy gradient method REINFORCE.
def __init__(self, env, model, lr, num_episodes):
self.model = model
self.lr = lr
self.num_episodes = num_episodes
self.numStates = env.observation_space.shape[0]
self.numActions = env.action_space.n
self.num_test_episodes = 100
self.file_index = 1
# TODO: Define any training operations and optimizers here, initialize
# your variables, or alternately compile your model here.
states = self.model.input
softmax = self.model.output
G_return = K.placeholder(shape=(None,))
onehot_action = K.placeholder(shape=(None, self.numActions))
prob_actions = K.sum(softmax * onehot_action, axis=1)
log_prob_actions = K.log(prob_actions)
loss = - log_prob_actions * G_return
loss = K.mean(loss)
Adam = keras.optimizers.Adam(lr=self.lr)
updates = Adam.get_updates(self.model.trainable_weights,[],loss)
self.fit = K.function(inputs=[states,onehot_action,G_return],
outputs=[],
updates=updates)
def compute_G(self,R, gamma=1):
G = np.zeros_like(R, dtype=np.float32)
temp = 0
for t in reversed(range(len(R))):
temp = temp * gamma + R[t]
G[t] = temp
#normalize
# G -= G.mean() / G.std()
return G
def train(self, env, gamma=1.0):
# Trains the model on a single episode using REINFORCE.
# TODO: Implement this method. It may be helpful to call the class
# method generate_episode() to generate training data.
start_time = timeit.default_timer()
loop_time = start_time
mean_reward = []
episodes_axes = []
error = []
for episode in range(1,self.num_episodes + 1):
states, actions, rewards = self.generate_episode(env)
# print("total return:",np.sum(rewards))#,"LR:", K.get_value(self.model.optimizer.lr) )
states = np.squeeze(states)
# print(states.shape)
onehot_actions = keras.utils.to_categorical(actions, num_classes=self.numActions)
#Downscale rewards by 1e-2
rewards = np.multiply(1e-2, rewards)
G = self.compute_G(rewards)
# T = len(rewards)
# print(T)
# G = np.zeros(T)
# for t in range(T-1, -1, -1):
# for k in range(t, T):
# G[t] += gamma**(k-t)*rewards[k]
self.fit([states, onehot_actions, G])
if episode % 500 == 0:
print("episode=", episode)
total_returns = np.zeros((self.num_test_episodes))
for ep in range(self.num_test_episodes):
states, actions, rewards = self.generate_episode(env)
total_returns[ep] = np.sum(rewards)
mean_tot_return = np.mean(total_returns)
std_dev = np.std(total_returns)
mean_reward.append(mean_tot_return)
episodes_axes.append(episode)
print("mean_rewards="+str(mean_tot_return))
print("std_dev_rewards="+str(std_dev))
error.append(std_dev)
# plt.figure()
plt.errorbar(episodes_axes,mean_reward,error,capsize=5)
plt.xlabel('Episodes')
plt.ylabel('Mean & Std_dev Reward per Episode')
plt.legend(loc='best')
elapsed_time = timeit.default_timer() - loop_time
print("Time="+str(elapsed_time))
print("Elapsed_time="+str(timeit.default_timer() - start_time))
loop_time = timeit.default_timer()
if episode % 1000 == 0:
plt.savefig('Reinforce_LC_' + str(self.file_index) + '.png')
plt.clf()
self.model.save('Reinforce_model_' + str(self.file_index) + '.h5')
self.file_index += 1
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
# TODO: Implement this method.
states = []
actions = []
rewards = []
state = env.reset()
done = False
while not done:
state = np.expand_dims(state,0)
prob = self.model.predict(state, batch_size=1).flatten()
action = np.random.choice(self.numActions, 1, p=prob)[0]
next_state, reward, done, _ = env.step(action)
states.append(state)
actions.append(action)
rewards.append(reward)
state = next_state
states = np.array(states)
# print(states)
# print(states.shape)
actions = np.array(actions)
rewards = np.array(rewards)
return states, actions, rewards
def parse_arguments():
# Command-line flags are defined here.
parser = argparse.ArgumentParser()
parser.add_argument('--model-config-path', dest='model_config_path',
type=str, default='LunarLander-v2-config.json',
help="Path to the model config file.")
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-3, help="The learning rate.")
# https://stackoverflow.com/questions/15008758/parsing-boolean-values-with-argparse
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()
model_config_path = args.model_config_path
num_episodes = args.num_episodes
lr = args.lr
render = args.render
# Create the environment.
env = gym.make('LunarLander-v2')
env.seed(1234)
# Load the policy model from file.
with open(model_config_path, 'r') as f:
model = keras.models.model_from_json(f.read())
# TODO: Train the model using REINFORCE and plot the learning curve.
agent = Reinforce(env, model, lr, num_episodes)
agent.train(env)
if __name__ == '__main__':
main(sys.argv)