Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow undounded boxes creation from gym spaces #778

Merged
merged 2 commits into from
Dec 31, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Allow undounded boxes creation from gym spaces
  • Loading branch information
matteobettini committed Dec 31, 2022
commit 54a3a73ea4d98d1a33c8c38dacbfe0dedf176cba
24 changes: 15 additions & 9 deletions torchrl/envs/libs/gym.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from warnings import warn

import torch

from torchrl.data import (
BinaryDiscreteTensorSpec,
BoundedTensorSpec,
Expand All @@ -20,10 +19,10 @@
UnboundedContinuousTensorSpec,
)

from ..._utils import implement_for
from ...data.utils import numpy_to_torch_dtype_dict
from ..gym_like import default_info_dict_reader, GymLikeEnv
from ..utils import _classproperty
from ..._utils import implement_for
from ...data.utils import numpy_to_torch_dtype_dict

try:
import gym
Expand Down Expand Up @@ -75,12 +74,19 @@ def _gym_to_torchrl_spec_transform(
shape = torch.Size([1])
if dtype is None:
dtype = numpy_to_torch_dtype_dict[spec.dtype]
return BoundedTensorSpec(
torch.tensor(spec.low, device=device, dtype=dtype),
torch.tensor(spec.high, device=device, dtype=dtype),
shape,
dtype=dtype,
device=device,
low = torch.tensor(spec.low, device=device, dtype=dtype)
high = torch.tensor(spec.high, device=device, dtype=dtype)
is_unbounded = low.isinf().all() and high.isinf().all()
return (
UnboundedContinuousTensorSpec(shape, device=device, dtype=dtype)
if is_unbounded
else BoundedTensorSpec(
low,
high,
shape,
dtype=dtype,
device=device,
)
)
elif isinstance(spec, (Dict,)):
spec_out = {}
Expand Down