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:
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.Tensor: shape=(2, 2), dtype=int32, numpy= array([[1, 2], [3, 4]], dtype=int32)>
tf.reshape(w,[-1]) will output
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([1, 2, 3, 4], dtype=int32)>