How to normalize a tensor to 0 mean and 1 variance?

Would this do it?

import torch
from torchvision import transforms

mu = 2
std = 0.5
t = torch.Tensor([1,2,3])
(t - 2)/0.5
# or if t is an image
transforms.Normalize(2, 0.5)(t)

see:

https://pytorch.org/docs/master/torchvision/transforms.html#torchvision.transforms.Normalize

1 Like