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

Method forward

bert/modeling_bert.py:1126–1200  ·  view source on GitHub ↗

r""" next_sentence_label (:obj:`torch.LongTensor` of shape :obj:`(batch_size,)`, `optional`, defaults to :obj:`None`): Labels for computing the next sequence prediction (classification) loss. Input should be a sequence pair (see ``input_ids`` docstring) Indices should

(
        self,
        input_ids=None,
        attention_mask=None,
        token_type_ids=None,
        position_ids=None,
        head_mask=None,
        inputs_embeds=None,
        next_sentence_label=None,
        output_attentions=None,
        output_hidden_states=None,
    )

Source from the content-addressed store, hash-verified

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')
1174
1175 >>> loss, logits = model(**encoding, next_sentence_label=torch.LongTensor([1]))
1176 >>> assert logits[0, 0] < logits[0, 1] # next sentence was random
1177 """
1178
1179 outputs = self.bert(
1180 input_ids,
1181 attention_mask=attention_mask,
1182 token_type_ids=token_type_ids,
1183 position_ids=position_ids,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected