Numpy ndarray与不同形状的学习模型在Keras

问题描述 投票:1回答:1

我有一个具有不同形状的numpy数组列表。我尝试将此列表转换为numpy数组,以便为​​Keras创建批处理示例。在输出中我想要一个带有形状的数组(batch_size,?,20),其中'?'是可变维度。我试试这个:

a = np.random.random((5,20))
b = np.random.random((2,20))
c = np.random.random((7,20))
d = [a,b,c]
np.array(d).shape
> (3,)

当我将此批次发送给Keras时,我遇到以下问题:

ValueError: Error when checking input: expected Input_Dim to have 3 dimensions, but got array with shape (3, 1)
python numpy multidimensional-array keras shapes
1个回答
1
投票

也许这个简单的例子可以帮助:

import numpy as np

a = np.random.random((5,20))
b = np.random.random((2,20))
c = np.random.random((7,20))

d = np.array([a,b,c])
print(d.shape) # (3,)

d = d[np.newaxis]
print(d.shape) # (1, 3)
© www.soinside.com 2019 - 2024. All rights reserved.