BERTForQuestionAnswering
PretrainedModelBERTForQuestionAnswering(config: BERTConfig)BERT with a 2-way span head for extractive question answering.
Wraps the bidirectional encoder with a single linear of output width 2,
producing start- and end-position logits over each token in the input.
This is the SQuAD v1.1 / v2.0 fine-tuning recipe of Devlin et al., 2018
§4.2 — given a (question, context) pair concatenated with [SEP],
the model predicts the answer span inside the context.
Parameters
configBERTConfignum_labels
is ignored here.Attributes
Notes
Reference: Devlin, Chang, Lee, and Toutanova, "BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding", NAACL 2019 (arXiv:1810.04805).
When both start_positions and end_positions are provided, the
loss is the symmetric average of two cross-entropies:
The returned logits tensor has shape (B, T, 2); index [..., 0]
for start scores and [..., 1] for end scores.
Examples
>>> import lucid
>>> from lucid.models.text.bert import BERTConfig, BERTForQuestionAnswering
>>> cfg = BERTConfig(num_hidden_layers=2, hidden_size=128,
... num_attention_heads=2, intermediate_size=512)
>>> model = BERTForQuestionAnswering(cfg).eval()
>>> input_ids = lucid.tensor([[101, 2040, 2003, 102, 1045, 2572, 102]])
>>> out = model(input_ids)
>>> out.logits.shape # (B=1, T=7, 2)
(1, 7, 2)Used by 2
Constructors
1Instance methods
1forward(input_ids: Tensor, attention_mask: Tensor | None = None, token_type_ids: Tensor | None = None, start_positions: Tensor | None = None, end_positions: Tensor | None = None)