Python “np.transpose(image_tensor,(2,1,0))”这行代码是做什么的?

Python “np.transpose(image_tensor,(2,1,0))”这行代码是做什么的?,python,numpy,Python,Numpy,我在看一些代码,有一行代码说: # transpose to standard format # You might want to comment this line or reverse the shuffle # if you will use a learning algorithm like CNN, since they like their channels separated. image_standard_form = np.transpose(image, (2, 1, 0)

我在看一些代码,有一行代码说:

# transpose to standard format
# You might want to comment this line or reverse the shuffle
# if you will use a learning algorithm like CNN, since they like their channels separated.
image_standard_form = np.transpose(image, (2, 1, 0))
我不知道它做了什么。我查看了参数,但我不太明白转置参数中的“轴”是什么意思。它说:

axes : list of ints, optional
By default, reverse the dimensions, otherwise permute the axes according to the values given.
但它并没有说这是为了什么。另外,使用转置和元组的示例也不是很有见地(或者至少没有告诉我它应该做什么)。有人能给我解释一下它应该做什么吗

我也做了一个自己的例子来了解它的作用,但我不是100%理解它:

>>> x
array([[[ 0.,  1.,  2.],
        [ 0.,  1.,  2.],
        [ 0.,  1.,  2.]],

       [[ 0.,  1.,  2.],
        [ 0.,  1.,  2.],
        [ 0.,  1.,  2.]],

       [[ 0.,  1.,  2.],
        [ 0.,  1.,  2.],
        [ 0.,  1.,  2.]]])
>>> np.transpose(x, (2, 1, 0))
array([[[ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]],

       [[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.]],

       [[ 2.,  2.,  2.],
        [ 2.,  2.,  2.],
        [ 2.,  2.,  2.]]])

假设您希望通过以下方式访问元素:

elem = image[i, j, k]
elem = image_standard_form[k, j, i]
转置后,现在您应该使用以下命令访问同一元素:

elem = image[i, j, k]
elem = image_standard_form[k, j, i]
转置中的
(2,1,0)
表示索引的排列

对于CNN来说,它可能想要转变一个形状张量:

[width, height, channels]
进入:


从一个数组开始,比如
np.arange(24)。重塑(2,3,4)
。当形状和值都不同时,更容易识别像这样的函数的动作。此转置是普通2d转置的推广。请确保您理解这一点,并尝试想象阵列为3d或更大时的可能性。我在看TensorFlow()中卷积/互相关的文档,似乎他们使用了给定形状的输入张量(批次、高度、宽度、通道),用于CNN。所以最后的频道似乎是。@Pinocchio,这要看情况而定。通过向
TF.nn.conv2d
提供
data\u format
参数,您可以在TF中将图像格式设置为
NCHW
NHWC
(默认值)。我发现非常神秘的一点是,如果您给出
(0,2,3,1)
,那么图像就会显示在各个方向。你知道发生了什么事吗?