Created
February 26, 2025 16:38
-
-
Save vmoens/c68b30b7b428b65caa6b9c181b809598 to your computer and use it in GitHub Desktop.
prob_actor_masked.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from tensordict import TensorDict | |
| from tensordict.nn import TensorDictModule as Mod | |
| from torchrl.modules import MaskedCategorical, ProbabilisticActor, MLP | |
| from torchrl.envs import set_exploration_type | |
| import torch | |
| td = TensorDict( | |
| observation=torch.randn(3, 4), | |
| mask=torch.zeros(3, 10, dtype=torch.bool).bernoulli_(0.9), | |
| batch_size=(3,) | |
| ) | |
| policy = ProbabilisticActor( | |
| module=Mod(MLP(in_features=4, out_features=10, num_cells=(32,)), in_keys=["observation"], out_keys=["logits"]), | |
| in_keys=["logits", "mask"], | |
| distribution_class=MaskedCategorical, | |
| return_log_prob=True, | |
| ) | |
| with set_exploration_type("RANDOM"): | |
| print(policy(td)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment