The transformers library uses complex output objects instead of plain tuples as return type since one of the updates after 3.5.1.:
from transformers import BertModel, BertTokenizer
t = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertModel.from_pretrained('bert-base-uncased')
o = t.encode_plus('this is a sample sentence', return_tensors='pt')
mo= model(**o)
print(type(mo))
print(mo.keys())
Output:
<class 'transformers.modeling_outputs.BaseModelOutputWithPoolingAndCrossAttentions'>
odict_keys(['last_hidden_state', 'pooler_output'])
You have to change your Sentimentclassifier
to either return to the previous behavior by specifying return_dict=False
or use the BaseModelOutputWithPoolingAndCrossAttentions
(recommended):
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)
def forward(self, input_ids, attention_mask):
bertOutput = self.bert(
input_ids=input_ids,
attention_mask=attention_mask
)
output = self.drop(bertOutput['pooler_output'])
return self.out(output)
Please have a look at this stackoverflow post in case you do not want to use my recommended solution.