我构建了一个需要 4 个输入维度的 NN。我现在只是将它用于单实例预测,但我不知道如何将维数增加到 4。当我最初训练和测试模型时,输入看起来像:
(16238, 40, 40, 1)
其中第一个维度是行数,第二个和第三个维度是矩阵维度,然后第四个维度是矩阵的深度。
现在,我只是尝试使用模型测试单个项目,但找不到增加维度的方法。就像:
>>> array = [[1, 1, 1], [1, 2, 3], [1, 2, 3]]
>>> array.shape
(3, 3)
所以我想将数组形状更改为:
(1, 3, 3, 1)
我试过了:
array = np.expand_dims(array, axis=-1)
但这没有用。
答案 0 :(得分:2)
我认为 array.reshape((1, 3, 3, 1))
是您要找的吗?
In [1]: import numpy as np
In [2]: array = np.asarray([[1, 1, 1], [1, 2, 3], [1, 2, 3]])
In [3]: array.shape
Out[3]: (3, 3)
In [4]: array
Out[4]:
array([[1, 1, 1],
[1, 2, 3],
[1, 2, 3]])
# keep original dimensions in 2nd and 3rd dimension
In [5]: reshaped = array.reshape((1, *array.shape, 1))
In [6]: reshaped.shape
Out[6]: (1, 3, 3, 1)
In [7]: reshaped
Out[7]:
array([[[[1],
[1],
[1]],
[[1],
[2],
[3]],
[[1],
[2],
[3]]]])