File size: 3,572 Bytes
10e12d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from transformers import XLMRobertaForSequenceClassification, XLMRobertaConfig
from torch.nn import MSELoss, CrossEntropyLoss, BCEWithLogitsLoss
from typing import Optional, Union, Tuple
from transformers.modeling_outputs import SequenceClassifierOutput
import torch
from torch.nn import Linear

class CustomXLMRobertaModelForSequenceClassification(XLMRobertaForSequenceClassification):
    config_class = XLMRobertaConfig

    def __init__(self, config):
        super().__init__(config)

        self.final_classifier = Linear(config.hidden_size, config.num_labels)
        self.init_weights()

    def forward(

            self,

            input_ids: Optional[torch.LongTensor] = None,

            attention_mask: Optional[torch.FloatTensor] = None,

            token_type_ids: Optional[torch.LongTensor] = None,

            position_ids: Optional[torch.LongTensor] = None,

            head_mask: Optional[torch.FloatTensor] = None,

            inputs_embeds: Optional[torch.FloatTensor] = None,

            labels: Optional[torch.LongTensor] = None,

            output_attentions: Optional[bool] = None,

            output_hidden_states: Optional[bool] = None,

            return_dict: Optional[bool] = None,

    ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]:

        return_dict = return_dict if return_dict is not None else self.config.use_return_dict
        outputs_sentence = self.roberta(input_ids,
                                        attention_mask=attention_mask,
                                        token_type_ids=token_type_ids,
                                        position_ids=position_ids,
                                        head_mask=head_mask,
                                        inputs_embeds=inputs_embeds,
                                        output_attentions=output_attentions,
                                        output_hidden_states=output_hidden_states,
                                        return_dict=True)

        sequence_output_sentence = outputs_sentence["last_hidden_state"][:, 0, :]

        logits = self.final_classifier(sequence_output_sentence)

        loss = None
        if labels is not None:
            if self.config.problem_type is None:
                if self.num_labels == 1:
                    self.config.problem_type = "regression"
                elif self.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.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.num_labels), labels.view(-1))
            elif self.config.problem_type == "multi_label_classification":
                loss_fct = BCEWithLogitsLoss()
                loss = loss_fct(logits, labels)

        if not return_dict:
            output = (logits,)
            return ((loss,) + output) if loss is not None else output

        return SequenceClassifierOutput(
            loss=loss,
            logits=logits
        )