File size: 7,968 Bytes
5432d4d
44e3dc5
5432d4d
 
 
 
 
 
 
 
 
 
 
 
 
44e3dc5
 
 
 
 
 
5432d4d
44e3dc5
 
 
 
5432d4d
44e3dc5
 
 
 
 
 
 
5432d4d
44e3dc5
 
 
 
5432d4d
 
44e3dc5
5432d4d
44e3dc5
 
 
 
 
5432d4d
44e3dc5
 
 
 
 
 
 
5432d4d
 
 
 
 
44e3dc5
 
 
 
 
 
5432d4d
 
44e3dc5
5432d4d
44e3dc5
 
 
 
 
 
 
5432d4d
 
 
44e3dc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5432d4d
 
 
 
 
 
 
44e3dc5
 
 
 
 
 
5432d4d
 
44e3dc5
 
5432d4d
44e3dc5
 
 
 
 
 
 
 
 
 
 
 
 
5432d4d
44e3dc5
 
 
5432d4d
44e3dc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5432d4d
 
 
44e3dc5
 
 
 
 
 
 
 
5432d4d
44e3dc5
 
 
5432d4d
44e3dc5
5432d4d
 
 
 
44e3dc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5432d4d
44e3dc5
5432d4d
 
44e3dc5
5432d4d
44e3dc5
 
 
 
 
 
 
 
5432d4d
44e3dc5
 
 
 
 
 
5432d4d
44e3dc5
5432d4d
44e3dc5
 
 
 
 
 
 
 
 
 
 
5432d4d
44e3dc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5432d4d
44e3dc5
 
 
 
 
 
 
 
 
 
 
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
from typing import List

import torch
import torch.nn as nn


class Vocab:
    def __init__(self, char2idx=None, idx2char=None):
        if char2idx is None:
            char2idx = {
                "<pad>": 0,
                "<sos>": 1,
                "<eos>": 2,
                "<unk>": 3,
            }

        self.char2idx = {
            char: int(index)
            for char, index in char2idx.items()
        }

        if idx2char is None:
            self.idx2char = {
                index: char
                for char, index in self.char2idx.items()
            }
        else:
            self.idx2char = {
                int(index): char
                for index, char in idx2char.items()
            }

    def encode(self, text: str) -> List[int]:
        unk_id = self.char2idx["<unk>"]

        return [
            self.char2idx.get(character, unk_id)
            for character in text
        ]

    def decode(self, ids: List[int]) -> str:
        characters = []
        eos_id = self.char2idx["<eos>"]

        for index in ids:
            index = int(index)

            if index == eos_id:
                break

            if index > eos_id:
                characters.append(
                    self.idx2char.get(index, "")
                )

        return "".join(characters)


