r""" labels (``torch.LongTensor`` of shape ``(batch_size, sequence_length)``, `optional`, defaults to :obj:`None`): Labels for computing the masked language modeling loss. Indices should be in ``[-100, 0, ..., config.vocab_size]`` (see ``input_ids`` docstring)
(
self,
input_ids=None,
attention_mask=None,
token_type_ids=None,
position_ids=None,
head_mask=None,
inputs_embeds=None,
labels=None,
next_sentence_label=None,
output_attentions=None,
output_hidden_states=None,
**kwargs
)
| 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)`. |
| 836 | |
| 837 | Attentions weights after the attention softmax, used to compute the weighted average in the self-attention |
| 838 | heads. |
| 839 | |
| 840 | |
| 841 | Examples:: |
| 842 | |
| 843 | >>> from transformers import BertTokenizer, BertForPreTraining |
| 844 | >>> import torch |
| 845 | |
| 846 | >>> tokenizer = BertTokenizer.from_pretrained('bert-base-uncased') |
| 847 | >>> model = BertForPreTraining.from_pretrained('bert-base-uncased') |
| 848 |