python - 使用 tensorflow 将 3D 矩阵 reshape 为 2D 矩阵

标签 python tensorflow reshape

我有一个尺寸为 549x19x50 的 3D 矩阵,我需要创建一个 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。如果我们不能对列表执行此操作,有什么方法可以轻松地将列表转换为 ndarray?

提前致谢,

巴希特

最佳答案

使用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) 是这里的主要内容 - 您需要定义要交换的轴。

关于python - 使用 tensorflow 将 3D 矩阵 reshape 为 2D 矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38220133/

相关文章:

python - 在numpy矩阵中获取每行的多数元素

python - Pycrypto - 使用 Crypto.Random 生成 key 和初始化向量

java - 将tensorflow keras LSTM模型转换为.tflite或任何工作格式

python - 在 Pytorch 中实现 SeparableConv2D

python - 在 TensorFlow 2.0 中,如何将 TFRecord 数据提供给 keras 模型?

python - 将 3D Numpy 数组 reshape 为 2D 数组

c++ - PythonQt 的智能指针

r - 如何改变R中数据框的形状? (将具有相同名称的列堆叠在一起)

python-3.x - Julia 的 reshape

Python:通过函数传递memmap数组?