Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the negative index in shape arrays used for? (Tensorflow)

Tags:

tensorflow

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!

like image 466
gwtw14 Avatar asked Jun 21 '16 23:06

gwtw14


People also ask

How do you find the shape of a tensor in TensorFlow?

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.

What is shape in TensorFlow?

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. []).

How do I add a none dimension in TensorFlow?

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.

What is a tf tensor?

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 Answers

-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].

like image 145
Yao Zhang Avatar answered Nov 07 '22 13:11

Yao Zhang