-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_td.py
More file actions
executable file
·380 lines (346 loc) · 15.5 KB
/
Copy pathtest_td.py
File metadata and controls
executable file
·380 lines (346 loc) · 15.5 KB
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
import pytest
import torch
from ding.rl_utils import q_nstep_td_data, q_nstep_td_error, q_1step_td_data, q_1step_td_error, td_lambda_data,\
td_lambda_error, q_nstep_td_error_with_rescale, dist_1step_td_data, dist_1step_td_error, dist_nstep_td_data,\
dqfd_nstep_td_data, dqfd_nstep_td_error, dist_nstep_td_error, v_1step_td_data, v_1step_td_error, v_nstep_td_data,\
v_nstep_td_error, q_nstep_sql_td_error, iqn_nstep_td_data, iqn_nstep_td_error, qrdqn_nstep_td_data,\
qrdqn_nstep_td_error
from ding.rl_utils.td import shape_fn_dntd, shape_fn_qntd, shape_fn_td_lambda, shape_fn_qntd_rescale
@pytest.mark.unittest
def test_q_nstep_td():
batch_size = 4
action_dim = 3
next_q = torch.randn(batch_size, action_dim)
done = torch.randn(batch_size)
action = torch.randint(0, action_dim, size=(batch_size, ))
next_action = torch.randint(0, action_dim, size=(batch_size, ))
for nstep in range(1, 10):
q = torch.randn(batch_size, action_dim).requires_grad_(True)
reward = torch.rand(nstep, batch_size)
data = q_nstep_td_data(q, next_q, action, next_action, reward, done, None)
loss, td_error_per_sample = q_nstep_td_error(data, 0.95, nstep=nstep)
assert td_error_per_sample.shape == (batch_size, )
assert loss.shape == ()
assert q.grad is None
loss.backward()
assert isinstance(q.grad, torch.Tensor)
data = q_nstep_td_data(q, next_q, action, next_action, reward, done, None)
loss, td_error_per_sample = q_nstep_td_error(data, 0.95, nstep=nstep, cum_reward=True)
value_gamma = torch.tensor(0.9)
data = q_nstep_td_data(q, next_q, action, next_action, reward, done, None)
loss, td_error_per_sample = q_nstep_td_error(data, 0.95, nstep=nstep, cum_reward=True, value_gamma=value_gamma)
loss.backward()
assert isinstance(q.grad, torch.Tensor)
@pytest.mark.unittest
def test_dist_1step_td():
batch_size = 4
action_dim = 3
n_atom = 51
v_min = -10.0
v_max = 10.0
dist = torch.randn(batch_size, action_dim, n_atom).abs().requires_grad_(True)
next_dist = torch.randn(batch_size, action_dim, n_atom).abs()
done = torch.randn(batch_size)
action = torch.randint(0, action_dim, size=(batch_size, ))
next_action = torch.randint(0, action_dim, size=(batch_size, ))
reward = torch.randn(batch_size)
data = dist_1step_td_data(dist, next_dist, action, next_action, reward, done, None)
loss = dist_1step_td_error(data, 0.95, v_min, v_max, n_atom)
assert loss.shape == ()
assert dist.grad is None
loss.backward()
assert isinstance(dist.grad, torch.Tensor)
@pytest.mark.unittest
def test_q_1step_compatible():
batch_size = 4
action_dim = 3
next_q = torch.randn(batch_size, action_dim)
done = torch.randn(batch_size)
action = torch.randint(0, action_dim, size=(batch_size, ))
next_action = torch.randint(0, action_dim, size=(batch_size, ))
q = torch.randn(batch_size, action_dim).requires_grad_(True)
reward = torch.rand(batch_size)
nstep_data = q_nstep_td_data(q, next_q, action, next_action, reward.unsqueeze(0), done, None)
onestep_data = q_1step_td_data(q, next_q, action, next_action, reward, done, None)
nstep_loss, _ = q_nstep_td_error(nstep_data, 0.99, nstep=1)
onestep_loss = q_1step_td_error(onestep_data, 0.99)
assert pytest.approx(nstep_loss.item(), onestep_loss.item())
@pytest.mark.unittest
def test_dist_nstep_td():
batch_size = 4
action_dim = 3
n_atom = 51
v_min = -10.0
v_max = 10.0
nstep = 5
dist = torch.randn(batch_size, action_dim, n_atom).abs().requires_grad_(True)
next_n_dist = torch.randn(batch_size, action_dim, n_atom).abs()
done = torch.randn(batch_size)
action = torch.randint(0, action_dim, size=(batch_size, ))
next_action = torch.randint(0, action_dim, size=(batch_size, ))
reward = torch.randn(nstep, batch_size)
data = dist_nstep_td_data(dist, next_n_dist, action, next_action, reward, done, None)
loss, _ = dist_nstep_td_error(data, 0.95, v_min, v_max, n_atom, nstep)
assert loss.shape == ()
assert dist.grad is None
loss.backward()
assert isinstance(dist.grad, torch.Tensor)
weight = torch.tensor([0.9])
value_gamma = torch.tensor(0.9)
data = dist_nstep_td_data(dist, next_n_dist, action, next_action, reward, done, weight)
loss, _ = dist_nstep_td_error(data, 0.95, v_min, v_max, n_atom, nstep, value_gamma)
assert loss.shape == ()
loss.backward()
assert isinstance(dist.grad, torch.Tensor)
@pytest.mark.unittest
def test_q_nstep_td_with_rescale():
batch_size = 4
action_dim = 3
next_q = torch.randn(batch_size, action_dim)
done = torch.randn(batch_size)
action = torch.randint(0, action_dim, size=(batch_size, ))
next_action = torch.randint(0, action_dim, size=(batch_size, ))
for nstep in range(1, 10):
q = torch.randn(batch_size, action_dim).requires_grad_(True)
reward = torch.rand(nstep, batch_size)
data = q_nstep_td_data(q, next_q, action, next_action, reward, done, None)
loss, _ = q_nstep_td_error_with_rescale(data, 0.95, nstep=nstep)
assert loss.shape == ()
assert q.grad is None
loss.backward()
assert isinstance(q.grad, torch.Tensor)
print(loss)
@pytest.mark.unittest
def test_qrdqn_nstep_td():
batch_size = 4
action_dim = 3
tau = 3
next_q = torch.randn(batch_size, action_dim, tau)
done = torch.randn(batch_size)
action = torch.randint(0, action_dim, size=(batch_size, ))
next_action = torch.randint(0, action_dim, size=(batch_size, ))
for nstep in range(1, 10):
q = torch.randn(batch_size, action_dim, tau).requires_grad_(True)
reward = torch.rand(nstep, batch_size)
data = qrdqn_nstep_td_data(q, next_q, action, next_action, reward, done, tau, None)
loss, td_error_per_sample = qrdqn_nstep_td_error(data, 0.95, nstep=nstep)
assert td_error_per_sample.shape == (batch_size, )
assert loss.shape == ()
assert q.grad is None
loss.backward()
assert isinstance(q.grad, torch.Tensor)
loss, td_error_per_sample = qrdqn_nstep_td_error(data, 0.95, nstep=nstep, value_gamma=torch.tensor(0.9))
assert td_error_per_sample.shape == (batch_size, )
@pytest.mark.unittest
def test_dist_1step_compatible():
batch_size = 4
action_dim = 3
n_atom = 51
v_min = -10.0
v_max = 10.0
dist = torch.randn(batch_size, action_dim, n_atom).abs().requires_grad_(True)
next_dist = torch.randn(batch_size, action_dim, n_atom).abs()
done = torch.randn(batch_size)
action = torch.randint(0, action_dim, size=(batch_size, ))
next_action = torch.randint(0, action_dim, size=(batch_size, ))
reward = torch.randn(batch_size)
onestep_data = dist_1step_td_data(dist, next_dist, action, next_action, reward, done, None)
nstep_data = dist_nstep_td_data(dist, next_dist, action, next_action, reward.unsqueeze(0), done, None)
onestep_loss = dist_1step_td_error(onestep_data, 0.95, v_min, v_max, n_atom)
nstep_loss, _ = dist_nstep_td_error(nstep_data, 0.95, v_min, v_max, n_atom, nstep=1)
assert pytest.approx(nstep_loss.item(), onestep_loss.item())
@pytest.mark.unittest
def test_td_lambda():
T, B = 8, 4
value = torch.randn(T + 1, B).requires_grad_(True)
reward = torch.rand(T, B)
loss = td_lambda_error(td_lambda_data(value, reward, None))
assert loss.shape == ()
assert value.grad is None
loss.backward()
assert isinstance(value.grad, torch.Tensor)
@pytest.mark.unittest
def test_v_1step_td():
batch_size = 5
v = torch.randn(batch_size).requires_grad_(True)
next_v = torch.randn(batch_size)
reward = torch.rand(batch_size)
done = torch.zeros(batch_size)
data = v_1step_td_data(v, next_v, reward, done, None)
loss, td_error_per_sample = v_1step_td_error(data, 0.99)
assert loss.shape == ()
assert v.grad is None
loss.backward()
assert isinstance(v.grad, torch.Tensor)
data = v_1step_td_data(v, next_v, reward, None, None)
loss, td_error_per_sample = v_1step_td_error(data, 0.99)
loss.backward()
assert isinstance(v.grad, torch.Tensor)
@pytest.mark.unittest
def test_v_nstep_td():
batch_size = 5
v = torch.randn(batch_size).requires_grad_(True)
next_v = torch.randn(batch_size)
reward = torch.rand(5, batch_size)
done = torch.zeros(batch_size)
data = v_nstep_td_data(v, next_v, reward, done, 0.9, 0.99)
loss, td_error_per_sample = v_nstep_td_error(data, 0.99, 5)
assert loss.shape == ()
assert v.grad is None
loss.backward()
assert isinstance(v.grad, torch.Tensor)
data = v_nstep_td_data(v, next_v, reward, done, None, 0.99)
loss, td_error_per_sample = v_nstep_td_error(data, 0.99, 5)
loss.backward()
assert isinstance(v.grad, torch.Tensor)
@pytest.mark.unittest
def test_dqfd_nstep_td():
batch_size = 4
action_dim = 3
next_q = torch.randn(batch_size, action_dim)
done = torch.randn(batch_size)
done_1 = torch.randn(batch_size)
next_q_one_step = torch.randn(batch_size, action_dim)
action = torch.randint(0, action_dim, size=(batch_size, ))
next_action = torch.randint(0, action_dim, size=(batch_size, ))
next_action_one_step = torch.randint(0, action_dim, size=(batch_size, ))
is_expert = torch.ones((batch_size))
for nstep in range(1, 10):
q = torch.randn(batch_size, action_dim).requires_grad_(True)
reward = torch.rand(nstep, batch_size)
data = dqfd_nstep_td_data(
q, next_q, action, next_action, reward, done, done_1, None, next_q_one_step, next_action_one_step, is_expert
)
loss, td_error_per_sample = dqfd_nstep_td_error(
data, 0.95, lambda1=(1, ), lambda2=(1, ), margin_function=0.8, nstep=nstep
)
assert td_error_per_sample.shape == (batch_size, )
assert loss.shape == ()
assert q.grad is None
loss.backward()
assert isinstance(q.grad, torch.Tensor)
print(loss)
@pytest.mark.unittest
def test_q_nstep_sql_td():
batch_size = 4
action_dim = 3
next_q = torch.randn(batch_size, action_dim)
done = torch.randn(batch_size)
action = torch.randint(0, action_dim, size=(batch_size, ))
next_action = torch.randint(0, action_dim, size=(batch_size, ))
for nstep in range(1, 10):
q = torch.randn(batch_size, action_dim).requires_grad_(True)
reward = torch.rand(nstep, batch_size)
data = q_nstep_td_data(q, next_q, action, next_action, reward, done, None)
loss, td_error_per_sample, record_target_v = q_nstep_sql_td_error(data, 0.95, 1.0, nstep=nstep)
assert td_error_per_sample.shape == (batch_size, )
assert loss.shape == ()
assert q.grad is None
loss.backward()
assert isinstance(q.grad, torch.Tensor)
data = q_nstep_td_data(q, next_q, action, next_action, reward, done, None)
loss, td_error_per_sample, record_target_v = q_nstep_sql_td_error(data, 0.95, 0.5, nstep=nstep, cum_reward=True)
value_gamma = torch.tensor(0.9)
data = q_nstep_td_data(q, next_q, action, next_action, reward, done, None)
loss, td_error_per_sample, record_target_v = q_nstep_sql_td_error(
data, 0.95, 0.5, nstep=nstep, cum_reward=True, value_gamma=value_gamma
)
loss.backward()
assert isinstance(q.grad, torch.Tensor)
@pytest.mark.unittest
def test_iqn_nstep_td():
batch_size = 4
action_dim = 3
tau = 3
next_q = torch.randn(tau, batch_size, action_dim)
done = torch.randn(batch_size)
action = torch.randint(0, action_dim, size=(batch_size, ))
next_action = torch.randint(0, action_dim, size=(batch_size, ))
for nstep in range(1, 10):
q = torch.randn(tau, batch_size, action_dim).requires_grad_(True)
replay_quantile = torch.randn([tau, batch_size, 1])
reward = torch.rand(nstep, batch_size)
data = iqn_nstep_td_data(q, next_q, action, next_action, reward, done, replay_quantile, None)
loss, td_error_per_sample = iqn_nstep_td_error(data, 0.95, nstep=nstep)
assert td_error_per_sample.shape == (batch_size, )
assert loss.shape == ()
assert q.grad is None
loss.backward()
assert isinstance(q.grad, torch.Tensor)
loss, td_error_per_sample = iqn_nstep_td_error(data, 0.95, nstep=nstep, value_gamma=torch.tensor(0.9))
assert td_error_per_sample.shape == (batch_size, )
@pytest.mark.unittest
def test_shape_fn_qntd():
batch_size = 4
action_dim = 3
next_q = torch.randn(batch_size, action_dim)
done = torch.randn(batch_size)
action = torch.randint(0, action_dim, size=(batch_size, ))
next_action = torch.randint(0, action_dim, size=(batch_size, ))
for nstep in range(1, 10):
q = torch.randn(batch_size, action_dim).requires_grad_(True)
reward = torch.rand(nstep, batch_size)
data = q_nstep_td_data(q, next_q, action, next_action, reward, done, None)
tmp = shape_fn_qntd([data, 0.95, 1], {})
assert tmp[0] == reward.shape[0]
assert tmp[1] == q.shape[0]
assert tmp[2] == q.shape[1]
tmp = shape_fn_qntd([], {'gamma': 0.95, 'nstep': 1, 'data': data})
assert tmp[0] == reward.shape[0]
assert tmp[1] == q.shape[0]
assert tmp[2] == q.shape[1]
@pytest.mark.unittest
def test_shape_fn_dntd():
batch_size = 4
action_dim = 3
n_atom = 51
v_min = -10.0
v_max = 10.0
nstep = 5
dist = torch.randn(batch_size, action_dim, n_atom).abs().requires_grad_(True)
next_n_dist = torch.randn(batch_size, action_dim, n_atom).abs()
done = torch.randn(batch_size)
action = torch.randint(0, action_dim, size=(batch_size, ))
next_action = torch.randint(0, action_dim, size=(batch_size, ))
reward = torch.randn(nstep, batch_size)
data = dist_nstep_td_data(dist, next_n_dist, action, next_action, reward, done, None)
tmp = shape_fn_dntd([data, 0.9, v_min, v_max, n_atom, nstep], {})
assert tmp[0] == reward.shape[0]
assert tmp[1] == dist.shape[0]
assert tmp[2] == dist.shape[1]
assert tmp[3] == n_atom
tmp = shape_fn_dntd([], {'data': data, 'gamma': 0.9, 'v_min': v_min, 'v_max': v_max, 'n_atom': n_atom, 'nstep': 5})
assert tmp[0] == reward.shape[0]
assert tmp[1] == dist.shape[0]
assert tmp[2] == dist.shape[1]
assert tmp[3] == n_atom
@pytest.mark.unittest
def test_shape_fn_qntd_rescale():
batch_size = 4
action_dim = 3
next_q = torch.randn(batch_size, action_dim)
done = torch.randn(batch_size)
action = torch.randint(0, action_dim, size=(batch_size, ))
next_action = torch.randint(0, action_dim, size=(batch_size, ))
for nstep in range(1, 10):
q = torch.randn(batch_size, action_dim).requires_grad_(True)
reward = torch.rand(nstep, batch_size)
data = q_nstep_td_data(q, next_q, action, next_action, reward, done, None)
tmp = shape_fn_qntd_rescale([data, 0.95, 1], {})
assert tmp[0] == reward.shape[0]
assert tmp[1] == q.shape[0]
assert tmp[2] == q.shape[1]
tmp = shape_fn_qntd_rescale([], {'gamma': 0.95, 'nstep': 1, 'data': data})
assert tmp[0] == reward.shape[0]
assert tmp[1] == q.shape[0]
assert tmp[2] == q.shape[1]
@pytest.mark.unittest
def test_fn_td_lambda():
T, B = 8, 4
value = torch.randn(T + 1, B).requires_grad_(True)
reward = torch.rand(T, B)
data = td_lambda_data(value, reward, None)
tmp = shape_fn_td_lambda([], {'data': data})
assert tmp == reward.shape[0]
tmp = shape_fn_td_lambda([data], {})
assert tmp == reward.shape