目前,对于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')
返回数组的 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]]]