BERT model for multiple choice tasks. This module is composed of the BERT model with a linear layer on top of the pooled output. Params: `config`: a BertConfig class instance with the configuration to build a new model. `num_choices`: the number of classes for the classi
| 1212 | |
| 1213 | |
| 1214 | class BertForMultipleChoice(PreTrainedBertModel): |
| 1215 | """BERT model for multiple choice tasks. |
| 1216 | This module is composed of the BERT model with a linear layer on top of |
| 1217 | the pooled output. |
| 1218 | |
| 1219 | Params: |
| 1220 | `config`: a BertConfig class instance with the configuration to build a new model. |
| 1221 | `num_choices`: the number of classes for the classifier. Default = 2. |
| 1222 | |
| 1223 | Inputs: |
| 1224 | `input_ids`: a torch.LongTensor of shape [batch_size, num_choices, sequence_length] |
| 1225 | with the word token indices in the vocabulary(see the tokens preprocessing logic in the scripts |
| 1226 | `extract_features.py`, `run_classifier.py` and `run_squad.py`) |
| 1227 | `token_type_ids`: an optional torch.LongTensor of shape [batch_size, num_choices, sequence_length] |
| 1228 | with the token types indices selected in [0, 1]. Type 0 corresponds to a `sentence A` |
| 1229 | and type 1 corresponds to a `sentence B` token (see BERT paper for more details). |
| 1230 | `attention_mask`: an optional torch.LongTensor of shape [batch_size, num_choices, sequence_length] with indices |
| 1231 | selected in [0, 1]. It's a mask to be used if the input sequence length is smaller than the max |
| 1232 | input sequence length in the current batch. It's the mask that we typically use for attention when |
| 1233 | a batch has varying length sentences. |
| 1234 | `labels`: labels for the classification output: torch.LongTensor of shape [batch_size] |
| 1235 | with indices selected in [0, ..., num_choices]. |
| 1236 | |
| 1237 | Outputs: |
| 1238 | if `labels` is not `None`: |
| 1239 | Outputs the CrossEntropy classification loss of the output with the labels. |
| 1240 | if `labels` is `None`: |
| 1241 | Outputs the classification logits of shape [batch_size, num_labels]. |
| 1242 | |
| 1243 | Example usage: |
| 1244 | ```python |
| 1245 | # Already been converted into WordPiece token ids |
| 1246 | input_ids = torch.LongTensor([[[31, 51, 99], [15, 5, 0]], [[12, 16, 42], [14, 28, 57]]]) |
| 1247 | input_mask = torch.LongTensor([[[1, 1, 1], [1, 1, 0]],[[1,1,0], [1, 0, 0]]]) |
| 1248 | token_type_ids = torch.LongTensor([[[0, 0, 1], [0, 1, 0]],[[0, 1, 1], [0, 0, 1]]]) |
| 1249 | config = BertConfig(vocab_size_or_config_json_file=32000, hidden_size=768, |
| 1250 | num_hidden_layers=12, num_attention_heads=12, intermediate_size=3072) |
| 1251 | |
| 1252 | num_choices = 2 |
| 1253 | |
| 1254 | model = BertForMultipleChoice(config, num_choices) |
| 1255 | logits = model(input_ids, token_type_ids, input_mask) |
| 1256 | ``` |
| 1257 | """ |
| 1258 | |
| 1259 | def __init__(self, config): |
| 1260 | super(BertForMultipleChoice, self).__init__(config) |
| 1261 | self.bert = BertModel(config) |
| 1262 | self.dropout = nn.Dropout(config.hidden_dropout_prob) |
| 1263 | self.classifier = nn.Linear(config.hidden_size, 1) |
| 1264 | self.apply(self.init_bert_weights) |
| 1265 | |
| 1266 | def forward(self, input_ids, token_type_ids=None, attention_mask=None, labels=None, checkpoint_activations=False): |
| 1267 | batch_size, num_choices = input_ids.shape[:2] |
| 1268 | flat_input_ids = input_ids.reshape(-1, input_ids.size(-1)) |
| 1269 | flat_token_type_ids = token_type_ids.reshape(-1, token_type_ids.size(-1)) |
| 1270 | flat_attention_mask = attention_mask.reshape(-1, attention_mask.size(-1)) |
| 1271 | _, pooled_output = self.bert(flat_input_ids, flat_token_type_ids, flat_attention_mask, |
nothing calls this directly
no outgoing calls
no test coverage detected