'TypeError: forward() takes 1 positional argument but 2 were given while inferencing of PyTorch model

My mode likes the following:

    class RankingModel(nn.Module):
        def __init__(self, conf: Dict[Text, Any], **kwargs: Any):
            super(RankingModel, self).__init__()
            self.conf = deepcopy(conf)
            ......

    def forward(self, **_features):   # the model input is a torch.utils.data.Dataset()
        ### model body part.
        return prob

Then I train my model using:

trainer = Trainer(
                model=model,
                args=training_args,
                train_dataset=training_dataset,
                eval_dataset=valid_dataset,
                compute_metrics=compute_metrics_for_binary_classification,
                callbacks=[callback],
          )

trainer.train()

Then I predict the result with the model.

test_predict = model(x_test)

I get the error:

TypeError                                 Traceback (most recent call last)
Input In [18], in <cell line: 9>()
     17 x_test.from_dict(feature_test)
     18 x_test.set_format(tensor_type="torch")
---> 20 test_predict = model(x_test) # trainer.predict(x_test).predictions
     21 if np.argmax(test_predict) < 5:
     22     recall_counter = recall_counter + 1

File ~/miniconda3/lib/python3.8/site-packages/torch/nn/modules/module.py:1051, in Module._call_impl(self, *input, **kwargs)
   1047 # If we don't have any hooks, we want to skip the rest of the logic in
   1048 # this function, and just call forward.
   1049 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1050         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1051     return forward_call(*input, **kwargs)
   1052 # Do not call functions when jit is used
   1053 full_backward_hooks, non_full_backward_hooks = [], []

TypeError: forward() takes 1 positional argument but 2 were given

But all is OK if I predict the result through:

test_predict = trainer.predict(x_test).predictions

Why may I not use model(x_test) to get inference result? Could you please give me any suggestions? Thanks.



Solution 1:[1]

Your forward expect argument with key like forward(data=myarray) because you used double asterix when defining it and didn't give positional argument.

either use def forward(self, input, **kwargs)which would read the first argument of the call and then use other argument as kwargs

or call it with: model(keyword=x_test) and then in your foward function you can access it with _features['keyword']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ThomaS