| 1114 | """Bert Model with a `next sentence prediction (classification)` head on top. """, BERT_START_DOCSTRING, |
| 1115 | ) |
| 1116 | class BertForNextSentencePrediction(BertPreTrainedModel): |
| 1117 | def __init__(self, config): |
| 1118 | super().__init__(config) |
| 1119 | |
| 1120 | self.bert = BertModel(config) |
| 1121 | self.cls = BertOnlyNSPHead(config) |
| 1122 | |
| 1123 | self.init_weights() |
| 1124 | |
| 1125 | @add_start_docstrings_to_callable(BERT_INPUTS_DOCSTRING.format("(batch_size, sequence_length)")) |
| 1126 | def forward( |
| 1127 | self, |
| 1128 | input_ids=None, |
| 1129 | attention_mask=None, |
| 1130 | token_type_ids=None, |
| 1131 | position_ids=None, |
| 1132 | head_mask=None, |
| 1133 | inputs_embeds=None, |
| 1134 | next_sentence_label=None, |
| 1135 | output_attentions=None, |
| 1136 | output_hidden_states=None, |
| 1137 | ): |
| 1138 | r""" |
| 1139 | next_sentence_label (:obj:`torch.LongTensor` of shape :obj:`(batch_size,)`, `optional`, defaults to :obj:`None`): |
| 1140 | Labels for computing the next sequence prediction (classification) loss. Input should be a sequence pair (see ``input_ids`` docstring) |
| 1141 | Indices should be in ``[0, 1]``. |
| 1142 | ``0`` indicates sequence B is a continuation of sequence A, |
| 1143 | ``1`` indicates sequence B is a random sequence. |
| 1144 | |
| 1145 | Returns: |
| 1146 | :obj:`tuple(torch.FloatTensor)` comprising various elements depending on the configuration (:class:`~transformers.BertConfig`) and inputs: |
| 1147 | loss (:obj:`torch.FloatTensor` of shape :obj:`(1,)`, `optional`, returned when :obj:`next_sentence_label` is provided): |
| 1148 | Next sequence prediction (classification) loss. |
| 1149 | seq_relationship_scores (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, 2)`): |
| 1150 | Prediction scores of the next sequence prediction (classification) head (scores of True/False continuation before SoftMax). |
| 1151 | hidden_states (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_hidden_states=True`` is passed or when ``config.output_hidden_states=True``): |
| 1152 | Tuple of :obj:`torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer) |
| 1153 | of shape :obj:`(batch_size, sequence_length, hidden_size)`. |
| 1154 | |
| 1155 | Hidden-states of the model at the output of each layer plus the initial embedding outputs. |
| 1156 | attentions (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_attentions=True`` is passed or when ``config.output_attentions=True``): |
| 1157 | Tuple of :obj:`torch.FloatTensor` (one for each layer) of shape |
| 1158 | :obj:`(batch_size, num_heads, sequence_length, sequence_length)`. |
| 1159 | |
| 1160 | Attentions weights after the attention softmax, used to compute the weighted average in the self-attention |
| 1161 | heads. |
| 1162 | |
| 1163 | Examples:: |
| 1164 | |
| 1165 | >>> from transformers import BertTokenizer, BertForNextSentencePrediction |
| 1166 | >>> import torch |
| 1167 | |
| 1168 | >>> tokenizer = BertTokenizer.from_pretrained('bert-base-uncased') |
| 1169 | >>> model = BertForNextSentencePrediction.from_pretrained('bert-base-uncased') |
| 1170 | |
| 1171 | >>> prompt = "In Italy, pizza served in formal settings, such as at a restaurant, is presented unsliced." |
| 1172 | >>> next_sentence = "The sky is blue due to the shorter wavelength of blue light." |
| 1173 | >>> encoding = tokenizer(prompt, next_sentence, return_tensors='pt') |
nothing calls this directly
no outgoing calls
no test coverage detected