| 1514 | BERT_START_DOCSTRING, |
| 1515 | ) |
| 1516 | class BertForSequenceClassification(BertPreTrainedModel): |
| 1517 | def __init__(self, config): |
| 1518 | super().__init__(config) |
| 1519 | self.num_labels = config.num_labels |
| 1520 | self.config = config |
| 1521 | |
| 1522 | self.bert = BertModel(config) |
| 1523 | classifier_dropout = ( |
| 1524 | config.classifier_dropout if config.classifier_dropout is not None else config.hidden_dropout_prob |
| 1525 | ) |
| 1526 | self.dropout = nn.Dropout(classifier_dropout) |
| 1527 | self.classifier = nn.Linear(config.hidden_size, config.num_labels) |
| 1528 | |
| 1529 | # Initialize weights and apply final processing |
| 1530 | self.post_init() |
| 1531 | |
| 1532 | @add_start_docstrings_to_model_forward(BERT_INPUTS_DOCSTRING.format("batch_size, sequence_length")) |
| 1533 | @add_code_sample_docstrings( |
| 1534 | processor_class=_TOKENIZER_FOR_DOC, |
| 1535 | checkpoint=_CHECKPOINT_FOR_SEQUENCE_CLASSIFICATION, |
| 1536 | output_type=SequenceClassifierOutput, |
| 1537 | config_class=_CONFIG_FOR_DOC, |
| 1538 | expected_output=_SEQ_CLASS_EXPECTED_OUTPUT, |
| 1539 | expected_loss=_SEQ_CLASS_EXPECTED_LOSS, |
| 1540 | ) |
| 1541 | def forward( |
| 1542 | self, |
| 1543 | input_ids: Optional[torch.Tensor] = None, |
| 1544 | attention_mask: Optional[torch.Tensor] = None, |
| 1545 | token_type_ids: Optional[torch.Tensor] = None, |
| 1546 | position_ids: Optional[torch.Tensor] = None, |
| 1547 | head_mask: Optional[torch.Tensor] = None, |
| 1548 | inputs_embeds: Optional[torch.Tensor] = None, |
| 1549 | labels: Optional[torch.Tensor] = None, |
| 1550 | output_attentions: Optional[bool] = None, |
| 1551 | output_hidden_states: Optional[bool] = None, |
| 1552 | return_dict: Optional[bool] = None, |
| 1553 | ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: |
| 1554 | r""" |
| 1555 | labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): |
| 1556 | Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., |
| 1557 | config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If |
| 1558 | `config.num_labels > 1` a classification loss is computed (Cross-Entropy). |
| 1559 | """ |
| 1560 | return_dict = return_dict if return_dict is not None else self.config.use_return_dict |
| 1561 | |
| 1562 | outputs = self.bert( |
| 1563 | input_ids, |
| 1564 | attention_mask=attention_mask, |
| 1565 | token_type_ids=token_type_ids, |
| 1566 | position_ids=position_ids, |
| 1567 | head_mask=head_mask, |
| 1568 | inputs_embeds=inputs_embeds, |
| 1569 | output_attentions=output_attentions, |
| 1570 | output_hidden_states=output_hidden_states, |
| 1571 | return_dict=return_dict, |
| 1572 | ) |
| 1573 |
nothing calls this directly
no outgoing calls
no test coverage detected