monkey_patch()π Monkey-patching
monkey_patch
def monkey_patch(
cls:_TensorMeta=Tensor
):
Monkey-patch lovely features into cls
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))
spicytensor[2, 6] n=12 xβ[-3.541e+03, -3.369e-05] ΞΌ=-393.776 Ο=1.180e+03 +Inf! -Inf! NaN!
spicy.vtensor[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.ptensor([[-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.deepertensor[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")
ttensor[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] rangetensor[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.pltimage.plt(center="mean")fig, ax = plt.subplots(figsize=(6, 2))
plt.close(fig)
image.plt(ax=ax)
fig