tf.transpose() 详解 —》理解为主

 tensorflow中用于转置,变换维度的方法tf.transpose():

def transpose(a, perm=None, name="transpose", conjugate=False):
     
-- Args:
    a: A `Tensor`.
    perm: A permutation of the dimensions of `a`.
    name: A name for the operation (optional).
    conjugate: Optional bool. Setting it to `True` is mathematically equivalent
      to tf.conj(tf.transpose(input)).

方法维度变换详解:

import tensorflow as tf
import numpy as np
sess = tf.Session()
sess.run(tf.global_variables_initializer())

# origin datas
test_data = np.arange(1, 25).reshape([2, 3, 4])
print('test_data: \n', test_data)
print('test_data.shape: \n', test_data.shape)
# test_data:
#  [[[ 1  2  3  4]
#   [ 5  6  7  8]
#   [ 9 10 11 12]]
#
#  [[13 14 15 16]
#   [17 18 19 20]
#   [21 22 23 24]]]
# test_data.shape:
#  (2, 3, 4)
print('-------------------------------------')

# transpose
transpose_1_0_2 = tf.transpose(test_data, [1, 0, 2])
transpose_1_0_2 = sess.run(transpose_1_0_2)
print('transpose_1_0_2: \n', transpose_1_0_2)
print('transpose_1_0_2.shape: \n', transpose_1_0_2.shape)
# transpose_1_0_2:
#  [[[ 1  2  3  4]
#   [13 14 15 16]]
#
#  [[ 5  6  7  8]
#   [17 18 19 20]]
#
#  [[ 9 10 11 12]
#   [21 22 23 24]]]
# transpose_1_0_2.shape:
#  (3, 2, 4)
print('-------------------------------------')

transpose_0_2_1 = tf.transpose(test_data, [0, 2, 1])
transpose_0_2_1 = sess.run(transpose_0_2_1)
print('transpose_1_0_2: \n', transpose_0_2_1)
print('transpose_1_0_2.shape: \n', transpose_0_2_1.shape)
# transpose_1_0_2:
#  [[[ 1  5  9]
#   [ 2  6 10]
#   [ 3  7 11]
#   [ 4  8 12]]
#
#  [[13 17 21]
#   [14 18 22]
#   [15 19 23]
#   [16 20 24]]]
# transpose_1_0_2.shape:
#  (2, 4, 3)
# 这个有两个理解方法:
# 方法一:
    # 第一个维度保持不变,第二个维度和第三个维度求转置
# 方法二:
    # 具体到一个实例中:
        # 在经过单层RNN动态rnn循环后, outputs输出维度为(样本数, 序列数, 隐藏层维度)
        # 如果tf.transpose(outputs, [0, 2, 1]) 后,此时维度变为了(样本数, 隐藏层维度, 序列数)
        # 此时,最外层是样本,那么在第一个样本下,第一个隐藏层中的第一个序列数据 为 转置前outputs第一个序列数且隐藏层维度为一的数据
        # 第一个样本下,第一个隐藏层中的数据    =====对应=====》ouputs中第一个样本下,所有序列中,第一个隐藏层的数据集合
        # 总体来说,就是无论是转置前,还是转置后,数据的归属没有变化。只是维度的转换而已
print('-------------------------------------')

transpose_2_1_0 = tf.transpose(test_data, [2, 1, 0])
transpose_2_1_0 = sess.run(transpose_2_1_0)
print('transpose_1_0_2: \n', transpose_2_1_0)
print('transpose_1_0_2.shape: \n', transpose_2_1_0.shape)
# transpose_1_0_2:
#  [[[ 1 13]    # 表示第一个编码层中,第一个样本下,所有序列的数据(还可以再分,一个意思)
#   [ 5 17]    # 表示第一个编码层中,第二个样本下,所有序列的数据
#   [ 9 21]]    # 表示第一个编码层中,第三个样本下,所有序列的数据
#
#  [[ 2 14]    # 表示第二个编码层中,第一个样本下,所有序列的数据
#   [ 6 18]    # 表示第二个编码层中,第二个样本下,所有序列的数据
#   [10 22]]    # 表示第二个编码层中,第三个样本下,所有序列的数据
#
#  [[ 3 15]    # 表示第三个编码层中,第一个样本下,所有序列的数据
#   [ 7 19]    # 表示第三个编码层中,第二个样本下,所有序列的数据
#   [11 23]]    # 表示第三个编码层中,第二个样本下,所有序列的数据
#
#  [[ 4 16]    # 表示第四个编码层中,第一个样本下,所有序列的数据
#   [ 8 20]    # 表示第四个编码层中,第二个样本下,所有序列的数据
#   [12 24]]]    # 表示第四个编码层中,第二个样本下,所有序列的数据
# transpose_1_0_2.shape:
#  (4, 3, 2)
print('-------------------------------------')

# 通过底层实现tf.transpose()效果
print(test_data)
test_data = np.arange(1, 121).reshape([2, 3, 4, 5])
a, b, c, d = test_data.shape
print(a, b, c, d)
# 底层遍历实现各维度转置
perm = [0, 1, 3, 2]
target_data = np.zeros([2, 3, 5, 4])
for i in range(c):
    for j in range(d):
        target_data[:, :, j, i] = test_data[:, :, i, j]
print('', target_data)
print(target_data.shape)

# through tf.transpose() to get the result
tr_0_1_3_2 = tf.transpose(test_data, perm)
tr_0_1_3_2 = sess.run(tr_0_1_3_2)
print(target_data == tr_0_1_3_2)    # 通过比较底层实现结果和tf.transpose验证结果
# [[[[ True  True  True  True]
#    [ True  True  True  True]
#    [ True  True  True  True]
#    [ True  True  True  True]
#    [ True  True  True  True]] 。。。]]



 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值