| 1293 | BERT_START_DOCSTRING, |
| 1294 | ) |
| 1295 | class BertForMultipleChoice(BertPreTrainedModel): |
| 1296 | def __init__(self, config): |
| 1297 | super().__init__(config) |
| 1298 | |
| 1299 | self.bert = BertModel(config) |
| 1300 | self.dropout = nn.Dropout(config.hidden_dropout_prob) |
| 1301 | self.classifier = nn.Linear(config.hidden_size, 1) |
| 1302 | |
| 1303 | self.init_weights() |
| 1304 | |
| 1305 | @add_start_docstrings_to_callable(BERT_INPUTS_DOCSTRING.format("(batch_size, num_choices, sequence_length)")) |
| 1306 | @add_code_sample_docstrings(tokenizer_class=_TOKENIZER_FOR_DOC, checkpoint="bert-base-uncased") |
| 1307 | def forward( |
| 1308 | self, |
| 1309 | input_ids=None, |
| 1310 | attention_mask=None, |
| 1311 | token_type_ids=None, |
| 1312 | position_ids=None, |
| 1313 | head_mask=None, |
| 1314 | inputs_embeds=None, |
| 1315 | labels=None, |
| 1316 | output_attentions=None, |
| 1317 | output_hidden_states=None, |
| 1318 | ): |
| 1319 | r""" |
| 1320 | labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size,)`, `optional`, defaults to :obj:`None`): |
| 1321 | Labels for computing the multiple choice classification loss. |
| 1322 | Indices should be in ``[0, ..., num_choices-1]`` where `num_choices` is the size of the second dimension |
| 1323 | of the input tensors. (see `input_ids` above) |
| 1324 | |
| 1325 | Returns: |
| 1326 | :obj:`tuple(torch.FloatTensor)` comprising various elements depending on the configuration (:class:`~transformers.BertConfig`) and inputs: |
| 1327 | loss (:obj:`torch.FloatTensor` of shape `(1,)`, `optional`, returned when :obj:`labels` is provided): |
| 1328 | Classification loss. |
| 1329 | classification_scores (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, num_choices)`): |
| 1330 | `num_choices` is the second dimension of the input tensors. (see `input_ids` above). |
| 1331 | |
| 1332 | Classification scores (before SoftMax). |
| 1333 | hidden_states (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_hidden_states=True`` is passed or when ``config.output_hidden_states=True``): |
| 1334 | Tuple of :obj:`torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer) |
| 1335 | of shape :obj:`(batch_size, sequence_length, hidden_size)`. |
| 1336 | |
| 1337 | Hidden-states of the model at the output of each layer plus the initial embedding outputs. |
| 1338 | attentions (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_attentions=True`` is passed or when ``config.output_attentions=True``): |
| 1339 | Tuple of :obj:`torch.FloatTensor` (one for each layer) of shape |
| 1340 | :obj:`(batch_size, num_heads, sequence_length, sequence_length)`. |
| 1341 | |
| 1342 | Attentions weights after the attention softmax, used to compute the weighted average in the self-attention |
| 1343 | heads. |
| 1344 | """ |
| 1345 | num_choices = input_ids.shape[1] if input_ids is not None else inputs_embeds.shape[1] |
| 1346 | |
| 1347 | input_ids = input_ids.view(-1, input_ids.size(-1)) if input_ids is not None else None |
| 1348 | attention_mask = attention_mask.view(-1, attention_mask.size(-1)) if attention_mask is not None else None |
| 1349 | token_type_ids = token_type_ids.view(-1, token_type_ids.size(-1)) if token_type_ids is not None else None |
| 1350 | position_ids = position_ids.view(-1, position_ids.size(-1)) if position_ids is not None else None |
| 1351 | inputs_embeds = ( |
| 1352 | inputs_embeds.view(-1, inputs_embeds.size(-2), inputs_embeds.size(-1)) |
nothing calls this directly
no outgoing calls
no test coverage detected