|
3 | 3 | import torch
|
4 | 4 | import torch.nn as nn
|
5 | 5 | from data_utils.constant import Constants
|
| 6 | +from model_utils import get_mask |
| 7 | + |
6 | 8 | import numpy as np
|
| 9 | +import logging |
| 10 | + |
| 11 | +logging.basicConfig(level=logging.INFO) |
7 | 12 |
|
8 | 13 | class CRF_Loss(nn.Module):
|
9 | 14 | def __init__(self, tagset_size, config):
|
@@ -66,7 +71,8 @@ def log_likelihood(self, emissions, tags):
|
66 | 71 | log_p_y_x = self.get_log_p_Y_X(emissions, mask, tags)
|
67 | 72 | return log_p_y_x - log_z
|
68 | 73 |
|
69 |
| - def viterbi_decode(self, emissions, mask): |
| 74 | + def viterbi_decode_batch(self, emissions, lengths): |
| 75 | + mask = get_mask(lengths, self.config) |
70 | 76 | seq_len = emissions.shape[1]
|
71 | 77 |
|
72 | 78 | log_prob = emissions[:, 0].clone()
|
@@ -133,10 +139,52 @@ def viterbi_decode(self, emissions, mask):
|
133 | 139 |
|
134 | 140 | return sentence_score, torch.flip(all_labels, [1])
|
135 | 141 |
|
| 142 | + def viterbi_decode(self, emissions, lengths): |
| 143 | + bsz = emissions.shape[0] |
| 144 | + all_path_indices = [] |
| 145 | + all_path_scores = [] |
| 146 | + |
| 147 | + for i in range(bsz): |
| 148 | + viterbi_path, viterbi_score = self.viterbi_decode_single(lengths[i], emissions[i]) |
| 149 | + all_path_indices.append(viterbi_path) |
| 150 | + all_path_scores.append(viterbi_score) |
| 151 | + |
| 152 | + return all_path_indices, all_path_scores |
| 153 | + |
| 154 | + def viterbi_decode_single(self, sequence_length, emission, top_k=1): |
| 155 | + num_tags = emission.shape[0] |
| 156 | + path_scores, path_indices= [], [] |
| 157 | + path_scores.append(emission[0, :].unsqueeze(0)) |
| 158 | + for timestep in range(1, sequence_length): |
| 159 | + summed_potentials = path_scores[timestep - 1].unsqueeze(-1) + self.transitions |
| 160 | + scores, paths = torch.topk(summed_potentials, k=top_k, dim=0) |
| 161 | + path_scores.append(emission[timestep, :] + scores.squeeze()) |
| 162 | + path_indices.append(paths.squeeze()) |
| 163 | + |
| 164 | + viterbi_score, best_paths = torch.topk(path_scores[-1], k=top_k, dim=0) |
| 165 | + viterbi_paths = [] |
| 166 | + for i in range(top_k): |
| 167 | + viterbi_path = [best_paths[i]] |
| 168 | + for backward_timestep in reversed(path_indices): |
| 169 | + viterbi_path.append(int(backward_timestep.view(-1)[viterbi_path[-1]])) |
| 170 | + viterbi_path.reverse() |
| 171 | + |
| 172 | + viterbi_path = [j % num_tags for j in viterbi_path] |
| 173 | + viterbi_paths.append(viterbi_path) |
| 174 | + ''' |
| 175 | + viterbi_path = [int(best_path.numpy())] |
| 176 | + for backward_timestep in reversed(path_indices): |
| 177 | + viterbi_path.append(int(backward_timestep[viterbi_path[-1]])) |
| 178 | + # Reverse the backward path. |
| 179 | + viterbi_path.reverse() |
| 180 | + ''' |
| 181 | + return viterbi_paths, viterbi_score |
| 182 | + |
| 183 | + |
136 | 184 | def structural_perceptron_loss(self, emissions, tags):
|
137 | 185 | mask = tags.ne(Constants.TAG_PAD_ID).float()
|
138 |
| - |
139 |
| - best_scores, pred = self.viterbi_decode(emissions, mask) |
| 186 | + sequence_lnegths = mask.sum(dim=1) |
| 187 | + best_scores, pred = self.viterbi_decode(emissions, sequence_lnegths) |
140 | 188 | log_p_y_x = self.get_log_p_Y_X(emissions, mask, tags)
|
141 | 189 |
|
142 | 190 | delta = torch.sum(tags.ne(pred).float()*mask, 1)
|
|
0 commit comments