检查输入时出错:预期density_input的形状为(21,),但数组的形状为(1,) [英] Error when checking input: expected dense_input to have shape (21,) but got array with shape (1,)

查看:79
本文介绍了检查输入时出错:预期density_input的形状为(21,),但数组的形状为(1,)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to fix the input array to meet the input shape?

我尝试转置输入数组,如

I tried to transpose the input array, as described here, but an error is the same.

ValueError:检查输入时出错:预期density_input具有形状(21,)但具有形状(1,)的数组

ValueError: Error when checking input: expected dense_input to have shape (21,) but got array with shape (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的一维向量:

Your test array, arrTest1, is a 1d vector of 21:

>>> arrTest1.ndim
1

您要提供给模型的是一排21个功能.您只需要再加上一组括号即可:

What you are trying to feed your model is a row of 21 features. You simply need one more set of brackets:

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个值:

And now you have a row with 21 values:

>>> arrTest1.shape
(1, 21)

这篇关于检查输入时出错:预期density_input的形状为(21,),但数组的形状为(1,)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