Tensorflow shape and reshape

How can we reshape a tensor in tensorflow using current shape of tensor. For example, if I want to reshape a 3x2 matrix in 2x3 matrix how can I do that. Like, the equivalent thing in numpy is:
x = np.reshape( x , ( x.shape[1] , x.shape[0] ) )

I know we can do it by taking transpose but is there any way to do it without using tf.Transpose.


Hi, @nomi.

You have tf.reshape, but be careful, reshaping does not change the order of tensor elements. It is not equivalent to taking the transpose:

>>> x = tf.constant([[1, 2], [3, 4], [5, 6]])
>>> tf.reshape(x, (x.shape[1], x.shape[0])).numpy()
array([[1, 2, 3],
       [4, 5, 6]], dtype=int32)
>>> tf.transpose(x).numpy()
array([[1, 3, 5],
       [2, 4, 6]], dtype=int32)

Thanks @nramon, you just made my day…!! This explanation means a lot and again thanks for correcting me.

1 Like

Thanks for the kind words, @nomi :slight_smile: Enjoy the rest of the course!

1 Like

@nramon Sir you deserve it.

1 Like