Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Training error in forward() method #2528

Closed
bianca2319 opened this issue Feb 20, 2019 · 7 comments
Closed

Training error in forward() method #2528

bianca2319 opened this issue Feb 20, 2019 · 7 comments

Comments

@bianca2319
Copy link

bianca2319 commented Feb 20, 2019

When I try to train a model based on "default" trainer and "simple_tagger", I always get the following error message:
File "/home/bianca/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in call
result = self.forward(*input, **kwargs)
TypeError: forward() got an unexpected keyword argument 'labels'

System:

  • OS: Linux
  • Python version: 3.6
  • AllenNLP version: 0.8.2

Are there any ideas how to solve it?

@matt-gardner
Copy link
Contributor

The simple tagger is expecting an argument called tags, not labels. How are you constructing your data?

@bianca2319
Copy link
Author

Thanks for your quick answer! I use a custom dataset reader and it seems, there is something going wrong since the newer AllenNLP versions.

@mithunpaul08
Copy link

@bianca2319 i get the same issue when I try feeding in a new data to the simple tagger. How did you solve this? Why was this closed btw?

@mithunpaul08
Copy link

update: so i found what was happening in my case. Posting it here just in case this helps someone.

So looks like in allennlp, they enforce you to use the same names of the fields you defined while reading the data. For example in the snli reader they were using the name premise and the corresponding forward method of decomposable attention HAD TO use the same word premise. That is really stupid imho because what if tomorrow someone wants to use a different model and wants to call their local variable inside the forward function something else.

In my case my fields instance (which had a TextField,TextField, LabelField, just like SNLI data) was called claims where as the arguments inside the forward method of decomposable attention was using the word claim (somehow?). So I got the error in forward() got an unexpected keyword argument claims. I think this happens because they are passing by reference a dictionary inside the batch_loss function here and not by just value.

While I agree its a good thing to enforce the usage of same title field everywhere, but in a fluid plug and play environment like this I wish it wasn't enforced. Even if it was I wish this was atleast documented more clearly. I wasted couple of days trying to figure this out.

@joelgrus
Copy link
Contributor

I am sorry you ran into issues with this, but it is covered in the tutorial:

https://allennlp.org/tutorials

The forward method expects dicts of tensors as input, and it expects their names to be the names of the fields in your Instance.

If that's not clear enough, I'm happy to make changes to it, do you have any suggestions?

(I feel like we also had an issue around programatically renaming fields to make it easier to mix and match dataset readers and models, although now I can't find the issue.)

@joelgrus
Copy link
Contributor

but also I'm curious how you expected the library to behave? that is, if I have an instance that looks like

Instance({"a": TextField(...), "b": TextField(...), "c": LabelField(...)})

and then a model whose forward function looks like

def forward(self, d: torch.Tensor, e: torch.Tensor, f: torch.Tensor): ...

what would you expect to happen? how would you expect the library to decide which field should be "d", which should be "e", and which should be "f" if the names don't match?

@mithunpaul08
Copy link

@joelgrus On second thoughts: regarding passing the arguments, you are right and am wrong. Though one humble suggestion is to maybe elaborate a little more in the documentation. i.e instead of The forward method expects dicts of tensors as input, and it expects their names to be the names of the fields in your Instance. maybe add an example of what it means? i did read this initially, but forgot about it, when i moved on to the second level of tutorials.

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

No branches or pull requests

4 participants