Rate this Page
★ ★ ★ ★ ★

TensorDictBase.fromkeys#

classmethod TensorDictBase.fromkeys(keys: List[NestedKey], value: Any = 0)#

Creates a tensordict with the same value for every key, like dict.fromkeys().

The result is a TensorDict with an empty batch size. A nested key creates nested tensordicts. A Python number becomes a tensor for each key, and a tensor is stored as it is: every key then holds the same tensor object.

Parameters:
  • keys (list of NestedKey) – the keys of the result.

  • value (Any, optional) – the value of every key: a tensor, a number, or any value that the TensorDict constructor takes. Defaults to 0.

Examples

>>> import torch
>>> from tensordict import TensorDict
>>> td = TensorDict.fromkeys(["a", ("b", "c")], torch.zeros(3))
>>> td["b", "c"]
tensor([0., 0., 0.])
>>> td["a"] is td["b", "c"]
True

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