Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
in AI and Deep Learning by (50.2k points)

I'm following the TensorFlow tutorial

Initially, x is defined as

x = tf.placeholder(tf.float32, shape=[None, 784])

Later on, it reshapes x, I'm trying to understand why.

To apply the layer, we first reshape x to a 4d tensor, with the second and third dimensions corresponding to image width and height, and the final dimension corresponding to the number of color channels.

x_image = tf.reshape(x, [-1,28,28,1])

What does -1 mean in the reshaping vector and why is x being reshaped?

1 Answer

0 votes
by (108k points)

 If one component of shape is the special value -1, the size of that dimension is computed so that the total size remains constant. In particular, the shape of [-1] flattens into 1-D. One component of shape can be -1. Basically, it means - I do not have time to calculate all the dimensions, so infer the one for me. In your case because x * 28 * 28 * 1 = 784 so your -1 = 1

now coming to your second question, x is being reshaped because they are planning to use convolution for image classification. So they need to use some spatial information. Current data is 1 dimensional. So they transform it into 4 dimensions. the 4th dimension is the batch. Now, why 4 dimensions?

TensorFlow’s convolutional conv2d operation expects a 4-dimensional tensor with dimensions corresponding to batch, width, height, and channel.

[batch, in_height, in_width, in_channels]

If you wish to know more about TensorFlow then visit this TensorFlow Tutorial.


Browse Categories