Understanding tf.transpose()

To get an intuitive understanding about this function, you can execute following code and check the result

import tensorflow as tf

v1 = tf.constant([[[0, 1, 3, 5],[2, 3, 4, 5]], [[5, 6, 7, 8], [9, 10, 11, 12]]], name='Data')

result = tf.transpose(v1, perm=[0, 1, 2])
result_1 = tf.transpose(v1, perm=[0, 2, 1])
result_2 = tf.transpose(v1, perm=[1, 0, 2])
result_3 = tf.transpose(v1, perm=[2, 0, 1])

with tf.Session() as sess:
    print (sess.run(result))
    print (sess.run(result_1))
    print (sess.run(result_2))
    print (sess.run(result_3))

If the tensor is a 2D tensor, you can think this operation is a matrix transposition, but you should forget matrix transposition when it comes to higher dimension. In higher dimension case, it is more like a reshape operation plus a re position.

The most straight forward way to understand it is, first this function defines shape of transposed tensor by just rearranging each element of original shape. For example, we have a tensor whose shape is (2, 2, 4), perm is (2, 0, 1) , so we pick shape[2] first, then we pick shape[0] , then we pick shape[1], so we get a new tensor whose shape is (4, 2, 2).

Next step, each element's coordinate is changed according to perm too. For example, coordinate of number 9 is (1, 1, 0), its new coordinate should be (0, 1, 1)

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 在家里的床上醒来就是爽!拉屎期间一口气解决了两个礼拜的雷顿谜题。 因为昨天做完飞机吃了夜宵,今天午饭没什么胃口。吃...
    真昼之月阅读 1,022评论 0 0
  • 念儿子好:上周儿子回家,我和孩子爸认为他在学校小卖部消费太多了,建议他在食堂吃好饭,减少去小卖部买零食。这两天通过...
    于宏伟1472阅读 1,085评论 0 0
  • 重写(override) = 覆盖一般是子类继承父类后,重写其中的方法。重写有几个需要注意的规则:1、重写方法的参...
    yz_wang阅读 4,845评论 0 0

友情链接更多精彩内容