![](/img/trans.png)
[英]Prediction failed: Error when checking input: expected dense_input to have shape (2898,) but got array with shape (1,)
[英]Error when checking input: expected dense_input to have shape (21,) but got array with shape (1,)
如何修復輸入數組以滿足輸入形狀?
我試圖轉輸入數組,如所描述這里 ,但誤差是相同的。
ValueError:檢查輸入時出錯:期望dense_input具有形狀(21,)但是得到了具有形狀的數組(1,)
import tensorflow as tf
import numpy as np
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(40, input_shape=(21,), activation=tf.nn.relu),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(1, activation=tf.nn.softmax)
])
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
arrTest1 = np.array([0.1,0.1,0.1,0.1,0.1,0.5,0.1,0.0,0.1,0.6,0.1,0.1,0.0,0.0,0.0,0.1,0.0,0.0,0.1,0.0,0.0])
scores = model.predict(arrTest1)
print(scores)
您的測試數組arrTest1
是21的1d向量:
>>> arrTest1.ndim
1
您嘗試為模型提供的功能是一排21個功能。 您只需要一組括號:
arrTest1 = np.array([[0.1, 0.1, 0.1, 0.1, 0.1, 0.5, 0.1, 0., 0.1, 0.6, 0.1, 0.1, 0., 0., 0., 0.1, 0., 0., 0.1, 0., 0.]])
現在你有一行有21個值:
>>> arrTest1.shape
(1, 21)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.