-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] CatFrames.make_rb_transform_and_sampler
ghstack-source-id: 11488a7c1d8ed1003148ff907d30195d153997f4 Pull Request resolved: #2643
- Loading branch information
Showing
3 changed files
with
176 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# 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 | ||
from torchrl.data import LazyTensorStorage, ReplayBuffer | ||
from torchrl.envs import ( | ||
CatFrames, | ||
Compose, | ||
DMControlEnv, | ||
StepCounter, | ||
ToTensorImage, | ||
TransformedEnv, | ||
UnsqueezeTransform, | ||
) | ||
|
||
# Number of frames to stack together | ||
frame_stack = 4 | ||
# Dimension along which the stack should occur | ||
stack_dim = -4 | ||
# Max size of the buffer | ||
max_size = 100_000 | ||
# Batch size of the replay buffer | ||
training_batch_size = 32 | ||
|
||
seed = 123 | ||
|
||
|
||
def main(): | ||
env = TransformedEnv( | ||
DMControlEnv( | ||
env_name="cartpole", | ||
task_name="balance", | ||
device="cpu", | ||
from_pixels=True, | ||
pixels_only=True, | ||
), | ||
Compose( | ||
ToTensorImage( | ||
from_int=True, | ||
dtype=torch.float32, | ||
in_keys=["pixels"], | ||
out_keys=["pixels_trsf"], | ||
shape_tolerant=True, | ||
), | ||
UnsqueezeTransform( | ||
dim=stack_dim, in_keys=["pixels_trsf"], out_keys=["pixels_trsf"] | ||
), | ||
CatFrames( | ||
N=frame_stack, | ||
dim=stack_dim, | ||
in_keys=["pixels_trsf"], | ||
out_keys=["pixels_trsf"], | ||
), | ||
StepCounter(), | ||
), | ||
) | ||
env.set_seed(seed) | ||
|
||
sampler = None | ||
transform = None | ||
|
||
def collect_transform_sampler(module): | ||
nonlocal sampler | ||
nonlocal transform | ||
if isinstance(module, CatFrames): | ||
transform, sampler = module.make_rb_transform_and_sampler( | ||
batch_size=training_batch_size, | ||
traj_key=("collector", "traj_ids"), | ||
strict_length=True, | ||
) | ||
|
||
env.apply(collect_transform_sampler) | ||
|
||
rb_transforms = Compose( | ||
ToTensorImage( | ||
from_int=True, | ||
dtype=torch.float32, | ||
in_keys=["pixels", ("next", "pixels")], | ||
out_keys=["pixels_trsf", ("next", "pixels_trsf")], | ||
shape_tolerant=True, | ||
), # C W' H' -> C W' H' (unchanged due to shape_tolerant) | ||
UnsqueezeTransform( | ||
dim=stack_dim, | ||
in_keys=["pixels_trsf", ("next", "pixels_trsf")], | ||
out_keys=["pixels_trsf", ("next", "pixels_trsf")], | ||
), # 1 C W' H' | ||
transform, | ||
) | ||
|
||
rb = ReplayBuffer( | ||
storage=LazyTensorStorage(max_size=max_size, device="cpu"), | ||
sampler=sampler, | ||
batch_size=training_batch_size, | ||
transform=rb_transforms, | ||
) | ||
|
||
data = env.rollout(1000, break_when_any_done=False) | ||
rb.extend(data) | ||
|
||
# This is where things do not work as expected right now. | ||
training_batch = rb.sample() | ||
print(training_batch) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains 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
This file contains 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