seq2seq Pytorch-尝试计算指标时出错(精度,召回率,F1得分)

时间:2020-07-14 08:17:16

标签: python pytorch metrics seq2seq

我遍历了seq2seq的本教程,并每1000次迭代保存了模型。

https://github.com/spro/practical-pytorch/blob/master/seq2seq-translation/seq2seq-translation.ipynb

我尝试计算指标并分析结果,因此我想为新测试集计算Precision,Recall和F1分数。 我创建了这个脚本:

import torch
import pandas as pd
from sklearn.metrics import precision_recall_fscore_support
import seq2seq_tutorial
max_length=50

test_vectors = pd.read_pickle("Pickels/first-second.pkl")
y_true = list(test_vectors[1])

test_vectors = [list(map(str, list(test_vectors.iloc[i])[0])) for i in range(len(test_vectors))]
test_vectors = [("".join(sent)).strip() for sent in test_vectors]

results = []
for k in range(1000, 75001, 1000):
    print("K: " + str(k))
    encoder = torch.load("Models/seq2seq/encoder_" + str(k) + "_steps.pth")
    decoder = torch.load("Models/seq2seq/decoder_" + str(k) + "_steps.pth")

    predictions_for_model_k = []
    for i in range(len(test_vectors)):
        try:
            prediction = explained_code.evaluate(encoder, decoder, test_vectors[i],max_length)
            prediction = prediction[0][0]
            predictions_for_model_k.append(prediction)
        except:
            print("Exception at %s" % i)

    accuracy = precision_recall_fscore_support(y_true, predictions_for_model_k, average='weighted')
    precision = accuracy[0]
    recall = accuracy[1]
    f_score = accuracy[2]

    print("Precision: " + str(round(precision * 100, 3)))
    print("Recall: " + str(round(recall * 100, 3)))
    print("F_score: " + str(round(f_score * 100, 3)))

    results.append([str(k) + "steps", predictions_for_model_k, y_true, precision, recall, f_score])

pd.to_pickle(results, "Pickles/Results/results.pkl")

但是我得到了这个错误:

AttributeError: Can't get attribute 'EncoderRNN' on <module '__main__'

您可以帮助解决此问题吗? 谢谢!

0 个答案:

没有答案