TensorFlow tf.reshape Fortran订单(如numpy)

eri*_*ell 10 python numpy reshape tensorflow

TensorFlow是否提供了一种重塑Fortran中的张量的方法(列主要顺序?NumPy允许:

a = ...
np.reshape(a, (32, 32, 3), order='F')
Run Code Online (Sandbox Code Playgroud)

我正在尝试将CIFAR图像重塑为32x32x3(来自形状为3072x1的矢量),但我得到的图像看起来像这样:

示例CIFAR图像

在Numpy中使用Fortran命令解决了这个问题,但我需要在TensorFlow中做同样的事情.

编辑: 我现在意识到我可以通过重新整形到3x32x32然后转置输出来获得正确的输出.我仍然有点惊讶的是,TF不提供开箱即用的主要列主要顺序重塑.

Dav*_*vid 5

Tensorflow似乎不支持Fortran(列主)排序,但有一个简单的解决方案。您必须将重塑与转置结合起来。下面的代码使用 numpy 来显示 Tensorflow 版本后面的等效操作。

麻木:

>>> import numpy as np
>>> want = np.arange(12).reshape((4,3),order='F')
>>> want
array([[ 0,  4,  8],
       [ 1,  5,  9],
       [ 2,  6, 10],
       [ 3,  7, 11]])
>>> have = np.arange(12).reshape((3,4))
>>> have
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> have.transpose()
array([[ 0,  4,  8],
       [ 1,  5,  9],
       [ 2,  6, 10],
       [ 3,  7, 11]])
Run Code Online (Sandbox Code Playgroud)

Tensorflow:(假设您最终想要 MxN)

want = tf.transpose(tr.reshape(input,(n,m)))
Run Code Online (Sandbox Code Playgroud)