python Keras K-fold

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Keras K-fold相关的知识,希望对你有一定的参考价值。

num_epochs = 500
k = 4
num_val_samples = len(train_data) // k
all_mae_histories = []

for i in range(k):
    print 'processing fold # %d' % i
    val_data = train_data[i * num_val_samples: (i+1) * num_val_samples]
    val_targets = train_targets[i * num_val_samples: (i+1) * num_val_samples]
    
    partial_train_data = np.concatenate(
        [train_data[:i*num_val_samples],
        train_data[(i+1)*num_val_samples:]],
        axis = 0)
    partial_train_targets = np.concatenate(
        [train_targets[:i*num_val_samples],
        train_targets[(i+1)*num_val_samples:]],
        axis = 0)
    
    model = build_model()
    history = model.fit(partial_train_data, partial_train_targets,
            validation_data = (val_data, val_targets),
             epochs = num_epochs, batch_size = 1, verbose = 0)
    
    mae_history = history.history['val_mean_absolute_error']
    all_mae_histories.append(mae_history)
    
average_mae_history = [
    np.mean([x[i] for x in all_mae_histories]) for i in range(num_epochs)]
    
import matplotlib.pyplot as plt
plt.plot(range(1, len(average_mae_history) + 1), average_mae_history)
plt.xlabel('Epoch')
plt.ylabel('Validation MAE')
plt.show()

以上是关于python Keras K-fold的主要内容,如果未能解决你的问题,请参考以下文章

小白学习keras教程十三种Model Selection方法:k-fold cross-validation,GridSearchCV和RandomizedSearchCV

python Albon k-fold

使用 scikit-learn 对具有多个输入的 Keras 模型进行交叉验证

K-fold 交叉验证查询

K-fold Train

如何在情感分析中添加混淆矩阵和k-fold 10折