Using BERT for next sentence prediction
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Bidirectional Encoder Representations from Transformers, or BERT, is a groundbreaking model architecture introduced by Google in 2018. BERT is designed to better understand the nuances of language, which includes context from both left and right sides of a word. One of the lesser-discussed features of BERT is its ability to perform next sentence prediction (NSP), which is crucial for tasks such as question answering, conversational agents, and text summarization.
Technical Overview of BERT
BERT is built upon the Transformer architecture introduced by Vaswani et al. in 2017. Transformers leverage a mechanism known as self-attention, which allows BERT to weigh the importance of different words in a sentence relative to the task at hand. The two core designs of BERT include pre-training and fine-tuning:
- Pre-training: In this phase, BERT is trained on two primary tasks:
- Masked Language Modeling (MLM): Randomly masks words in the input sentence and learns to predict them.
- Next Sentence Prediction (NSP): Given two sentences, determines whether the second sentence naturally follows the first.
- Fine-tuning: After pre-training, BERT can be fine-tuned for specific tasks like sentiment analysis, named entity recognition, and more.
Next Sentence Prediction in BERT
NSP is instrumental in understanding context over sentence boundaries. During pre-training for NSP, BERT is fed sentence pairs: 50% of the time, the second sentence is the immediate continuation of the first (labeled as IsNext), and 50% of the time, the second sentence is randomly picked (labeled as NotNext).
The architecture setup for this task involves:
- CLS Token: The special
[CLS]token is added at the beginning of the sentence pair and is used to make the NSP classification. - SEP Token: The
[SEP]token separates the two sentences.
Technical Process of NSP
- Input Embedding: Sentences are tokenized and converted into input embeddings, including position embeddings, token embeddings, and segment embeddings.
- Transformer Encoding: The sentence pair is processed through BERT's multiple layers of transformers.
- Classification Layer: The output associated with the
[CLS]token is passed to a classification layer, producing probabilities for the IsNext and NotNext classes.
Example of Next Sentence Prediction
Consider the following sentence pair:
- Sentence A: "The quick brown fox jumps over the lazy dog."
- Sentence B1: "The dog barks loudly."
In this context, Sentence B1 is likely to follow Sentence A, and thus, the NSP task should predict it as IsNext.
Conversely, for:
- Sentence B2: "The stars twinkle in the night sky."
The model should predict NotNext.
Challenges and Opportunities
While BERT’s NSP is powerful, it has limitations and opportunities:
- Coherence vs. Common-Sense Understanding: Although BERT excels at predicting sentence order, its understanding is surface-level, missing deeper semantic coherence unless specifically fine-tuned.
- Limited Generalization: In some contexts, NSP may produce incorrect predictions when faced with highly coherent yet non-sequential sentences.
Applications of Next Sentence Prediction
- Question-Answering Systems: Identifying contextually relevant answers.
- Conversational AI: Maintaining coherent conversations by predicting logically following responses.
- Text Summarization: Understanding document flow helps in extracting key points.
Implementation Strategy
To implement NSP with BERT, leveraging the Hugging Face Transformers library is highly recommended, given its ease and flexibility. Here is a high-level example of how you might set this up:
Related reading
- Using celery to process huge text files
- Using keras tokenizer for new words not in training set
- Using NLTK/NaiveBayesClassifier, always getting ''Positive'' output on test data
- Using pre-trained word2vec with LSTM for word generation
- using confusion matrix as scoring metric in cross validation in scikit learn
- Using Contrastive Divergence for Conditional Restricted Boltzmann Machines
- Using Syntaxnet with TensorFlow Serving
- Using word2vec to classify words in categories
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.