Skip to content

Commit

Permalink
[Feature] env.append_transform (#2040)
Browse files Browse the repository at this point in the history
  • Loading branch information
vmoens authored Mar 26, 2024
1 parent 247ed6e commit e57d0bc
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions torchrl/envs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,35 @@ def ndimension(self):
def ndim(self):
return self.ndimension()

def append_transform(
self,
transform: "Transform" # noqa: F821
| Callable[[TensorDictBase], TensorDictBase],
) -> None:
"""Returns a transformed environment where the callable/transform passed is applied.
Args:
transform (Transform or Callable[[TensorDictBase], TensorDictBase]): the transform to apply
to the environment.
Examples:
>>> from torchrl.envs import GymEnv
>>> import torch
>>> env = GymEnv("CartPole-v1")
>>> loc = 0.5
>>> scale = 1.0
>>> transform = lambda data: data.set("observation", (data.get("observation") - loc)/scale)
>>> env = env.append_transform(transform=transform)
>>> print(env)
TransformedEnv(
env=GymEnv(env=CartPole-v1, batch_size=torch.Size([]), device=cpu),
transform=_CallableTransform(keys=[]))
"""
from torchrl.envs.transforms.transforms import TransformedEnv

return TransformedEnv(self, transform)

# Parent specs: input and output spec.
@property
def input_spec(self) -> TensorSpec:
Expand Down

0 comments on commit e57d0bc

Please sign in to comment.