lovely-tensors
  1. ✨ Misc
  2. πŸ€” Config
  • ❀️ Lovely Tensors
  • πŸ”Ž Tensor representations
    • 🧾 View as a summary
    • πŸ–ŒοΈ View as RGB images
    • πŸ“Š View as a histogram
    • πŸ“Ί View channels
  • ✨ Misc
    • πŸ€” Config
    • πŸ™‰ Monkey-patching
    • 🎭 Matplotlib integration
    • πŸ“œ IPython’s history obsession

On this page

  • Defaults:
  • set_config
  • get_config
  • config
  • Examples
    • Verbose gradient
    • Histogram
    • Precision
    • Scientific mode
    • Color on/off
    • Control .deeper
    • In-memory size of data
    • Reser to defaults
    • Context manager
    • Matplotlib and seed
  1. ✨ Misc
  2. πŸ€” Config

πŸ€” Config

Defaults:

Type Default Details
precision int 3 Digits after .
threshold_max int 3 .abs() larger than 1e3 -> Sci mode
threshold_min int -4 .abs() smaller that 1e-4 -> Sci mode
sci_mode NoneType None Sci mode (2.3e4). None=auto
show_mem_above int 1024 Show memory footprint above this threshold
indent int 2 Indent for .deeper()
color bool True ANSI colors in text
deeper_width int 9 For .deeper, width per level
verbose_grad bool True Print the gradient statistics
plt_seed int 42 Sampling seed for plot
fig_close bool True Close matplotlib Figure
fig_show bool False Call plt.show() for .plt, .chans and .rgb
show_histogram bool True Show the histogram in lovely(): β€˜β–β–‚β–ƒβ–ƒβ–†β–ˆβ–†β–ƒβ–β–β€™

set_config


def set_config(
    precision:Union=Ignore, threshold_min:Union=Ignore, threshold_max:Union=Ignore, sci_mode:Union=Ignore,
    show_mem_above:Union=Ignore, indent:Union=Ignore, color:Union=Ignore, deeper_width:Union=Ignore,
    verbose_grad:Union=Ignore, plt_seed:Union=Ignore, fig_close:Union=Ignore, fig_show:Union=Ignore,
    show_histogram:Union=Ignore
)->None:

Set config variables


get_config


def get_config(
    
)->Config:

Get a copy of config variables


config


def config(
    precision:Union=Ignore, threshold_min:Union=Ignore, threshold_max:Union=Ignore, sci_mode:Union=Ignore,
    show_mem_above:Union=Ignore, indent:Union=Ignore, color:Union=Ignore, deeper_width:Union=Ignore,
    verbose_grad:Union=Ignore, plt_seed:Union=Ignore, fig_close:Union=Ignore, fig_show:Union=Ignore,
    show_histogram:Union=Ignore
):

Context manager for temporarily setting printting options.

Examples

import torch
from lovely_tensors import set_config, get_config, config, monkey_patch
monkey_patch()

Verbose gradient

torch.manual_seed(1)
grad = torch.randn((10, 10), requires_grad=True)

print(f"After .backward():\n{grad}\n")

grad.prod().backward()

print(f"After .backward():\n{grad}\n")

with config(verbose_grad=False):
    print(f"After .backward() verbose_grad=False:\n{grad}\n")
After .backward():
tensor[10, 10] n=100 x∈[-3.331 |β–β–β–β–ƒβ–ˆβ–†β–…β–ƒβ–‚β–| 2.357] ΞΌ=-0.203 Οƒ=0.978 grad=None

After .backward():
tensor[10, 10] n=100 x∈[-3.331 |β–β–β–β–ƒβ–ˆβ–†β–…β–ƒβ–‚β–| 2.357] ΞΌ=-0.203 Οƒ=0.978 grad={ x∈[-3.465e-30 |▁  β–β–ˆβ–   ▁| 4.748e-30] ΞΌ=3.893e-32 Οƒ=6.414e-31 }

After .backward() verbose_grad=False:
tensor[10, 10] n=100 x∈[-3.331 |β–β–β–β–ƒβ–ˆβ–†β–…β–ƒβ–‚β–| 2.357] ΞΌ=-0.203 Οƒ=0.978 grad*

Histogram

numbers = torch.load("mysteryman.pt")

print(numbers)
set_config(show_histogram=False)
print(numbers)
tensor[3, 196, 196] n=115248 (0.4Mb) x∈[-2.118 |β–β–β–…β–ˆβ–β–β–β–β–‚β–| 2.640] ΞΌ=-0.388 Οƒ=1.073
tensor[3, 196, 196] n=115248 (0.4Mb) x∈[-2.118, 2.640] ΞΌ=-0.388 Οƒ=1.073

Precision

set_config(precision=5)
torch.tensor([1., 2, float("nan")])
tensor[3] ΞΌ=1.50000 Οƒ=0.70711 NaN! [1.00000, 2.00000, nan]

Scientific mode

set_config(sci_mode=True) # Force always on
torch.tensor([1., 2, float("nan")])
tensor[3] ΞΌ=1.50000e+00 Οƒ=7.07107e-01 NaN! [1.00000e+00, 2.00000e+00, nan]

Color on/off

set_config(color=False) # Force always off
torch.tensor([1., 2, float("nan")])
tensor[3] ΞΌ=1.50000e+00 Οƒ=7.07107e-01 NaN! [1.00000e+00, 2.00000e+00, nan]
test_eq(str(torch.tensor([1., 2, float("nan")])),
        'tensor[3] ΞΌ=1.50000e+00 Οƒ=7.07107e-01 NaN! [1.00000e+00, 2.00000e+00, nan]')

Control .deeper

set_config(deeper_width=3)
image = torch.load("mysteryman.pt")
image[1,100,100] = float('nan')

image.deeper(2)
tensor[3, 196, 196] n=115248 (0.4Mb) x∈[-2.11790e+00, 2.64000e+00] ΞΌ=-3.88310e-01 Οƒ=1.07319e+00 NaN!
  tensor[196, 196] n=38416 x∈[-2.11790e+00, 2.24891e+00] ΞΌ=-3.24352e-01 Οƒ=1.03588e+00
    tensor[196] x∈[-1.91241e+00, 2.24891e+00] ΞΌ=-6.73483e-01 Οƒ=5.21962e-01
    tensor[196] x∈[-1.86103e+00, 2.16328e+00] ΞΌ=-7.38488e-01 Οƒ=4.18080e-01
    tensor[196] x∈[-1.75828e+00, 2.19753e+00] ΞΌ=-8.05501e-01 Οƒ=3.96848e-01
    ...
  tensor[196, 196] n=38416 x∈[-1.96569e+00, 2.42857e+00] ΞΌ=-2.73903e-01 Οƒ=9.72665e-01 NaN!
    tensor[196] x∈[-1.86064e+00, 2.41106e+00] ΞΌ=-5.28772e-01 Οƒ=5.55960e-01
    tensor[196] x∈[-1.82563e+00, 2.35854e+00] ΞΌ=-5.61732e-01 Οƒ=4.72772e-01
    tensor[196] x∈[-1.75560e+00, 2.37605e+00] ΞΌ=-6.21756e-01 Οƒ=4.58436e-01
    ...
  tensor[196, 196] n=38416 x∈[-1.80444e+00, 2.64000e+00] ΞΌ=-5.66673e-01 Οƒ=1.17776e+00
    tensor[196] x∈[-1.71730e+00, 2.39599e+00] ΞΌ=-9.81537e-01 Οƒ=3.50000e-01
    tensor[196] x∈[-1.75216e+00, 2.32627e+00] ΞΌ=-1.03418e+00 Οƒ=3.13970e-01
    tensor[196] x∈[-1.64758e+00, 2.37856e+00] ΞΌ=-1.08647e+00 Οƒ=3.14213e-01
    ...
test_eq(len(str(image.deeper(2))), 1062)

In-memory size of data

print(torch.ones((1024, 1024)))
set_config(show_mem_above=torch.inf) # Don't show the memory footprint
print(torch.ones((1024, 1024)))
tensor[1024, 1024] n=1048576 (4Mb) x∈[1.00000e+00, 1.00000e+00] ΞΌ=1.00000e+00 Οƒ=0.
tensor[1024, 1024] n=1048576 x∈[1.00000e+00, 1.00000e+00] ΞΌ=1.00000e+00 Οƒ=0.

Reser to defaults

set_config(precision=None, sci_mode=None, color=None, deeper_width=None, show_mem_above=None)
torch.tensor([1., 2, float("nan")])
tensor[3] ΞΌ=1.500 Οƒ=0.707 NaN! [1.000, 2.000, nan]
test_eq(str(torch.tensor([1., 2, float("nan")])),
    'tensor[3] ΞΌ=1.500 Οƒ=0.707 \x1b[31mNaN!\x1b[0m [1.000, 2.000, nan]')

Context manager

display(torch.tensor([1., 2, torch.nan]))
with config(sci_mode=True, color=False):
    display(torch.tensor([1., 2, torch.nan]))
display(torch.tensor([1., 2, torch.nan]))
tensor[3] ΞΌ=1.500 Οƒ=0.707 NaN! [1.000, 2.000, nan]
tensor[3] ΞΌ=1.500e+00 Οƒ=7.071e-01 NaN! [1.000e+00, 2.000e+00, nan]
tensor[3] ΞΌ=1.500 Οƒ=0.707 NaN! [1.000, 2.000, nan]

Matplotlib and seed

_ = a.plt() # The figure was closed, nothing is displayed
set_config(fig_close=False)
_ = a.plt() # figure was not closed. All figures that are not closed are displayed after the cell runs.

For performance reasons, .plt will randomly sample up tp max_s elements from the data (10k be default).

You can change the seed used for this sampling (42 by default):

set_config(plt_seed=1)
a.plt(max_s=100)

set_config(plt_seed=2)
a.plt(max_s=100)

More details in matplotlib