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

Method forward

bert/modeling_bert.py:1402–1469  ·  view source on GitHub ↗

r""" labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`, defaults to :obj:`None`): Labels for computing the token classification loss. Indices should be in ``[0, ..., config.num_labels - 1]``. Returns: :obj:`tuple(to

(
        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

1400 @add_start_docstrings_to_callable(BERT_INPUTS_DOCSTRING.format("(batch_size, sequence_length)"))
1401 @add_code_sample_docstrings(tokenizer_class=_TOKENIZER_FOR_DOC, checkpoint="bert-base-uncased")
1402 def forward(
1403 self,
1404 input_ids=None,
1405 attention_mask=None,
1406 token_type_ids=None,
1407 position_ids=None,
1408 head_mask=None,
1409 inputs_embeds=None,
1410 labels=None,
1411 output_attentions=None,
1412 output_hidden_states=None,
1413 ):
1414 r"""
1415 labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`, defaults to :obj:`None`):
1416 Labels for computing the token classification loss.
1417 Indices should be in ``[0, ..., config.num_labels - 1]``.
1418
1419 Returns:
1420 :obj:`tuple(torch.FloatTensor)` comprising various elements depending on the configuration (:class:`~transformers.BertConfig`) and inputs:
1421 loss (:obj:`torch.FloatTensor` of shape :obj:`(1,)`, `optional`, returned when ``labels`` is provided) :
1422 Classification loss.
1423 scores (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length, config.num_labels)`)
1424 Classification scores (before SoftMax).
1425 hidden_states (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_hidden_states=True`` is passed or when ``config.output_hidden_states=True``):
1426 Tuple of :obj:`torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer)
1427 of shape :obj:`(batch_size, sequence_length, hidden_size)`.
1428
1429 Hidden-states of the model at the output of each layer plus the initial embedding outputs.
1430 attentions (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_attentions=True`` is passed or when ``config.output_attentions=True``):
1431 Tuple of :obj:`torch.FloatTensor` (one for each layer) of shape
1432 :obj:`(batch_size, num_heads, sequence_length, sequence_length)`.
1433
1434 Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
1435 heads.
1436 """
1437
1438 outputs = self.bert(
1439 input_ids,
1440 attention_mask=attention_mask,
1441 token_type_ids=token_type_ids,
1442 position_ids=position_ids,
1443 head_mask=head_mask,
1444 inputs_embeds=inputs_embeds,
1445 output_attentions=output_attentions,
1446 output_hidden_states=output_hidden_states,
1447 )
1448
1449 sequence_output = outputs[0]
1450
1451 sequence_output = self.dropout(sequence_output)
1452 logits = self.classifier(sequence_output)
1453
1454 outputs = (logits,) + outputs[2:] # add hidden states and attention if they are here
1455 if labels is not None:
1456 loss_fct = CrossEntropyLoss()
1457 # Only keep active parts of the loss
1458 if attention_mask is not None:
1459 active_loss = attention_mask.view(-1) == 1

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected