monkey_patch()
🙉 Monkey-patching
monkey_patch
monkey_patch (cls=<class 'torch.Tensor'>)
Monkey-patch lovely features into cls
= torch.load("mysteryman.pt") image
/tmp/ipykernel_172230/672345934.py:1: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
image = torch.load("mysteryman.pt")
= 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 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
3,:3,:5].deeper(depth=2) image[:
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.
10, 10)) torch.nn.Parameter(torch.zeros(
Parameter[10, 10] n=100 all_zeros grad
= torch.empty(3, 3, device="meta")
t t
tensor[3, 3] n=9 meta meta
image.rgb
= ( (0.485, 0.456, 0.406), # mean
in_stats 0.229, 0.224, 0.225) ) # std
( image.rgb(in_stats)
= torch.tensor(in_stats[0])[:,None,None]
mean = torch.tensor(in_stats[1])[:,None,None]
std
*std + mean).chans # all pixels in [0, 1] range (image
*0.3+0.5) # Slightly outside of [0, 1] range (image
tensor[3, 196, 196] n=115248 x∈[-0.135, 1.292] μ=0.384 σ=0.322
*0.3+0.5).chans # shows clipping (bright blue/red) (image
image.plt
="mean") image.plt(center
= plt.subplots(figsize=(6, 2))
fig, ax
plt.close(fig)=ax)
image.plt(ax fig