-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
422 lines (345 loc) · 16.5 KB
/
utils.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import numpy as np
from rl_glue import RLGlue
from environment import BaseEnvironment
from lunar_lander import LunarLanderEnvironment
from agent import BaseAgent
from copy import deepcopy
class ActionValueNetwork:
def __init__(self, network_config):
self.state_dim = network_config.get("state_dim")
self.num_hidden_units = network_config.get("num_hidden_units")
self.num_actions = network_config.get("num_actions")
self.rand_generator = np.random.RandomState(network_config.get("seed"))
# neural network with 3 layers (input, hidden, output)
self.layer_sizes = [self.state_dim, self.num_hidden_units, self.num_actions]
# weights are initialized randomly, biases are initialized to zero
self.weights = [dict() for i in range(0, len(self.layer_sizes) - 1)]
for i in range(0, len(self.layer_sizes) - 1):
self.weights[i]['W'] = self.init_saxe(self.layer_sizes[i], self.layer_sizes[i + 1])
self.weights[i]['b'] = np.zeros((1, self.layer_sizes[i + 1]))
def get_action_values(self, s):
"""
Args:
s (Numpy array): The state.
Returns:
The action-values (Numpy array) calculated using the network's weights (forward pass).
"""
W0, b0 = self.weights[0]['W'], self.weights[0]['b']
psi = np.dot(s, W0) + b0
x = np.maximum(psi, 0)
W1, b1 = self.weights[1]['W'], self.weights[1]['b']
q_vals = np.dot(x, W1) + b1
return q_vals
def get_TD_update(self, s, delta_mat):
"""
Args:
s (Numpy array): The state.
delta_mat (Numpy array): A 2D array of shape (batch_size, num_actions). Each row of delta_mat
correspond to one state in the batch. Each row has only one non-zero element
which is the TD-error corresponding to the action taken.
Returns:
The TD update (Array of dictionaries with gradient times TD errors) for the network's weights (backpropagation).
"""
W0, b0 = self.weights[0]['W'], self.weights[0]['b']
W1, b1 = self.weights[1]['W'], self.weights[1]['b']
psi = np.dot(s, W0) + b0
x = np.maximum(psi, 0)
dx = (psi > 0).astype(float)
td_update = [dict() for i in range(len(self.weights))]
v = delta_mat
td_update[1]['W'] = np.dot(x.T, v) * 1. / s.shape[0]
td_update[1]['b'] = np.sum(v, axis=0, keepdims=True) * 1. / s.shape[0]
v = np.dot(v, W1.T) * dx
td_update[0]['W'] = np.dot(s.T, v) * 1. / s.shape[0]
td_update[0]['b'] = np.sum(v, axis=0, keepdims=True) * 1. / s.shape[0]
return td_update
def init_saxe(self, rows, cols):
"""
Args:
rows (int): number of input units for layer.
cols (int): number of output units for layer.
Returns:
NumPy Array consisting of weights for the layer based on the initialization in Saxe et al.
"""
tensor = self.rand_generator.normal(0, 1, (rows, cols))
if rows < cols:
tensor = tensor.T
tensor, r = np.linalg.qr(tensor)
d = np.diag(r, 0)
ph = np.sign(d)
tensor *= ph
if rows < cols:
tensor = tensor.T
return tensor
def get_weights(self):
"""
Returns:
A copy of the current weights of this network.
"""
return deepcopy(self.weights)
def set_weights(self, weights):
"""
Args:
weights (list of dictionaries): Consists of weights that this network will set as its own weights.
"""
self.weights = deepcopy(weights)
# manual implementtation of the Adam optimizer
class Adam():
def __init__(self, layer_sizes,
optimizer_info):
self.layer_sizes = layer_sizes
# specify Adam algorithm's hyper parameters
self.step_size = optimizer_info.get("step_size")
self.beta_m = optimizer_info.get("beta_m")
self.beta_v = optimizer_info.get("beta_v")
self.epsilon = optimizer_info.get("epsilon")
# initialize Adam algorithm's m and v
self.m = [dict() for i in range(1, len(self.layer_sizes))]
self.v = [dict() for i in range(1, len(self.layer_sizes))]
for i in range(0, len(self.layer_sizes) - 1):
self.m[i]["W"] = np.zeros((self.layer_sizes[i], self.layer_sizes[i+1]))
self.m[i]["b"] = np.zeros((1, self.layer_sizes[i+1]))
self.v[i]["W"] = np.zeros((self.layer_sizes[i], self.layer_sizes[i+1]))
self.v[i]["b"] = np.zeros((1, self.layer_sizes[i+1]))
# used to store the powers of beta_m and beta_v
self.beta_m_product = self.beta_m
self.beta_v_product = self.beta_v
def update_weights(self, weights, td_errors_times_gradients):
"""
Args:
weights (Array of dictionaries): The weights of the neural network.
td_errors_times_gradients (Array of dictionaries): The gradient of the
action-values with respect to the network's weights times the TD-error
Returns:
The updated weights (Array of dictionaries).
"""
for i in range(len(weights)):
for param in weights[i].keys():
self.m[i][param] = self.beta_m*self.m[i][param] + (1-self.beta_m)*td_errors_times_gradients[i][param]
self.v[i][param] = self.beta_v*self.v[i][param] + (1-self.beta_v)*td_errors_times_gradients[i][param]**2
m_hat = self.m[i][param]/(1-self.beta_m_product)
v_hat = self.v[i][param]/(1-self.beta_v_product)
weight_update = self.step_size/(np.sqrt(v_hat)+self.epsilon)*m_hat
weights[i][param] = weights[i][param] + weight_update
self.beta_m_product *= self.beta_m
self.beta_v_product *= self.beta_v
return weights
# experience replay is implemented to be more data efficient
class ReplayBuffer:
def __init__(self, size, minibatch_size, seed):
"""
Args:
size (integer): The size of the replay buffer.
minibatch_size (integer): The sample size.
seed (integer): The seed for the random number generator.
"""
self.buffer = []
self.minibatch_size = minibatch_size
self.rand_generator = np.random.RandomState(seed)
self.max_size = size
def append(self, state, action, reward, terminal, next_state):
"""
Args:
state (Numpy array): The state.
action (integer): The action.
reward (float): The reward.
terminal (integer): 1 if the next state is a terminal state and 0 otherwise.
next_state (Numpy array): The next state.
"""
if len(self.buffer) == self.max_size:
del self.buffer[0]
self.buffer.append([state, action, reward, terminal, next_state])
def sample(self):
"""
Returns:
A list of transition tuples including state, action, reward, terinal, and next_state
"""
idxs = self.rand_generator.choice(np.arange(len(self.buffer)), size=self.minibatch_size)
return [self.buffer[idx] for idx in idxs]
def size(self):
return len(self.buffer)
# manual implementation of the softmax policy
def softmax(action_values, tau=1.0):
"""
Args:
action_values (Numpy array): A 2D array of shape (batch_size, num_actions).
The action-values computed by an action-value network.
tau (float): The temperature parameter scalar.
Returns:
A 2D array of shape (batch_size, num_actions). Where each column is a probability distribution over
the actions representing the policy.
"""
preferences = action_values/tau
max_preference = np.max(preferences, axis=1)
reshaped_max_preference = max_preference.reshape((-1, 1))
# the max is subtracted to avoid overflows in exponentiation
exp_preferences = np.exp(preferences - reshaped_max_preference)
sum_of_exp_preferences = np.sum(exp_preferences, axis=1)
reshaped_sum_of_exp_preferences = sum_of_exp_preferences.reshape((-1, 1))
action_probs = exp_preferences/reshaped_sum_of_exp_preferences
action_probs = action_probs.squeeze()
return action_probs
# compute td_error for expèected sarsa update
def get_td_error(states, next_states, actions, rewards, discount, terminals, network, current_q, tau):
"""
Args:
states (Numpy array): The batch of states with the shape (batch_size, state_dim).
next_states (Numpy array): The batch of next states with the shape (batch_size, state_dim).
actions (Numpy array): The batch of actions with the shape (batch_size,).
rewards (Numpy array): The batch of rewards with the shape (batch_size,).
discount (float): The discount factor.
terminals (Numpy array): The batch of terminals with the shape (batch_size,).
network (ActionValueNetwork): The latest state of the network that is getting replay updates.
current_q (ActionValueNetwork): The fixed network used for computing the targets,
and particularly, the action-values at the next-states.
Returns:
The TD errors (Numpy array) for actions taken, of shape (batch_size,)
"""
# action-values for fixed network at next states
q_next_mat = current_q.get_action_values(next_states)
probs_mat = softmax(q_next_mat, tau)
v_next_vec = np.sum(probs_mat*q_next_mat, axis=1)*(1-terminals)
# expected sarsa target
target_vec = rewards + discount*v_next_vec
# action-values for updated network at cuurent states
q_mat = network.get_action_values(states)
batch_indices = np.arange(q_mat.shape[0])
q_vec = q_mat[batch_indices, actions]
# td errors
delta_vec = target_vec - q_vec
return delta_vec
# perform Adam optimizer step
def optimize_network(experiences, discount, optimizer, network, current_q, tau):
"""
Args:
experiences (Numpy array): The batch of experiences including the states, actions,
rewards, terminals, and next_states.
discount (float): The discount factor.
network (ActionValueNetwork): The latest state of the network that is getting replay updates.
current_q (ActionValueNetwork): The fixed network used for computing the targets,
and particularly, the action-values at the next-states.
"""
# get states, action, rewards, terminals, and next_states from experiences
states, actions, rewards, terminals, next_states = map(list, zip(*experiences))
states = np.concatenate(states)
next_states = np.concatenate(next_states)
rewards = np.array(rewards)
terminals = np.array(terminals)
batch_size = states.shape[0]
# compute TD error using the get_td_error function
delta_vec = get_td_error(states, next_states, actions, rewards, discount, terminals, network, current_q, tau)
batch_indices = np.arange(batch_size)
# cast to matrix of shape (batch_size, num_actions)
delta_mat = np.zeros((batch_size, network.num_actions))
delta_mat[batch_indices, actions] = delta_vec
# pass delta_mat to compute the TD errors times the gradients of the network's weights from back-propagation
td_update = network.get_TD_update(states, delta_mat)
# pass network.get_weights and the td_update to the optimizer to get updated weights
weights = optimizer.update_weights(network.get_weights(), td_update)
network.set_weights(weights)
# the rl_glue agent
class Agent(BaseAgent):
def __init__(self):
self.name = "expected_sarsa_agent"
def agent_init(self, agent_config):
"""Setup for the agent called when the experiment first starts.
Set parameters needed to setup the agent.
Assume agent_config dict contains:
{
network_config: dictionary,
optimizer_config: dictionary,
replay_buffer_size: integer,
minibatch_sz: integer,
num_replay_updates_per_step: float
discount_factor: float,
}
"""
self.replay_buffer = ReplayBuffer(agent_config['replay_buffer_size'],
agent_config['minibatch_sz'], agent_config.get("seed"))
self.network = ActionValueNetwork(agent_config['network_config'])
self.optimizer = Adam(self.network.layer_sizes, agent_config["optimizer_config"])
self.num_actions = agent_config['network_config']['num_actions']
self.num_replay = agent_config['num_replay_updates_per_step']
self.discount = agent_config['gamma']
self.tau = agent_config['tau']
self.rand_generator = np.random.RandomState(agent_config.get("seed"))
self.last_state = None
self.last_action = None
self.sum_rewards = 0
self.episode_steps = 0
def policy(self, state):
"""
Args:
state (Numpy array): the state.
Returns:
the action.
"""
action_values = self.network.get_action_values(state)
probs_batch = softmax(action_values, self.tau)
action = self.rand_generator.choice(self.num_actions, p=probs_batch.squeeze())
return action
def agent_start(self, state):
"""The first method called when the experiment starts, called after
the environment starts.
Args:
state (Numpy array): the state from the
environment's evn_start function.
Returns:
The first action the agent takes.
"""
self.sum_rewards = 0
self.episode_steps = 0
self.last_state = np.array([state])
self.last_action = self.policy(self.last_state)
return self.last_action
def agent_step(self, reward, state):
"""A step taken by the agent.
Args:
reward (float): the reward received for taking the last action taken
state (Numpy array): the state from the
environment's step based, where the agent ended up after the
last step
Returns:
The action the agent is taking.
"""
self.sum_rewards += reward
self.episode_steps += 1
state = np.array([state])
action = self.policy(state)
# append new experience to the replay buffer
self.replay_buffer.append(self.last_state, self.last_action, reward, False, state)
# perform replay steps:
if self.replay_buffer.size() > self.replay_buffer.minibatch_size:
current_q = deepcopy(self.network)
for _ in range(self.num_replay):
# get sample experiences from the replay buffer
experiences = self.replay_buffer.sample()
# call optimize_network to update the weights of the network
optimize_network(experiences, self.discount, self.optimizer, self.network, current_q, self.tau)
self.last_state = state
self.last_action = action
return action
def agent_end(self, reward):
"""Run when the agent terminates.
Args:
reward (float): the reward the agent received for entering the
terminal state.
"""
self.sum_rewards += reward
self.episode_steps += 1
# set terminal state to an array of zeros
state = np.zeros_like(self.last_state)
self.replay_buffer.append(self.last_state, self.last_action, reward, True, state)
# perform replay steps:
if self.replay_buffer.size() > self.replay_buffer.minibatch_size:
current_q = deepcopy(self.network)
for _ in range(self.num_replay):
# Get sample experiences from the replay buffer
experiences = self.replay_buffer.sample()
# call optimize_network to update the weights of the network
optimize_network(experiences, self.discount, self.optimizer, self.network, current_q, self.tau)
def agent_message(self, message):
if message == "get_sum_reward":
return self.sum_rewards
else:
raise Exception("Unrecognized Message!")