如何使用CN折叠的k折叠交叉验证

时间:2018-06-10 09:13:48

标签: tensorflow keras

我正在使用keras实现CNN以进行多重分类。模型在训练数据上表现良好,但验证准确度很低 有一个过度拟合的问题。请指导如何使用K折叠交叉验证减少过度拟合。如何对此代码应用交叉验证。

embedding_layer = w2v_model_wv.wv.get_keras_embedding()
sequence_input = Input(shape=(maxlen,), dtype='int32')
embedded_sequences = embedding_layer(sequence_input)
x = Conv1D(filters=nb_filters, kernel_size=n_gram, padding='valid', 
activation='relu', input_shape=(maxlen, vecsize))(embedded_sequences)
x=Dropout(0.2)(x)
x = MaxPooling1D(pool_size=maxlen - n_gram + 1)(x)
x= Dropout(0.2)(x)
x = Flatten()(x)
x= Dropout(0.5)(x)
preds = Dense(nb_labels, activation=final_activation, 
kernel_regularizer=l2(dense_wl2reg), bias_regularizer=l2(dense_bl2reg))(x)

classlabels, x_train, y_train = convert_trainingdata(trainclassdict)

tokenizer = Tokenizer()
tokenizer.fit_on_texts(x_train)
x_train = tokenizer.texts_to_sequences(x_train)
x_train = pad_sequences(x_train, maxlen=maxlen)

model = Model(sequence_input, preds)

tensorboard = TensorBoard(log_dir="logs/{}".format('cnn_deepnn'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics= 
['acc'])
model.fit(np.array(x_train), np.array(y_train), validation_split=0.2, 
epochs=1700, callbacks=[tensorboard])

1 个答案:

答案 0 :(得分:1)

如Matias Valdenegro所述,K-fold不是正确的方法 您可以通过以下方式减少过度拟合:

1)添加更多数据
2)增强您的数据。在处理文本数据时这可能很棘手,但是我很幸运重新采样数据,同时删除不重要的单词(副词/ adj)
3)正则化方法(好你正在使用Dropout。尝试在Conv1D功能中使用内核调节器)
4)我会尝试添加更多图层