Shortcuts

set_printoptions

class tensordict.set_printoptions(**kwargs)

Controls which attributes appear in TensorDict’s __repr__ output.

Can be used as a global setter (via set()), a context manager, or a decorator. Follows the same pattern as set_lazy_legacy.

Keyword Arguments:
  • show_batch_size (bool, optional) – Show batch_size in TensorDict repr. Defaults to True.

  • show_device (bool, optional) – Show device in TensorDict repr. Defaults to True.

  • show_is_shared (bool, optional) – Show is_shared in TensorDict repr. Defaults to True.

  • show_shape (bool, optional) – Show shape in per-tensor field descriptors. Defaults to True.

  • show_field_device (bool, optional) – Show device in per-tensor field descriptors. Defaults to True.

  • show_dtype (bool, optional) – Show dtype in per-tensor field descriptors. Defaults to True.

  • show_field_is_shared (bool, optional) – Show is_shared in per-tensor field descriptors. Defaults to True.

  • show_grad (bool, optional) – Show requires_grad in per-tensor field descriptors. Defaults to False.

  • show_is_contiguous (bool, optional) – Show is_contiguous in per-tensor field descriptors. Defaults to False.

  • show_is_view (bool, optional) – Show is_view in per-tensor field descriptors. Defaults to False.

  • show_storage_size (bool, optional) – Show storage_size (in bytes) in per-tensor field descriptors. Defaults to False.

  • plain (bool, optional) – When True, include a short summary of the actual tensor values in the field descriptors. Defaults to False.

  • sort_keys (str or callable, optional) – Controls the order of keys in the repr. "alphabetical" (default) sorts keys lexicographically. "insertion" preserves the order in which keys were added. A callable is passed as the key argument to sorted().

Examples

>>> import torch
>>> from tensordict import TensorDict, set_printoptions
>>> td = TensorDict({"x": torch.randn(3, 4)})
>>> # Global
>>> set_printoptions(show_device=False, show_is_shared=False).set()
>>> print(td)
>>> # Context manager
>>> with set_printoptions(show_dtype=False):
...     print(td)
>>> # Decorator
>>> @set_printoptions(show_is_shared=False)
... def my_func(td):
...     print(td)

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources