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

Class BertLMHeadModel

bert/modeling_bert.py:894–1001  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

892 """Bert Model with a `language modeling` head on top for CLM fine-tuning. """, BERT_START_DOCSTRING
893)
894class BertLMHeadModel(BertPreTrainedModel):
895 def __init__(self, config):
896 super().__init__(config)
897 assert config.is_decoder, "If you want to use `BertLMHeadModel` as a standalone, add `is_decoder=True`."
898
899 self.bert = BertModel(config)
900 self.cls = BertOnlyMLMHead(config)
901
902 self.init_weights()
903
904 def get_output_embeddings(self):
905 return self.cls.predictions.decoder
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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected