File size: 2,019 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 |
import pytest
import torch
from ding.torch_utils.distribution import Pd, CategoricalPd, CategoricalPdPytorch
@pytest.mark.unittest
class TestProbDistribution:
def test_Pd(self):
pd = Pd()
with pytest.raises(NotImplementedError):
pd.neglogp(torch.randn(5, ))
with pytest.raises(NotImplementedError):
pd.noise_mode()
with pytest.raises(NotImplementedError):
pd.mode()
with pytest.raises(NotImplementedError):
pd.sample()
def test_CatePD(self):
pd = CategoricalPd()
logit1 = torch.randn(3, 5, requires_grad=True)
logit2 = torch.randint(5, (3, ), dtype=torch.int64)
pd.update_logits(logit1)
entropy = pd.neglogp(logit2)
assert entropy.requires_grad
assert entropy.shape == torch.Size([])
entropy = pd.entropy()
assert entropy.requires_grad
assert entropy.shape == torch.Size([])
entropy = pd.entropy(reduction=None)
assert entropy.requires_grad
assert entropy.shape == torch.Size([3])
ret = pd.sample()
assert ret.shape == torch.Size([3])
ret = pd.sample(viz=True)
assert ret[0].shape == torch.Size([3])
ret = pd.mode()
assert ret.shape == torch.Size([3])
ret = pd.mode(viz=True)
assert ret[0].shape == torch.Size([3])
ret = pd.noise_mode()
assert ret.shape == torch.Size([3])
ret = pd.noise_mode(viz=True)
assert ret[0].shape == torch.Size([3])
pd = CategoricalPdPytorch()
pd.update_logits(logit1)
ret = pd.sample()
assert ret.shape == torch.Size([3])
ret = pd.mode()
assert ret.shape == torch.Size([3])
entropy = pd.entropy(reduction='mean')
assert entropy.requires_grad
assert entropy.shape == torch.Size([])
entropy = pd.entropy(reduction=None)
assert entropy.requires_grad
assert entropy.shape == torch.Size([3])
|