Keras CNN中的标签预处理

时间:2018-05-08 18:44:58

标签: python tensorflow neural-network keras conv-neural-network

我有一个600行和271列的数据集(最后一列包含类)。每行包含恶意软件功能及其类标签。我正在编写一个卷积神经网络代码来预测这些类。共有9个班级。以下是我的代码:

 dataset = pd.read_csv('train.csv')
 X = dataset.iloc[:, 0:270].values
 y = dataset.iloc[:, 270].values
 from sklearn.preprocessing import LabelEncoder, OneHotEncoder
 from sklearn.model_selection import train_test_split
 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20)
 model = Sequential()
 model.add(Convolution1D(64, 10, input_shape=(643,270)))
 model.add(Activation('relu'))
 model.add(MaxPooling1D(1))
 model.add(Flatten())
 model.add(Dense(100))
 model.add(Dropout(0.5))
 model.add(Dense(9))
 model.add(Activation('softmax'))
 model.compile(loss='categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
 X_train = np.reshape(X_train, (1, X_train.shape[0], X_train.shape[1]))
 X_test = np.reshape(X_test, (1, X_test.shape[0], X_test.shape[1]))
 y_train = np_utils.to_categorical(y_train, 10) 
 y_test = np_utils.to_categorical(y_test, 10)
 model.fit(X_train,y_train,validation_data=(X_test,y_test))
 print(str(model.evaluate(x_test,y_test)))

我在这一行收到错误:

 model.fit(X_train,y_train,validation_data=(X_test,y_test))
 Error: Error when checking target: expected activation_2 to have shape (None, 9) 
 but got array with shape (643, 10)

因为" y_train"这种训练形式不合适。有人可以解释我如何解决这个问题并重塑y_train以获得成功的培训吗?

修改input_shape更改为第二和第三维形状后,错误现在更改为:

 fValueError: Error when checking target: expected activation_2 to have shape (None, 1) 
 but got array with shape (643, 10) 

请解决这个问题吗? 谢谢!

0 个答案:

没有答案