forked from pytorch/rl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_actors.py
126 lines (111 loc) · 4.36 KB
/
test_actors.py
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
import pytest
import torch
from torchrl.modules.tensordict_module.actors import (
QValueHook,
DistributionalQValueHook,
)
class TestQValue:
def test_qvalue_hook_wrong_action_space(self):
with pytest.raises(ValueError) as exc:
QValueHook(action_space="wrong_value")
assert "action_space must be one of" in str(exc.value)
def test_distributional_qvalue_hook_wrong_action_space(self):
with pytest.raises(ValueError) as exc:
DistributionalQValueHook(action_space="wrong_value", support=None)
assert "action_space must be one of" in str(exc.value)
@pytest.mark.parametrize(
"action_space, expected_action",
(
("one_hot", [0, 0, 1, 0, 0]),
("categorical", 2),
),
)
def test_qvalue_hook_0_dim_batch(self, action_space, expected_action):
hook = QValueHook(action_space=action_space)
in_values = torch.tensor([1.0, -1.0, 100.0, -2.0, -3.0])
action, values, chosen_action_value = hook(
net=None, observation=None, values=in_values
)
assert (torch.tensor(expected_action, dtype=torch.long) == action).all()
assert (values == in_values).all()
assert (torch.tensor([100.0]) == chosen_action_value).all()
@pytest.mark.parametrize(
"action_space, expected_action",
(
("one_hot", [[0, 0, 1, 0, 0], [1, 0, 0, 0, 0]]),
("categorical", [2, 0]),
),
)
def test_qvalue_hook_1_dim_batch(self, action_space, expected_action):
hook = QValueHook(action_space=action_space)
in_values = torch.tensor(
[
[1.0, -1.0, 100.0, -2.0, -3.0],
[5.0, 4.0, 3.0, 2.0, -5.0],
]
)
action, values, chosen_action_value = hook(
net=None, observation=None, values=in_values
)
assert (torch.tensor(expected_action, dtype=torch.long) == action).all()
assert (values == in_values).all()
assert (torch.tensor([[100.0], [5.0]]) == chosen_action_value).all()
@pytest.mark.parametrize(
"action_space, expected_action",
(
("one_hot", [0, 0, 1, 0, 0]),
("categorical", 2),
),
)
def test_distributional_qvalue_hook_0_dim_batch(
self, action_space, expected_action
):
support = torch.tensor([-2.0, 0.0, 2.0])
hook = DistributionalQValueHook(action_space=action_space, support=support)
in_values = torch.nn.LogSoftmax(dim=-1)(
torch.tensor(
[
[1.0, -1.0, 11.0, -2.0, 30.0],
[1.0, -1.0, 1.0, -2.0, -3.0],
[1.0, -1.0, 10.0, -2.0, -3.0],
]
)
)
action, values = hook(net=None, observation=None, values=in_values)
expected_action = torch.tensor(expected_action, dtype=torch.long)
assert action.shape == expected_action.shape
assert (action == expected_action).all()
assert values.shape == in_values.shape
assert (values == in_values).all()
@pytest.mark.parametrize(
"action_space, expected_action",
(
("one_hot", [[0, 0, 1, 0, 0], [1, 0, 0, 0, 0]]),
("categorical", [2, 0]),
),
)
def test_qvalue_hook_categorical_1_dim_batch(self, action_space, expected_action):
support = torch.tensor([-2.0, 0.0, 2.0])
hook = DistributionalQValueHook(action_space=action_space, support=support)
in_values = torch.nn.LogSoftmax(dim=-1)(
torch.tensor(
[
[
[1.0, -1.0, 11.0, -2.0, 30.0],
[1.0, -1.0, 1.0, -2.0, -3.0],
[1.0, -1.0, 10.0, -2.0, -3.0],
],
[
[11.0, -1.0, 7.0, -1.0, 20.0],
[10.0, 19.0, 1.0, -2.0, -3.0],
[1.0, -1.0, 0.0, -2.0, -3.0],
],
]
)
)
action, values = hook(net=None, observation=None, values=in_values)
expected_action = torch.tensor(expected_action, dtype=torch.long)
assert action.shape == expected_action.shape
assert (action == expected_action).all()
assert values.shape == in_values.shape
assert (values == in_values).all()