Best way to flatten a 2D tensor containing a vector in TensorFlow?
Solution 1:
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.
Solution 2:
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)>