-
Notifications
You must be signed in to change notification settings - Fork 0
/
cartpole_train_ppo_v2.py
208 lines (167 loc) · 7.37 KB
/
cartpole_train_ppo_v2.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
203
204
205
206
207
208
# Initial framework taken from https://github.com/OctThe16th/PPO-Keras/blob/master/Main.py
import numpy as np
import gym
from keras.models import Model
from keras.layers import Input, Dense
from keras import backend as K
from keras.optimizers import Adam
import tensorflow as tf
import random
from tensorboardX import SummaryWriter
from keras.models import Sequential
from keras.optimizers import RMSprop
from keras import backend as K
from keras.utils import to_categorical
import matplotlib.pyplot as plt
%matplotlib inline
ENV = 'CartPole-v1'
env = gym.make(ENV)
CONTINUOUS = False
#num_states = env.observation_space.shape[0]
EPISODES = 100000
LOSS_CLIPPING = 0.2 # Only implemented clipping for the surrogate loss, paper said it was best
EPOCHS = 10
NOISE = 1.0 # Exploration noise
GAMMA = 0.99
BUFFER_SIZE = 256
BATCH_SIZE = 64
NUM_ACTIONS = 2
NUM_STATE = 4
HIDDEN_SIZE = 128
NUM_LAYERS = 2
ENTROPY_LOSS = 1e-3
LR = 1e-4 # Lower lr stabilises training greatly
DUMMY_ACTION, DUMMY_VALUE = np.zeros((1, NUM_ACTIONS)), np.zeros((1, 1))
tensorboard = TensorBoard(log_dir='./logs', update_freq='epoch')
'''def exponential_average(old, new, b1):
return old * b1 + (1-b1) * new'''
def proximal_policy_optimization_loss(advantage, old_prediction):
def loss(y_true, y_pred):
prob = y_true * y_pred
old_prob = y_true * old_prediction
r = prob/(old_prob + 1e-10)
return -K.mean(K.minimum(r * advantage, K.clip(r, min_value=1 - LOSS_CLIPPING, max_value=1 + LOSS_CLIPPING) * advantage) + ENTROPY_LOSS * (prob * K.log(prob + 1e-10)))
return loss
class Agent:
def __init__(self):
self.critic = self.build_critic()
self.actor = self.build_actor()
self.env = gym.make(ENV)
print(self.env.action_space, 'action_space', self.env.observation_space, 'observation_space')
self.episode = 0
self.observation = self.env.reset()
self.val = False
self.reward = []
self.reward_over_time = []
self.name = self.get_name()
self.writer = SummaryWriter(self.name)
self.gradient_steps = 0
self.scores = []
self.episode_reward = 0
def get_name(self):
name = 'AllRuns/'
name += 'discrete/'
name += ENV
return name
def build_actor(self):
state_input = Input(shape=(NUM_STATE,))
advantage = Input(shape=(1,))
old_prediction = Input(shape=(NUM_ACTIONS,))
x = Dense(HIDDEN_SIZE, activation='tanh')(state_input)
for _ in range(NUM_LAYERS - 1):
x = Dense(HIDDEN_SIZE, activation='tanh')(x)
out_actions = Dense(NUM_ACTIONS, activation='softmax', name='output')(x)
model = Model(inputs=[state_input, advantage, old_prediction], outputs=[out_actions])
model.compile(optimizer=Adam(lr=LR),loss=[proximal_policy_optimization_loss(advantage=advantage,old_prediction=old_prediction)])
model.summary()
return model
def build_critic(self):
state_input = Input(shape=(NUM_STATE,))
x = Dense(HIDDEN_SIZE, activation='tanh')(state_input)
for _ in range(NUM_LAYERS - 1):
x = Dense(HIDDEN_SIZE, activation='tanh')(x)
out_value = Dense(1)(x)
model = Model(inputs=[state_input], outputs=[out_value])
model.compile(optimizer=Adam(lr=LR), loss='mse')
model.summary()
return model
def reset_env(self):
self.episode += 1
if self.episode % 100 == 0:
self.val = True
else:
self.val = False
self.observation = self.env.reset()
self.reward = []
self.episode_reward = 0
def get_action(self):
p = self.actor.predict([self.observation.reshape(1, NUM_STATE), DUMMY_VALUE, DUMMY_ACTION])
if self.val is False:
action = np.random.choice(NUM_ACTIONS, p=np.nan_to_num(p[0]))
else:
action = np.argmax(p[0])
action_matrix = np.zeros(NUM_ACTIONS)
action_matrix[action] = 1
return action, action_matrix, p
def transform_reward(self):
if self.val is True:
self.writer.add_scalar('Val episode reward', np.array(self.reward).sum(), self.episode)
else:
self.writer.add_scalar('Episode reward', np.array(self.reward).sum(), self.episode)
for j in range(len(self.reward) - 2, -1, -1):
self.reward[j] += self.reward[j + 1] * GAMMA
def get_batch(self):
batch = [[], [], [], []]
tmp_batch = [[], [], []]
while len(batch[0]) < BUFFER_SIZE:
action, action_matrix, predicted_action = self.get_action()
observation, reward, done, info = self.env.step(action)
self.reward.append(reward)
self.episode_reward = self.episode_reward + reward
tmp_batch[0].append(self.observation)
tmp_batch[1].append(action_matrix)
tmp_batch[2].append(predicted_action)
self.observation = observation
if done:
self.transform_reward()
if self.val is False:
for i in range(len(tmp_batch[0])):
obs, action, pred = tmp_batch[0][i], tmp_batch[1][i], tmp_batch[2][i]
r = self.reward[i]
batch[0].append(obs)
batch[1].append(action)
batch[2].append(pred)
batch[3].append(r)
tmp_batch = [[], [], []]
print("EPISODE REWARD ",self.episode_reward)
self.scores.append(self.episode_reward)
self.reset_env()
obs, action, pred, reward = np.array(batch[0]), np.array(batch[1]), np.array(batch[2]), np.reshape(np.array(batch[3]), (len(batch[3]), 1))
pred = np.reshape(pred, (pred.shape[0], pred.shape[2]))
return obs, action, pred, reward
def run(self):
while self.episode < EPISODES:
#print("EPISODE ",self.episode)
obs, action, pred, reward = self.get_batch()
obs, action, pred, reward = obs[:BUFFER_SIZE], action[:BUFFER_SIZE], pred[:BUFFER_SIZE], reward[:BUFFER_SIZE]
old_prediction = pred
pred_values = self.critic.predict(obs)
advantage = reward - pred_values
# advantage = (advantage - advantage.mean()) / advantage.std()
#print("STARTING TRAINING--------------------------")
actor_loss = self.actor.fit([obs, advantage, old_prediction], [action], batch_size=BATCH_SIZE, shuffle=True, epochs=EPOCHS, verbose=0)
critic_loss = self.critic.fit([obs], [reward], batch_size=BATCH_SIZE, shuffle=True, epochs=EPOCHS, verbose=0)
self.writer.add_scalar('Actor loss', actor_loss.history['loss'][-1], self.gradient_steps)
self.writer.add_scalar('Critic loss', critic_loss.history['loss'][-1], self.gradient_steps)
self.gradient_steps += 1
'''if self.episode % 10 == 0:
print ('(episode, score) = ' + str((self.episode,self.episode_reward)))'''
#Solved condition
if len(self.scores) >= 110:
if np.mean(self.scores[-100:]) >= 195.0:
print (' \ Solved after ' + str(self.episode-100) + ' episodes')
break
plt.plot(self.scores)
if __name__ == '__main__':
ag = Agent()
ag.run()