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

Method forward

bert/modeling_bert.py:1307–1381  ·  view source on GitHub ↗

r""" labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size,)`, `optional`, defaults to :obj:`None`): Labels for computing the multiple choice classification loss. Indices should be in ``[0, ..., num_choices-1]`` where `num_choices` is the size of the second dimen

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

Source from the content-addressed store, hash-verified

1305 @add_start_docstrings_to_callable(BERT_INPUTS_DOCSTRING.format("(batch_size, num_choices, sequence_length)"))
1306 @add_code_sample_docstrings(tokenizer_class=_TOKENIZER_FOR_DOC, checkpoint="bert-base-uncased")
1307 def forward(
1308 self,
1309 input_ids=None,
1310 attention_mask=None,
1311 token_type_ids=None,
1312 position_ids=None,
1313 head_mask=None,
1314 inputs_embeds=None,
1315 labels=None,
1316 output_attentions=None,
1317 output_hidden_states=None,
1318 ):
1319 r"""
1320 labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size,)`, `optional`, defaults to :obj:`None`):
1321 Labels for computing the multiple choice classification loss.
1322 Indices should be in ``[0, ..., num_choices-1]`` where `num_choices` is the size of the second dimension
1323 of the input tensors. (see `input_ids` above)
1324
1325 Returns:
1326 :obj:`tuple(torch.FloatTensor)` comprising various elements depending on the configuration (:class:`~transformers.BertConfig`) and inputs:
1327 loss (:obj:`torch.FloatTensor` of shape `(1,)`, `optional`, returned when :obj:`labels` is provided):
1328 Classification loss.
1329 classification_scores (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, num_choices)`):
1330 `num_choices` is the second dimension of the input tensors. (see `input_ids` above).
1331
1332 Classification scores (before SoftMax).
1333 hidden_states (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_hidden_states=True`` is passed or when ``config.output_hidden_states=True``):
1334 Tuple of :obj:`torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer)
1335 of shape :obj:`(batch_size, sequence_length, hidden_size)`.
1336
1337 Hidden-states of the model at the output of each layer plus the initial embedding outputs.
1338 attentions (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_attentions=True`` is passed or when ``config.output_attentions=True``):
1339 Tuple of :obj:`torch.FloatTensor` (one for each layer) of shape
1340 :obj:`(batch_size, num_heads, sequence_length, sequence_length)`.
1341
1342 Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
1343 heads.
1344 """
1345 num_choices = input_ids.shape[1] if input_ids is not None else inputs_embeds.shape[1]
1346
1347 input_ids = input_ids.view(-1, input_ids.size(-1)) if input_ids is not None else None
1348 attention_mask = attention_mask.view(-1, attention_mask.size(-1)) if attention_mask is not None else None
1349 token_type_ids = token_type_ids.view(-1, token_type_ids.size(-1)) if token_type_ids is not None else None
1350 position_ids = position_ids.view(-1, position_ids.size(-1)) if position_ids is not None else None
1351 inputs_embeds = (
1352 inputs_embeds.view(-1, inputs_embeds.size(-2), inputs_embeds.size(-1))
1353 if inputs_embeds is not None
1354 else None
1355 )
1356
1357 outputs = self.bert(
1358 input_ids,
1359 attention_mask=attention_mask,
1360 token_type_ids=token_type_ids,
1361 position_ids=position_ids,
1362 head_mask=head_mask,
1363 inputs_embeds=inputs_embeds,
1364 output_attentions=output_attentions,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected