File size: 13,804 Bytes
079c32c |
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 |
import pytest
from itertools import product
import torch
from ding.model.template import DQN, RainbowDQN, QRDQN, IQN, FQF, DRQN, C51DQN, BDQ, GTrXLDQN
from ding.torch_utils import is_differentiable
T, B = 3, 4
obs_shape = [4, (8, ), (4, 64, 64)]
act_shape = [3, (6, ), [2, 3, 6]]
args = list(product(*[obs_shape, act_shape]))
@pytest.mark.unittest
class TestQLearning:
def output_check(self, model, outputs):
if isinstance(outputs, torch.Tensor):
loss = outputs.sum()
elif isinstance(outputs, list):
loss = sum([t.sum() for t in outputs])
elif isinstance(outputs, dict):
loss = sum([v.sum() for v in outputs.values()])
is_differentiable(loss, model)
@pytest.mark.parametrize('obs_shape, act_shape', args)
def test_dqn(self, obs_shape, act_shape):
if isinstance(obs_shape, int):
inputs = torch.randn(B, obs_shape)
else:
inputs = torch.randn(B, *obs_shape)
model = DQN(obs_shape, act_shape)
outputs = model(inputs)
assert isinstance(outputs, dict)
if isinstance(act_shape, int):
assert outputs['logit'].shape == (B, act_shape)
elif len(act_shape) == 1:
assert outputs['logit'].shape == (B, *act_shape)
else:
for i, s in enumerate(act_shape):
assert outputs['logit'][i].shape == (B, s)
self.output_check(model, outputs['logit'])
@pytest.mark.parametrize('obs_shape, act_shape', args)
def test_bdq(self, obs_shape, act_shape):
if isinstance(obs_shape, int):
inputs = torch.randn(B, obs_shape)
else:
inputs = torch.randn(B, *obs_shape)
if not isinstance(act_shape, int) and len(act_shape) > 1:
return
num_branches = act_shape
for action_bins_per_branch in range(1, 10):
model = BDQ(obs_shape, num_branches, action_bins_per_branch)
outputs = model(inputs)
assert isinstance(outputs, dict)
if isinstance(act_shape, int):
assert outputs['logit'].shape == (B, act_shape, action_bins_per_branch)
else:
assert outputs['logit'].shape == (B, *act_shape, action_bins_per_branch)
self.output_check(model, outputs['logit'])
@pytest.mark.parametrize('obs_shape, act_shape', args)
def test_rainbowdqn(self, obs_shape, act_shape):
if isinstance(obs_shape, int):
inputs = torch.randn(B, obs_shape)
else:
inputs = torch.randn(B, *obs_shape)
model = RainbowDQN(obs_shape, act_shape, n_atom=41)
outputs = model(inputs)
assert isinstance(outputs, dict)
if isinstance(act_shape, int):
assert outputs['logit'].shape == (B, act_shape)
assert outputs['distribution'].shape == (B, act_shape, 41)
elif len(act_shape) == 1:
assert outputs['logit'].shape == (B, *act_shape)
assert outputs['distribution'].shape == (B, *act_shape, 41)
else:
for i, s in enumerate(act_shape):
assert outputs['logit'][i].shape == (B, s)
assert outputs['distribution'][i].shape == (B, s, 41)
self.output_check(model, outputs['logit'])
@pytest.mark.parametrize('obs_shape, act_shape', args)
def test_c51(self, obs_shape, act_shape):
if isinstance(obs_shape, int):
inputs = torch.randn(B, obs_shape)
else:
inputs = torch.randn(B, *obs_shape)
model = C51DQN(obs_shape, act_shape, n_atom=41)
outputs = model(inputs)
assert isinstance(outputs, dict)
if isinstance(act_shape, int):
assert outputs['logit'].shape == (B, act_shape)
assert outputs['distribution'].shape == (B, act_shape, 41)
elif len(act_shape) == 1:
assert outputs['logit'].shape == (B, *act_shape)
assert outputs['distribution'].shape == (B, *act_shape, 41)
else:
for i, s in enumerate(act_shape):
assert outputs['logit'][i].shape == (B, s)
assert outputs['distribution'][i].shape == (B, s, 41)
self.output_check(model, outputs['logit'])
@pytest.mark.parametrize('obs_shape, act_shape', args)
def test_iqn(self, obs_shape, act_shape):
if isinstance(obs_shape, int):
inputs = torch.randn(B, obs_shape)
else:
inputs = torch.randn(B, *obs_shape)
num_quantiles = 48
model = IQN(obs_shape, act_shape, num_quantiles=num_quantiles, quantile_embedding_size=64)
outputs = model(inputs)
print(model)
assert isinstance(outputs, dict)
if isinstance(act_shape, int):
assert outputs['logit'].shape == (B, act_shape)
assert outputs['q'].shape == (num_quantiles, B, act_shape)
assert outputs['quantiles'].shape == (B * num_quantiles, 1)
elif len(act_shape) == 1:
assert outputs['logit'].shape == (B, *act_shape)
assert outputs['q'].shape == (num_quantiles, B, *act_shape)
assert outputs['quantiles'].shape == (B * num_quantiles, 1)
else:
for i, s in enumerate(act_shape):
assert outputs['logit'][i].shape == (B, s)
assert outputs['q'][i].shape == (num_quantiles, B, s)
assert outputs['quantiles'][i].shape == (B * num_quantiles, 1)
self.output_check(model, outputs['logit'])
@pytest.mark.parametrize('obs_shape, act_shape', args)
def test_fqf(self, obs_shape, act_shape):
if isinstance(obs_shape, int):
inputs = torch.randn(B, obs_shape)
else:
inputs = torch.randn(B, *obs_shape)
num_quantiles = 48
model = FQF(obs_shape, act_shape, num_quantiles=num_quantiles, quantile_embedding_size=64)
outputs = model(inputs)
print(model)
assert isinstance(outputs, dict)
if isinstance(act_shape, int):
assert outputs['logit'].shape == (B, act_shape)
assert outputs['q'].shape == (B, num_quantiles, act_shape)
assert outputs['quantiles'].shape == (B, num_quantiles + 1)
assert outputs['quantiles_hats'].shape == (B, num_quantiles)
assert outputs['q_tau_i'].shape == (B, num_quantiles - 1, act_shape)
all_quantiles_proposal = model.head.quantiles_proposal
all_fqf_fc = model.head.fqf_fc
elif len(act_shape) == 1:
assert outputs['logit'].shape == (B, *act_shape)
assert outputs['q'].shape == (B, num_quantiles, *act_shape)
assert outputs['quantiles'].shape == (B, num_quantiles + 1)
assert outputs['quantiles_hats'].shape == (B, num_quantiles)
assert outputs['q_tau_i'].shape == (B, num_quantiles - 1, *act_shape)
all_quantiles_proposal = model.head.quantiles_proposal
all_fqf_fc = model.head.fqf_fc
else:
for i, s in enumerate(act_shape):
assert outputs['logit'][i].shape == (B, s)
assert outputs['q'][i].shape == (B, num_quantiles, s)
assert outputs['quantiles'][i].shape == (B, num_quantiles + 1)
assert outputs['quantiles_hats'][i].shape == (B, num_quantiles)
assert outputs['q_tau_i'][i].shape == (B, num_quantiles - 1, s)
all_quantiles_proposal = [h.quantiles_proposal for h in model.head.pred]
all_fqf_fc = [h.fqf_fc for h in model.head.pred]
self.output_check(all_quantiles_proposal, outputs['quantiles'])
for p in model.parameters():
p.grad = None
self.output_check(all_fqf_fc, outputs['q'])
@pytest.mark.parametrize('obs_shape, act_shape', args)
def test_qrdqn(self, obs_shape, act_shape):
if isinstance(obs_shape, int):
inputs = torch.randn(B, obs_shape)
else:
inputs = torch.randn(B, *obs_shape)
model = QRDQN(obs_shape, act_shape, num_quantiles=32)
outputs = model(inputs)
assert isinstance(outputs, dict)
if isinstance(act_shape, int):
assert outputs['logit'].shape == (B, act_shape)
assert outputs['q'].shape == (B, act_shape, 32)
assert outputs['tau'].shape == (B, 32, 1)
elif len(act_shape) == 1:
assert outputs['logit'].shape == (B, *act_shape)
assert outputs['q'].shape == (B, *act_shape, 32)
assert outputs['tau'].shape == (B, 32, 1)
else:
for i, s in enumerate(act_shape):
assert outputs['logit'][i].shape == (B, s)
assert outputs['q'][i].shape == (B, s, 32)
assert outputs['tau'][i].shape == (B, 32, 1)
self.output_check(model, outputs['logit'])
@pytest.mark.parametrize('obs_shape, act_shape', args)
def test_drqn(self, obs_shape, act_shape):
if isinstance(obs_shape, int):
inputs = torch.randn(T, B, obs_shape)
else:
inputs = torch.randn(T, B, *obs_shape)
# (num_layer * num_direction, 1, head_hidden_size)
prev_state = [{k: torch.randn(1, 1, 64) for k in ['h', 'c']} for _ in range(B)]
model = DRQN(obs_shape, act_shape)
outputs = model({'obs': inputs, 'prev_state': prev_state}, inference=False)
assert isinstance(outputs, dict)
if isinstance(act_shape, int):
assert outputs['logit'].shape == (T, B, act_shape)
elif len(act_shape) == 1:
assert outputs['logit'].shape == (T, B, *act_shape)
else:
for i, s in enumerate(act_shape):
assert outputs['logit'][i].shape == (T, B, s)
assert len(outputs['next_state']) == B
assert all([len(t) == 2 for t in outputs['next_state']])
assert all([t['h'].shape == (1, 1, 64) for t in outputs['next_state']])
self.output_check(model, outputs['logit'])
@pytest.mark.parametrize('obs_shape, act_shape', args)
def test_drqn_inference(self, obs_shape, act_shape):
if isinstance(obs_shape, int):
inputs = torch.randn(B, obs_shape)
else:
inputs = torch.randn(B, *obs_shape)
# (num_layer * num_direction, 1, head_hidden_size)
prev_state = [{k: torch.randn(1, 1, 64) for k in ['h', 'c']} for _ in range(B)]
model = DRQN(obs_shape, act_shape)
outputs = model({'obs': inputs, 'prev_state': prev_state}, inference=True)
assert isinstance(outputs, dict)
if isinstance(act_shape, int):
assert outputs['logit'].shape == (B, act_shape)
elif len(act_shape) == 1:
assert outputs['logit'].shape == (B, *act_shape)
else:
for i, s in enumerate(act_shape):
assert outputs['logit'][i].shape == (B, s)
assert len(outputs['next_state']) == B
assert all([len(t) == 2 for t in outputs['next_state']])
assert all([t['h'].shape == (1, 1, 64) for t in outputs['next_state']])
self.output_check(model, outputs['logit'])
@pytest.mark.parametrize('obs_shape, act_shape', args)
def test_drqn_res_link(self, obs_shape, act_shape):
if isinstance(obs_shape, int):
inputs = torch.randn(T, B, obs_shape)
else:
inputs = torch.randn(T, B, *obs_shape)
# (num_layer * num_direction, 1, head_hidden_size)
prev_state = [{k: torch.randn(1, 1, 64) for k in ['h', 'c']} for _ in range(B)]
model = DRQN(obs_shape, act_shape, res_link=True)
outputs = model({'obs': inputs, 'prev_state': prev_state}, inference=False)
assert isinstance(outputs, dict)
if isinstance(act_shape, int):
assert outputs['logit'].shape == (T, B, act_shape)
elif len(act_shape) == 1:
assert outputs['logit'].shape == (T, B, *act_shape)
else:
for i, s in enumerate(act_shape):
assert outputs['logit'][i].shape == (T, B, s)
assert len(outputs['next_state']) == B
assert all([len(t) == 2 for t in outputs['next_state']])
assert all([t['h'].shape == (1, 1, 64) for t in outputs['next_state']])
self.output_check(model, outputs['logit'])
@pytest.mark.parametrize('obs_shape, act_shape', args)
def test_drqn_inference_res_link(self, obs_shape, act_shape):
if isinstance(obs_shape, int):
inputs = torch.randn(B, obs_shape)
else:
inputs = torch.randn(B, *obs_shape)
# (num_layer * num_direction, 1, head_hidden_size)
prev_state = [{k: torch.randn(1, 1, 64) for k in ['h', 'c']} for _ in range(B)]
model = DRQN(obs_shape, act_shape, res_link=True)
outputs = model({'obs': inputs, 'prev_state': prev_state}, inference=True)
assert isinstance(outputs, dict)
if isinstance(act_shape, int):
assert outputs['logit'].shape == (B, act_shape)
elif len(act_shape) == 1:
assert outputs['logit'].shape == (B, *act_shape)
else:
for i, s in enumerate(act_shape):
assert outputs['logit'][i].shape == (B, s)
assert len(outputs['next_state']) == B
assert all([len(t) == 2 for t in outputs['next_state']])
assert all([t['h'].shape == (1, 1, 64) for t in outputs['next_state']])
self.output_check(model, outputs['logit'])
@pytest.mark.tmp
def test_GTrXLDQN(self):
obs_dim, seq_len, bs, action_dim = [4, 64, 64], 64, 32, 4
obs = torch.rand(seq_len, bs, *obs_dim)
model = GTrXLDQN(obs_dim, action_dim, encoder_hidden_size_list=[16, 16, 16])
outputs = model(obs)
assert isinstance(outputs, dict)
|