TensorDictBase.empty#
- TensorDictBase.empty(recurse=False, *, batch_size=None, device=NO_DEFAULT, names=None) Self#
Returns a new, empty tensordict with the same device and batch size.
The result is not locked, even if
selfis.- Parameters:
recurse (bool, optional) – if
True, the entire structure of theTensorDictwill be reproduced without content: the result has the sub-tensordicts ofself, without their leaves. Otherwise, only the root will be duplicated. Defaults toFalse.- Keyword Arguments:
batch_size (Size, optional) – a new batch-size for the tensordict. With
recurse=True, a sub-tensordict whose batch size is the start of the new one gets the new one, a sub-tensordict whose batch size starts with the new one keeps it, and any other sub-tensordict raises aRuntimeError. ALazyStackedTensorDictwhose members took the batch size before 0.15 (one stacked along dim 0, for instance) keeps that result, the empty stack of its members, with aFutureWarning, when a sub-tensordict does not fit the new batch size ornameshas another length. Starting with TensorDict 0.17, the default will change to raising aRuntimeErroror aValueError, asTensorDict.emptyand the names setter do.device (device, optional) – a new device.
Examples
>>> import torch >>> from tensordict import TensorDict >>> td = TensorDict({"a": torch.zeros(3), "b": {"c": torch.ones(3)}}, [3]) >>> empty = td.empty() >>> list(empty.keys()), empty.batch_size ([], torch.Size([3])) >>> list(td.empty(recurse=True).keys(include_nested=True)) ['b']
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.