-
Notifications
You must be signed in to change notification settings - Fork 10
/
crf.py
200 lines (164 loc) · 7.24 KB
/
crf.py
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
__author__ = 'max'
import math
import numpy as np
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch.nn.parameter import Parameter
def logsumexp(x, dim=None):
"""
Args:
x: A pytorch tensor (any dimension will do)
dim: int or None, over which to perform the summation. `None`, the
default, performs over all axes.
Returns: The result of the log(sum(exp(...))) operation.
"""
if dim is None:
xmax = x.max()
xmax_ = x.max()
return xmax_ + torch.log(torch.exp(x - xmax).sum())
else:
xmax, _ = x.max(dim, keepdim=True)
xmax_, _ = x.max(dim)
return xmax_ + torch.log(torch.exp(x - xmax).sum(dim))
class ChainCRF(nn.Module):
def __init__(self, input_size, num_labels, bigram=True, **kwargs):
'''
Args:
input_size: int
the dimension of the input.
num_labels: int
the number of labels of the crf layer
bigram: bool
if apply bi-gram parameter.
**kwargs:
'''
super(ChainCRF, self).__init__()
self.input_size = input_size
self.num_labels = num_labels + 1
self.pad_label_id = num_labels
self.bigram = bigram
# state weight tensor
self.state_nn = nn.Linear(input_size, self.num_labels)
if bigram:
# transition weight tensor
self.trans_nn = nn.Linear(input_size, self.num_labels * self.num_labels)
self.register_parameter('trans_matrix', None)
else:
self.trans_nn = None
self.trans_matrix = Parameter(torch.Tensor(self.num_labels, self.num_labels))
self.reset_parameters()
def reset_parameters(self):
nn.init.constant(self.state_nn.bias, 0.)
if self.bigram:
nn.init.xavier_uniform_(self.trans_nn.weight)
nn.init.constant_(self.trans_nn.bias, 0.)
else:
nn.init.normal_(self.trans_matrix)
# if not self.bigram:
# nn.init.normal(self.trans_matrix)
def forward(self, input, mask=None):
'''
Args:
input: Tensor
the input tensor with shape = [batch, length, input_size]
mask: Tensor or None
the mask tensor with shape = [batch, length]
Returns: Tensor
the energy tensor with shape = [batch, length, num_label, num_label]
'''
batch, length, _ = input.size()
# compute out_s by tensor dot [batch, length, input_size] * [input_size, num_label]
# thus out_s should be [batch, length, num_label] --> [batch, length, num_label, 1]
out_s = self.state_nn(input).unsqueeze(2)
if self.bigram:
# compute out_s by tensor dot: [batch, length, input_size] * [input_size, num_label * num_label]
# the output should be [batch, length, num_label, num_label]
out_t = self.trans_nn(input).view(batch, length, self.num_labels, self.num_labels)
output = out_t + out_s
else:
# [batch, length, num_label, num_label]
output = self.trans_matrix + out_s
if mask is not None:
output = output * mask.unsqueeze(2).unsqueeze(3)
return output
def loss(self, input, target, mask=None):
'''
Args:
input: Tensor
the input tensor with shape = [batch, length, input_size]
target: Tensor
the tensor of target labels with shape [batch, length]
mask:Tensor or None
the mask tensor with shape = [batch, length]
Returns: Tensor
A 1D tensor for minus log likelihood loss
'''
batch, length, _ = input.size()
energy = self.forward(input, mask=mask)
# shape = [length, batch, num_label, num_label]
energy_transpose = energy.transpose(0, 1)
# shape = [length, batch]
target_transpose = target.transpose(0, 1)
# shape = [length, batch, 1]
mask_transpose = None
if mask is not None:
mask_transpose = mask.unsqueeze(2).transpose(0, 1)
# shape = [batch, num_label]
partition = None
# shape = [batch]
batch_index = torch.arange(0, batch).long().cuda()
prev_label = torch.cuda.LongTensor(batch).fill_(self.num_labels - 1)
tgt_energy = Variable(torch.zeros(batch)).cuda()
for t in range(length):
# shape = [batch, num_label, num_label]
curr_energy = energy_transpose[t]
if t == 0:
partition = curr_energy[:, -1, :]
else:
# shape = [batch, num_label]
partition_new = logsumexp(curr_energy + partition.unsqueeze(2), dim=1)
if mask_transpose is None:
partition = partition_new
else:
mask_t = mask_transpose[t]
partition = partition + (partition_new - partition) * mask_t
tgt_energy += curr_energy[batch_index, prev_label, target_transpose[t].data]
prev_label = target_transpose[t].data
return logsumexp(partition, dim=1) - tgt_energy
def decode(self, input, mask=None, leading_symbolic=0):
"""
Args:
input: Tensor
the input tensor with shape = [batch, length, input_size]
mask: Tensor or None
the mask tensor with shape = [batch, length]
leading_symbolic: nt
number of symbolic labels leading in type alphabets (set it to 0 if you are not sure)
Returns: Tensor
decoding results in shape [batch, length]
"""
energy = self.forward(input, mask=mask).data
# Input should be provided as (n_batch, n_time_steps, num_labels, num_labels)
# For convenience, we need to dimshuffle to (n_time_steps, n_batch, num_labels, num_labels)
energy_transpose = energy.transpose(0, 1)
# the last row and column is the tag for pad symbol. reduce these two dimensions by 1 to remove that.
# also remove the first #symbolic rows and columns.
# now the shape of energies_shuffled is [n_time_steps, b_batch, t, t] where t = num_labels - #symbolic - 1.
energy_transpose = energy_transpose[:, :, leading_symbolic:-1, leading_symbolic:-1]
length, batch_size, num_label, _ = energy_transpose.size()
batch_index = torch.arange(0, batch_size).long().cuda()
pi = torch.zeros([length, batch_size, num_label, 1]).cuda()
pointer = torch.cuda.LongTensor(length, batch_size, num_label).zero_()
back_pointer = torch.cuda.LongTensor(length, batch_size).zero_()
pi[0] = energy[:, 0, -1, leading_symbolic:-1].unsqueeze(2)
pointer[0] = -1
for t in range(1, length):
pi_prev = pi[t - 1]
pi_t, pointer[t] = torch.max(energy_transpose[t] + pi_prev, dim=1)
pi[t] = pi_t.unsqueeze(2)
_, back_pointer[-1] = torch.max(pi[-1].squeeze(2), dim=1)
for t in reversed(range(length - 1)):
pointer_last = pointer[t + 1]
back_pointer[t] = pointer_last[batch_index, back_pointer[t + 1]]
return back_pointer.transpose(0, 1) + leading_symbolic