检查输入时出错:预期dense_input的形状为(21,),但得到的数组的形状为(1,)

2024-01-07

如何修复输入数组以满足输入形状?

我尝试转置输入数组,如上所述here https://stackoverflow.com/questions/50336110/valueerror-error-when-checking-expected-dense-1-input-to-have-shape-3-but,但错误是相同的。

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 的一维向量:

>>> 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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

检查输入时出错:预期dense_input的形状为(21,),但得到的数组的形状为(1,) 的相关文章

随机推荐