Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the x variable tensor reshaped with -1 in the MNIST tutorial for tensorflow?

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?

like image 501
tt_Gantz Avatar asked Jan 21 '17 11:01

tt_Gantz


People also ask

How does tf reshape works?

The tf. reshape does not change the order of or the total number of elements in the tensor, and so it can reuse the underlying data buffer. This makes it a fast operation independent of how big of a tensor it is operating on. To instead reorder the data to rearrange the dimensions of a tensor, see tf.


1 Answers

1) What does -1 mean in the reshaping vector

From the documentation of reshape:

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, a shape of [-1] flattens into 1-D. At most one component of shape can be -1.

this is a standard feature and is available in numpy as well. 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

2) Why is x being reshaped

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 to 4 dimensions. I do not know the point of the forth dimension because in my opinion they might have used only (x, y, color). Or even (x, y). Try to modify their reshape and convolution and most probably you will get similar accuracy.

like image 72
Salvador Dali Avatar answered Sep 19 '22 12:09

Salvador Dali