简体   繁体   English

使用 kfold 交叉验证进行深度学习

[英]deep learning with kfold cross validation

I am new into neural networks, I want to use K-fold cross-validation to train my neural network.我是神经网络的新手,我想使用 K 折交叉验证来训练我的神经网络。 I want to use 5 folds 50 epochs and a batch size of 64 I found a function in scikit for k-fold cross validation我想使用 5 折 50 epochs 和 64 的批量大小我在 scikit 中找到了一个用于 k 折交叉验证的函数

model_selection.cross_val_score(model_kfold, x_train, y_train, cv=5)

and my code without cross validation is我没有交叉验证的代码是

history = alexNet_model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1,validation_data=(x_validation, y_validation))

I don't know how to implement this with batch size and epochs in python using keras and scikit.我不知道如何使用 keras 和 scikit 在 python 中使用批量大小和纪元来实现这一点。 any idea?任何的想法?

Be sure to use test data when validating your model, not the same training data.确保在验证模型时使用测试数据,而不是相同的训练数据。 Using training data to validate will bias your results.使用训练数据进行验证会使您的结果产生偏差。

In your example, I would use the KerasClassifier module instead of the numpy KFolds module.在您的示例中,我将使用 KerasClassifier 模块而不是 numpy KFolds 模块。

from keras.wrappers.scikit_learn import KerasClassifier

After you've imported the module, your code would be (with results output:导入模块后,您的代码将是(带有结果输出:

evaluator=KerasClassifier(build_fn=baseline_model, epochs=50, batch_size=64)
kfold=KFold(n_splits=5, shuffle=True, random_state=random_seed)

results=cross_val_score(evaluator, x_test, onehot_y_test, cv=kfold)
print("Model: %.2f%% (%.2F%%)" % (results.mean()*100, results.std()*100))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM