python-3.x 如何在Fastai Tsai中加载最佳模型

8wigbo56  于 2023-08-08  发布在  Python
关注(0)|答案(1)|浏览(119)

我正在使用下面的代码在tsai图书馆训练我的timeseries二进制分类模型。但是,当我尝试加载最佳模型并在其上测试验证数据时,我无法在验证集上获得相同的准确性。也许我在测试过程中加载最佳模型的方法是错误的。有人可以请帮助我在这里做错了什么,什么是正确的方法来加载保存的最佳模型和测试它。
列车代码:

def train(dataset_idx):
    print("starting process:", dataset_idx)
    y_test = load(open(r"y_test_"+str(dataset_idx)+".pkl", 'rb'))
    X_test = load(open(r"X_test_"+str(dataset_idx)+".pkl", 'rb'))
    y_train = load(open(r"y_train_"+str(dataset_idx)+".pkl", 'rb'))
    X_train = load(open(r"X_train_"+str(dataset_idx)+".pkl", 'rb'))
    l = X_train.shape[0]
    print("data loaded")
    X_train = np.concatenate([X_train, X_test], axis=0)
    y_train = np.concatenate([y_train, y_test], axis=0)
    del X_test, y_test
    splits = [i for i in range(l)], [i for i in range(l, X_train.shape[0])]
    print("dataset generated")

    tfms = [None, TSClassification()]
    batch_tfms = TSStandardize()
    precision = Precision()
    recall = Recall()
    save_callback =  SaveModelCallback(monitor='valid_loss', comp=None, fname=str(dataset_idx)+'_best_model', every_epoch=False, at_end=False, with_opt=False, reset_on_fit=True)
    early_stopping = EarlyStoppingCallback(monitor='valid_loss', patience=3)
    clf = TSClassifier(X_train, y_train, splits=splits, arch="InceptionTimePlus", tfms=tfms, batch_tfms=batch_tfms, bs=[1024], metrics=[precision, recall], cbs=[save_callback, early_stopping])
    clf.fit_one_cycle(50, 2.5e-4)
    clf.export(str(dataset_idx)+".pkl")

字符串
测试代码:

learner = TSClassifier(X_sample, y_sample, splits=splits, arch="InceptionTimePlus", tfms=tfms, batch_tfms=batch_tfms)
learner.load(str(dataset_idx)+'_best_model')
y_pred, _, _ = learner.get_X_preds(X_test)
y_pred = y_pred.numpy()[:,0]
y_pred = np.asarray(y_pred>0.5).astype(int)


我在测试中得到了全零,但在训练中,它显示了70%的准确率和20%的召回率。
另外,如果我最初加载不同的模型,然后将相同的最佳模型加载到2个不同的分类器示例,那么我对相同数据的输出是不同的。但理想情况下,它应该与加载相同的最佳模型进行测试相同。

clf = load_learner(“models/clf1.pkl”)
clf .load(str(dataset_idx)+‘_best_model’)
probas, target, preds = clf.get_X_preds(X[splits[1]], y[splits[1]])

clf = load_learner(“models/clf2.pkl”)
clf .load(str(dataset_idx)+‘_best_model’)
probas, target, preds = clf.get_X_preds(X[splits[1]], y[splits[1]])


在上述2个场景中,相同数据的输出不同。我不明白为什么。有人能告诉我正确的方法吗?

sz81bmfz

sz81bmfz1#

在savemodel回调中使用“at_end=False”,训练后保存模型,然后加载模型:-

save_callback =  SaveModelCallback(monitor='valid_loss', comp=None, fname='sample_best_model', every_epoch=False, at_end=False, with_opt=False, reset_on_fit=True)
early_stopping = EarlyStoppingCallback(monitor='valid_loss', patience=3)
clf = TSClassifier(X_sample, y_sample, splits=splits, arch="InceptionTimePlus", tfms=tfms, batch_tfms=batch_tfms, bs=[1024], metrics=[precision, recall], cbs=[save_callback, early_stopping])
clf.fit_one_cycle(10, 2.5e-4)
clf.export("sample_trained_model.pkl")

from tsai.inference import load_learner
mv_clf = load_learner("sample_trained_model.pkl")

字符串

相关问题