set_list_to_stack#
- class tensordict.set_list_to_stack(mode: bool)#
Sets whether assigning a list to a key stores a stack of non-tensor data.
By default (
mode=True),td[key] = ["a", "b"]stores aNonTensorStackwith one value per element along the first batch dimension. Withmode=False, the list is stored as oneNonTensorDatavalue. TheLIST_TO_STACKenvironment variable sets the mode at import. Use it as a context manager or a decorator, or callset_list_to_stack(mode).set()to change the mode for the rest of the program.Deprecated since version 0.15: Turning list-to-stack conversion off (
set_list_to_stack(False)orLIST_TO_STACK=0) is deprecated and will be removed in TensorDict 0.17. Usetd.set_non_tensor(key, value)to store a list as one value.set_list_to_stack(True), the default, does not warn, and will keep working, doing nothing, after the removal.- Parameters:
mode (bool) – if
True, a list is stored as a stack.Falsestores it as one value, and emits aDeprecationWarning.
Examples
>>> from tensordict import TensorDict, set_list_to_stack >>> td = TensorDict(batch_size=[2]) >>> with set_list_to_stack(True): ... td["x"] = ["a", "b"] >>> type(td.get("x")).__name__, td["x"] ('NonTensorStack', LinkedList(['a', 'b']))