class LemmaModel(nn.Module):
    def __init__(
        self,
        vocab_size,
        char_emb_dim=96,
        hidden_size=128,
        drop_prob=0.30,
        num_heads=16,
        max_gen_len=30,
    ):
        super().__init__()

        self.max_gen_len = max_gen_len

        self.emb = nn.Embedding(
            vocab_size,
            char_emb_dim,
            padding_idx=0,
        )

        self.dropout_enc = nn.Dropout(drop_prob)
        self.dropout_dec = nn.Dropout(drop_prob)
        self.dropout_att = nn.Dropout(drop_prob)

        self.enc1 = nn.LSTM(
            char_emb_dim,
            hidden_size,
            bidirectional=True,
            batch_first=True,
        )

        self.enc2 = nn.LSTM(
            hidden_size * 2,
            hidden_size,
            bidirectional=True,
            batch_first=True,
        )

        self.attn = nn.MultiheadAttention(
            hidden_size * 2,
            num_heads,
            batch_first=True,
        )

        self.dec = nn.LSTM(
            char_emb_dim + hidden_size * 4,
            hidden_size * 2,
            batch_first=True,
        )

        self.dec_cross_attn = nn.MultiheadAttention(
            embed_dim=hidden_size * 2,
            num_heads=num_heads,
            kdim=hidden_size * 4,
            vdim=hidden_size * 4,
            batch_first=True,
        )

        self.out = nn.Linear(
            hidden_size * 2,
            vocab_size,
            bias=True,
        )

    def encode(self, src, src_lens):
        embedded = self.emb(src)

        packed1 = nn.utils.rnn.pack_padded_sequence(
            embedded,
            src_lens.cpu(),
            batch_first=True,
            enforce_sorted=False,
        )

        enc1_output, _ = self.enc1(packed1)

        enc1_output, _ = (
            nn.utils.rnn.pad_packed_sequence(
                enc1_output,
                batch_first=True,
            )
        )

        enc1_output = self.dropout_enc(enc1_output)

        packed2 = nn.utils.rnn.pack_padded_sequence(
            enc1_output,
            src_lens.cpu(),
            batch_first=True,
            enforce_sorted=False,
        )

        enc2_output, _ = self.enc2(packed2)

        enc2_output, _ = (
            nn.utils.rnn.pad_packed_sequence(
                enc2_output,
                batch_first=True,
            )
        )

        enc2_output = self.dropout_enc(enc2_output)

        attention_output, _ = self.attn(
            enc1_output,
            enc2_output,
            enc2_output,
        )

        attention_output = self.dropout_att(
            attention_output
        )

        return torch.cat(
            [enc2_output, attention_output],
            dim=-1,
        )

    def forward(self, src, src_lens, tgt):
        encoder_combined = self.encode(
            src,
            src_lens,
        )

        decoder_target = self.emb(tgt[:, :-1])
        target_len = decoder_target.size(1)

        if encoder_combined.size(1) >= target_len:
            combined_trimmed = encoder_combined[
                :, :target_len, :
            ]
        else:
            padding = encoder_combined.new_zeros(
                encoder_combined.size(0),
                target_len - encoder_combined.size(1),
                encoder_combined.size(2),
            )

            combined_trimmed = torch.cat(
                [encoder_combined, padding],
                dim=1,
            )

        decoder_input = torch.cat(
            [decoder_target, combined_trimmed],
            dim=-1,
        )

        decoder_output, _ = self.dec(decoder_input)
        decoder_output = self.dropout_dec(decoder_output)

        cross_output, _ = self.dec_cross_attn(
            decoder_output,
            encoder_combined,
            encoder_combined,
        )

        cross_output = self.dropout_att(cross_output)

        return self.out(cross_output)

    def generate(
        self,
        src,
        src_lens,
        vocab,
        max_len=None,
    ):
        self.eval()

        if max_len is None:
            max_len = self.max_gen_len

        batch_size = src.size(0)
        eos_id = vocab.char2idx["<eos>"]

        with torch.inference_mode():
            encoder_combined = self.encode(
                src,
                src_lens,
            )

            source_len = encoder_combined.size(1)

            current = torch.full(
                (batch_size, 1),
                vocab.char2idx["<sos>"],
                device=src.device,
                dtype=torch.long,
            )

            hidden = None
            hypotheses = [
                []
                for _ in range(batch_size)
            ]

            finished = torch.zeros(
                batch_size,
                dtype=torch.bool,
                device=src.device,
            )

            for step in range(max_len):
                embedded = self.emb(current).squeeze(1)

                combined_step = encoder_combined[
                    :,
                    min(step, source_len - 1),
                    :,
                ]

                decoder_input = torch.cat(
                    [embedded, combined_step],
                    dim=-1,
                ).unsqueeze(1)

                decoder_output, hidden = self.dec(
                    decoder_input,
                    hidden,
                )

                decoder_output = self.dropout_dec(
                    decoder_output
                )

                cross_output, _ = self.dec_cross_attn(
                    decoder_output,
                    encoder_combined,
                    encoder_combined,
                )

                cross_output = self.dropout_att(
                    cross_output
                )

                logits = self.out(cross_output)
                next_ids = logits.argmax(dim=-1)
                current = next_ids

                for index in range(batch_size):
                    if not finished[index]:
                        token_id = int(
                            next_ids[index, 0].item()
                        )

                        hypotheses[index].append(token_id)

                        if token_id == eos_id:
                            finished[index] = True

                if finished.all():
                    break

                current = torch.where(
                    finished.unsqueeze(1),
                    torch.full_like(current, eos_id),
                    current,
                )

        return [
            vocab.decode(hypothesis)
            for hypothesis in hypotheses
        ]