tensorflow官方文档有误 关于transpose

tf.transpose(a, perm=None, name='transpose')

Transposes a. Permutes the dimensions according to perm.

The returned tensor's dimension i will correspond to the input dimensionperm[i]. If perm is not given, it is set to (n-1...0), where n isthe rank of the input tensor. Hence by default, this operation performs aregular matrix transpose on 2-D input Tensors.

For example:

# 'x' is [[1 2 3]
#         [4 5 6]]
tf.transpose(x) ==> [[1 4]
                     [2 5]
                     [3 6]]

# Equivalently
tf.transpose(x perm=[0, 1]) ==> [[1 4]
                                 [2 5]
                                 [3 6]]
应该是:
tf.transpose(x perm=[1,0]) ==> [[1 4]
                                 [2 5]
                                 [3 6]]


# 'perm' is more useful for n-dimensional tensors, for n > 2# 'x' is [[[1 2 3]# [4 5 6]]# [[7 8 9]# [10 11 12]]]# Take the transpose of the matrices in dimension-0tf.transpose(b, perm=[0, 2, 1]) ==> [[[1 4] [2 5] [3 6]] [[7 10] [8 11] [9 12]]]
Args:
  • a: A Tensor.
  • perm: A permutation of the dimensions of a.
  • name: A name for the operation (optional).
Returns:

A transposed Tensor.


我的测试代码:

import tensorflow as tf
x = tf.constant([[1, 2 ,3],[4, 5, 6]])


b=tf.transpose(x)


# Equivalently
c=tf.transpose(x ,[1,0])


# 'perm' is more useful for n-dimensional tensors, for n > 2
# 'x' is   [[[1  2  3]
#            [4  5  6]]
#           [[7  8  9]
#            [10 11 12]]]
# Take the transpose of the matrices in dimension-0
#tf.transpose(b, perm=[0, 2, 1])
with tf.Session() as sess:
    print (sess.run(x))
    print (sess.run(b))
    print (sess.run(c))

效果:

[[1 2 3]
 [4 5 6]]
[[1 4]
 [2 5]
 [3 6]]
[[1 4]
 [2 5]
 [3 6]]



猜你喜欢

转载自blog.csdn.net/liyaoqing/article/details/54095920