【问题标题】:Error when checking input: expected dense_input to have shape (21,) but got array with shape (1,)检查输入时出错:预期的 dense_input 的形状为 (21,) 但得到的数组的形状为 (1,)
【发布时间】:2019-02-12 02:16:55
【问题描述】:

如何固定输入数组以满足输入形状?

我尝试转置输入数组,如here 所述,但错误相同。

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)

【问题讨论】:

  • Tgsmith61591,非常感谢,它有效:),只需添加额外的括号 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]])
  • 请接受答案...

标签: python tensorflow machine-learning neural-network keras


【解决方案1】:

您的测试数组 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)

【讨论】:

    猜你喜欢
    • 2020-05-17
    • 2020-06-30
    • 2020-09-04
    • 2019-10-22
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    • 2021-06-08
    • 2020-04-11
    相关资源
    最近更新 更多