39

My code was working fine and when I tried to run it today without changing anything I got the following error:

dropout(): argument 'input' (position 1) must be Tensor, not str

Would appreciate if help could be provided.
Could be an issue with the data loader?

Vincent Doba
  • 4,343
  • 3
  • 22
  • 42
Tashinga Musanhu
  • 401
  • 1
  • 4
  • 3
  • 1
    Welcome to StackOverflow. Please take a look at the guidelines before posting a question. If you want to get answer from SO, your question needs to be reproducible in order for others to help you. https://stackoverflow.com/help/how-to-ask. This could be a problem anywhere within your code. – Ananda Dec 01 '20 at 04:58

3 Answers3

56

if you use HuggingFace, this information could be useful. I have same error and fix it with adding parameter return_dict=False in model class before dropout: outputs = model(**inputs, return_dict=False)

Evgeny Vorsin
  • 561
  • 3
  • 3
19

I was also working on same repo. There is a class probably named Bert_Arch that inherits the nn.Module and this class has a overriden method named forward. Inside forward method just add the parameter 'return_dict=False' to the self.bert() method call. Replace

_, cls_hs = self.bert(sent_id, attention_mask=mask)

with

_, cls_hs = self.bert(sent_id, attention_mask=mask, return_dict=False)
tomerpacific
  • 4,704
  • 13
  • 34
  • 52
Utkarsh Dadhich
  • 191
  • 1
  • 3
18

If you are using the Hugging Face transformers library, this error pops up when running code written in v3 on the transformers v4 library. To resolve it, simply add return_dict=False when loading the model as follows:

model = BertModel.from_pretrained("bert-base-cased")
outputs = model(**inputs, return_dict=False)

or

model = BertModel.from_pretrained("bert-base-cased", return_dict=False)
outputs = model(**inputs)

I hope this helps. It worked for me.

Reference: https://huggingface.co/transformers/migration.html

Mwenda
  • 422
  • 3
  • 6