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

Trial predict fails with the given example #616

Closed
danielhuoo opened this issue Jul 28, 2019 · 5 comments · Fixed by #617
Closed

Trial predict fails with the given example #616

danielhuoo opened this issue Jul 28, 2019 · 5 comments · Fixed by #617
Assignees
Labels
bug Something isn't working

Comments

@danielhuoo
Copy link

After training the model, I want to get the prediction on the test set. Not the accuracy. I know that should be Trial.evaluate(). It works well. Therefore I used Trial.predict(). Is that right?

But the error says that AttributeError: 'dict' object has no attribute 'data'.

I read the instruction of Trial that provides an example,

# Simple trial to predict on some validation and test data
>>> from torchbearer import Trial
>>> val_data = torch.rand(5, 5)
>>> test_data = torch.rand(5, 5)
>>> t = Trial(None).with_test_data(test_data)
>>> test_predictions = t.predict(data_key=torchbearer.TEST_DATA)

I ran it but got an error AttributeError: 'NoneType' object has no attribute 'eval'

So, is there any problem in this method?

@ethanwharris ethanwharris added the bug Something isn't working label Jul 29, 2019
@ethanwharris
Copy link
Member

Hi, thanks for the issue! We think this may relate to some old bugs that have since been fixed. Could I get you to update your torchbearer version with
pip install --upgrade torchbearer
and then try again and let us know if the issue persists?

@danielhuoo
Copy link
Author

@ethanwharris Thanks for your instruction.
I updated the version as you said.
Then I got an new and same error from my own program and the official example:

from torchbearer import Trial
test_data = torch.rand(5, 5)
t = Trial(None).with_test_data(test_data)
test_predictions = t.predict(data_key=torchbearer.TEST_DATA)

>>>TypeError: expected Tensor as element 0 in argument 0, but got NoneType

@MattPainter01
Copy link
Member

Hi, the example uses a None model which doesn't actually make predictions, so the code that aggregates predictions across batches has trouble with it. This code with a model that returns a tensor should give you the predictions properly, so it should work with the original model you wanted predictions from.

The predict method should definitely deal with the None model better though so I've put up a PR to deal with it. It'll now just return a list of the model outputs for each batch if it can't combine them into a single tensor.

Thanks for bringing this to our attention!

@MattPainter01
Copy link
Member

Hi, the example uses a None model which doesn't actually make predictions, so the code that aggregates predictions across batches has trouble with it. This code with a model that returns a tensor should give you the predictions properly, so it should work with the original model you wanted predictions from.

The predict method should definitely deal with the None model better though so I've put up a PR to deal with it. It'll now just return a list of the model outputs for each batch if it can't combine them into a single tensor.

Thanks for bringing this to our attention!

Oops, sorry @danielhuoo , just realised you said you got the same error from your code too. Could you post a sample of it so we can have a look?

@danielhuoo
Copy link
Author

@MattPainter01 Thanks for your "Oops".
I just made a new file to reproduce the error.
Then I found it works.
It seems that I didn't reset the environment of jupyter notebook.
Anyway, my problem has been solved, but the "None model" problem exists.
Thank you very much :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants