NTMC-Community / MatchZoo-py

Facilitating the design, comparison and sharing of deep text matching models.
Apache License 2.0
495 stars 106 forks source link

Is there any tutorial for using the pretrained model for predicting new sentence pair? #157

Open anbo724 opened 3 years ago

anbo724 commented 3 years ago

I have trained the Esim and Bert model. However, I can not found any code for predicting new sentence pair? Is anyone can help?

cuter44 commented 3 years ago

Here's one:

import torch
import matchzoo as mz

from anmm_train import *

test_pack = mz.datasets.wiki_qa.load_data('test', task=ranking_task)
test_processed = preprocessor.transform(test_pack, verbose=0)
testset = mz.dataloader.Dataset(
    data_pack=test_processed,
    mode='point',
    batch_size=32
)
testloader = mz.dataloader.DataLoader(
    dataset=testset,
    device=device,
    stage='test',
    callback=padding_callback
)

if __name__ == "__main__":

    trainer.restore_model("./save/model.pt")
    prediction = trainer.predict(testloader)

    pass

# end main

, where anmm_train is the code copied from repo's README, except Trainer.run(). Though you're in predict stage, the train/test dataset is still required as Trainer takes them as required parameter to construct.