forked from LuEE-C/PPO-Keras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
239 lines (186 loc) · 8.27 KB
/
Main.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
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
# Initial framework taken from https://github.com/jaara/AI-blog/blob/master/CartPole-A3C.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 numba as nb
from tensorboardX import SummaryWriter
ENV = 'LunarLander-v2'
CONTINUOUS = False
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 = 2048
BATCH_SIZE = 256
NUM_ACTIONS = 4
NUM_STATE = 8
HIDDEN_SIZE = 128
NUM_LAYERS = 2
ENTROPY_LOSS = 5e-3
LR = 1e-4 # Lower lr stabilises training greatly
DUMMY_ACTION, DUMMY_VALUE = np.zeros((1, NUM_ACTIONS)), np.zeros((1, 1))
@nb.jit
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 = K.sum(y_true * y_pred, axis=-1)
old_prob = K.sum(y_true * old_prediction, axis=-1)
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
def proximal_policy_optimization_loss_continuous(advantage, old_prediction):
def loss(y_true, y_pred):
var = K.square(NOISE)
pi = 3.1415926
denom = K.sqrt(2 * pi * var)
prob_num = K.exp(- K.square(y_true - y_pred) / (2 * var))
old_prob_num = K.exp(- K.square(y_true - old_prediction) / (2 * var))
prob = prob_num/denom
old_prob = old_prob_num/denom
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))
return loss
class Agent:
def __init__(self):
self.critic = self.build_critic()
if CONTINUOUS is False:
self.actor = self.build_actor()
else:
self.actor = self.build_actor_continuous()
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
def get_name(self):
name = 'AllRuns/'
if CONTINUOUS is True:
name += 'continous/'
else:
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_actor_continuous(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, name='output', activation='tanh')(x)
model = Model(inputs=[state_input, advantage, old_prediction], outputs=[out_actions])
model.compile(optimizer=Adam(lr=LR),
loss=[proximal_policy_optimization_loss_continuous(
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')
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 = []
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 get_action_continuous(self):
p = self.actor.predict([self.observation.reshape(1, NUM_STATE), DUMMY_VALUE, DUMMY_ACTION])
if self.val is False:
action = action_matrix = p[0] + np.random.normal(loc=0, scale=NOISE, size=p[0].shape)
else:
action = action_matrix = p[0]
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:
if CONTINUOUS is False:
action, action_matrix, predicted_action = self.get_action()
else:
action, action_matrix, predicted_action = self.get_action_continuous()
observation, reward, done, info = self.env.step(action)
self.reward.append(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 = [[], [], []]
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:
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
actor_loss = self.actor.fit([obs, advantage, old_prediction], [action], batch_size=BATCH_SIZE, shuffle=True, epochs=EPOCHS, verbose=False)
critic_loss = self.critic.fit([obs], [reward], batch_size=BATCH_SIZE, shuffle=True, epochs=EPOCHS, verbose=False)
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 __name__ == '__main__':
ag = Agent()
ag.run()