tf.transpose(a, perm=None, name='transpose')
转置a。它根据 perm 排列尺寸。因此,如果我使用此矩阵进行转换:
import tensorflow as tt
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]="3"
import numpy as bb
ab=([[[1,2,3],[6,5,4]],[[4,5,6],[3,6,3]]])
v=bb.array(ab)
fg=tt.transpose(v)
print(v)
with tt.Session() as df:
print("\n New tranformed matrix is: \n\n{}".format(df.run(fg)))
结果是:
[[[1 2 3]
[6 5 4]]
[[4 5 6]
[3 6 3]]]
New tranformed matrix is:
[[[1 4]
[6 3]]
[[2 5]
[5 6]]
[[3 6]
[4 3]]]
Process finished with exit code 0
现在如果我使用 perm 参数则:
import tensorflow as tt
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]="3"
import numpy as bb
ab=([[[1,2,3],[6,5,4]],[[4,5,6],[3,6,3]]])
v=bb.array(ab)
fg=tt.transpose(v,perm=[0,2,1])
print(v)
with tt.Session() as df:
print("\n New tranformed matrix is: \n\n{}".format(df.run(fg)))
结果是:
[[[1 2 3]
[6 5 4]]
[[4 5 6]
[3 6 3]]]
New tranformed matrix is:
[[[1 6]
[2 5]
[3 4]]
[[4 3]
[5 6]
[6 3]]]
Process finished with exit code 0
因此,我很困惑,我有两个问题:
- 每当我想转置一个矩阵时,我必须给 perm[0,2,1] 作为 默认 ?
- 这里的 0,2,1 是什么?
最佳答案
查看 numpy.transpose
文档,我们发现 transpose
接受争论
axes
: list of ints, optional
By default, reverse the dimensions, otherwise permute the axes according to the values given.
所以默认调用transpose
翻译成np.transpose(a, axes=[1,0])
对于 2D 情况,或 np.transpose(a, axes=[2,1,0])
.
您希望在此处进行的操作是使“深度”维度保持不变的操作。因此在 axes 参数中,深度轴是 0
th 轴,需要保持不变。轴 1
和 2
(其中 1 是垂直轴),需要改变位置。因此,您更改了初始 [0,1,2]
的轴顺序至 [0,2,1]
([stays the same, changes with other, changes with other]
)。
在 tensorflow 中,由于某种原因,他们重命名为 axes
至 perm
.上面的论点保持不变。
图片
关于图像,它们与问题中的数组不同。图像通常将 x 和 y 存储在前两个维度中, channel 存储在最后一个维度中,[y,x,channel]
.
为了在二维转置的意义上“转置”图像,其中水平轴和垂直轴交换,您需要使用
np.transpose(a, axes=[1,0,2])
( channel 保持不变,x 和 y 交换)。
关于python - tf.transpose 如何在 tensorflow 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43563609/