Week3, Normalizing components of image tensor

Hello,
Near the beginning of the final programming exercise of Week 3 (Course 2), inside the normalize function, there was a normalization of image tensor done as following:
image = tf.cast(image, tf.float32) / 256.0
This is before the image tensor of shape (64,64,3) was reshaped to (64643,1) tensor. My question specifically is why divide by the number 256.0 to normalize?
Thank you so much in advance.

Regards,
Nay

Hi @Nay,

The way the images of this dataset are stored is with every pixel having three values (one for each RGB channel) in the range (0, 255), so in order to normalize the values you divide by 256.0.

2 Likes

Right! They are RGB images, so the pixel color values are 8 bit unsigned ints meaning the range is 0 to 255. Dividing by 256 converts them to floating point values between 0 and 1, which will be a lot more stable for Gradient Descent.

1 Like