gpt4 book ai didi

python - Keras:模型预测,检查输入形状时出错

转载 作者:行者123 更新时间:2023-12-01 09:02:53 27 4
gpt4 key购买 nike

我有一个 Keras 模型和一个 numpy 数组,我想对其调用predict。具体来说,我有:

一个名为 testnumpy.ndarray ,如下所示:

array([    0,     0,     0,     0,     0,     0,     0,     0,     0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 12920, 11891, 4605, 2425, 6780, 5096,
13821, 4405, 10345, 4468, 5910, 11891, 10906, 14994, 12073,
8581, 3544, 13846, 3110, 2425, 3407, 9631, 13846, 4479,
9964, 2556, 4479, 2686, 8895, 10959, 1531, 11891, 1494,
10376, 13846, 12856, 13846, 3110, 2425, 3407, 3267, 181,
4479, 14842, 4639, 7723, 11891, 11449, 2425, 5662, 2282,
5129, 2518, 13846, 4479, 4780, 2598, 4926, 543, 7304,
12020, 8143, 10998, 13846, 12853, 13846, 12856, 11891, 3785,
9131, 7448, 13846, 10376, 13846, 8245, 3788, 12211, 2425,
13614, 10049, 2556, 8245, 1406, 6423, 3110, 2425, 3407,
5726, 2619, 1494, 13694, 7434, 12086, 7304, 3267, 9184])

当我执行test.shape时,我看到:

(180,)

当我执行model.predict(test)时,我得到:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "...python2.7/site-packages/keras/engine/training.py", line 1152, in predict
x, _, _ = self._standardize_user_data(x)
File "...python2.7/site-packages/keras/engine/training.py", line 754, in _standardize_user_data
exception_prefix='input')
File "...python2.7/site-packages/keras/engine/training_utils.py", line 136, in standardize_input_data
str(data_shape))
ValueError: Error when checking input: expected sequence to have shape (180,) but got array with shape (1,)

知道我做错了什么吗?

谢谢!

最佳答案

Keras 需要批量数据,因此以下是您应该了解的有关数据形状的一些提示:

数据的形状应该是:

  • 对于矢量数据:(样本、特征)
  • 对于时间序列数据:(样本、时间步长、特征)
  • 对于图像数据:(样本、高度、宽度、 channel )或(样本、 channel 、高度、宽度)
  • 对于视频数据:(样本、帧、高度、宽度、 channel )或(样本、帧、 channel 、高度、宽度)

所以你的测试数据应该具有 (1, 180) 的形状。只需 reshape 您的数据即可:

test.reshape(1,-1)

您应该知道的另一件事是,当 Keras 想要特定形状时,它意味着一个样本的形状,不包括真实输入数据的第一个维度:(samples,)。但您的数据应该具有样本维度。 Keras 将处理第一个维度,即样本数量。

关于python - Keras:模型预测,检查输入形状时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52322794/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com