TensorDictBase.set_at#
- TensorDictBase.set_at_(key: NestedKey, value: Tensor | TensorCollection, index: None | int | slice | str | Tensor | List[Any] | Tuple[Any, ...], *, non_blocking: bool = False) Self#
Writes
valueinto the existing entrykeyat the indexindex, in place.td.set_at_(key, value, index)is the same astd[key][index] = value.A list, or a
bool,intorfloat(also as a NumPy scalar), written into a non-tensor entry is cast to a tensor, and the write emits aFutureWarning. Starting with TensorDict 0.17, the default will change to storing it as non-tensor data. Wrap the value inNonTensorDatato store it as non-tensor data now, or pass a tensor to keep the tensor.td[index] = valuewrites throughset_at_too, but announces another change: see__setitem__().The entry keeps its storage, so
set_at_works on a locked tensordict.- Parameters:
- Keyword Arguments:
non_blocking (bool, optional) – if
True, copies between devices can run asynchronously with respect to the host. Defaults toFalse.- Returns:
self.- Raises:
KeyError – if the key is missing.
RuntimeError – if the value does not fit
td[key][index].
Examples
>>> import torch >>> from tensordict import TensorDict >>> x = torch.zeros(3, 4) >>> td = TensorDict(x=x, batch_size=[3, 4]) >>> td = td.set_at_("x", value=torch.ones(1, 4), index=slice(1)) >>> x[:, 0] tensor([1., 0., 0.])
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.