File size: 2,087 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
import torch
import pytest
from itertools import product

from ding.model.template import EDAC
from ding.torch_utils import is_differentiable

B = 4
obs_shape = [4, (8, )]
act_shape = [3, (6, )]
args = list(product(*[obs_shape, act_shape]))


@pytest.mark.unittest
class TestEDAC:

    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_EDAC(self, obs_shape, act_shape):
        if isinstance(obs_shape, int):
            inputs_obs = torch.randn(B, obs_shape)
        else:
            inputs_obs = torch.randn(B, *obs_shape)
        if isinstance(act_shape, int):
            inputs_act = torch.randn(B, act_shape)
        else:
            inputs_act = torch.randn(B, *act_shape)
        inputs = {'obs': inputs_obs, 'action': inputs_act}
        model = EDAC(obs_shape, act_shape, ensemble_num=2)

        outputs_c = model(inputs, mode='compute_critic')
        assert isinstance(outputs_c, dict)
        assert outputs_c['q_value'].shape == (2, B)
        self.output_check(model.critic, outputs_c)

        if isinstance(obs_shape, int):
            inputs = torch.randn(B, obs_shape)
        else:
            inputs = torch.randn(B, *obs_shape)
        outputs_a = model(inputs, mode='compute_actor')
        assert isinstance(outputs_a, dict)
        if isinstance(act_shape, int):
            assert outputs_a['logit'][0].shape == (B, act_shape)
            assert outputs_a['logit'][1].shape == (B, act_shape)
        elif len(act_shape) == 1:
            assert outputs_a['logit'][0].shape == (B, *act_shape)
            assert outputs_a['logit'][1].shape == (B, *act_shape)
        outputs = {'mu': outputs_a['logit'][0], 'sigma': outputs_a['logit'][1]}
        self.output_check(model.actor, outputs)