In the MNIST tutorial for Tensorflow, we reshape the output from the last Pool layer to a single vector. The code written was:
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
What is the -1 index for? Aren't we just trying to shape the output to a single vector, so why do we not just reshape to: [1, 7*7*64]?
Thanks in advance!
In TensorFlow, a tensor has both a static (inferred) shape and a dynamic (true) shape. The static shape can be read using the tf. Tensor. get_shape() method: this shape is inferred from the operations that were used to create the tensor, and may be partially complete.
Used in the notebooks tf. shape returns a 1-D integer tensor representing the shape of input . For a scalar input, the tensor returned has a shape of (0,) and its value is the empty vector (i.e. []).
You can either use "None" or numpy's "newaxis" to create the new dimension. General Tip: You can also use None in place of np. newaxis; These are in fact the same objects.
A tensor is a generalization of vectors and matrices to potentially higher dimensions. Internally, TensorFlow represents tensors as n-dimensional arrays of base datatypes. Each element in the Tensor has the same data type, and the data type is always known.
-1 means auto-expand. For example, a reshape with [-1, 7*7*64] will translate a 1-dimensional shape of, say, [19*7*7*64], to a 2-dimensional shape of [19, 7*7*64].
Another example, a reshape with [5, -1, 7] will translate a 1-dimensional shape of, say, [70], to a 3-dimensional shape of [5, 2, 7].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With