使用Keras ValueError的多类分类:检查目标时出错:

时间:2018-07-08 12:29:14

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

我正在尝试建立一个模型来预测房屋的损坏。我正在为此使用Keras。

“ damage_grade”列中的1到5之间有5个值可以预测。数字越大,房屋遭受的破坏越大。

我还要提到我是Keras的初学者,这是我在Keras的第一个模特。我正在尝试从Keras documentation那里寻求帮助。

我的代码是:

X_train = rtrain_df.drop("damage_grade", axis=1) 
Y_train = rtrain_df["damage_grade"] 
X_test = rtest_df.drop("building_id", axis=1).copy() 
X_train.shape, Y_train.shape, X_test.shape

import keras 
from keras.models import Sequential 
from keras.layers import Dense, Dropout, Activation 
from keras.optimizers import SGD

model = Sequential() 
model.add(Dense(64, activation='relu', input_dim=46)) #there are 46 feature in my dataset to be trained 
model.add(Dropout(0.5)) 
model.add(Dense(64, activation='relu')) 
model.add(Dropout(0.5)) 
model.add(Dense(10, activation='softmax'))

model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

model.fit(X_train, Y_train, epochs=20, batch_size=128)

尝试拟合模型时,会出现以下错误:

  

ValueError:检查目标时出错:预期density_6的形状为(10,),但数组的形状为(1,)

大约有60万条记录需要培训

1 个答案:

答案 0 :(得分:1)

您的代码中有一些错误:

  • 您在最后一个Dense层中给出了10。它必须等于要预测的值的数量,即5。
  • 您必须将Y_train转换为具有5个分类特征(“ damage_grade”从0到4)的分类数组。

下面是更正的代码:

X_train = rtrain_df.drop("damage_grade", axis=1) 
Y_train = rtrain_df["damage_grade"] 
X_test = rtest_df.drop("building_id", axis=1).copy() 
X_train.shape, Y_train.shape, X_test.shape

import keras 
from keras.models import Sequential 
from keras.layers import Dense, Dropout, Activation 
from keras.optimizers import SGD

from keras.utils import np_utils
Y_train_cat = np_utils.to_categorical(Y_train) # converts into 5 categorical features

model = Sequential() 
model.add(Dense(64, activation='relu', input_dim=46))
model.add(Dropout(0.5)) 
model.add(Dense(64, activation='relu')) 
model.add(Dropout(0.5)) 
model.add(Dense(5, activation='softmax')) 

# last Dense layer is the output layer that'll produce the probabilities for the 5 
# outputs

model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

model.fit(X_train, Y_train_cat, epochs=20, batch_size=128)

import numpy as np

predictions = model.predict(X_test)
result = np.argmax(predictions,axis=1) # sets the output with max probability to 1