ValueError:检查时出错:预期dense_1_input具有形状(3,)但得到形状为(1,)的数组 [英] ValueError: Error when checking : expected dense_1_input to have shape (3,) but got array with shape (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.
ValueError:检查时出错:预期dense_1_input 具有形状(3,) 但得到形状为(1,) 的数组
ValueError: Error when checking : expected dense_1_input to have shape (3,) but got array with shape (1,)
x
的形状显然是(3,1)
,但是上面的错误并没有消失(数据来自一个csv文件,格式为<代码>(值 1,值 2,值 3,类)).
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?
推荐答案
x的形状明明是
(3,1)
,但是上面的错误还在继续.
The shape of x is obviously
(3,1)
, but the above error continues.
你是对的,但这不是 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:检查时出错:预期dense_1_input具有形状(3,)但得到形状为(1,)的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!