Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to save model for test? #3

Open
w5688414 opened this issue Oct 22, 2019 · 4 comments
Open

how to save model for test? #3

w5688414 opened this issue Oct 22, 2019 · 4 comments

Comments

@w5688414
Copy link

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
Copy link
Owner

Could you please show the error trace information?

@w5688414
Copy link
Author

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
Copy link
Owner

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

@zhangtianlin
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants