MCPcopy Create free account
hub / github.com/SooLab/CGFormer / BertForQuestionAnswering

Class BertForQuestionAnswering

bert/modeling_bert.py:1477–1569  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1475 BERT_START_DOCSTRING,
1476)
1477class BertForQuestionAnswering(BertPreTrainedModel):
1478 def __init__(self, config):
1479 super().__init__(config)
1480 self.num_labels = config.num_labels
1481
1482 self.bert = BertModel(config)
1483 self.qa_outputs = nn.Linear(config.hidden_size, config.num_labels)
1484
1485 self.init_weights()
1486
1487 @add_start_docstrings_to_callable(BERT_INPUTS_DOCSTRING.format("(batch_size, sequence_length)"))
1488 @add_code_sample_docstrings(tokenizer_class=_TOKENIZER_FOR_DOC, checkpoint="bert-base-uncased")
1489 def forward(
1490 self,
1491 input_ids=None,
1492 attention_mask=None,
1493 token_type_ids=None,
1494 position_ids=None,
1495 head_mask=None,
1496 inputs_embeds=None,
1497 start_positions=None,
1498 end_positions=None,
1499 output_attentions=None,
1500 output_hidden_states=None,
1501 ):
1502 r"""
1503 start_positions (:obj:`torch.LongTensor` of shape :obj:`(batch_size,)`, `optional`, defaults to :obj:`None`):
1504 Labels for position (index) of the start of the labelled span for computing the token classification loss.
1505 Positions are clamped to the length of the sequence (`sequence_length`).
1506 Position outside of the sequence are not taken into account for computing the loss.
1507 end_positions (:obj:`torch.LongTensor` of shape :obj:`(batch_size,)`, `optional`, defaults to :obj:`None`):
1508 Labels for position (index) of the end of the labelled span for computing the token classification loss.
1509 Positions are clamped to the length of the sequence (`sequence_length`).
1510 Position outside of the sequence are not taken into account for computing the loss.
1511
1512 Returns:
1513 :obj:`tuple(torch.FloatTensor)` comprising various elements depending on the configuration (:class:`~transformers.BertConfig`) and inputs:
1514 loss (:obj:`torch.FloatTensor` of shape :obj:`(1,)`, `optional`, returned when :obj:`labels` is provided):
1515 Total span extraction loss is the sum of a Cross-Entropy for the start and end positions.
1516 start_scores (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length,)`):
1517 Span-start scores (before SoftMax).
1518 end_scores (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length,)`):
1519 Span-end scores (before SoftMax).
1520 hidden_states (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_hidden_states=True`` is passed or when ``config.output_hidden_states=True``):
1521 Tuple of :obj:`torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer)
1522 of shape :obj:`(batch_size, sequence_length, hidden_size)`.
1523
1524 Hidden-states of the model at the output of each layer plus the initial embedding outputs.
1525 attentions (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_attentions=True`` is passed or when ``config.output_attentions=True``):
1526 Tuple of :obj:`torch.FloatTensor` (one for each layer) of shape
1527 :obj:`(batch_size, num_heads, sequence_length, sequence_length)`.
1528
1529 Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
1530 heads.
1531 """
1532
1533 outputs = self.bert(
1534 input_ids,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected