I am experimenting with the various BertFor*
model classes with pre-trained models
Using bert-base-uncased
with BertForNextSentencePrediction
I have the following code which seems to work nicely:
from transformers import BertTokenizer, BertForNextSentencePrediction
import torch
# Load pre-trained tokenizer and model
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertForNextSentencePrediction.from_pretrained('bert-base-uncased').eval()
# Tokenize input sentences
sentence_a = "The cat sat on the mat."
sentence_b = "It was a nice day outside."
encoded = tokenizer.encode_plus(sentence_a, sentence_b, return_tensors='pt')
# Get model predictions
with torch.no_grad():
outputs = model(**encoded)
logits = outputs[0]
probs = torch.softmax(logits, dim=1)
is_next_sentence = torch.argmax(probs, dim=1).bool().item()
is_next_sentence
is either True
or False
The only problem is that I can’t seem to find any pair of sentences which return True
.
(Does anybody have any examples? Is the code above wrong?)
I am guessing that maybe the base model is just not able to do this task and I need to use a fine-tuned variant?
Does anyone have a bert-base
variant on HF Hub that is fine-tuned on NSP?