-
Notifications
You must be signed in to change notification settings - Fork 0
/
Modules.py
194 lines (158 loc) · 6.99 KB
/
Modules.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
import math
import torch
from torch.autograd import Variable
import torch.nn.functional as F
class CharEmbedding(torch.nn.Module):
def __init__(self, char_size, embed_dim, lstm_dim, lstm_layers):
super().__init__()
self.embedding_chars = torch.nn.Embedding(char_size, 100)
self.lstm = torch.nn.LSTM(100, int(150), 1,
batch_first=True, bidirectional=False, dropout=0.33)
self.attention = LinearAttention(int(150))
self.mlp = torch.nn.Linear(300, 100, bias=False)
def forward(self, forms, pack_sent):
# input: B x S x W
batch_size, max_words, max_chars = forms.size()
forms = forms.contiguous().view(batch_size * max_words, -1)
pack = pack_sent.contiguous().view(batch_size * max_words)
out = self.embedding_chars(forms)
embeds, (_, c) = self.lstm(out)
# embeds = embeds.contiguous().view(batch_size, max_words, max_chars, -1)
embeds = self.attention(embeds).squeeze(dim=2)
c = c[-1]
out = torch.cat([embeds, c], dim=1)
embed_mat = self.mlp(out).view(batch_size, max_words, -1)
# embeds, _ = torch.nn.utils.rnn.pad_packed_sequence(embeds, batch_first=True)
return embed_mat
class LinearAttention(torch.nn.Module):
def __init__(self, lstm_features):
super().__init__()
self.lstm_features = lstm_features
self.weight = torch.nn.Parameter(torch.rand(self.lstm_features, 1))
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.weight.size(0))
self.weight.data.uniform_(-stdv, stdv)
def forward(self, input1):
soft = F.softmax(input1 @ self.weight, dim=0)
return input1.transpose(1, 2) @ soft
out = input1.transpose(1, 2) @ soft
return out
class Biaffine(torch.nn.Module):
def __init__(self, in1_features, in2_features, batch_size):
super(Biaffine, self).__init__()
self.in1_features = in1_features
self.in2_features = in2_features
self.weight = torch.nn.Parameter(torch.rand(batch_size, in1_features, in2_features))
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.weight.size(0))
self.weight.data.uniform_(-stdv, stdv)
def forward(self, input1, input2):
is_cuda = next(self.parameters()).is_cuda
batch_size, len1, dim1 = input1.size()
ones = torch.ones(batch_size, len1, 1)
if is_cuda:
ones = ones.cuda()
input1 = torch.cat((input1, Variable(ones)), dim=2)
biaffine = input1 @ self.weight @ input2.transpose(1, 2)
return biaffine
def __repr__(self):
return self.__class__.__name__ + ' (' \
+ 'in1_features=' + str(self.in1_features) \
+ ', in2_features=' + str(self.in2_features) \
+ ', out_features=' + str(self.out_features) + ')'
class RowBiaffine(torch.nn.Module):
def __init__(self, in1_features, in2_features, dep_labels):
super().__init__()
self.in1_features = in1_features
self.in2_features = in2_features
self.dep_labels = dep_labels
self.weight = torch.nn.Parameter(torch.rand(dep_labels, in1_features, in2_features))
self.bias = torch.nn.Parameter(torch.rand(dep_labels))
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.weight.size(0))
self.weight.data.uniform_(-stdv, stdv)
self.bias.data.uniform_(-stdv, stdv)
def forward(self, input1, input2):
batch_size, sent_len, dim = input1.size()
'''
S = []
for batch in range(batch_size):
s_i = []
for word in range(sent_len):
h_head = input1[batch, word].view(1, -1)
h_dep = input2[batch, word]
s_i.append(h_head @ self.weight @ h_dep)
s_i = torch.stack(s_i)
S.append(s_i)
S = torch.stack(S)
return S.squeeze(3)
'''
return (input1 @ self.weight).transpose(1, 2) @ input2
def forward_(self, input1, input2):
batch_size, sent_len, dim = input1.size()
S = []
for batch in range(batch_size):
s_i = []
for word in range(sent_len):
h_head = input1[batch, word].view(1, -1)
h_dep = input2[batch, word]
s_i.append(h_head @ self.weight @ h_dep)
s_i = torch.stack(s_i)
S.append(s_i)
S = torch.stack(S)
return S.squeeze(3)
class ShorterBiaffine(torch.nn.Module):
def __init__(self, in_features):
super().__init__()
self.in_features = in_features,
self.weight = torch.nn.Parameter(torch.rand(in_features + 1, in_features, 1))
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.weight.size(0))
self.weight.data.uniform_(-stdv, stdv)
def forward(self, input1, input2):
is_cuda = next(self.parameters()).is_cuda
batch_size, len1, dim1 = input1.size()
ones = torch.ones(batch_size, len1, 1)
if is_cuda:
ones = ones.cuda()
input1 = torch.cat((input1, Variable(ones)), dim=2)
dim1 += 1
input1 = input1.contiguous().view(batch_size * len1, dim1)
W = self.weight.transpose(1, 2).contiguous().view(dim1, dim1 - 1)
affine = (input1 @ W).view(batch_size, len1, dim1 - 1)
biaffine = (affine @ input2.transpose(1, 2)).view(batch_size, len1, 1, len1).transpose(2, 3).squeeze(3)
return biaffine
class LongerBiaffine(torch.nn.Module):
def __init__(self, in1_features, in2_features, dep_labels):
super().__init__()
self.in1_features = in1_features
self.in2_features = in2_features
self.dep_labels = dep_labels
self.weight = torch.nn.Parameter(torch.rand(in1_features + 1, in2_features + 1, dep_labels))
self.bias = torch.nn.Parameter(torch.rand(dep_labels))
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.weight.size(0))
self.weight.data.uniform_(-stdv, stdv)
self.bias.data.uniform_(-stdv, stdv)
def forward(self, input1, input2):
is_cuda = next(self.parameters()).is_cuda
batch_size, len1, dim1 = input1.size()
batch_size, len2, dim2 = input2.size()
ones = torch.ones(batch_size, len1, 1)
if is_cuda:
ones = ones.cuda()
input1 = torch.cat((input1, Variable(ones)), dim=2)
input2 = torch.cat((input2, Variable(ones)), dim=2)
dim1 += 1
dim2 += 1
input1 = input1.view(batch_size * len1, dim1)
weight = self.weight.transpose(1, 2).contiguous().view(dim1, self.dep_labels * dim2)
affine = (input1 @ weight).view(batch_size, len1 * self.dep_labels, dim2)
biaffine = (affine @ input2.transpose(1, 2)).view(batch_size, len1, self.dep_labels, len2).transpose(2, 3)
biaffine += self.bias.expand_as(biaffine)
return biaffine