Rate this Page
★ ★ ★ ★ ★

TensorDictBase.new_tensor#

TensorDictBase.new_tensor(data: Tensor | TensorCollection, *, dtype: dtype = None, device: device | str | int = NO_DEFAULT, requires_grad: bool = False, pin_memory: bool | None = None) → Self#

Returns a new TensorDict with data as the tensor data.

By default, the returned TensorDict values have the same torch.dtype and torch.device as this tensor.

The data can also be a tensor collection (TensorDict or tensorclass), in which case the new_tensor method iterates over the tensor pairs of self and data.

Parameters:

data (Tensor or TensorDictBase) – the data to be copied.

Keyword Arguments:
  • dtype (dtype, optional) – the desired type of returned tensordict. Default: if None, the torch.dtype will be unchanged.

  • device (device, optional) – the desired device of returned tensordict. Default: if None, the torch.device will be unchanged.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensors. Default: False.

  • pin_memory (bool, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False.

Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.