将[28,28,2]matlab数组转换为[2,28,28,1]十位数

2024-06-16 13:52:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我在学习tensorflow。在完成expert(https://www.tensorflow.org/get_started/mnist/pros)的tensorflow教程MNist之后,我尝试使用经过训练的模型来运行推理。你知道吗

  • 我制作了两个[28x28]图像并将它们放入[28x28x2]数组并保存了Matlab文件。

  • 然后我使用scipy.io将数组加载到python。你知道吗

    但是,我的网络需要一个[2, 28, 28, 1]张量。你知道吗

    如何将[28x28x2]数组转换成[2, 28, 28, 1]张量?


Tags: httpsorg模型图像gettensorflowwww教程
1条回答
网友
1楼 · 发布于 2024-06-16 13:52:43

首先,变换数组,使28x28x2变为2x28x28 (第三维度是第一个维度,然后是第一个维度,然后是第二个维度)。你知道吗

arr = arr.transpose((2, 0, 1))

Attention: you could have obtained the shape 2x28x28 by using arr.reshape((2, 28, 28)), but that would have messed up the order of your data. I used transpose because I believe you want arr[0] to be a picture, and the same for arr[1].

然后展开数组,得到最后一个维度

arr = np.expand_dims(arr, -1)

4x4代替28x28的示例:

>>> arr = np.empty((4, 4, 2))  # an empty array
>>> arr[..., :] = 0, 1  # first picture is all 0s and second is all 1s
>>> arr[..., 0]
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])
>>> arr[..., 1]
array([[ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.]])
>>> arr.shape
(4, 4, 2)

现在是转变

>>> arr = arr.transpose((2, 0, 1))
>>> arr = np.expand_dims(arr, -1)
>>> arr.shape
(2, 4, 4, 1)

相关问题 更多 >