-
Notifications
You must be signed in to change notification settings - Fork 17
/
tests.py
executable file
·341 lines (268 loc) · 13.7 KB
/
tests.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
#!/usr/bin/env python
import unittest
import numpy as np
import torch
import torch.nn as nn
from models import EnsembleDenseLayer, Model
from buffer import Buffer
from normalizer import TransitionNormalizer
from utilities import JensenRenyiDivergenceUtilityMeasure, SlowJensenRenyiDivergenceUtilityMeasure
from time import time
class TestEnsembleDenseLayer(unittest.TestCase):
def setUp(self):
self.ensemble_size = 2
self.n_in = 5
self.n_out = 5
self.batch_size = 7
self.layer = EnsembleDenseLayer(n_in=self.n_in, n_out=self.n_out, ensemble_size=self.ensemble_size)
self.activation = lambda x: np.where(x > 0, x, x * 0.01)
def test_output_shape(self):
x = torch.rand(self.ensemble_size, self.batch_size, self.n_in)
y = self.layer(x)
self.assertEqual(y.size(0), self.ensemble_size)
self.assertEqual(y.size(1), self.batch_size)
self.assertEqual(y.size(2), self.n_out)
def test_identity_weights(self):
for w in self.layer.weights:
nn.init.eye_(w)
x = torch.rand(self.ensemble_size, self.batch_size, self.n_in)
y = self.layer(x)
y = y.detach().numpy()
y_true = self.activation(x.numpy())
self.assertTrue(np.allclose(y_true, y))
def test_bias(self):
for idx, (w, b) in enumerate(zip(self.layer.weights, self.layer.biases)):
nn.init.constant_(w, 0)
nn.init.constant_(b, idx)
x = torch.rand(self.ensemble_size, self.batch_size, self.n_in)
y = self.layer(x)
y = y.detach().numpy()
for idx, y_element in enumerate(y):
y_true_element = self.activation(idx) * np.ones((self.batch_size, self.n_out))
self.assertTrue(np.allclose(y_true_element, y_element))
def test_bias_and_weights(self):
for b in self.layer.biases:
b.normal_(0, 1)
x = torch.rand(self.ensemble_size, self.batch_size, self.n_in)
y = self.layer(x)
y = y.detach().numpy()
for x_element, y_element, w, b in zip(x, y, self.layer.weights, self.layer.biases):
y_true_element = np.dot(x_element, w.detach().numpy())
y_true_element += b.detach().numpy()
y_true_element = self.activation(y_true_element)
self.assertTrue(np.allclose(y_true_element, y_element))
class TestModel(unittest.TestCase):
def setUp(self):
self.ensemble_size = 5
self.d_action = 3
self.d_state = 5
self.n_hidden = 16
self.n_layers = 4
self.batch_size = 7
self.model = Model(d_action=self.d_action,
d_state=self.d_state,
n_hidden=self.n_hidden,
n_layers=self.n_layers,
ensemble_size=self.ensemble_size)
self.states = torch.rand(self.ensemble_size, self.batch_size, self.d_state)
self.actions = torch.rand(self.ensemble_size, self.batch_size, self.d_action)
def test_model_output_shape(self):
next_state_means, next_state_vars = self.model(self.states, self.actions)
self.assertEqual(next_state_means.size(0), self.ensemble_size)
self.assertEqual(next_state_means.size(1), self.batch_size)
self.assertEqual(next_state_means.size(2), self.d_state)
self.assertEqual(next_state_vars.size(0), self.ensemble_size)
self.assertEqual(next_state_vars.size(1), self.batch_size)
self.assertEqual(next_state_vars.size(2), self.d_state)
def test_model_sample_shape(self):
next_state_samples = self.model.sample(self.states, self.states)
assert next_state_samples.size(0) == self.ensemble_size
assert next_state_samples.size(1) == self.batch_size
assert next_state_samples.size(2) == self.d_state
class TestBufferBasic(unittest.TestCase):
def setUp(self):
self.n_samples = 10
self.d_state = 3
self.d_action = 2
self.buffer_size = 10
self.batch_size = 4
self.ensemble_size = 3
self.buf = Buffer(d_state=self.d_state,
d_action=self.d_action,
buffer_size=self.buffer_size,
ensemble_size=self.ensemble_size)
self.samples = [(np.random.random(self.d_state),
np.random.random(self.d_action),
np.random.random(self.d_state)) for _ in range(self.n_samples)]
for state, action, next_state in self.samples:
self.buf.add(state, action, next_state)
def test_insertion(self):
for i, (state, action, next_state) in enumerate(self.samples):
self.assertTrue(np.allclose(self.buf.states[i], state))
self.assertTrue(np.allclose(self.buf.actions[i], action))
self.assertTrue(np.allclose(self.buf.state_deltas[i], next_state - state))
def test_sampling_size(self):
for states, actions, state_deltas in self.buf.train_batches(batch_size=self.batch_size):
self.assertEqual(states.shape[0], self.ensemble_size)
self.assertEqual(states.shape[1], self.batch_size)
self.assertEqual(states.shape[2], self.d_state)
self.assertEqual(actions.shape[0], self.ensemble_size)
self.assertEqual(actions.shape[1], self.batch_size)
self.assertEqual(actions.shape[2], self.d_action)
self.assertEqual(state_deltas.shape[0], self.ensemble_size)
self.assertEqual(state_deltas.shape[1], self.batch_size)
self.assertEqual(state_deltas.shape[2], self.d_state)
break
def test_sampling(self):
for e_state, e_action, e_state_delta in self.buf.train_batches(batch_size=3):
for b_state, b_action, b_state_delta in zip(e_state, e_action, e_state_delta):
for s_state, s_action, s_state_delta in zip(b_state, b_action, b_state_delta):
found = False
for state, action, next_state in self.samples:
if np.allclose(s_state, state) and np.allclose(s_action, action) and np.allclose(s_state_delta, next_state - state):
found = True
break
assert found
class TestBufferReplacement(unittest.TestCase):
def test_complete_replace_once(self):
n_samples = 10
d_state = 3
d_action = 2
buffer_size = 5
ensemble_size = 5
buf = Buffer(d_state=d_state,
d_action=d_action,
buffer_size=buffer_size,
ensemble_size=ensemble_size)
samples = [(np.random.random(d_state),
np.random.random(d_action),
np.random.random(d_state)) for _ in range(n_samples)]
for state, action, next_state in samples:
buf.add(state, action, next_state)
for i, (state, action, next_state) in enumerate(samples[-buffer_size:]):
self.assertTrue(np.allclose(buf.states[i], state))
self.assertTrue(np.allclose(buf.actions[i], action))
self.assertTrue(np.allclose(buf.state_deltas[i], next_state - state))
def test_complete_replace_twice(self):
n_samples = 9
d_state = 3
d_action = 2
buffer_size = 3
ensemble_size = 5
buf = Buffer(d_state=d_state,
d_action=d_action,
buffer_size=buffer_size,
ensemble_size=ensemble_size)
samples = [(np.random.random(d_state),
np.random.random(d_action),
np.random.random(d_state)) for _ in range(n_samples)]
for state, action, next_state in samples:
buf.add(state, action, next_state)
for i, (state, action, next_state) in enumerate(samples[-buffer_size:]):
self.assertTrue(np.allclose(buf.states[i], state))
self.assertTrue(np.allclose(buf.actions[i], action))
self.assertTrue(np.allclose(buf.state_deltas[i], next_state - state))
def test_partial_replacement(self):
n_samples = 17
d_state = 3
d_action = 2
buffer_size = 7
ensemble_size = 3
buf = Buffer(d_state=d_state,
d_action=d_action,
buffer_size=buffer_size,
ensemble_size=ensemble_size)
samples = [(np.random.random(d_state),
np.random.random(d_action),
np.random.random(d_state)) for _ in range(n_samples)]
for state, action, next_state in samples:
buf.add(state, action, next_state)
r = n_samples % buffer_size
for i, (state, action, next_state) in enumerate(samples[-r:]):
self.assertTrue(np.allclose(buf.states[i], state))
self.assertTrue(np.allclose(buf.actions[i], action))
self.assertTrue(np.allclose(buf.state_deltas[i], next_state - state))
class TestNormalizer(unittest.TestCase):
def setUp(self):
self.n_samples = 1000
self.d_state = 10
self.d_action = 5
self.normalizer = TransitionNormalizer()
self.states = [np.random.random(self.d_state) for _ in range(self.n_samples)]
self.actions = [np.random.random(self.d_action) for _ in range(self.n_samples)]
self.next_states = [np.random.random(self.d_state) for _ in range(self.n_samples)]
self.state_deltas = [next_state - state for state, next_state in zip(self.next_states, self.states)]
for state, action, state_delta in zip(self.states, self.actions, self.state_deltas):
state, action, state_delta = torch.from_numpy(state).float().clone(), torch.from_numpy(
action).float().clone(), torch.from_numpy(state_delta).float().clone()
self.normalizer.update(state, action, state_delta)
def test_stats(self):
self.assertTrue(np.allclose(np.array(self.states).mean(axis=0), self.normalizer.state_mean))
self.assertTrue(np.allclose(np.array(self.actions).mean(axis=0), self.normalizer.action_mean))
self.assertTrue(np.allclose(np.array(self.state_deltas).mean(axis=0), self.normalizer.state_delta_mean))
self.assertTrue(np.allclose(np.array(self.states).std(axis=0), self.normalizer.state_stdev))
self.assertTrue(np.allclose(np.array(self.actions).std(axis=0), self.normalizer.action_stdev))
self.assertTrue(np.allclose(np.array(self.state_deltas).std(axis=0), self.normalizer.state_delta_stdev))
def test_tensor_shape_handling(self):
x = torch.rand(self.d_state)
a = self.normalizer.normalize_states(x)
y = x.clone()
y = y.unsqueeze(0).unsqueeze(0).unsqueeze(0)
b = self.normalizer.normalize_states(y)
self.assertTrue(np.allclose(a, b))
class FastRenyi(unittest.TestCase):
def setUp(self):
self.n_pl = 2
self.n_tr = 3
self.es = 5
self.d_s = 7
self.d_a = 11
self.n_runs = 13
self.device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
self.model = Model(d_action=self.d_a,
d_state=self.d_s,
ensemble_size=self.es,
n_hidden=32,
n_layers=3,
device=self.device)
self.slow = SlowJensenRenyiDivergenceUtilityMeasure(action_norm_penalty=0)
self.fast = JensenRenyiDivergenceUtilityMeasure(action_norm_penalty=0)
def test_fast_slow_match(self):
for _ in range(self.n_runs):
states = torch.rand(self.n_pl, self.d_s).to(self.device)
actions = torch.rand(self.n_pl, self.d_a).to(self.device)
next_states = torch.rand(self.n_pl, self.d_s).to(self.device)
next_state_mu = torch.rand(self.n_pl, self.es, self.d_s).to(self.device)
next_state_var = torch.rand(self.n_pl, self.es, self.d_s).to(self.device)
with torch.no_grad():
slow_out = self.slow(states, actions, next_states, next_state_mu, next_state_var, self.model)
fast_out = self.fast(states, actions, next_states, next_state_mu, next_state_var, self.model)
self.assertTrue(torch.allclose(slow_out, fast_out, atol=1e-6))
def test_speed(self):
tick = time()
for _ in range(self.n_runs):
states = torch.rand(self.n_pl, self.d_s).to(self.device)
actions = torch.rand(self.n_pl, self.d_a).to(self.device)
next_states = torch.rand(self.n_pl, self.d_s).to(self.device)
next_state_mu = torch.rand(self.n_pl, self.es, self.d_s).to(self.device)
next_state_var = torch.rand(self.n_pl, self.es, self.d_s).to(self.device)
with torch.no_grad():
self.slow(states, actions, next_states, next_state_mu, next_state_var, self.model)
tock = time()
slow_time = tock - tick
print(f"slow renyi divergence calculation time taken for {self.n_runs} calls: {slow_time}")
tick = time()
for _ in range(self.n_runs):
states = torch.rand(self.n_pl, self.d_s).to(self.device)
actions = torch.rand(self.n_pl, self.d_a).to(self.device)
next_states = torch.rand(self.n_pl, self.d_s).to(self.device)
next_state_mu = torch.rand(self.n_pl, self.es, self.d_s).to(self.device)
next_state_var = torch.rand(self.n_pl, self.es, self.d_s).to(self.device)
with torch.no_grad():
self.fast(states, actions, next_states, next_state_mu, next_state_var, self.model)
tock = time()
fast_time = tock - tick
print(f"fast renyi divergence calculation time taken for {self.n_runs} calls: {fast_time}")
print(f"speedup: {slow_time / fast_time}")
if __name__ == '__main__':
unittest.main()