python - tf.transpose 如何在 tensorflow 中工作?

标签 python numpy matrix matplotlib tensorflow

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 轴,需要保持不变。轴 12 (其中 1 是垂直轴),需要改变位置。因此,您更改了初始 [0,1,2] 的轴顺序至 [0,2,1] ([stays the same, changes with other, changes with other])。

在 tensorflow 中,由于某种原因,他们重命名为 axesperm .上面的论点保持不变。

图片

关于图像,它们与问题中的数组不同。图像通常将 x 和 y 存储在前两个维度中, channel 存储在最后一个维度中,[y,x,channel] .

为了在二维转置的意义上“转置”图像,其中水平轴和垂直轴交换,您需要使用

np.transpose(a, axes=[1,0,2])

( channel 保持不变,x 和 y 交换)。

enter image description here

关于python - tf.transpose 如何在 tensorflow 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43563609/

相关文章:

python - 线程 django-main-thread 中的异常

python - 从 Python Decimal 中提取数值

python - numpy 按两个字段(升序和降序)对结构化数组进行排序

python - 如果数组中的两个连续行在第一列中具有相同的字符串,则将第一行中的剩余条目设置为零

r - 如何检查矩阵是否包含特定行?

java - 将顶点着色器计算转换/移植到 Java

Python正则表达式,如何替换段落中两个单词之间的文本?

Python:将整数写入二进制文件

python - 无法将 Pandas Dataframe 列转换为 float

python - DataFrame 的给定列非零的行数