首页 > 解决方案 > Jupyter Lab 未执行 K 折交叉验证

问题描述

我正在使用 5 交叉验证和 F1 指标来评估我的神经网络。

但是,在 Jupyter Labs 中运行我的代码时,它似乎永远加载。不产生任何输出,但运行其他任务很好。

我总共有 5 折和 500 个 epoch。每 50 个时期,我输出均方误差损失值(每折叠 x10)。在 500 个 epoch(1 倍)之后,我输出我给它的任何测试集的当前 F1 值。

import numpy as np
from sklearn.model_selection import cross_val_score
from sklearn import metrics

lr = 0.01
cross_val_score(lr,x_train,y_train,cv=5,scoring='mean_squared_error')

f1s = [] # to capture F1 score for every Fold
kf = KFold(n_splits=5)
for train, test in kf.split(x):
    for epoch in range(500):
        ridge.fit(x[train], y[train])
        if epoch % 50 == 0:  # prints MSE 10 times over each fold
            print(mean_squared_error(y[test], ridge.predict(x[test])))
            print("MSE: " + metrics.mean_squared_error(predictions, y))
    f1 = metrics.f1_score(predictions, y) # appends each fold
    f1s.append(f1)
    print("F1: " + f1)

print("Average F1: " + np.mean(macro_f1))

注意:由于我的计算机规格较低,折叠和历元值较低。我正在使用 Jupyter 实验室。

我做错了什么导致它永远运行?

标签: pythonscikit-learndeep-learningjupytercross-validation

解决方案


推荐阅读