tensorflow中的不懂得知识点——转置函数 transpose

目前,对于tensorflow处于学习阶段,将不会的知识点进行罗列,以后明白了解了在对其进行补充。


1、转置函数 transpose

该函数包含两点:

(1)

在tensorflow的教学网站上给出了一段可以实现图像逆时针旋转90度的程序代码,对代码实现后并未出现结果图同时代码也未报错。代码如下:

import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import tensorflow as tf

#加载图像
filename = "MarshOrchid.jpg"
image = mpimg.imread(filename)

#创建tensorflow变量
x = tf.Variable(image,name='x')

model = tf.initialize_all_variables()

with tf.Session() as session:
    x = tf.transpose(x, perm=[1,0,2])
    session.run(model)
    result = session.run(x)
  
plt.imshow(result)
plt.show()

解决:

上方代码是可以实现转置的,可能我的环境是虚拟机,在进行图像处理时,内存占用比较大,导致程序中断,今天做图像翻转时开始也不好使,加载图像都加载不了,后来重启了eclipse,结果就出来了,然后就试了一下转置的代码,代码也显示正常。

(2)该函数如何实现的转置,其perm值该如何设置。


以下内容是从wiki上找到的:


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 dimension perm[i]. If perm is not given, it is set to (n-1...0), where n is the rank of the input tensor. Hence by default, this operation performs a regular 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=[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-0
tf.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.
</pre><div><strong><span style="font-size:18px; color:#cc0000">对其做了简单的翻译:</span></strong></div><div><strong><span style="font-size:18px; color:#cc0000"></span></strong></div><div><pre class="python" name="code">tf.transpose(a, perm=None, name='transpose')

其中,a 是一个张量(Tensor),实际上就是一个数组
perm 是 a 维度的置换
name :操作的名称
其返回的是一个转置的张量。


a的转置是根据 perm 的设定值来进行的。

返回数组的 dimension(尺寸、维度) i与输入的 perm[i]的维度相一致。如果未给定perm,默认设置为 (n-1...0),这里的 n 值是输入变量的 rank 。因此默认情况下,这个操作执行了一个正规(regular)的2维矩形的转置


例如:


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


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

tf.transpose(x) 等价于:
tf.transpose(x perm=[1, 0]) ==> [[1 4]
                                                    [2 5]
                                                    [3 6]]


'perm' 更多的用于 n维矩阵的转置,例如 n > 2时


'x' =  [[[1  2  3]
           [4  5  6]]
         [[7  8  9]
           [10 11 12]]]
在 dimension-0 上进行矩阵的转置


tf.transpose(b, perm=[0, 2, 1]) ==> [[[1  4]
                                      [2  5]
                                      [3  6]]

                                     [[7 10]
                                      [8 11]
                                      [9 12]]]


  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值