323 lines
12 KiB
Python
Raw Normal View History

2020-03-14 21:48:31 +08:00
import torch
import copy
2020-04-28 20:56:02 +08:00
import pprint
2020-06-20 22:23:12 +08:00
import warnings
2020-03-13 17:49:22 +08:00
import numpy as np
2020-06-23 17:37:26 +02:00
from typing import Any, List, Tuple, Union, Iterator, Optional
2020-03-13 17:49:22 +08:00
# Disable pickle warning related to torch, since it has been removed
# on torch master branch. See Pull Request #39003 for details:
# https://github.com/pytorch/pytorch/pull/39003
warnings.filterwarnings(
"ignore", message="pickle support for Storage will be removed in 1.5.")
2020-03-13 17:49:22 +08:00
class Batch:
"""Tianshou provides :class:`~tianshou.data.Batch` as the internal data
2020-04-03 21:28:12 +08:00
structure to pass any kind of data to other methods, for example, a
collector gives a :class:`~tianshou.data.Batch` to policy for learning.
Here is the usage:
::
>>> import numpy as np
>>> from tianshou.data import Batch
>>> data = Batch(a=4, b=[5, 5], c='2312312')
>>> data.b
[5, 5]
>>> data.b = np.array([3, 4, 5])
>>> print(data)
Batch(
a: 4,
2020-05-12 11:31:47 +08:00
b: array([3, 4, 5]),
c: '2312312',
)
2020-04-03 21:28:12 +08:00
In short, you can define a :class:`Batch` with any key-value pair. The
current implementation of Tianshou typically use 7 reserved keys in
2020-04-03 21:28:12 +08:00
:class:`~tianshou.data.Batch`:
* ``obs`` the observation of step :math:`t` ;
* ``act`` the action of step :math:`t` ;
* ``rew`` the reward of step :math:`t` ;
* ``done`` the done flag of step :math:`t` ;
* ``obs_next`` the observation of step :math:`t+1` ;
* ``info`` the info of step :math:`t` (in ``gym.Env``, the ``env.step()``\
2020-04-03 21:28:12 +08:00
function return 4 arguments, and the last one is ``info``);
* ``policy`` the data computed by policy in step :math:`t`;
2020-04-03 21:28:12 +08:00
:class:`~tianshou.data.Batch` has other methods, including
:meth:`~tianshou.data.Batch.__getitem__`,
:meth:`~tianshou.data.Batch.__len__`,
:meth:`~tianshou.data.Batch.append`,
and :meth:`~tianshou.data.Batch.split`:
::
>>> data = Batch(obs=np.array([0, 11, 22]), rew=np.array([6, 6, 6]))
>>> # here we test __getitem__
>>> index = [2, 1]
>>> data[index].obs
array([22, 11])
>>> # here we test __len__
>>> len(data)
3
>>> data.append(data) # similar to list.append
>>> data.obs
array([0, 11, 22, 0, 11, 22])
>>> # split whole data into multiple small batch
2020-04-28 20:56:02 +08:00
>>> for d in data.split(size=2, shuffle=False):
2020-04-03 21:28:12 +08:00
... print(d.obs, d.rew)
[ 0 11] [6 6]
[22 0] [6 6]
[11 22] [6 6]
"""
2020-03-13 17:49:22 +08:00
def __init__(self,
batch_dict: Optional[
2020-06-23 17:37:26 +02:00
Union[dict, Tuple[dict], List[dict], np.ndarray]] = None,
**kwargs) -> None:
def _is_batch_set(data: Any) -> bool:
if isinstance(data, (list, tuple)):
if len(data) > 0 and isinstance(data[0], dict):
return True
elif isinstance(data, np.ndarray):
if isinstance(data.item(0), dict):
return True
return False
if isinstance(batch_dict, np.ndarray) and batch_dict.ndim == 0:
batch_dict = batch_dict[()]
if _is_batch_set(batch_dict):
for k, v in zip(batch_dict[0].keys(),
zip(*[e.values() for e in batch_dict])):
if isinstance(v[0], dict) or _is_batch_set(v[0]):
2020-06-23 17:37:26 +02:00
self.__dict__[k] = Batch(v)
elif isinstance(v[0], (np.generic, np.ndarray)):
self.__dict__[k] = np.stack(v, axis=0)
elif isinstance(v[0], torch.Tensor):
self.__dict__[k] = torch.stack(v, dim=0)
elif isinstance(v[0], Batch):
self.__dict__[k] = Batch.stack(v)
else:
self.__dict__[k] = list(v)
elif isinstance(batch_dict, dict):
for k, v in batch_dict.items():
if isinstance(v, dict) or _is_batch_set(v):
self.__dict__[k] = Batch(v)
else:
self.__dict__[k] = v
if len(kwargs) > 0:
self.__init__(kwargs)
2020-03-12 22:20:33 +08:00
def __getstate__(self):
"""Pickling interface. Only the actual data are serialized
for both efficiency and simplicity.
"""
state = {}
for k in self.keys():
v = self[k]
if isinstance(v, Batch):
v = v.__getstate__()
state[k] = v
return state
def __setstate__(self, state):
"""Unpickling interface. At this point, self is an empty Batch
instance that has not been initialized, so it can safely be
initialized by the pickle state.
"""
self.__init__(**state)
def __getitem__(self, index: Union[
str, slice, int, np.integer, np.ndarray, List[int]]) -> 'Batch':
2020-04-04 21:02:06 +08:00
"""Return self[index]."""
def _valid_bounds(length: int, index: Union[
slice, int, np.integer, np.ndarray, List[int]]) -> bool:
if isinstance(index, (int, np.integer)):
return -length <= index and index < length
elif isinstance(index, (list, np.ndarray)):
return _valid_bounds(length, min(index)) and \
_valid_bounds(length, max(index))
elif isinstance(index, slice):
return _valid_bounds(length, index.start) and \
_valid_bounds(length, index.stop - 1)
2020-04-28 20:56:02 +08:00
if isinstance(index, str):
return self.__getattr__(index)
else:
b = Batch()
for k, v in self.__dict__.items():
if isinstance(v, Batch):
b.__dict__[k] = v[index]
elif hasattr(v, '__len__') and (not isinstance(
v, (np.ndarray, torch.Tensor)) or v.ndim > 0):
if _valid_bounds(len(v), index):
b.__dict__[k] = v[index]
return b
2020-03-14 21:48:31 +08:00
2020-05-12 11:31:47 +08:00
def __getattr__(self, key: str) -> Union['Batch', Any]:
2020-04-28 20:56:02 +08:00
"""Return self.key"""
if key not in self.__dict__:
raise AttributeError(key)
return self.__dict__[key]
2020-04-28 20:56:02 +08:00
2020-05-12 11:31:47 +08:00
def __repr__(self) -> str:
"""Return str(self)."""
s = self.__class__.__name__ + '(\n'
flag = False
for k in sorted(self.__dict__.keys()):
if self.__dict__.get(k, None) is not None:
rpl = '\n' + ' ' * (6 + len(k))
2020-04-28 20:56:02 +08:00
obj = pprint.pformat(self.__getattr__(k)).replace('\n', rpl)
s += f' {k}: {obj},\n'
flag = True
if flag:
s += ')'
else:
s = self.__class__.__name__ + '()'
return s
2020-05-12 11:31:47 +08:00
def keys(self) -> List[str]:
2020-04-28 20:56:02 +08:00
"""Return self.keys()."""
return self.__dict__.keys()
2020-04-28 20:56:02 +08:00
2020-05-29 08:03:37 +08:00
def values(self) -> List[Any]:
"""Return self.values()."""
return self.__dict__.values()
def items(self) -> Any:
"""Return self.items()."""
return self.__dict__.items()
2020-05-29 08:03:37 +08:00
2020-05-12 11:31:47 +08:00
def get(self, k: str, d: Optional[Any] = None) -> Union['Batch', Any]:
2020-05-05 13:39:51 +08:00
"""Return self[k] if k in self else d. d defaults to None."""
if k in self.__dict__:
2020-05-05 13:39:51 +08:00
return self.__getattr__(k)
return d
def to_numpy(self) -> None:
"""Change all torch.Tensor to numpy.ndarray. This is an inplace
operation.
"""
for k, v in self.__dict__.items():
if isinstance(v, torch.Tensor):
self.__dict__[k] = v.detach().cpu().numpy()
elif isinstance(v, Batch):
v.to_numpy()
def to_torch(self,
dtype: Optional[torch.dtype] = None,
device: Union[str, int, torch.device] = 'cpu'
) -> None:
"""Change all numpy.ndarray to torch.Tensor. This is an inplace
operation.
"""
if not isinstance(device, torch.device):
device = torch.device(device)
for k, v in self.__dict__.items():
if isinstance(v, (np.generic, np.ndarray)):
v = torch.from_numpy(v).to(device)
if dtype is not None:
v = v.type(dtype)
self.__dict__[k] = v
if isinstance(v, torch.Tensor):
if dtype is not None and v.dtype != dtype:
must_update_tensor = True
elif v.device.type != device.type:
must_update_tensor = True
elif device.index is not None and \
device.index != v.device.index:
must_update_tensor = True
else:
must_update_tensor = False
if must_update_tensor:
if dtype is not None:
v = v.type(dtype)
self.__dict__[k] = v.to(device)
elif isinstance(v, Batch):
v.to_torch(dtype, device)
2020-05-12 11:31:47 +08:00
def append(self, batch: 'Batch') -> None:
2020-06-20 22:23:12 +08:00
warnings.warn('Method append will be removed soon, please use '
':meth:`~tianshou.data.Batch.cat`')
return self.cat_(batch)
2020-06-20 22:23:12 +08:00
def cat_(self, batch: 'Batch') -> None:
2020-06-20 22:23:12 +08:00
"""Concatenate a :class:`~tianshou.data.Batch` object to current
batch.
"""
assert isinstance(batch, Batch), \
'Only Batch is allowed to be concatenated in-place!'
for k, v in batch.__dict__.items():
if v is None:
2020-03-13 17:49:22 +08:00
continue
if not hasattr(self, k) or self.__dict__[k] is None:
self.__dict__[k] = copy.deepcopy(v)
elif isinstance(v, np.ndarray) and v.ndim > 0:
self.__dict__[k] = np.concatenate([self.__dict__[k], v])
elif isinstance(v, torch.Tensor):
self.__dict__[k] = torch.cat([self.__dict__[k], v])
elif isinstance(v, list):
self.__dict__[k] += copy.deepcopy(v)
2020-06-20 22:03:22 +08:00
elif isinstance(v, Batch):
self.__dict__[k].cat_(v)
2020-03-13 17:49:22 +08:00
else:
s = 'No support for method "cat" with type '\
f'{type(v)} in class Batch.'
2020-03-28 09:43:35 +08:00
raise TypeError(s)
2020-03-17 11:37:31 +08:00
@staticmethod
def cat(batches: List['Batch']) -> None:
"""Concatenate a :class:`~tianshou.data.Batch` object into a
single new batch.
"""
assert isinstance(batches, (tuple, list)), \
'Only list of Batch instances is allowed to be '\
'concatenated out-of-place!'
batch = Batch()
for batch_ in batches:
batch.cat_(batch_)
return batch
@staticmethod
def stack(batches: List['Batch']):
"""Stack a :class:`~tianshou.data.Batch` object into a
single new batch.
"""
assert isinstance(batches, (tuple, list)), \
'Only list of Batch instances is allowed to be '\
'stacked out-of-place!'
return Batch(np.array([batch.__dict__ for batch in batches]))
2020-05-12 11:31:47 +08:00
def __len__(self) -> int:
2020-04-04 21:02:06 +08:00
"""Return len(self)."""
r = []
for v in self.__dict__.values():
if hasattr(v, '__len__') and (not isinstance(
v, (np.ndarray, torch.Tensor)) or v.ndim > 0):
r.append(len(v))
2020-06-01 08:30:09 +08:00
return max(r) if len(r) > 0 else 0
2020-04-03 21:28:12 +08:00
2020-05-12 11:31:47 +08:00
def split(self, size: Optional[int] = None,
2020-05-16 20:08:32 +08:00
shuffle: bool = True) -> Iterator['Batch']:
"""Split whole data into multiple small batch.
2020-04-03 21:28:12 +08:00
2020-04-06 19:36:59 +08:00
:param int size: if it is ``None``, it does not split the data batch;
2020-04-03 21:28:12 +08:00
otherwise it will divide the data batch with the given size.
2020-04-06 19:36:59 +08:00
Default to ``None``.
2020-04-28 20:56:02 +08:00
:param bool shuffle: randomly shuffle the entire data batch if it is
2020-04-06 19:36:59 +08:00
``True``, otherwise remain in the same. Default to ``True``.
2020-04-03 21:28:12 +08:00
"""
length = len(self)
2020-03-17 11:37:31 +08:00
if size is None:
size = length
2020-04-28 20:56:02 +08:00
if shuffle:
indices = np.random.permutation(length)
2020-03-20 19:52:29 +08:00
else:
indices = np.arange(length)
for idx in np.arange(0, length, size):
yield self[indices[idx:(idx + size)]]