![](/img/trans.png)
[英]Keras model.predict Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (128, 56)
[英]Keras: Model Predict, Error When Checking Input Shape
我有一個Keras模型和一個numpy數組,我想在上面調用predict
。 具體來說,我有:
名為test
的numpy.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期望一組樣本能夠做出預測-它需要一組 shape (180,)
數組。 似乎您正在傳遞一個樣本,它將被解釋為180個形狀為(1,)
樣本。您可以嘗試將此一個樣本包裝在數組中,或者使用test.reshape(1, -1)
創建一組。 換句話說,將數組傳遞給(1, 180)
,它將返回一個預測的數組。
Keras需要批量數據,因此這里有一些有關數據形狀的提示,您應該知道:
數據的形狀應為:
因此您的測試數據應具有(1,180)的形狀。 只需重塑您的數據即可:
test.reshape(1,-1)
您應該知道的另一件事是,當Keras需要特定形狀時,它表示一個樣本的形狀,不包括實際輸入數據的第一維: (samples,)
。 但是您的數據應具有樣本維度。 Keras將處理第一維,即單獨的樣本數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.