| 776 | BERT_START_DOCSTRING, |
| 777 | ) |
| 778 | class BertForPreTraining(BertPreTrainedModel): |
| 779 | def __init__(self, config): |
| 780 | super().__init__(config) |
| 781 | |
| 782 | self.bert = BertModel(config) |
| 783 | self.cls = BertPreTrainingHeads(config) |
| 784 | |
| 785 | self.init_weights() |
| 786 | |
| 787 | def get_output_embeddings(self): |
| 788 | return self.cls.predictions.decoder |
| 789 | |
| 790 | @add_start_docstrings_to_callable(BERT_INPUTS_DOCSTRING.format("(batch_size, sequence_length)")) |
| 791 | def forward( |
| 792 | self, |
| 793 | input_ids=None, |
| 794 | attention_mask=None, |
| 795 | token_type_ids=None, |
| 796 | position_ids=None, |
| 797 | head_mask=None, |
| 798 | inputs_embeds=None, |
| 799 | labels=None, |
| 800 | next_sentence_label=None, |
| 801 | output_attentions=None, |
| 802 | output_hidden_states=None, |
| 803 | **kwargs |
| 804 | ): |
| 805 | r""" |
| 806 | labels (``torch.LongTensor`` of shape ``(batch_size, sequence_length)``, `optional`, defaults to :obj:`None`): |
| 807 | Labels for computing the masked language modeling loss. |
| 808 | Indices should be in ``[-100, 0, ..., config.vocab_size]`` (see ``input_ids`` docstring) |
| 809 | Tokens with indices set to ``-100`` are ignored (masked), the loss is only computed for the tokens with labels |
| 810 | in ``[0, ..., config.vocab_size]`` |
| 811 | next_sentence_label (``torch.LongTensor`` of shape ``(batch_size,)``, `optional`, defaults to :obj:`None`): |
| 812 | Labels for computing the next sequence prediction (classification) loss. Input should be a sequence pair (see :obj:`input_ids` docstring) |
| 813 | Indices should be in ``[0, 1]``. |
| 814 | ``0`` indicates sequence B is a continuation of sequence A, |
| 815 | ``1`` indicates sequence B is a random sequence. |
| 816 | kwargs (:obj:`Dict[str, any]`, optional, defaults to `{}`): |
| 817 | Used to hide legacy arguments that have been deprecated. |
| 818 | |
| 819 | Returns: |
| 820 | :obj:`tuple(torch.FloatTensor)` comprising various elements depending on the configuration (:class:`~transformers.BertConfig`) and inputs: |
| 821 | loss (`optional`, returned when ``labels`` is provided) ``torch.FloatTensor`` of shape ``(1,)``: |
| 822 | Total loss as the sum of the masked language modeling loss and the next sequence prediction (classification) loss. |
| 823 | prediction_scores (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length, config.vocab_size)`) |
| 824 | Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax). |
| 825 | seq_relationship_scores (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, 2)`): |
| 826 | Prediction scores of the next sequence prediction (classification) head (scores of True/False |
| 827 | continuation before SoftMax). |
| 828 | hidden_states (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_hidden_states=True`` is passed or when ``config.output_hidden_states=True``): |
| 829 | Tuple of :obj:`torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer) |
| 830 | of shape :obj:`(batch_size, sequence_length, hidden_size)`. |
| 831 | |
| 832 | Hidden-states of the model at the output of each layer plus the initial embedding outputs. |
| 833 | attentions (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_attentions=True`` is passed or when ``config.output_attentions=True``): |
| 834 | Tuple of :obj:`torch.FloatTensor` (one for each layer) of shape |
| 835 | :obj:`(batch_size, num_heads, sequence_length, sequence_length)`. |
nothing calls this directly
no outgoing calls
no test coverage detected