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

Tashinga_A: <pre><code class="lang-auto">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)
</code></pre>
<p><strong>When I run the following Method on a text input in an attempt to classify its sentiment I get an error</strong></p>
<pre><code class="lang-auto">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]
</code></pre>
<p><strong>I get an error message that says</strong>:</p>
<pre><code class="lang-auto">/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
--&gt; 983             else _VF.dropout(input, p, training))
    984 
    985 

TypeError: dropout(): argument 'input' (position 1) must be Tensor, not str
</code></pre>
<p>Notebook example can be found here: <a href="https://colab.research.google.com/drive/1Wp3CG4Agk5YMlhTctr_Y4soAMHl6mXq4?usp=sharing" rel="noopener nofollow ugc">here</a></p>

Aishwarya_Allada: <p>I got the exact same error today at the exact place. I did not face the problem yesterday. When I ran the code today, I got this. Anyone, any leads?</p>

ptrblck: <p>Could you post an executable code snippet to reproduce this issue, please?<br>
The linked Colab notebook is not accessible and the initial code snippet is not executable.</p>

Tashinga_A: <p>Hey,</p>
<p>The fix was to keep transformers to version 3</p>
<p><code>pip install transformers==3</code></p>

vikrant06: <p>I have transformers 3.5.1 and ‘1.7.1’. I am getting the same error.<br>
Here is my code:</p>
<pre><code class="lang-auto">class SentimentClassifier(nn.Module):
    def __init__(self, n_classes):
        super(SentimentClassifier, self).__init__()
        self.bert = DistilBertModel.from_pretrained("distilbert-base-uncased",cache_dir=cache_path
                                                    ,output_hidden_states=True, return_dict=False)
        self.drop = nn.Dropout(p=0.3)
        self.out = nn.Linear(self.bert.config.hidden_size, n_classes)
    def forward(self, input_ids, attention_mask):
        _, pooled_output = self.bert(input_ids=input_ids,attention_mask=attention_mask, return_dict=False)
        output = self.drop(pooled_output)
        return self.out(output)
</code></pre>