[英]How tf.transpose works in tensorflow?
tf.transpose(a, perm=None, name='transpose')
transposes a. 转置一个。 It permutes the dimensions according to perm.
它根据烫发来排列尺寸。 So if I am using this matrix to transform:
所以,如果我使用这个矩阵进行转换:
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)))
Result is : 结果是:
[[[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
now if i use perm argument then : 现在,如果我使用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)))
Result is : 结果是:
[[[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
Due to this, I am confused and I have two questions : 因此,我很困惑,我有两个问题:
Looking at the numpy.transpose
documentation, we find that transpose
takes the argument 查看
numpy.transpose
文档,我们发现transpose
接受了参数
axes
: list of ints, optionalaxes
: 整数列表,可选
By default, reverse the dimensions, otherwise permute the axes according to the values given.默认情况下,反转尺寸,否则根据给定的值置换轴。
So the default call to transpose
translates into np.transpose(a, axes=[1,0])
for the 2D case, or np.transpose(a, axes=[2,1,0])
. 因此,对于2D情况,默认调用
transpose
转换为np.transpose(a, axes=[1,0])
,或np.transpose(a, axes=[2,1,0])
。
The operation you want to have here, is one that leaves the "depth" dimension unchanged. 您希望在此处执行的操作是保持“深度”维度不变的操作。 Therefore in the axes argument, the depth axes, which is the
0
th axes, needs to stay unchanged. 因此,在轴参数中,深度轴(即第
0
轴)需要保持不变。 The axes 1
and 2
(where 1 is the vertical axis), need to change positions. 轴
1
和2
(其中1是垂直轴)需要改变位置。 So you change the axes order from the initial [0,1,2]
to [0,2,1]
( [stays the same, changes with other, changes with other]
). 因此,您将轴顺序从初始
[0,1,2]
更改为[0,2,1]
( [stays the same, changes with other, changes with other]
)。
In tensorflow, they have for some reason renamed axes
to perm
. 在tensorflow,他们出于某种原因改名为
axes
,以perm
。 The argument from above stays the same. 上面的论点保持不变。
Concerning images, they differ from the arrays in the question. 关于图像,它们与问题中的数组不同。 Images normally have their x and y stored in the first two dimensions and the channel in the last,
[y,x,channel]
. 图像的x和y通常存储在前两个维度中,而通道最后存储在
[y,x,channel]
。
In order to "transpose" an image in the sense of a 2D transposition, where horizontal and vertical axes are exchanged, you would need to use 为了在2D换位的意义上“转置”图像,交换水平和垂直轴,你需要使用
np.transpose(a, axes=[1,0,2])
(channel stays the same, x and y are exchanged). (通道保持不变,x和y交换)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.