keras模型的权重如何重新初始化?

时间:2020-05-04 18:24:27

标签: python machine-learning keras

我已经在多种类型的模型(sklearn,XGBoost,LightGBM)中使用了交叉验证功能,并且正在尝试将其扩展到keras。

问题似乎是,当我将keras模型传递给函数时,它没有使用每一步重新开始训练,而是使用了以前学习过的权重。我一直在向左和向右搜索,但是没有找到直接将keras模型权重重新初始化为随机状态的简单方法。 现在,我正在使用的解决方法是在第一次折叠训练之前保存模型的随机权重,并在每次折叠之后重新加载它们。这有点丑陋,宁愿避免。

简化功能是:

def cvloopcv(X,y,model,n,**kwargs):

    kf = StratifiedKFold(n_splits=n,
                shuffle=True,
                random_state=SEED)


    for train_index, test_index in kf.split(X,y):

        model.fit(X[train_index], y[train_index])

        if isinstance(model,Sequential):
            preds = model.predict_proba(X[test_index]).flatten()
        else:
            preds = model.predict_proba(X[test_index])[:,1]

        ...do a bunch of things here

1 个答案:

答案 0 :(得分:0)

您可以尝试在训练循环中编译模型。

for train_index, test_index in kf.split(X,y): model.compile()

这将重新编译模型并将其初始化为一些新的随机权重