|
1 |
| -from typing import List, Tuple |
| 1 | +from typing import Dict, List, Tuple, cast |
2 | 2 |
|
3 | 3 | import spacy_alignments as tokenizations
|
4 |
| -from partial_tagger.decoders.viterbi import Constrainer, ViterbiDecoder |
| 4 | +import torch |
| 5 | +from partial_tagger.crf import functional as F |
| 6 | +from partial_tagger.crf.nn import CRF |
| 7 | +from partial_tagger.encoders.base import BaseEncoder |
5 | 8 | from partial_tagger.encoders.transformer import TransformerModelEncoderFactory
|
6 |
| -from partial_tagger.tagger import SequenceTagger |
7 | 9 | from sequence_label import LabelSet
|
| 10 | +from torch import nn |
8 | 11 | from transformers import PreTrainedTokenizer
|
9 | 12 |
|
10 | 13 |
|
| 14 | +class SequenceTagger(nn.Module): |
| 15 | + def __init__( |
| 16 | + self, |
| 17 | + encoder: BaseEncoder, |
| 18 | + padding_index: int, |
| 19 | + start_states: Tuple[bool, ...], |
| 20 | + end_states: Tuple[bool, ...], |
| 21 | + transitions: Tuple[Tuple[bool, ...], ...], |
| 22 | + ): |
| 23 | + super().__init__() |
| 24 | + |
| 25 | + self.encoder = encoder |
| 26 | + self.crf = CRF(encoder.get_hidden_size()) |
| 27 | + self.start_constraints = nn.Parameter( |
| 28 | + torch.tensor(start_states), requires_grad=False |
| 29 | + ) |
| 30 | + self.end_constraints = nn.Parameter( |
| 31 | + torch.tensor(end_states), requires_grad=False |
| 32 | + ) |
| 33 | + self.transition_constraints = nn.Parameter( |
| 34 | + torch.tensor(transitions), requires_grad=False |
| 35 | + ) |
| 36 | + self.padding_index = padding_index |
| 37 | + |
| 38 | + def __constrain( |
| 39 | + self, log_potentials: torch.Tensor, mask: torch.Tensor |
| 40 | + ) -> torch.Tensor: |
| 41 | + return F.constrain_log_potentials( |
| 42 | + log_potentials, |
| 43 | + mask, |
| 44 | + self.start_constraints, |
| 45 | + self.end_constraints, |
| 46 | + self.transition_constraints, |
| 47 | + ) |
| 48 | + |
| 49 | + def forward( |
| 50 | + self, inputs: Dict[str, torch.Tensor], mask: torch.Tensor |
| 51 | + ) -> Tuple[torch.Tensor, torch.Tensor]: |
| 52 | + log_potentials = self.crf(self.encoder(inputs), mask) |
| 53 | + |
| 54 | + contrained = self.__constrain(log_potentials, mask) |
| 55 | + |
| 56 | + contrained.requires_grad_() |
| 57 | + |
| 58 | + with torch.enable_grad(): |
| 59 | + _, tag_indices = F.decode(contrained) |
| 60 | + |
| 61 | + return log_potentials, tag_indices * mask + self.padding_index * (~mask) |
| 62 | + |
| 63 | + def predict( |
| 64 | + self, inputs: Dict[str, torch.Tensor], mask: torch.Tensor |
| 65 | + ) -> torch.Tensor: |
| 66 | + return cast(torch.Tensor, self(inputs, mask)[1]) |
| 67 | + |
| 68 | + |
11 | 69 | def create_tagger(
|
12 | 70 | model_name: str, label_set: LabelSet, padding_index: int
|
13 | 71 | ) -> SequenceTagger:
|
14 | 72 | return SequenceTagger(
|
15 | 73 | TransformerModelEncoderFactory(model_name).create(label_set),
|
16 |
| - ViterbiDecoder( |
17 |
| - padding_index, |
18 |
| - Constrainer( |
19 |
| - label_set.start_states, |
20 |
| - label_set.end_states, |
21 |
| - label_set.transitions, |
22 |
| - ), |
23 |
| - ), |
| 74 | + padding_index, |
| 75 | + label_set.start_states, |
| 76 | + label_set.end_states, |
| 77 | + label_set.transitions, |
24 | 78 | )
|
25 | 79 |
|
26 | 80 |
|
|
0 commit comments