How to permute dimmensions in tensorflow?

Issue

I am able to permute the dimmension of the tensor: I’m able to do this in pytorch! But not in tensorflow!

A = torch.rand(1, 2,5)
A = A.permute(0,2,1) 
A.shape

torch.Size([1, 5, 2])

Tensorflow (just a try,I don’t know about this):

A = tf.random.normal(1, 2,5)
A = tf.keras.layers.Permute((0, 2, 1))

Not working

Solution

Use tf.transpose:

import tensorflow as tf

A = tf.random.normal((1, 2, 5))

A_t = tf.transpose(A, perm=[0, 2, 1])

print(A.shape, A_t.shape)
# (1, 2, 5) (1, 5, 2)

Answered By – AloneTogether

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published