dropout(): argument 'input' (position 1) must be Tensor, not str With Bert
Author: Tashsub
Pull request: no

## Environment info
<!-- You can run the command `transformers-cli env` and copy-and-paste its output below.
     Don't forget to fill out the missing fields in that output! -->
     
- `transformers` version:
- Platform: google colab 
- Python version: 3
- PyTorch version (GPU?):
- Tensorflow version (GPU?):
- Using GPU in script?:
- Using distributed or parallel set-up in script?:

### Who can help
<!-- Your issue will be replied to more quickly if you can figure out the right person to tag with @
 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.
 

 albert, bert, GPT2, XLM: @LysandreJik 
 tokenizers: @mfuntowicz
 Trainer: @sgugger
 Speed and Memory Benchmarks: @patrickvonplaten
 Model Cards: @julien-c
 TextGeneration: @TevenLeScao 
 examples/distillation: @VictorSanh
 nlp datasets: [different repo](https://github.com/huggingface/nlp)
 rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)
 Text Generation: @patrickvonplaten @TevenLeScao
 Blenderbot: @patrickvonplaten
 Bart: @patrickvonplaten
 Marian: @patrickvonplaten
 Pegasus: @patrickvonplaten
 mBART: @patrickvonplaten
 T5: @patrickvonplaten
 Longformer/Reformer: @patrickvonplaten
 TransfoXL/XLNet: @TevenLeScao
 RAG: @patrickvonplaten, @lhoestq
 FSMT: @stas00
 examples/seq2seq: @patil-suraj
 examples/bert-loses-patience: @JetRunner
 tensorflow: @jplu
 examples/token-classification: @stefan-it
 documentation: @sgugger
 -->

## Information

Model I am using (Bert, XLNet ...): Bert

@LysandreJik 
@jplu

The problem arises when using:
* [x] the official example scripts: (give details below)
* [ ] my own modified scripts: (give details below)

The tasks I am working on is:
* [ ] an official GLUE/SQUaD task: (give the name)
* [x] my own task or dataset: (give details below)

I am trying to do sentiment analysis using Bert. My code was working perfectly fine and then last night I tried to run it without changing anything and I am getting the following error message: 

"dropout(): argument 'input' (position 1) must be Tensor, not str"

I trained my Bert model and saved the bin file. This occurs when I load the bin file into collab and try to predict the sentiment of any text. 


## To reproduce

Steps to reproduce the behavior:

1. Loaded my model that was saved in a bin file in google colab 
2. Ran the following code: 

`def conclude_sentiment(text):
  encoded_review = tokenizer.encode_plus(
    text,
    max_length=MAX_LEN,
    add_special_tokens=True,
    return_token_type_ids=False,
    pad_to_max_length=True,
    return_attention_mask=True,
    return_tensors='pt',
  )

  input_ids = encoded_review['input_ids'].to(device)
  attention_mask = encoded_review['attention_mask'].to(device)

  output = model(input_ids, attention_mask)
  _, prediction = torch.max(output, dim=1)

  #print(f'Review text: {text}')
  #print(f'Sentiment  : {class_names[prediction]}')
  return class_names[prediction]`

3. Got an error that says 

`/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in dropout(input, p, training, inplace)
    981     return (_VF.dropout_(input, p, training)
    982             if inplace
--> 983             else _VF.dropout(input, p, training))
    984 
    985 

TypeError: dropout(): argument 'input' (position 1) must be Tensor, not str`

<!-- If you have code snippets, error messages, stack traces please provide them here as well.
     Important! Use code tags to correctly format your code. See https://help.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks#syntax-highlighting
     Do not use screenshots, as they are hard to read and (more importantly) don't allow others to copy-and-paste your code.-->

## Expected behavior

<!-- A clear and concise description of what you would expect to happen. -->
An output of either 'positive' or 'negative' when a string is passed into the method named 'conclude_sentiment'


Comments:
having the **same problem**, what is happening, it was working just fine for the past like 90 days!!



Hello! It would be very helpful if you could complete the information related to your environment. If you could have a reproducible code example, that would really be great as well.

