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

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)

When I run the following Method on a text input in an attempt to classify its sentiment I get an error

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]

I get an error message 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

Notebook example can be found here: here

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?

Could you post an executable code snippet to reproduce this issue, please?
The linked Colab notebook is not accessible and the initial code snippet is not executable.

Hey,

The fix was to keep transformers to version 3

pip install transformers==3

1 Like

I have transformers 3.5.1 and ‘1.7.1’. I am getting the same error.
Here is my code:

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)