TensorDictBase.copy_at#
- TensorDictBase.copy_at_(tensordict: T, idx: None | int | slice | str | Tensor | List[Any] | Tuple[Any, ...], non_blocking: bool = False, *, fast: bool = True) Self#
Copies the tensors of
tensordictinto the existing entries atidx, and returnsself.copy_at_is a fasterupdate_at_()for the hot paths where every key oftensordictis a tensor entry of the tensordict. Withfast=True, atensordictwith only non-tensor entries raises aRuntimeError. If it also holds tensors, its non-tensor entries are skipped, not copied, with aFutureWarning: starting with TensorDict 0.17, the default will change to raising the sameRuntimeError. Usefast=Falseorupdate_at_()to write them.- Parameters:
tensordict (TensorDictBase) – the entries to copy. Their leading dimensions are those of
td[idx].idx (int, slice, Tensor or tuple) – the index to write at, along the batch dimensions.
non_blocking (bool, optional) – if
True, copies between devices can run asynchronously with respect to the host. Defaults toFalse.
- Keyword Arguments:
fast (bool, optional) –
if
True, uses only the tensor-only copy, and raises aRuntimeErrorif the source, the destination or the index does not allow it (for example a dictionary source, a tensordict that is not aTensorDict, a tensor index, a value that needs broadcasting, or a key missing from the tensordict). IfFalse, callsupdate_at_(). Defaults toTrue.Changed in version 0.15:
fast=None, deprecated in 0.14, is no longer accepted.- Returns:
self.
Examples
>>> import torch >>> from tensordict import TensorDict >>> td = TensorDict(x=torch.zeros(3, 2), batch_size=[3]) >>> _ = td.copy_at_(TensorDict(x=torch.ones(2), batch_size=[]), 1) >>> td["x"][:, 0] tensor([0., 1., 0.])
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.