| 1713 | BERT_START_DOCSTRING, |
| 1714 | ) |
| 1715 | class BertForTokenClassification(BertPreTrainedModel): |
| 1716 | |
| 1717 | _keys_to_ignore_on_load_unexpected = [r"pooler"] |
| 1718 | |
| 1719 | def __init__(self, config): |
| 1720 | super().__init__(config) |
| 1721 | self.num_labels = config.num_labels |
| 1722 | |
| 1723 | self.bert = BertModel(config, add_pooling_layer=False) |
| 1724 | classifier_dropout = ( |
| 1725 | config.classifier_dropout if config.classifier_dropout is not None else config.hidden_dropout_prob |
| 1726 | ) |
| 1727 | self.dropout = nn.Dropout(classifier_dropout) |
| 1728 | self.classifier = nn.Linear(config.hidden_size, config.num_labels) |
| 1729 | |
| 1730 | # Initialize weights and apply final processing |
| 1731 | self.post_init() |
| 1732 | |
| 1733 | @add_start_docstrings_to_model_forward(BERT_INPUTS_DOCSTRING.format("batch_size, sequence_length")) |
| 1734 | @add_code_sample_docstrings( |
| 1735 | processor_class=_TOKENIZER_FOR_DOC, |
| 1736 | checkpoint=_CHECKPOINT_FOR_TOKEN_CLASSIFICATION, |
| 1737 | output_type=TokenClassifierOutput, |
| 1738 | config_class=_CONFIG_FOR_DOC, |
| 1739 | expected_output=_TOKEN_CLASS_EXPECTED_OUTPUT, |
| 1740 | expected_loss=_TOKEN_CLASS_EXPECTED_LOSS, |
| 1741 | ) |
| 1742 | def forward( |
| 1743 | self, |
| 1744 | input_ids: Optional[torch.Tensor] = None, |
| 1745 | attention_mask: Optional[torch.Tensor] = None, |
| 1746 | token_type_ids: Optional[torch.Tensor] = None, |
| 1747 | position_ids: Optional[torch.Tensor] = None, |
| 1748 | head_mask: Optional[torch.Tensor] = None, |
| 1749 | inputs_embeds: Optional[torch.Tensor] = None, |
| 1750 | labels: Optional[torch.Tensor] = None, |
| 1751 | output_attentions: Optional[bool] = None, |
| 1752 | output_hidden_states: Optional[bool] = None, |
| 1753 | return_dict: Optional[bool] = None, |
| 1754 | ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: |
| 1755 | r""" |
| 1756 | labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): |
| 1757 | Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. |
| 1758 | """ |
| 1759 | return_dict = return_dict if return_dict is not None else self.config.use_return_dict |
| 1760 | |
| 1761 | outputs = self.bert( |
| 1762 | input_ids, |
| 1763 | attention_mask=attention_mask, |
| 1764 | token_type_ids=token_type_ids, |
| 1765 | position_ids=position_ids, |
| 1766 | head_mask=head_mask, |
| 1767 | inputs_embeds=inputs_embeds, |
| 1768 | output_attentions=output_attentions, |
| 1769 | output_hidden_states=output_hidden_states, |
| 1770 | return_dict=return_dict, |
| 1771 | ) |
| 1772 |
nothing calls this directly
no outgoing calls
no test coverage detected