hemingkx/CLUENER2020

请问怎么调用训练好的模型进行预测?

stay-leave opened this issue · 3 comments

请问怎么调用训练好的模型进行预测?

同问

参考函数

./model.py/dev(data_loader, vocab, model, device, mode='dev')

参考代码

model.eval()
true_tags = []
pred_tags = []
sent_data = []

for idx, batch_samples in enumerate(dev_loader):
    sentences, labels, masks, lens = batch_samples
    sent_data.extend([[vocab.id2word.get(idx.item()) for i, idx in enumerate(indices) if mask[i] > 0]
                        for (mask, indices) in zip(masks, sentences)])
    sentences = sentences.to(device)
    labels = labels.to(device)
    masks = masks.to(device)
    y_pred = model.forward(sentences)
    labels_pred = model.crf.decode(y_pred, mask=masks)
    targets = [itag[:ilen] for itag, ilen in zip(labels.cpu().numpy(), lens)]
    true_tags.extend([[vocab.id2label.get(idx) for idx in indices] for indices in targets])
    pred_tags.extend([[vocab.id2label.get(idx) for idx in indices] for indices in labels_pred])

print(f'预测标签:{pred_tags }')