DL0066 BERT

What is BERT and how is it used in NLP tasks?

Answer

BERT is a Transformer encoder pretrained to build bidirectional contextual representations of text. Its input combines token, segment, and positional embeddings, and every encoder layer lets each unmasked token attend to context on both sides. Original BERT is pretrained with masked language modeling and next sentence prediction, then adapted by adding a small task head and fine-tuning the whole model or using its representations. The [CLS] output supports sequence-level tasks, token outputs support tagging, and paired start/end scores support extractive question answering.

BERT input embeddings and bidirectional Transformer encoder producing contextual token representations.

Figure 1: BERT combines token, segment, and position embeddings, processes them with bidirectional encoder layers, and exposes contextual outputs.

(1) Bidirectional Encoder: Unlike a causal decoder, BERT‘s self-attention normally uses both left and right context for every visible input token.
(2) Pre-Training: Original BERT predicts selected masked tokens and also trains an NSP classifier; later BERT-family models often modify or remove NSP.
(3) Task Adaptation: A lightweight output head maps contextual states to sequence labels, token labels, sentence-pair scores, or answer spans.

BERT fine-tuning flowchart for classification, token labeling, question answering, and sentence pairs.

Figure 2: The same pretrained encoder can be adapted with small heads that consume either [CLS], every token state, or start/end span scores.

Mathematical Formulation:
H=\mathrm{Encoder}(E_{\mathrm{token}}+E_{\mathrm{segment}}+E_{\mathrm{position}})
\mathcal{L}_{\mathrm{MLM}}=-\sum_{i\in\mathcal{M}}\log p(x_i\mid x_{\setminus\mathcal{M}})

Where:

  • E_{\mathrm{token}}, E_{\mathrm{segment}}, and E_{\mathrm{position}} are token, segment, and positional embedding matrices for the input sequence.
  • \mathrm{Encoder} is the bidirectional Transformer stack and H contains one contextual vector per input position.
  • \mathcal{L}_{\mathrm{MLM}} is masked-language-modeling loss and \mathcal{M} is the selected set of prediction positions.
  • i\in\mathcal{M} indexes one selected target, x_i is its original token, and x_{\setminus\mathcal{M}} denotes the visible corrupted context outside the selected targets.
  • p(x_i\mid x_{\setminus\mathcal{M}}) is the model probability assigned to the original target token given visible context.

Login to view more content

Did you solve the problem?

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *