File size: 8,946 Bytes
97c46f0
 
 
 
 
 
 
7fe102c
 
 
301bdd5
7fe102c
 
46f1d8c
7fe102c
 
 
 
 
46f1d8c
 
7fe102c
 
 
 
46f1d8c
 
 
 
 
 
7fe102c
 
 
 
46f1d8c
97c46f0
46f1d8c
 
7fe102c
46f1d8c
 
 
 
97c46f0
46f1d8c
 
 
 
97c46f0
 
46f1d8c
 
97c46f0
7fe102c
46f1d8c
7fe102c
46f1d8c
 
 
97c46f0
7fe102c
46f1d8c
 
 
 
 
 
 
 
 
 
 
 
 
 
7fe102c
46f1d8c
 
7fe102c
46f1d8c
7fe102c
 
 
 
 
46f1d8c
 
 
 
 
 
 
 
 
 
 
 
 
7fe102c
cd708ec
 
7fe102c
 
 
 
 
97c46f0
46f1d8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97c46f0
 
7fe102c
67a2f9a
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
from __future__ import annotations
import torch
from torchtyping import TensorType
from .fasttext_jp_embedding import FastTextJpModel, FastTextJpConfig
from transformers.modeling_outputs import SequenceClassifierOutput


class FastTextForSeuqenceClassificationConfig(FastTextJpConfig):
    """FastTextJpModelのConfig
    """
    model_type = "fasttext_classification"

    def __init__(self,
                 ngram: int | list[int] = 2,
                 tokenizer_class="FastTextJpTokenizer",
                 **kwargs):
        """初期化処理

        Args:
            ngram (int | list[int], optional):
                文章を分割する際のNgram。
            tokenizer_class (str, optional): 
                tokenizer_classを指定しないと、pipelineから読み込まれません。
                config.jsonに記載されます。
        """
        if isinstance(ngram, int):
            self.ngrams = [ngram]
        elif isinstance(ngram, list):
            self.ngrams = ngram
        else:
            raise TypeError(f"got unknown type {type(ngram)}")
        kwargs["tokenizer_class"] = tokenizer_class
        super().__init__(**kwargs)


class NgramForSeuqenceClassification():

    def __init__(self):
        ...

    def __call__(self, sentence: TensorType["A", "vectors"],
                 candidate_label: TensorType["B", "vectors"],
                 ngram: int) -> TensorType[3]:
        """Ngramで文章を分けてコサイン類似度を算出する。

        Args:
            sentence (TensorType["A", "vectors"]): 文章ベクトル
            candidate_label (TensorType["B", "vectors"]): ラベルベクトル
            ngram (int): Ngram

        Returns:
            TensorType[3]:
                文章の類似度。[Entailment, Neutral, Contradiction]
        """

        sentence_ngrams = self.split_ngram(sentence, ngram)

        candidate_label_mean = torch.mean(candidate_label, dim=0, keepdim=True)
        p = self.cosine_similarity(sentence_ngrams, candidate_label_mean)
        return torch.tensor([torch.log(p), -torch.inf, torch.log(1 - p)])

    def cosine_similarity(
            self, sentence_ngrams: TensorType["ngrams", "vectors"],
            candidate_label_mean: TensorType[1, "vectors"]) -> TensorType[1]:
        """コサイン類似度を計算する。

        Args:
            sentence_ngrams (TensorType["ngrams", "vectors"]):
                Ngram化された文章ベクトル
            candidate_label_mean (TensorType[1, "vectors"]): 
                ラベルベクトル

        Returns:
            TensorType[1]: _description_
        """

        res = torch.tensor(0.)
        for i in range(len(sentence_ngrams)):
            sw = sentence_ngrams[i]
            p = torch.nn.functional.cosine_similarity(sw,
                                                      candidate_label_mean[0],
                                                      dim=0)
            if p > res:
                res = p
        return res

    def split_ngram(self, sentences: TensorType["A", "vectors"],
                    n: int) -> TensorType["ngrams", "vectors"]:
        """AとBの関連度を計算します。
        Args:
            sentences(TensorType["A", "vectors"]): 
                対象の文章
            n(int):
                ngram
        Returns:
            TensorType["ngrams", "vectors"]:
                Ngram化された文章
        """

        res = []
        if len(sentences) <= n:
            return torch.stack([torch.mean(sentences, dim=0, keepdim=False)])
        for i in range(len(sentences) - n + 1):
            ngram = sentences[i:i + n]
            res.append(torch.mean(ngram, dim=0, keepdim=False))
        return torch.stack(res)


class NgramsForSeuqenceClassification():

    def __init__(self, config: FastTextForSeuqenceClassificationConfig):
        self.max_ngrams = config.ngrams
        self.ngram_layer = NgramForSeuqenceClassification()

    def __call__(self, sentence: TensorType["A", "vectors"],
                 candidate_label: TensorType["B", "vectors"]) -> TensorType[3]:
        """AとBの関連度を計算します。
        Args:
            sentence(TensorType["A", "vectors"]): 
                対象の文章
            candidate_label(TensorType["B", "vectors"]):
                ラベルの文章

        Returns:
            TensorType[3]:
                文章の類似度。[Entailment, Neutral, Contradiction]
        """

        res = [-torch.inf, -torch.inf, -torch.inf]
        for ngram in self.max_ngrams:
            logit = self.ngram_layer(sentence, candidate_label, ngram)
            if logit[0] > res[0]:
                res = logit
        return torch.tensor(res)


class BatchedNgramsForSeuqenceClassification():

    def __init__(self, config: FastTextForSeuqenceClassificationConfig):
        self.ngrams_layer = NgramsForSeuqenceClassification(config)

    def __call__(
        self,
        last_hidden_state: TensorType["batch", "A+B", "vectors"],
        token_type_ids: TensorType["batch", "A+B"],
        attention_mask: TensorType["batch", "A+B"],
    ) -> TensorType["batch", 3]:
        """AとBの関連度を計算します。
        Args:
            last_hidden_state(TensorType["batch", "A+B", "vectors"]): 
                embeddingsの値。
            token_type_ids(TensorType["A+B"]):
                文章のid。0か1で、Bの場合1。
            attention_mask(TensorType["A+B"]):
                padを識別する。0か1で、padの場合1。

        Returns:
            TensorType["batch", 3]:
                文章の類似度。[Entailment, Neutral, Contradiction]
        """

        logits = []
        embeddings = last_hidden_state
        for idx in range(len(embeddings)):
            vec = embeddings[idx]
            # token_type_ids == 0が文章、1がラベルです。
            token_type_ids = token_type_ids[idx]
            # attention_mask == 1がパディングでないもの
            attention_mask = attention_mask[idx]

            sentence, candidate_label = self.split_sentence(
                vec, token_type_ids, attention_mask)
            logit = self.ngrams_layer(sentence, candidate_label)
            logits.append(logit)
        logits = torch.tensor(logits)
        return logits

    def split_sentence(
        self, vec: TensorType["A+B", "vectors"],
        token_type_ids: TensorType["A+B"], attention_mask: TensorType["A+B"]
    ) -> tuple[TensorType["A", "vectors"], TensorType["B", "vectors"]]:
        """CrossEncoderになっているので、文章を分割します。
        
        Args:
            vec(TensorType["A+B","vectors"]):
                単語ベクトル

            token_type_ids(TensorType["A+B"]):
                文章のid。0か1で、Bの場合1。

            attention_mask(TensorType["A+B"]):
                padを識別する。0か1で、padの場合1。

        Returns:
            tuple[TensorType["A", "vectors"], TensorType["B", "vectors"]]:
                AとBの文章を分割して返します。
        """

        sentence = vec[torch.logical_and(token_type_ids == 0,
                                         attention_mask == 1)]
        candidate_label = vec[torch.logical_and(token_type_ids == 1,
                                                attention_mask == 1)]
        return sentence, candidate_label


class FastTextForSeuqenceClassification(FastTextJpModel):
    """FastTextのベクトルをベースとした分類を行います。
    """

    def __init__(self, config: FastTextForSeuqenceClassificationConfig):

        self.layer = BatchedNgramsForSeuqenceClassification(config)
        super().__init__(config)

    def forward(
        self,
        input_ids: TensorType["batch", "A+B", "vecotors"] = None,
        attention_mask: TensorType["batch", "A+B"] = None,
        token_type_ids: TensorType["batch", "A+B"] = None
    ) -> SequenceClassifierOutput:
        """候補となるラベルから分類を行います。

        Returns:
            SequenceClassifierOutput: 候補が正解している確率
        """
        outputs = self.word_embeddings(input_ids)
        logits = self.layer(last_hidden_state=outputs,
                            attention_mask=attention_mask,
                            token_type_ids=token_type_ids)

        return SequenceClassifierOutput(
            loss=None,
            logits=logits,
            hidden_states=None,
            attentions=None,
        )


# AutoModelに登録が必要だが、いろいろやり方が変わっているようで定まっていない。(2022/11/6)
# https://huggingface.co/docs/transformers/custom_models#sending-the-code-to-the-hub
FastTextForSeuqenceClassificationConfig.register_for_auto_class()
FastTextForSeuqenceClassification.register_for_auto_class(
    "AutoModelForSequenceClassification")