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