| import torch |
| import torch.nn as nn |
| from torch.nn import CrossEntropyLoss, MSELoss, BCEWithLogitsLoss |
| from typing import Optional, Tuple, Union |
| from transformers.modeling_outputs import SequenceClassifierOutput, TokenClassifierOutput |
| from transformers import T5EncoderModel, T5Config |
|
|
|
|
| class T5ClassificationHead(nn.Module): |
| """Head for sentence-level classification tasks.""" |
|
|
| def __init__(self, config: T5Config): |
| super().__init__() |
| self.dense = nn.Linear(config.d_model, config.d_model) |
| self.dropout = nn.Dropout(p=config.classifier_dropout) |
| self.out_proj = nn.Linear(config.d_model, config.num_labels) |
|
|
| def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: |
| hidden_states = self.dropout(hidden_states) |
| hidden_states = self.dense(hidden_states) |
| hidden_states = torch.tanh(hidden_states) |
| hidden_states = self.dropout(hidden_states) |
| hidden_states = self.out_proj(hidden_states) |
| return hidden_states |
|
|
|
|
| class T5ForSequenceClassification(T5EncoderModel): |
| config_class = T5Config |
| all_tied_weights_keys = {} |
| def __init__(self, config: T5Config): |
| super().__init__(config) |
| self.classifier = T5ClassificationHead(config) |
|
|
| def forward( |
| self, |
| input_ids: Optional[torch.LongTensor] = None, |
| attention_mask: Optional[torch.FloatTensor] = None, |
| head_mask: Optional[torch.FloatTensor] = None, |
| inputs_embeds: Optional[torch.FloatTensor] = None, |
| output_attentions: Optional[bool] = None, |
| output_hidden_states: Optional[bool] = None, |
| return_dict: Optional[bool] = None, |
| ) -> SequenceClassifierOutput: |
| return_dict = return_dict if return_dict is not None else self.config.use_return_dict |
|
|
| encoder_outputs = self.encoder( |
| input_ids=input_ids, |
| attention_mask=attention_mask, |
| inputs_embeds=inputs_embeds, |
| head_mask=head_mask, |
| output_attentions=output_attentions, |
| output_hidden_states=output_hidden_states, |
| return_dict=return_dict, |
| ) |
|
|
| sequence_output = encoder_outputs[0] |
| cls_token = sequence_output[:, 0, :] |
|
|
| logits = self.classifier(cls_token) |
|
|
| loss = None |
| if labels is not None: |
| labels = labels.to(logits.device) |
| if self.config.problem_type is None: |
| if self.config.num_labels == 1: |
| self.config.problem_type = "regression" |
| elif self.config.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): |
| self.config.problem_type = "single_label_classification" |
| else: |
| self.config.problem_type = "multi_label_classification" |
|
|
| if self.config.problem_type == "regression": |
| loss_fct = MSELoss() |
| if self.config.num_labels == 1: |
| loss = loss_fct(logits.squeeze(), labels.squeeze()) |
| else: |
| loss = loss_fct(logits, labels) |
| elif self.config.problem_type == "single_label_classification": |
| loss_fct = CrossEntropyLoss() |
| loss = loss_fct(logits.view(-1, self.config.num_labels), labels.view(-1)) |
| elif self.config.problem_type == "multi_label_classification": |
| loss_fct = BCEWithLogitsLoss() |
| loss = loss_fct(logits, labels) |
|
|
| return SequenceClassifierOutput( |
| loss=loss, |
| logits=logits |
| ) |
|
|
|
|
| class T5ForTokenClassification(T5EncoderModel): |
| config_class = T5Config |
| all_tied_weights_keys = {} |
| def __init__(self, config: T5Config): |
| super().__init__(config) |
| self.classifier = T5ClassificationHead(config) |
|
|
| def forward( |
| self, |
| input_ids: Optional[torch.LongTensor] = None, |
| attention_mask: Optional[torch.FloatTensor] = None, |
| head_mask: Optional[torch.FloatTensor] = None, |
| inputs_embeds: Optional[torch.FloatTensor] = None, |
| output_attentions: Optional[bool] = None, |
| output_hidden_states: Optional[bool] = None, |
| return_dict: Optional[bool] = None, |
| ) -> SequenceClassifierOutput: |
| return_dict = return_dict if return_dict is not None else self.config.use_return_dict |
|
|
| encoder_outputs = self.encoder( |
| input_ids=input_ids, |
| attention_mask=attention_mask, |
| inputs_embeds=inputs_embeds, |
| head_mask=head_mask, |
| output_attentions=output_attentions, |
| output_hidden_states=output_hidden_states, |
| return_dict=return_dict, |
| ) |
|
|
| sequence_output = encoder_outputs[0] |
|
|
| logits = self.classifier(sequence_output) |
|
|
| loss = None |
| if labels is not None: |
| labels = labels.to(logits.device) |
| if self.config.problem_type is None: |
| if self.config.num_labels == 1: |
| self.config.problem_type = "regression" |
| elif self.config.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): |
| self.config.problem_type = "single_label_classification" |
| else: |
| self.config.problem_type = "multi_label_classification" |
|
|
| if self.config.problem_type == "regression": |
| loss_fct = MSELoss() |
| if self.config.num_labels == 1: |
| loss = loss_fct(logits.squeeze(), labels.squeeze()) |
| else: |
| loss = loss_fct(logits, labels) |
| elif self.config.problem_type == "single_label_classification": |
| loss_fct = CrossEntropyLoss() |
| loss = loss_fct(logits.view(-1, self.config.num_labels), labels.view(-1)) |
| elif self.config.problem_type == "multi_label_classification": |
| loss_fct = BCEWithLogitsLoss() |
| loss = loss_fct(logits, labels) |
|
|
| return TokenClassifierOutput( |
| loss=loss, |
| logits=logits, |
| ) |