-
Notifications
You must be signed in to change notification settings - Fork 1
/
boundary_chain_classifier.py
101 lines (76 loc) · 2.35 KB
/
boundary_chain_classifier.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
import torch
from torch import nn
from torch.autograd import Variable
from view import *
from holder import *
from util import *
from join_table import *
from locked_dropout import *
# the boundary chain classifier used in BIDAF+Elmo
class BoundaryChainClassifier(torch.nn.Module):
def __init__(self, opt, shared):
super(BoundaryChainClassifier, self).__init__()
self.opt = opt
self.shared = shared
self.linear1 = nn.Sequential(
nn.Dropout(opt.dropout),
nn.Linear(opt.hidden_size*2, 1))
self.linear2 = nn.Sequential(
nn.Dropout(opt.dropout),
nn.Linear(opt.hidden_size*2, 1))
self.drop = LockedDropout(opt.dropout)
self.linear_view = View(1,1,1)
self.linear_unview = View(1,1)
bidir = opt.birnn == 1
rnn1_in_size = opt.hidden_size * 2
rnn2_in_size = opt.hidden_size * 4
rnn_hidden_size = opt.hidden_size*2 if not bidir else opt.hidden_size
self.rnn1 = build_rnn(
opt.rnn_type,
input_size=rnn1_in_size,
hidden_size=rnn_hidden_size,
num_layers=opt.cls_rnn_layer,
bias=True,
batch_first=True,
dropout=opt.dropout,
bidirectional=bidir)
self.rnn2 = build_rnn(
opt.rnn_type,
input_size=rnn2_in_size,
hidden_size=rnn_hidden_size,
num_layers=opt.cls_rnn_layer,
bias=True,
batch_first=True,
dropout=opt.dropout,
bidirectional=bidir)
self.logsoftmax = nn.LogSoftmax(1)
self.phi_joiner = JoinTable(2)
def rnn_over(self, rnn, x):
# no dropout here !
M, _ = rnn(x)
return M
# G: output of biattention of shape (batch_l, context_l, enc_size1)
# M: output of match_encoder of shape (batch_l, context_l, enc_size2)
def forward(self, M, G):
self.update_context()
M = self.drop(M)
M1 = M
phi1 = self.rnn_over(self.rnn1, M1).contiguous()
y_scores1 = self.linear_unview(self.linear1(self.linear_view(phi1)))
M2 = self.phi_joiner([phi1, M1])
phi2 = self.rnn_over(self.rnn2, M2).contiguous()
y_scores2 = self.linear_unview(self.linear2(self.linear_view(phi2)))
# log probabilities
log_p1 = self.logsoftmax(y_scores1)
log_p2 = self.logsoftmax(y_scores2)
return log_p1, log_p2
def update_context(self):
batch_l = self.shared.batch_l
context_l = self.shared.context_l
hidden_size = self.opt.hidden_size
self.linear_view.dims = (batch_l * context_l, hidden_size*2)
self.linear_unview.dims = (batch_l, context_l)
def begin_pass(self):
pass
def end_pass(self):
pass