It is possible you were affected by the breaking changes from v3.x to v4.x. If this is the case, I invite you to read the [migration notes](https://huggingface.co/transformers/migration.html), or to pin the transformers library to the major version 3: `pip install transformers==3`

https://github.com/mosh98/Swedish_Sentiment_BERTIL/blob/main/Swe_Bert_Training_Bigger_dataset.ipynb

I am afraid I see no error in your notebook.

okej thanks for the tip, pinning it to version 3 did the trick!

Thanks @LysandreJik, 

It works but creates a new error here that says: 

Error(s) in loading state_dict for SentimentClassifier:
	Unexpected key(s) in state_dict: "bert.embeddings.position_ids". 
[Notebook 
](https://colab.research.google.com/drive/1fEXY3IQ82u41KvwoDOg-oY95RDD1OpKg?usp=sharing)

**After running the code below**: 

```
saved_model = torch.load('selective_stock_dataset_state-2.bin')
model = SentimentClassifier(len(class_names))
model.load_state_dict(saved_model)
model = model.to(device)

```



```
class SentimentClassifier(nn.Module): 
  def __init__(self, n_classes):
    super(SentimentClassifier, self).__init__()
    self.bert = BertModel.from_pretrained(PRE_TRAINED_MODEL_NAME)
    self.drop = nn.Dropout(p=0.3)
    self.out = nn.Linear(self.bert.config.hidden_size, n_classes)
    self.softmax = nn.Softmax(dim=1)

  def forward(self, input_ids, attention_mask):
    _, pooled_output = self.bert(
      input_ids=input_ids,
      attention_mask=attention_mask
    )
    output = self.drop(pooled_output)
    output = self.out(output)
    return self.softmax(output)
```

Could you advise. Please and thanks 

as he mentioned earlier, try using `pip install transformers==3`

@mosh98 , I have tried with `pip install transformers==3` and it removed the first error. 

But i then get a new error that I was no getting before that says 

`Error(s) in loading state_dict for SentimentClassifier:
Unexpected key(s) in state_dict: "bert.embeddings.position_ids".`

see my notebook here: [notebook](https://colab.research.google.com/drive/1fEXY3IQ82u41KvwoDOg-oY95RDD1OpKg?usp=sharing#scrollTo=iQ93LDzMXO58l)

Ahh it was solved by changing 

`model.load_state_dict(saved_model)`

to 

`model.load_state_dict(saved_model, strict=False)`


Hi, indeed, this is a different error. We recommend using the `from_pretrained` method (your custom model would need to inherit from `PreTrainedModel` rather than `nn.Module`) rather than using `load_state_dict` to ensure maximum compatibility between checkpoints and architectures, otherwise the state dicts might not be 100% loadable on each custom architecture.

Your workaround using `strict=False` also works!

"""pip install transformers==3""" doesnt seem to work


No need to downgrade the transformers. Just do the following - it's from the migration guide.

```
model = BertModel.from_pretrained("bert-base-cased")
outputs = model(**inputs, return_dict=False)
```


This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md) are likely to be ignored.

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

> `outputs = model(**inputs, return_dict=False)`
> 
> or
> 
> `model = BertModel.from_pretrained("bert-base-cased",return_dict=False)`

cool, it works.

> > `outputs = model(**inputs, return_dict=False)`
> > or
> > `model = BertModel.from_pretrained("bert-base-cased",return_dict=False)`
> 
> cool, it works.

great! it worked for me too, thanks a million :) 

Still does not work for me transformers 4.24

hi 
i am also facing the same issue. i am applying transformer on image data. i did the training. i haven't face any issues while training. but in prediction it throws the error
TypeError: dropout(): argument 'input' (position 1) must be Tensor, not NoneType

please resolve if possible

Hey @dishamohini could you make sure to do the following:
- check that you are using the latest version of `transformers`
- check that you are correctly running the model in eval mode
If you still have an issue:
- open a new issue on `transformers`, following the contribution guidelines with the output of `transformers-cli envs`, and a full minimal reproducer as well as the trace-back 
- ping me there