使用张量流将3D矩阵重塑为2D矩阵

时间:2016-07-06 09:06:07

标签: python tensorflow reshape

我有一个维度的3D矩阵,549x19x50我需要创建一个2D矩阵,它可以得到549x950矩阵。

到目前为止我所做的是使用tensorflow;

#data_3d is the 3D matrix
data_2d = tf.reshape(data_3d,[549,-1])

这会在提示中打印出data_3d的所有值,当我尝试访问data_2d时,它会给我一个NameError

data_3d是列表列表的列表。不是张量或ndarray。如果我们不能为列表执行此操作,是否有任何方法可以轻松地将列表转换为ndarrays?

提前致谢,

Bhashithe

1 个答案:

答案 0 :(得分:3)

使用numpy有一种简单的方法:

import numpy as np

data_3d = np.arange(27).reshape((3,3,3))
data_2d = data_3d.swapaxes(1,2).reshape(3,-1)

<强>输出继电器:

data_2d

[[0 3 6 1 4 7 2 5 8]
 [9 12 15 10 13 16 11 14 17]
 [18 21 24 19 22 25 20 23 26]]

print data_3d

[[[0 1 2]
  [3 4 5]
  [6 7 8]]

 [[9 10 11]
  [12 13 14]
  [15 16 17]]

 [[18 19 20]
  [21 22 23]
  [24 25 26]]]

注意swapaxes(1,2)是主要内容 - 您需要定义要交换的轴。

相关问题