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

问题描述

我正在尝试使用学习的.h5文件进行预测.学习模型如下.

I am trying to predict using the learned .h5 file.The learning model is as follows.

model =Sequential()
model.add(Dense(12, input_dim=3, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(4, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])

然后,我将输入的形式编写如下.

And I wrote the form of the input as follows.

x = np.array([[band1_input[input_cols_loop][input_rows_loop]],[band2_input[input_cols_loop][input_rows_loop]],[band3_input[input_cols_loop][input_rows_loop]]])

prediction_prob = model.predict(x)

我认为形状正确,但是发生了以下错误.

I thought the shape was correct, but the following error occurred.

x的形状显然是(3,1),但是上述错误并没有消失(数据来自形式的csv文件).

The shape of x is obviously (3,1), but the above error doesn't disappear (the data is from a csv file in the form of (value 1, value 2, value 3, class)).

我该如何解决这个问题?

How can I solve this problem?

推荐答案

您是对的,但这不是keras期望的.预期为(1, 3)形状:按照惯例,轴0表示批次大小,轴1表示特征.第一个Dense层接受3个功能,这就是为什么它仅看到一个功能时就会抱怨的原因.

You are right, but that's not what keras expects. It expects (1, 3) shape: by convention, axis 0 denotes the batch size and axis 1 denotes the features. The first Dense layer accepts 3 features, that's why it complains when it sees just one.

解决方案就是简单地转置x.

The solution is simply to transpose x.

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

09-03 07:46