Why do you need to divide by 255 when using stack function?

I’m trying to understand the concept behind dividing by 255 when you call the stack function when placing all images into a rank-3 tensor. This is related to the 04_mnist_basics code.

stacked_sevens = torch.stack(seven_tensors).float()/255
stacked_threes = torch.stack(three_tensors).float()/255

There was no explanation as to why the number to divide by is 255 and I can’t figure out where the math for this comes into play.

found the answer to my own question.

As the pixel values range from 0 to 256, apart from 0 the range is 255. So dividing all the values by 255 will convert it to range from 0 to 1.

1 Like