Un-normalizing PyTorch data
Below code : 

ux = torch.tensor(np.array([[255,1,255],[255,1,255]])).float()

print(ux)

ux = F.normalize(ux, p=2, dim=1)

print(ux)


prints : 

tensor([[ 255.,    1.,  255.],
        [ 255.,    1.,  255.]])
tensor([[ 0.7071,  0.0028,  0.7071],
        [ 0.7071,  0.0028,  0.7071]])


How can I un-normalize the ux in order to return to values 

tensor([[ 255.,    1.,  255.],
        [ 255.,    1.,  255.]])


from

tensor([[ 0.7071,  0.0028,  0.7071],
        [ 0.7071,  0.0028,  0.7071]])


There are various resources that detail this process such as https://discuss.pytorch.org/t/simple-way-to-inverse-normalize-a-batch-of-input-variable/12385/3 but do not detail unnormalizing result of F.normalize

F.normalize simply divides by the norm according to the documentation, so you simply need to multiply it by its magnitude.

This means you still need access to the magnitude of the original vector ux, otherwise, this is not possible, since the information about the magnitude cannot be recovered from the normalized vector.

Here's how this can be done:

# I modified the input to make it more interesting, but you can use any other value
ux = torch.tensor(np.array([[255,1,255],[101,10,123]])).float()

magnitude = ux.norm(p=2, dim=1, keepdim=True)    # NEW
ux = F.normalize(ux, p=2, dim=1)

ux_orig = ux * magnitude                         # NEW

print(ux_orig)
# Outputs:
# tensor([[255.,   1., 255.],
#         [101.,  10., 123.]])

