|
| 1 | +import pytest |
| 2 | +import torch |
| 3 | + |
| 4 | +from mlagents.trainers.torch.distributions import ( |
| 5 | + GaussianDistribution, |
| 6 | + MultiCategoricalDistribution, |
| 7 | + GaussianDistInstance, |
| 8 | + TanhGaussianDistInstance, |
| 9 | + CategoricalDistInstance, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.parametrize("tanh_squash", [True, False]) |
| 14 | +@pytest.mark.parametrize("conditional_sigma", [True, False]) |
| 15 | +def test_gaussian_distribution(conditional_sigma, tanh_squash): |
| 16 | + torch.manual_seed(0) |
| 17 | + hidden_size = 16 |
| 18 | + act_size = 4 |
| 19 | + sample_embedding = torch.ones((1, 16)) |
| 20 | + gauss_dist = GaussianDistribution( |
| 21 | + hidden_size, |
| 22 | + act_size, |
| 23 | + conditional_sigma=conditional_sigma, |
| 24 | + tanh_squash=tanh_squash, |
| 25 | + ) |
| 26 | + |
| 27 | + # Make sure backprop works |
| 28 | + force_action = torch.zeros((1, act_size)) |
| 29 | + optimizer = torch.optim.Adam(gauss_dist.parameters(), lr=3e-3) |
| 30 | + |
| 31 | + for _ in range(50): |
| 32 | + dist_inst = gauss_dist(sample_embedding)[0] |
| 33 | + if tanh_squash: |
| 34 | + assert isinstance(dist_inst, TanhGaussianDistInstance) |
| 35 | + else: |
| 36 | + assert isinstance(dist_inst, GaussianDistInstance) |
| 37 | + log_prob = dist_inst.log_prob(force_action) |
| 38 | + loss = torch.nn.functional.mse_loss(log_prob, -2 * torch.ones(log_prob.shape)) |
| 39 | + optimizer.zero_grad() |
| 40 | + loss.backward() |
| 41 | + optimizer.step() |
| 42 | + for prob in log_prob.flatten(): |
| 43 | + assert prob == pytest.approx(-2, abs=0.1) |
| 44 | + |
| 45 | + |
| 46 | +def test_multi_categorical_distribution(): |
| 47 | + torch.manual_seed(0) |
| 48 | + hidden_size = 16 |
| 49 | + act_size = [3, 3, 4] |
| 50 | + sample_embedding = torch.ones((1, 16)) |
| 51 | + gauss_dist = MultiCategoricalDistribution(hidden_size, act_size) |
| 52 | + |
| 53 | + # Make sure backprop works |
| 54 | + optimizer = torch.optim.Adam(gauss_dist.parameters(), lr=3e-3) |
| 55 | + |
| 56 | + def create_test_prob(size: int) -> torch.Tensor: |
| 57 | + test_prob = torch.tensor( |
| 58 | + [[1.0 - 0.01 * (size - 1)] + [0.01] * (size - 1)] |
| 59 | + ) # High prob for first action |
| 60 | + return test_prob.log() |
| 61 | + |
| 62 | + for _ in range(100): |
| 63 | + dist_insts = gauss_dist(sample_embedding, masks=torch.ones((1, sum(act_size)))) |
| 64 | + loss = 0 |
| 65 | + for i, dist_inst in enumerate(dist_insts): |
| 66 | + assert isinstance(dist_inst, CategoricalDistInstance) |
| 67 | + log_prob = dist_inst.all_log_prob() |
| 68 | + test_log_prob = create_test_prob(act_size[i]) |
| 69 | + # Force log_probs to match the high probability for the first action generated by |
| 70 | + # create_test_prob |
| 71 | + loss += torch.nn.functional.mse_loss(log_prob, test_log_prob) |
| 72 | + optimizer.zero_grad() |
| 73 | + loss.backward() |
| 74 | + optimizer.step() |
| 75 | + for dist_inst, size in zip(dist_insts, act_size): |
| 76 | + # Check that the log probs are close to the fake ones that we generated. |
| 77 | + test_log_probs = create_test_prob(size) |
| 78 | + for _prob, _test_prob in zip( |
| 79 | + dist_inst.all_log_prob().flatten().tolist(), |
| 80 | + test_log_probs.flatten().tolist(), |
| 81 | + ): |
| 82 | + assert _prob == pytest.approx(_test_prob, abs=0.1) |
| 83 | + |
| 84 | + # Test masks |
| 85 | + masks = [] |
| 86 | + for branch in act_size: |
| 87 | + masks += [0] * (branch - 1) + [1] |
| 88 | + masks = torch.tensor([masks]) |
| 89 | + dist_insts = gauss_dist(sample_embedding, masks=masks) |
| 90 | + for dist_inst in dist_insts: |
| 91 | + log_prob = dist_inst.all_log_prob() |
| 92 | + assert log_prob.flatten()[-1] == pytest.approx(0, abs=0.001) |
| 93 | + |
| 94 | + |
| 95 | +def test_gaussian_dist_instance(): |
| 96 | + torch.manual_seed(0) |
| 97 | + act_size = 4 |
| 98 | + dist_instance = GaussianDistInstance( |
| 99 | + torch.zeros(1, act_size), torch.ones(1, act_size) |
| 100 | + ) |
| 101 | + action = dist_instance.sample() |
| 102 | + assert action.shape == (1, act_size) |
| 103 | + for log_prob in dist_instance.log_prob(torch.zeros((1, act_size))).flatten(): |
| 104 | + # Log prob of standard normal at 0 |
| 105 | + assert log_prob == pytest.approx(-0.919, abs=0.01) |
| 106 | + |
| 107 | + for ent in dist_instance.entropy().flatten(): |
| 108 | + # entropy of standard normal at 0 |
| 109 | + assert ent == pytest.approx(2.83, abs=0.01) |
| 110 | + |
| 111 | + |
| 112 | +def test_tanh_gaussian_dist_instance(): |
| 113 | + torch.manual_seed(0) |
| 114 | + act_size = 4 |
| 115 | + dist_instance = GaussianDistInstance( |
| 116 | + torch.zeros(1, act_size), torch.ones(1, act_size) |
| 117 | + ) |
| 118 | + for _ in range(10): |
| 119 | + action = dist_instance.sample() |
| 120 | + assert action.shape == (1, act_size) |
| 121 | + assert torch.max(action) < 1.0 and torch.min(action) > -1.0 |
| 122 | + |
| 123 | + |
| 124 | +def test_categorical_dist_instance(): |
| 125 | + torch.manual_seed(0) |
| 126 | + act_size = 4 |
| 127 | + test_prob = torch.tensor( |
| 128 | + [1.0 - 0.1 * (act_size - 1)] + [0.1] * (act_size - 1) |
| 129 | + ) # High prob for first action |
| 130 | + dist_instance = CategoricalDistInstance(test_prob) |
| 131 | + |
| 132 | + for _ in range(10): |
| 133 | + action = dist_instance.sample() |
| 134 | + assert action.shape == (1,) |
| 135 | + assert action < act_size |
| 136 | + |
| 137 | + # Make sure the first action as higher probability than the others. |
| 138 | + prob_first_action = dist_instance.log_prob(torch.tensor([0])) |
| 139 | + |
| 140 | + for i in range(1, act_size): |
| 141 | + assert dist_instance.log_prob(torch.tensor([i])) < prob_first_action |
0 commit comments