LeeSureman / Batch_Parallel_LatticeLSTM

Chinese NER using Lattice LSTM. Reproduction for ACL 2018 paper.
130 stars 16 forks source link

how to save model for test? #3

Open w5688414 opened 4 years ago

w5688414 commented 4 years ago

I want to save the trained model, and load it for prediction, here is my code: ` saver = ModelSaver("./cache/model_ckpt_100.pkl")

model=ModelLoader.load_pytorch_model("./cache/model_ckpt_100.pkl")

ModelLoader.load_pytorch(model,"./cache/model_ckpt_100.pkl") tester = Tester(datasets['test'], model, metrics=AccuracyMetric()) tester.test() ` but it doesn't work, Can the author provide a way for prediction after saving the model

LeeSureman commented 4 years ago

Could you please show the error trace information?

w5688414 commented 4 years ago

I change the code like this, and it works, thanks for your response, sorry to interrupt you ` if(args.train): trainer.train() saver = ModelSaver("./cache/model_ckpt_100.pkl") saver.save_pytorch(model,param_only=False)

if(args.predict): model=ModelLoader.load_pytorch_model("./cache/model_ckpt_100.pkl")

ModelLoader.load_pytorch(model,"./cache/model_ckpt_100.pkl")

# tester = Tester(datasets['test'], model, metrics=AccuracyMetric())
# tester.test()

from fastNLP import Tester

tester = Tester(datasets['test'], model, batch_size=args.test_batch, metrics=metrics)
tester.test()

`

LeeSureman commented 4 years ago

congratulations! And if you use fitlog, you can run 'fitlog log logs' in the project dictionary to scan the experiment result.

zhangtianlin commented 4 years ago

Dear w5688414 or LeeSureman: i used the code if(args.train): trainer.train() saver = ModelSaver("./cache/model_ckpt_100.pkl") saver.save_pytorch(model,param_only=False)

if(args.predict): model=ModelLoader.load_pytorch_model("./cache/model_ckpt_100.pkl") tester = Tester(datasets['test'], model, batch_size=args.test_batch, metrics=metrics) tester.test()

But it has error, which said TypeError: The type of model must be 'torch.nn.Module', got '<class'collections.OrderedDict'>' i donot the reason. Who can help me?