MCPcopy Create free account
hub / github.com/THUDM/GLM / BertForMaskedLM

Class BertForMaskedLM

model/modeling_bert.py:1019–1079  ·  view source on GitHub ↗

BERT model with the masked language modeling head. This module comprises the BERT model followed by the masked language modeling head. Params: config: a BertConfig class instance with the configuration to build a new model. Inputs: `input_ids`: a torch.LongTensor of sha

Source from the content-addressed store, hash-verified

1017
1018
1019class BertForMaskedLM(PreTrainedBertModel):
1020 """BERT model with the masked language modeling head.
1021 This module comprises the BERT model followed by the masked language modeling head.
1022
1023 Params:
1024 config: a BertConfig class instance with the configuration to build a new model.
1025
1026 Inputs:
1027 `input_ids`: a torch.LongTensor of shape [batch_size, sequence_length]
1028 with the word token indices in the vocabulary(see the tokens preprocessing logic in the scripts
1029 `extract_features.py`, `run_classifier.py` and `run_squad.py`)
1030 `token_type_ids`: an optional torch.LongTensor of shape [batch_size, sequence_length] with the token
1031 types indices selected in [0, 1]. Type 0 corresponds to a `sentence A` and type 1 corresponds to
1032 a `sentence B` token (see BERT paper for more details).
1033 `attention_mask`: an optional torch.LongTensor of shape [batch_size, sequence_length] with indices
1034 selected in [0, 1]. It's a mask to be used if the input sequence length is smaller than the max
1035 input sequence length in the current batch. It's the mask that we typically use for attention when
1036 a batch has varying length sentences.
1037 `masked_lm_labels`: masked language modeling labels: torch.LongTensor of shape [batch_size, sequence_length]
1038 with indices selected in [-1, 0, ..., vocab_size]. All labels set to -1 are ignored (masked), the loss
1039 is only computed for the labels set in [0, ..., vocab_size]
1040
1041 Outputs:
1042 if `masked_lm_labels` is not `None`:
1043 Outputs the masked language modeling loss.
1044 if `masked_lm_labels` is `None`:
1045 Outputs the masked language modeling logits of shape [batch_size, sequence_length, vocab_size].
1046
1047 Example usage:
1048 ```python
1049 # Already been converted into WordPiece token ids
1050 input_ids = torch.LongTensor([[31, 51, 99], [15, 5, 0]])
1051 input_mask = torch.LongTensor([[1, 1, 1], [1, 1, 0]])
1052 token_type_ids = torch.LongTensor([[0, 0, 1], [0, 1, 0]])
1053
1054 config = BertConfig(vocab_size_or_config_json_file=32000, hidden_size=768,
1055 num_hidden_layers=12, num_attention_heads=12, intermediate_size=3072)
1056
1057 model = BertForMaskedLM(config)
1058 masked_lm_logits_scores = model(input_ids, token_type_ids, input_mask)
1059 ```
1060 """
1061
1062 def __init__(self, config):
1063 super(BertForMaskedLM, self).__init__(config)
1064 self.bert = BertModel(config)
1065 self.cls = BertOnlyMLMHead(config, self.bert.embeddings.word_embeddings.weight)
1066 self.apply(self.init_bert_weights)
1067
1068 def forward(self, input_ids, token_type_ids=None, attention_mask=None, masked_lm_labels=None,
1069 checkpoint_activations=False):
1070 sequence_output, _ = self.bert(input_ids, token_type_ids, attention_mask,
1071 output_all_encoded_layers=False, checkpoint_activations=checkpoint_activations)
1072 prediction_scores = self.cls(sequence_output)
1073
1074 if masked_lm_labels is not None:
1075 loss_fct = CrossEntropyLoss(ignore_index=-1)
1076 masked_lm_loss = loss_fct(prediction_scores.view(-1, self.config.vocab_size), masked_lm_labels.view(-1))

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected