lovely-tensors
  1. ✨ Misc
  2. πŸ™‰ Monkey-patching
  • ❀️ 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

  • monkey_patch
  1. ✨ Misc
  2. πŸ™‰ Monkey-patching

πŸ™‰ Monkey-patching


monkey_patch


def monkey_patch(
    cls:_TensorMeta=Tensor
):

Monkey-patch lovely features into cls

monkey_patch()
image = torch.load("mysteryman.pt")
spicy = image.flatten()[:12].clone()

spicy[0] *= 10000
spicy[1] /= 10000
spicy[2] = float('inf')
spicy[3] = float('-inf')
spicy[4] = float('nan')

spicy = spicy.reshape((2,6))
spicy
tensor[2, 6] n=12 x∈[-3.541e+03, -3.369e-05] ΞΌ=-393.776 Οƒ=1.180e+03 +Inf! -Inf! NaN!
spicy.v
tensor[2, 6] n=12 x∈[-3.541e+03, -3.369e-05] ΞΌ=-393.776 Οƒ=1.180e+03 +Inf! -Inf! NaN!
tensor([[-3.5405e+03, -3.3693e-05,         inf,        -inf,         nan,
         -4.0543e-01],
        [-4.2255e-01, -4.9105e-01, -5.0818e-01, -5.5955e-01, -5.4243e-01,
         -5.0818e-01]])
spicy.p
tensor([[-3.5405e+03, -3.3693e-05,         inf,        -inf,         nan,
         -4.0543e-01],
        [-4.2255e-01, -4.9105e-01, -5.0818e-01, -5.5955e-01, -5.4243e-01,
         -5.0818e-01]])
image.deeper
tensor[3, 196, 196] n=115248 (0.4Mb) x∈[-2.118 |β–β–β–…β–ˆβ–β–β–β–β–‚β–| 2.640] ΞΌ=-0.388 Οƒ=1.073
  tensor[196, 196] n=38416 x∈[-2.118 |β–β–β–ƒβ–ˆβ–ƒβ–β–β–β–β–‚| 2.249] ΞΌ=-0.324 Οƒ=1.036
  tensor[196, 196] n=38416 x∈[-1.966 |β–β–β–ƒβ–ˆβ–β–β–β–β–β–‚| 2.429] ΞΌ=-0.274 Οƒ=0.973
  tensor[196, 196] n=38416 x∈[-1.804 |β–β–ˆβ–‚β–β–β–β–β–β–β–‚| 2.640] ΞΌ=-0.567 Οƒ=1.178
image[:3,:3,:5].deeper(depth=2)
tensor[3, 3, 5] n=45 x∈[-1.316, -0.197] ΞΌ=-0.593 Οƒ=0.306
  tensor[3, 5] n=15 x∈[-0.765, -0.337] ΞΌ=-0.492 Οƒ=0.124
    tensor[5] x∈[-0.440, -0.337] ΞΌ=-0.385 Οƒ=0.041 [-0.354, -0.337, -0.405, -0.440, -0.388]
    tensor[5] x∈[-0.662, -0.405] ΞΌ=-0.512 Οƒ=0.108 [-0.405, -0.423, -0.491, -0.577, -0.662]
    tensor[5] x∈[-0.765, -0.474] ΞΌ=-0.580 Οƒ=0.125 [-0.474, -0.474, -0.542, -0.645, -0.765]
  tensor[3, 5] n=15 x∈[-0.513, -0.197] ΞΌ=-0.321 Οƒ=0.099
    tensor[5] x∈[-0.303, -0.197] ΞΌ=-0.243 Οƒ=0.055 [-0.197, -0.197, -0.303, -0.303, -0.215]
    tensor[5] x∈[-0.408, -0.232] ΞΌ=-0.327 Οƒ=0.084 [-0.250, -0.232, -0.338, -0.408, -0.408]
    tensor[5] x∈[-0.513, -0.285] ΞΌ=-0.394 Οƒ=0.102 [-0.303, -0.285, -0.390, -0.478, -0.513]
  tensor[3, 5] n=15 x∈[-1.316, -0.672] ΞΌ=-0.964 Οƒ=0.176
    tensor[5] x∈[-0.985, -0.672] ΞΌ=-0.846 Οƒ=0.123 [-0.672, -0.985, -0.881, -0.776, -0.916]
    tensor[5] x∈[-1.212, -0.724] ΞΌ=-0.989 Οƒ=0.179 [-0.724, -1.072, -0.968, -0.968, -1.212]
    tensor[5] x∈[-1.316, -0.828] ΞΌ=-1.058 Οƒ=0.179 [-0.828, -1.125, -1.020, -1.003, -1.316]
# Paramster is printed in a slingle line with the class name.
torch.nn.Parameter(torch.zeros(10, 10))
Parameter[10, 10] n=100 all_zeros grad=None
t = torch.empty(3, 3, device="meta")
t
tensor[3, 3] n=9 meta meta
image.rgb

in_stats = ( (0.485, 0.456, 0.406),     # mean
             (0.229, 0.224, 0.225) )    # std
image.rgb(in_stats)

mean = torch.tensor(in_stats[0])[:,None,None]
std = torch.tensor(in_stats[1])[:,None,None]

(image*std + mean).chans # all pixels in [0, 1] range

(image*0.3+0.5) # Slightly outside of [0, 1] range
tensor[3, 196, 196] n=115248 (0.4Mb) x∈[-0.135 |β–β–β–…β–ˆβ–β–β–β–β–‚β–| 1.292] ΞΌ=0.384 Οƒ=0.322
(image*0.3+0.5).chans # shows clipping (bright blue/red)

image.plt

image.plt(center="mean")

fig, ax = plt.subplots(figsize=(6, 2))
plt.close(fig)
image.plt(ax=ax)
fig