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

Method forward

bert/modeling_bert.py:908–992  ·  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 left-to-right language modeling loss (next word prediction). Indices should be in ``[-100, 0, ..., config.vocab_size]`` (

(
        self,
        input_ids=None,
        attention_mask=None,
        token_type_ids=None,
        position_ids=None,
        head_mask=None,
        inputs_embeds=None,
        labels=None,
        encoder_hidden_states=None,
        encoder_attention_mask=None,
        output_attentions=None,
        output_hidden_states=None,
        **kwargs
    )

Source from the content-addressed store, hash-verified

906
907 @add_start_docstrings_to_callable(BERT_INPUTS_DOCSTRING.format("(batch_size, sequence_length)"))
908 def forward(
909 self,
910 input_ids=None,
911 attention_mask=None,
912 token_type_ids=None,
913 position_ids=None,
914 head_mask=None,
915 inputs_embeds=None,
916 labels=None,
917 encoder_hidden_states=None,
918 encoder_attention_mask=None,
919 output_attentions=None,
920 output_hidden_states=None,
921 **kwargs
922 ):
923 r"""
924 labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`, defaults to :obj:`None`):
925 Labels for computing the left-to-right language modeling loss (next word prediction).
926 Indices should be in ``[-100, 0, ..., config.vocab_size]`` (see ``input_ids`` docstring)
927 Tokens with indices set to ``-100`` are ignored (masked), the loss is only computed for the tokens with labels
928 in ``[0, ..., config.vocab_size]``
929 kwargs (:obj:`Dict[str, any]`, optional, defaults to `{}`):
930 Used to hide legacy arguments that have been deprecated.
931
932 Returns:
933 :obj:`tuple(torch.FloatTensor)` comprising various elements depending on the configuration (:class:`~transformers.BertConfig`) and inputs:
934 ltr_lm_loss (:obj:`torch.FloatTensor` of shape :obj:`(1,)`, `optional`, returned when :obj:`labels` is provided):
935 Next token prediction loss.
936 prediction_scores (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length, config.vocab_size)`)
937 Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
938 hidden_states (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_hidden_states=True`` is passed or when ``config.output_hidden_states=True``):
939 Tuple of :obj:`torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer)
940 of shape :obj:`(batch_size, sequence_length, hidden_size)`.
941
942 Hidden-states of the model at the output of each layer plus the initial embedding outputs.
943 attentions (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_attentions=True`` is passed or when ``config.output_attentions=True``):
944 Tuple of :obj:`torch.FloatTensor` (one for each layer) of shape
945 :obj:`(batch_size, num_heads, sequence_length, sequence_length)`.
946
947 Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
948 heads.
949
950 Example::
951
952 >>> from transformers import BertTokenizer, BertLMHeadModel, BertConfig
953 >>> import torch
954
955 >>> tokenizer = BertTokenizer.from_pretrained('bert-base-cased')
956 >>> config = BertConfig.from_pretrained("bert-base-cased")
957 >>> config.is_decoder = True
958 >>> model = BertLMHeadModel.from_pretrained('bert-base-cased', config=config)
959
960 >>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
961 >>> outputs = model(**inputs)
962
963 >>> last_hidden_states = outputs[0] # The last hidden-state is the first element of the output tuple
964 """
965

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected