-
Notifications
You must be signed in to change notification settings - Fork 10
/
TD3.py
337 lines (270 loc) · 11.6 KB
/
TD3.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
from datetime import datetime
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras import regularizers
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
import tracks
racer = tracks.Racer()
########################################
###### HYPERPARAMETERS #################
total_iterations = 50000
# Discount factor
gamma = 0.99
# Target network parameter update factor, for double DQN
tau = 0.005
# Learning rate for actor-critic models
critic_lr = 0.001
aux_lr = 0.001
noise_clip = 0.5
target_freq = 2
num_states = 5 #we reduce the state dim through observation (see below)
num_actions = 2 #acceleration and steering
print("State Space dim: {}, Action Space dim: {}".format(num_states,num_actions))
upper_bound = 1
lower_bound = -1
print("Min and Max Value of Action: {}".format(lower_bound,upper_bound))
buffer_dim = 50000
batch_size = 64
is_training = True
#pesi
load_weights = False
save_weights = True #beware when saving weights to not overwrite previous data
weights_file_actor = "weights/td3_actor_model_car"
weights_file_critic = "weights/td3_critic_model_car"
weights_file_critic2 = "weights/td3_critic2_model_car"
#The actor choose the move, given the state
def get_actor():
#no special initialization is required
# Initialize weights between -3e-3 and 3-e3
#last_init = tf.random_uniform_initializer(minval=-0.003, maxval=0.003)
inputs = layers.Input(shape=(num_states,))
out = layers.Dense(64, activation="relu")(inputs)
out = layers.Dense(64, activation="relu")(out)
#outputs = layers.Dense(num_actions, kernel_regularizer=regularizers.l2(0.01), kernel_initializer=last_init)(out)
#outputs = layers.Activation('tanh')(outputs)
#outputs = layers.Dense(num_actions, name="out", activation="tanh", kernel_initializer=last_init)(out)
outputs = layers.Dense(num_actions, name="out", activation="tanh")(out)
#outputs = outputs * upper_bound
model = tf.keras.Model(inputs, outputs, name="actor")
return model
#the critic compute the q-value, given the state and the action
def get_critic():
# State as input
state_input = layers.Input(shape=(num_states))
# Action as input
action_input = layers.Input(shape=(num_actions))
concat = layers.Concatenate()([state_input, action_input])
out = layers.Dense(64, activation="relu")(concat)
out = layers.Dense(64, activation="relu")(out)
outputs = layers.Dense(1)(out) #Outputs single value
model = tf.keras.Model([state_input, action_input], outputs, name="critic")
return model
#Replay buffer
class Buffer:
def __init__(self, buffer_capacity=100000, batch_size=64):
# Max Number of tuples that can be stored
self.buffer_capacity = buffer_capacity
# Num of tuples used for training
self.batch_size = batch_size
# Current number of tuples in buffer
self.buffer_counter = 0
# We have a different array for each tuple element
self.state_buffer = np.zeros((self.buffer_capacity, num_states))
self.action_buffer = np.zeros((self.buffer_capacity, num_actions))
self.reward_buffer = np.zeros((self.buffer_capacity, 1))
self.done_buffer = np.zeros((self.buffer_capacity, 1))
self.next_state_buffer = np.zeros((self.buffer_capacity, num_states))
# Stores a transition (s,a,r,s') in the buffer
def record(self, obs_tuple):
s,a,r,T,sn = obs_tuple
# restart form zero if buffer_capacity is exceeded, replacing old records
index = self.buffer_counter % self.buffer_capacity
self.state_buffer[index] = tf.squeeze(s)
self.action_buffer[index] = a
self.reward_buffer[index] = r
self.done_buffer[index] = T
self.next_state_buffer[index] = tf.squeeze(sn)
self.buffer_counter += 1
def sample_batch(self):
# Get sampling range
record_range = min(self.buffer_counter, self.buffer_capacity)
# Randomly sample indices
batch_indices = np.random.choice(record_range, self.batch_size)
s = self.state_buffer[batch_indices]
a = self.action_buffer[batch_indices]
r = self.reward_buffer[batch_indices]
T = self.done_buffer[batch_indices]
sn = self.next_state_buffer[batch_indices]
return ((s,a,r,T,sn))
# Slowly updating target parameters according to the tau rate <<1
@tf.function
def update_target(target_weights, weights, tau):
for (a, b) in zip(target_weights, weights):
a.assign(b * tau + a * (1 - tau))
def update_weights(target_weights, weights, tau):
return(target_weights * (1- tau) + weights * tau)
def policy(state,verbose=False):
#the policy used for training just add noise to the action
#the amount of noise is kept constant during training
sampled_action = tf.squeeze(actor_model(state))
noise = np.random.normal(scale=0.1,size=2)
#we may change the amount of noise for actions during training
noise[0] *= 2
noise[1] *= .5
# Adding noise to action
sampled_action = sampled_action.numpy()
sampled_action += noise
#in verbose mode, we may print information about selected actions
if verbose and sampled_action[0] < 0:
print("decelerating")
#Finally, we ensure actions are within bounds
legal_action = np.clip(sampled_action, lower_bound, upper_bound)
return [np.squeeze(legal_action)]
def policy_target(state,verbose=False):
#the policy used for training just add noise to the action
#the amount of noise is kept constant during training
newactions = []
sampled_action = tf.squeeze(target_actor(state))
for a in sampled_action:
noise = np.random.normal(scale=0.1,size=2)
#we may change the amount of noise for actions during training
noise[0] *= 2
noise[1] *= .5
noise = np.clip(noise,-noise_clip, noise_clip)
# Adding noise to action
a = a.numpy()
a += noise
legal_action = np.clip(a, lower_bound, upper_bound)
legal_action = np.squeeze(legal_action)
newactions.append(legal_action)
return np.asarray(tf.squeeze(newactions))
buffer = Buffer(buffer_dim, batch_size)
#creating models
actor_model = get_actor()
critic_model = get_critic()
critic2_model = get_critic()
#actor_model.summary()
#critic_model.summary()
#we create the target model for double learning (to prevent a moving target phenomenon)
target_actor = get_actor()
target_critic = get_critic()
target_critic2 = get_critic()
target_actor.trainable = False
target_critic.trainable = False
target_critic2.trainable = False
def compose(actor,critic):
state_input = layers.Input(shape=(num_states))
a = actor(state_input)
q = critic([state_input,a])
#reg_weights = actor.get_layer('out').get_weights()[0]
#print(tf.reduce_sum(0.01 * tf.square(reg_weights)))
m = tf.keras.Model(state_input, q)
#the loss function of the compound model is just the opposite of the critic output
m.add_loss(-q)
return(m)
aux_model = compose(actor_model,target_critic)
## TRAINING ##
if load_weights:
target_actor(layers.Input(shape=(num_states)))
target_critic([layers.Input(shape=(num_states)),layers.Input(shape=(num_actions))])
target_critic2([layers.Input(shape=(num_states)),layers.Input(shape=(num_actions))])
critic_model = keras.models.load_model(weights_file_critic)
critic2_model = keras.models.load_model(weights_file_critic2)
actor_model = keras.models.load_model(weights_file_actor)
# Making the weights equal initially
target_actor_weights = actor_model.get_weights()
target_critic_weights = critic_model.get_weights()
target_critic2_weights = critic2_model.get_weights()
target_actor.set_weights(target_actor_weights)
target_critic.set_weights(target_critic_weights)
target_critic2.set_weights(target_critic2_weights)
critic_optimizer = tf.keras.optimizers.Adam(critic_lr)
aux_optimizer = tf.keras.optimizers.Adam(aux_lr)
critic_model.compile(loss='mse',optimizer=critic_optimizer)
critic2_model.compile(loss='mse',optimizer=critic_optimizer)
aux_model.compile(optimizer=aux_optimizer)
# History of rewards per episode
ep_reward_list = []
# Average reward history of last few episodes
avg_reward_list = []
# We introduce a probability of doing n empty actions to separate the environment time-step from the agent
def step(action):
n = 1
t = np.random.randint(0,n)
state ,reward,done = racer.step(action)
for i in range(t):
if not done:
state ,t_r, done =racer.step([0, 0])
#state ,t_r, done =racer.step(action)
reward+=t_r
return (state, reward, done)
def train(total_iterations=total_iterations):
i = 0
mean_speed = 0
ep = 0
avg_reward = 0
while i<total_iterations:
prev_state = racer.reset()
episodic_reward = 0
mean_speed += prev_state[4]
done = False
while not(done):
i = i+1
tf_prev_state = tf.expand_dims(tf.convert_to_tensor(prev_state), 0)
#our policy is always noisy
action = policy(tf_prev_state)[0]
# Get state and reward from the environment
state, reward, done = step(action)
#we distinguish between termination with failure (state = None) and succesfull termination on track completion
#succesfull termination is stored as a normal tuple
fail = done and len(state)<5
buffer.record((prev_state, action, reward, fail, state))
if not(done):
mean_speed += state[4]
episodic_reward += reward
if buffer.buffer_counter>batch_size:
states,actions,rewards,dones,newstates= buffer.sample_batch()
newactions = policy_target(states)
minQ = tf.math.minimum(target_critic([newstates,newactions]), target_critic2([newstates,newactions]))
targetQ = rewards + (1-dones)*gamma*(minQ)
loss1 = critic_model.train_on_batch([states,actions],targetQ)
loss2 = critic2_model.train_on_batch([states,actions],targetQ)
if i%target_freq==0:
loss3 = aux_model.train_on_batch(states)
update_target(target_actor.variables, actor_model.variables, tau)
update_target(target_critic.variables, critic_model.variables, tau)
update_target(target_critic2.variables, critic2_model.variables, tau)
prev_state = state
if i%100 == 0:
avg_reward_list.append(avg_reward)
ep_reward_list.append(episodic_reward)
# Mean of last 40 episodes
avg_reward = np.mean(ep_reward_list[-40:])
print("Episode {}: Iterations {}, Avg. Reward = {}, Last reward = {}. Avg. speed = {}".format(ep, i, avg_reward,episodic_reward,mean_speed/i))
print("\n")
if ep>0 and ep%40 == 0:
print("## Evaluating policy ##")
tracks.metrics_run(actor_model, 10)
ep += 1
if total_iterations > 0:
if save_weights:
critic_model.save(weights_file_critic)
critic2_model.save(weights_file_critic2)
actor_model.save(weights_file_actor)
# Plotting Episodes versus Avg. Rewards
plt.plot(avg_reward_list)
plt.xlabel("Training steps x100")
plt.ylabel("Avg. Episodic Reward")
plt.ylim(-3.5,7)
plt.show(block=False)
plt.pause(0.001)
print("### TD3 Training ended ###")
print("Trained over {} steps".format(i))
if is_training == True:
start_t = datetime.now()
train()
end_t = datetime.now()
print("Time elapsed: {}".format(end_t-start_t))
tracks.newrun([actor_model])