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:
Are there any ideas how to solve it?
The simple tagger is expecting an argument called tags, not labels. How are you constructing your data?
Thanks for your quick answer! I use a custom dataset reader and it seems, there is something going wrong since the newer AllenNLP versions.
@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?
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.
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.)
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?
@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.
Most helpful comment
The simple tagger is expecting an argument called
tags, notlabels. How are you constructing your data?