Rate this Page
★ ★ ★ ★ ★

set_printoptions#

class tensordict.set_printoptions(*, verbose: bool = True, **kwargs)#

Sets the options that control what the repr of a tensordict shows.

Use it as a context manager or a decorator to change the options for a block of code; the previous options come back at the end of the block. Call set_printoptions(...).set() to change them for the rest of the program. The options that are not passed keep their current value. Printing and display describes every option with examples, and get_printoptions() returns the current values.

Keyword Arguments:
  • verbose (bool, optional) – if False, hides the device, dtype and is_shared attributes, unless a keyword argument sets them. Defaults to True.

  • **kwargs – the options to set: show_batch_size, show_device, show_is_shared, show_shape, show_field_device, show_dtype, show_field_is_shared, show_grad, show_is_contiguous, show_is_view, show_storage_size, plain, sort_keys, compact and html. compact=True, the default, prints each entry as its dtype and its feature dimensions (f32[3]); compact=False prints each entry as a Tensor(...) line with its full shape, device, dtype and is_shared. The options that only the detailed form uses (show_is_shared, show_shape, show_field_device, show_field_is_shared, show_grad, show_is_contiguous, show_is_view, show_storage_size and plain) also set compact=False when compact is not passed; with an explicit compact=True, the compact form ignores them. html=False turns off the HTML display of notebooks; with compact=False, a notebook shows the detailed text instead of the HTML tree.

Raises:

TypeError – if an option is unknown.

Examples

>>> import torch
>>> from tensordict import TensorDict, set_printoptions
>>> td = TensorDict(a=torch.zeros(3), batch_size=[3])
>>> print(td)
TensorDict(batch_size=[3], fields={a: f32[]})
>>> with set_printoptions(compact=False, verbose=False):
...     print(td)
TensorDict(
    fields={
        a: Tensor(shape=torch.Size([3]))},
    batch_size=torch.Size([3]))