2020-05-29 14:45:21 +02:00
|
|
|
import torch
|
|
|
|
import numpy as np
|
2020-07-07 12:40:55 +02:00
|
|
|
from numbers import Number
|
2020-05-29 14:45:21 +02:00
|
|
|
from typing import Union, Optional
|
|
|
|
|
2020-07-21 10:47:56 +02:00
|
|
|
from tianshou.data.batch import _parse_value, Batch
|
2020-05-29 14:45:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
def to_numpy(x: Union[
|
2020-07-21 10:47:56 +02:00
|
|
|
Batch, dict, list, tuple, np.ndarray, torch.Tensor]) -> Union[
|
|
|
|
Batch, dict, list, tuple, np.ndarray, torch.Tensor]:
|
2020-05-29 14:45:21 +02:00
|
|
|
"""Return an object without torch.Tensor."""
|
2020-08-27 12:15:18 +08:00
|
|
|
if isinstance(x, torch.Tensor): # most often case
|
2020-05-29 14:45:21 +02:00
|
|
|
x = x.detach().cpu().numpy()
|
2020-08-27 12:15:18 +08:00
|
|
|
elif isinstance(x, np.ndarray): # second often case
|
|
|
|
pass
|
|
|
|
elif isinstance(x, (np.number, np.bool_, Number)):
|
|
|
|
x = np.asanyarray(x)
|
|
|
|
elif x is None:
|
|
|
|
x = np.array(None, dtype=np.object)
|
|
|
|
elif isinstance(x, Batch):
|
|
|
|
x.to_numpy()
|
2020-05-29 14:45:21 +02:00
|
|
|
elif isinstance(x, dict):
|
|
|
|
for k, v in x.items():
|
|
|
|
x[k] = to_numpy(v)
|
2020-07-21 10:47:56 +02:00
|
|
|
elif isinstance(x, (list, tuple)):
|
|
|
|
try:
|
|
|
|
x = to_numpy(_parse_value(x))
|
|
|
|
except TypeError:
|
|
|
|
x = [to_numpy(e) for e in x]
|
|
|
|
else: # fallback
|
|
|
|
x = np.asanyarray(x)
|
2020-05-29 14:45:21 +02:00
|
|
|
return x
|
|
|
|
|
|
|
|
|
2020-07-21 10:47:56 +02:00
|
|
|
def to_torch(x: Union[Batch, dict, list, tuple, np.ndarray, torch.Tensor],
|
2020-05-29 14:45:21 +02:00
|
|
|
dtype: Optional[torch.dtype] = None,
|
2020-07-06 10:44:34 +08:00
|
|
|
device: Union[str, int, torch.device] = 'cpu'
|
2020-07-21 10:47:56 +02:00
|
|
|
) -> Union[Batch, dict, list, tuple, np.ndarray, torch.Tensor]:
|
2020-05-29 14:45:21 +02:00
|
|
|
"""Return an object without np.ndarray."""
|
2020-08-27 12:15:18 +08:00
|
|
|
if isinstance(x, np.ndarray) and \
|
|
|
|
issubclass(x.dtype.type, (np.bool_, np.number)): # most often case
|
|
|
|
x = torch.from_numpy(x).to(device)
|
|
|
|
if dtype is not None:
|
|
|
|
x = x.type(dtype)
|
|
|
|
elif isinstance(x, torch.Tensor): # second often case
|
2020-05-30 15:40:31 +02:00
|
|
|
if dtype is not None:
|
|
|
|
x = x.type(dtype)
|
|
|
|
x = x.to(device)
|
2020-08-27 12:15:18 +08:00
|
|
|
elif isinstance(x, (np.number, np.bool_, Number)):
|
|
|
|
x = to_torch(np.asanyarray(x), dtype, device)
|
2020-05-29 14:45:21 +02:00
|
|
|
elif isinstance(x, dict):
|
|
|
|
for k, v in x.items():
|
|
|
|
x[k] = to_torch(v, dtype, device)
|
|
|
|
elif isinstance(x, Batch):
|
2020-05-30 15:40:31 +02:00
|
|
|
x.to_torch(dtype, device)
|
2020-07-21 10:47:56 +02:00
|
|
|
elif isinstance(x, (list, tuple)):
|
|
|
|
try:
|
|
|
|
x = to_torch(_parse_value(x), dtype, device)
|
|
|
|
except TypeError:
|
|
|
|
x = [to_torch(e, dtype, device) for e in x]
|
|
|
|
else: # fallback
|
2020-08-27 12:15:18 +08:00
|
|
|
raise TypeError(f"object {x} cannot be converted to torch.")
|
2020-05-29 14:45:21 +02:00
|
|
|
return x
|
2020-06-03 13:59:47 +08:00
|
|
|
|
|
|
|
|
2020-08-27 12:15:18 +08:00
|
|
|
def to_torch_as(x: Union[Batch, dict, list, tuple, np.ndarray, torch.Tensor],
|
2020-06-03 13:59:47 +08:00
|
|
|
y: torch.Tensor
|
2020-08-27 12:15:18 +08:00
|
|
|
) -> Union[Batch, dict, list, tuple, np.ndarray, torch.Tensor]:
|
2020-06-03 13:59:47 +08:00
|
|
|
"""Return an object without np.ndarray. Same as
|
|
|
|
``to_torch(x, dtype=y.dtype, device=y.device)``.
|
|
|
|
"""
|
|
|
|
assert isinstance(y, torch.Tensor)
|
|
|
|
return to_torch(x, dtype=y.dtype, device=y.device)
|