-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
330 lines (257 loc) · 10.2 KB
/
models.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
"""
Models
------
This file contains classes for defining agent models such as the
action-value network (Critic) and the agent policy (Actor) models.
"""
import tensorflow as tf
from tensorflow import keras
import numpy as np
from memory import ReplayBuffer
class Actor(keras.Model):
def __init__(self, params):
"""
Implements the agent policy (actor), that can be used to sample actions.
Args:
params: dict
Hyperparameters dictionary
"""
super(Actor, self).__init__()
n_actions = params['n_actions']
fc1_neurons, fc2_neurons = params['n_neurons']
lr = params['actor_lr']
self.action_bounds = params['action_bounds']
self.fc1 = keras.layers.Dense(units=fc1_neurons, activation='relu')
self.fc2 = keras.layers.Dense(units=fc2_neurons, activation='relu',)
self.output_layer = keras.layers.Dense( units=n_actions, activation='tanh',
kernel_initializer=keras.initializers.
RandomUniform(minval=-0.003, maxval=0.003)
)
self.optimizer = keras.optimizers.Adam(learning_rate=lr)
def call(self, inputs):
"""
Compute action(s) for given observation(s)
Args:
inputs: numpy.ndarray
One or a minibatch of observation arrays.
Returns:
action: tf.Tensor
The predicted action(s) value(s).
"""
x = self.fc1(inputs)
x = self.fc2(x)
x = self.output_layer(x)
action = x * self.action_bounds[1]
return action
@tf.function
def optimize(self, actor_gradients):
"""
Update weight parameters of the model given computed gradients.
Args:
actor_gradients: list
Gradients of the actor model.
Returns:
None
"""
self.optimizer.apply_gradients(zip(actor_gradients, self.trainable_variables))
class Critic(keras.Model):
def __init__(self, params):
"""
Implements the agent Q-values (critic) model, that can be used to compute actions values.
Args:
params: dict
Hyperparameters dictionary
"""
super(Critic, self).__init__()
fc1_neurons, fc2_neurons = params['n_neurons']
lr = params['critic_lr']
self.fc1 = keras.layers.Dense(units=fc1_neurons, activation="relu")
self.fc2 = keras.layers.Dense(units=fc2_neurons, activation="relu",)
self.out = keras.layers.Dense(units=1, kernel_initializer=keras.initializers.
RandomUniform(minval=-3 * 10 ** -3, maxval=3 * 10 ** -3),
)
self.concat = keras.layers.Concatenate()
self.optimizer = keras.optimizers.Adam(learning_rate=lr)
def call(self, inputs):
"""
Compute action value(s) for given [observation(s), actions(s)]
Args:
inputs: list
A pair of one or a minibatch of states and actions, both of which are tf.Tensor.
Returns:
q_values: tf.Tensor
The predicted state-action(s) value(s).
"""
x = self.concat(inputs)
x = self.fc1(x)
x = self.fc2(x)
q_values = self.out(x)
return q_values
@tf.function
def optimize(self, critic_gradients):
"""
Update weight parameters of the model given computed gradients.
Args:
critic_gradients: list
Gradients of the critic model.
Returns:
None
"""
self.optimizer.apply_gradients(zip(critic_gradients, self.trainable_variables))
class Agent:
def __init__(self, params):
"""
Defining agent class, that has both memory buffer, actor and critic models.
Args:
params: : dict
Hyperparameters dictionary
"""
self.params = params
self.gamma = params['gamma']
self.tau = params['tau']
self.memory = ReplayBuffer(obs_shape=params['obs_shape'], n_actions=params['n_actions'],
buffer_size=params['buffer_size'], minibatch_size=params['minibatch'])
self.actor = Actor(params)
self.critic = Critic(params)
def act(self, state):
"""
Predict action for given state(s).
Args:
state: numpy.ndarray or tf.Tensor
One or a minibatch of states.
Returns:
action: tf.Tensor
Predicted action(s).
"""
action = self.actor.call(state)
return action
def get_q_values(self, state, action):
"""
Predicts the action value for given state-action pair(s).
Args:
state: tf.Tensor
One or a minibatch of states.
action: tf.Tensor
One or a minibatch of actions.
Returns:
q_values: tf.Tensor
The action values.
"""
q_values = self.critic.call(inputs=[state, action])
return q_values
def store_transition(self, state, action, reward, next_state, terminal):
"""
Adds a new experienced transition to the agent memory buffer.
Args:
state: numpy.ndarray
Agent state at the current step.
action: numpy.ndarray
Performed action.
reward: float
Received reward.
next_state: numpy.ndarray
Agent state at the next step.
terminal: int
0 if agent step cause episode termination, else 1.
Returns:
None
"""
self.memory.store_transition(state, action, reward, next_state, terminal)
def get_weights(self):
"""
Retrieve and return both actor and critic models learnable weights.
Returns:
weight_actor, weight_critic: list
Weights of all layers in a list. Weights of each layer is a numpy.ndarray.
"""
actor_weights = self.actor.get_weights()
critic_weights = self.critic.get_weights()
return actor_weights, critic_weights
@tf.function
def _update_weights(self, weights, target_weights, tau):
result = [0 for _ in range(len(weights))]
for i in range(len(weights)):
result[i] = tau*weights[i] + (1-tau)*target_weights[i]
return result
def set_weights(self, actor_weights, critic_weights, tau=None):
"""
Update weights of both actor and critic using the soft update rule.
E.g. theta = tau*actor_weights + (1-tau)*theta.
Args:
actor_weights: list
Actor model trainable weights, which is a list of numpy.ndarrays.
critic_weights: list
Critic model trainable weights, which is a list of numpy.ndarrays.
tau: float (default: None)
The soft update rule prarameter.
Returns:
None
"""
if tau is None:
tau = self.tau
target_actor_weights = self.actor.get_weights()
target_actor_weights = self._update_weights(actor_weights, target_actor_weights, tau)
self.actor.set_weights(target_actor_weights)
target_critic_weights = self.critic.get_weights()
target_critic_weights = self._update_weights(critic_weights, target_critic_weights, tau)
self.critic.set_weights(target_critic_weights)
return
@tf.function
def _learn(self, states, actions, rewards, next_states, terminals, target_agent):
# --------- Update the Critic model ---------
mse = keras.losses.MeanSquaredError()
with tf.GradientTape() as critic_tape:
target_actions = target_agent.act(next_states)
target_q_values = target_agent.get_q_values(next_states, target_actions)
y = rewards + self.gamma * terminals * target_q_values
Qs = self.get_q_values(states, actions)
critic_loss = mse(y, Qs)
critic_gradients = critic_tape.gradient(critic_loss, self.critic.trainable_variables)
self.critic.optimize(critic_gradients)
# --------- Update the Actor model ---------
with tf.GradientTape() as tape:
actions = self.act(states)
# multiply by -1 to minimize the negative value, same as maximizing it
q_values = -1 * tf.reduce_mean(self.get_q_values(state=states, action=actions))
dq_dtheta = tape.gradient(q_values, self.actor.trainable_variables)
self.actor.optimize(dq_dtheta)
return critic_loss, y, Qs
def learn(self, target_agent):
"""
Perform optimization step to update both the actor and critic model parameters.
Args:
target_agent: Agent object
Target agent.
Returns:
loss: numpy.ndarray
The q-value loss between agent and target models on minibatch of samples.
"""
states, actions, rewards, next_states, terminals = self.memory.sample()
loss, y, Qs = self._learn(states, actions, rewards, next_states, terminals, target_agent)
loss = loss.numpy()
y = tf.reduce_mean(y).numpy()
Qs = tf.reduce_mean(Qs).numpy()
return loss, y, Qs
def save_model(self, model_name):
"""
Save the model weights to a file in the ./trained_models forlder.
Both actor and critic weights are stored separately into the folder.
Args:
model_name: str
Name to save model to.
Returns:
None
"""
filepath = f'trained_models/{model_name}'
self.actor.save_weights(filepath+"/actor_weights/")
self.critic.save_weights(filepath + "/critic_weights/")
def load_model(self, model_name):
"""
Load a saved model from the `./trained_models`.
Returns:
None
"""
filepath = f"trained_models/{model_name}"
self.actor.load_weights(filepath+"/actor_weights/").expect_partial()
self.critic.load_weights(filepath + "/critic_weights/").expect_partial()
# =============== END OF FILE ===============