Rate this Page
★ ★ ★ ★ ★

fromkeys#

tensordict.fromkeys(keys: list[NestedKey], value: Any = 0)#

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

Same as tensordict.TensorDict.fromkeys(), and as dict.fromkeys() for a dict. The batch size of the result is [].

Parameters:
  • keys (list of NestedKey) – the keys of the new tensordict. A tuple of strings creates a nested entry.

  • value (Tensor, number or Any, optional) – the value of every entry. Defaults to 0.

Returns:

A TensorDict.

Examples

>>> import tensordict
>>> td = tensordict.fromkeys(["a", ("b", "c")], 1)
>>> td["b", "c"]
tensor(1)