forked from pytorch/rl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
182 lines (153 loc) · 5.13 KB
/
utils.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
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
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import torch.nn
import torch.optim
from tensordict.nn import TensorDictModule
from torchrl.data import CompositeSpec
from torchrl.envs import (
CatFrames,
DoubleToFloat,
EndOfLifeTransform,
ExplorationType,
GrayScale,
GymEnv,
NoopResetEnv,
Resize,
RewardClipping,
RewardSum,
StepCounter,
ToTensorImage,
TransformedEnv,
VecNorm,
)
from torchrl.modules import (
ActorValueOperator,
ConvNet,
MLP,
OneHotCategorical,
ProbabilisticActor,
ValueOperator,
)
# ====================================================================
# Environment utils
# --------------------------------------------------------------------
def make_env(env_name, device, is_test=False):
env = GymEnv(
env_name, frame_skip=4, from_pixels=True, pixels_only=False, device=device
)
env = TransformedEnv(env)
env.append_transform(NoopResetEnv(noops=30, random=True))
if not is_test:
env.append_transform(EndOfLifeTransform())
env.append_transform(RewardClipping(-1, 1))
env.append_transform(ToTensorImage(from_int=False))
env.append_transform(GrayScale())
env.append_transform(Resize(84, 84))
env.append_transform(CatFrames(N=4, dim=-3))
env.append_transform(RewardSum())
env.append_transform(StepCounter(max_steps=4500))
env.append_transform(DoubleToFloat())
env.append_transform(VecNorm(in_keys=["pixels"]))
return env
# ====================================================================
# Model utils
# --------------------------------------------------------------------
def make_ppo_modules_pixels(proof_environment):
# Define input shape
input_shape = proof_environment.observation_spec["pixels"].shape
# Define distribution class and kwargs
num_outputs = proof_environment.action_spec.space.n
distribution_class = OneHotCategorical
distribution_kwargs = {}
# Define input keys
in_keys = ["pixels"]
# Define a shared Module and TensorDictModule (CNN + MLP)
common_cnn = ConvNet(
activation_class=torch.nn.ReLU,
num_cells=[32, 64, 64],
kernel_sizes=[8, 4, 3],
strides=[4, 2, 1],
)
common_cnn_output = common_cnn(torch.ones(input_shape))
common_mlp = MLP(
in_features=common_cnn_output.shape[-1],
activation_class=torch.nn.ReLU,
activate_last_layer=True,
out_features=512,
num_cells=[],
)
common_mlp_output = common_mlp(common_cnn_output)
# Define shared net as TensorDictModule
common_module = TensorDictModule(
module=torch.nn.Sequential(common_cnn, common_mlp),
in_keys=in_keys,
out_keys=["common_features"],
)
# Define on head for the policy
policy_net = MLP(
in_features=common_mlp_output.shape[-1],
out_features=num_outputs,
activation_class=torch.nn.ReLU,
num_cells=[],
)
policy_module = TensorDictModule(
module=policy_net,
in_keys=["common_features"],
out_keys=["logits"],
)
# Add probabilistic sampling of the actions
policy_module = ProbabilisticActor(
policy_module,
in_keys=["logits"],
spec=CompositeSpec(action=proof_environment.action_spec),
distribution_class=distribution_class,
distribution_kwargs=distribution_kwargs,
return_log_prob=True,
default_interaction_type=ExplorationType.RANDOM,
)
# Define another head for the value
value_net = MLP(
activation_class=torch.nn.ReLU,
in_features=common_mlp_output.shape[-1],
out_features=1,
num_cells=[],
)
value_module = ValueOperator(
value_net,
in_keys=["common_features"],
)
return common_module, policy_module, value_module
def make_ppo_models(env_name):
proof_environment = make_env(env_name, device="cpu")
common_module, policy_module, value_module = make_ppo_modules_pixels(
proof_environment
)
# Wrap modules in a single ActorCritic operator
actor_critic = ActorValueOperator(
common_operator=common_module,
policy_operator=policy_module,
value_operator=value_module,
)
actor = actor_critic.get_policy_operator()
critic = actor_critic.get_value_operator()
del proof_environment
return actor, critic
# ====================================================================
# Evaluation utils
# --------------------------------------------------------------------
def eval_model(actor, test_env, num_episodes=3):
test_rewards = torch.zeros(num_episodes, dtype=torch.float32)
for i in range(num_episodes):
td_test = test_env.rollout(
policy=actor,
auto_reset=True,
auto_cast_to_device=True,
break_when_any_done=True,
max_steps=10_000_000,
)
reward = td_test["next", "episode_reward"][td_test["next", "done"]]
test_rewards[i] = reward.sum()
del td_test
return test_rewards.mean()