Best way to flatten a 2D tensor containing a vector in TensorFlow?

Question:

What is the most efficient way to flatten a 2D tensor which is actually a horizontal or vertical vector into a 1D tensor?

Is there a difference in terms of performance between:

tf.reshape(w, [-1])

and

tf.squeeze(w)

?

Asked By: Andrzej Pronobis

||

Answers:

Both tf.reshape(w, [-1]) and tf.squeeze(w) are “cheap” in that they operate only on the metadata (i.e. the shape) of the given tensor, and don’t modify the data itself. Of the two tf.reshape() has slightly simpler logic internally, but the performance of the two should be indistinguishable.

Answered By: mrry

For a simple 2D tensor the two should function identically, as mentioned by @sv_jan5. However, please note that tf.squeeze(w) only squeezes the first layer in the case of a multilayer tensor, whereas tf.reshape(w,[-1]) will flatten the entire tensor regardless of depth.

For example, let’s look at

w = [[1,2,],[3,4]]    

now the output of the two functions will no longer be the same. tf.squeeze(w)will output

<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[1, 2],
       [3, 4]], dtype=int32)>

while tf.reshape(w,[-1]) will output

<tf.Tensor: shape=(4,), dtype=int32, numpy=array([1, 2, 3, 4], dtype=int32)>
Answered By: Calimero
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.