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

Each Answer to this Q is separated by one/two green lines.

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)

?

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.

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)>


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .