Why would casting tensor to numpy change the value? and how to avoid it?

Here are the values of the tensor and its numpy version using X.numpy()

tensor(28.0000)
27.999998

what i did in the original code is to normalize an image using transforms and then apply the inverse. the numbers printed above are the min value in the image.

I think it might just be a printing issue. Try to set the precision a bit higher using: torch.set_printoptions.

this is true I set torch.set_printoptions(precision=8) and the new values are:
tensor(27.99999809)
27.999998

it seems like the normalization inversion is not accurate to get the original value ‘28’ back

I think it’s not a problem of accuracy but rather of the floating point precision. Some values can’t be represented exactly. Have a look here for more information.

Yes I solved it with round.