-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
283 lines (206 loc) · 11 KB
/
util.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
'''
Adapted from https://github.com/llamazing/numnet_plus
'''
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from tools import allennlp as util
def gelu(x):
return x * 0.5 * (1.0 + torch.erf(x / math.sqrt(2.0)))
def swish(x):
return x * torch.sigmoid(x)
class ResidualGRU(nn.Module):
def __init__(self, hidden_size, dropout=0.1, num_layers=2):
super(ResidualGRU, self).__init__()
self.enc_layer = nn.GRU(input_size=hidden_size, hidden_size=hidden_size // 2, num_layers=num_layers,
batch_first=True, dropout=dropout, bidirectional=True)
self.enc_ln = nn.LayerNorm(hidden_size)
def forward(self, input):
output, _ = self.enc_layer(input)
return self.enc_ln(output + input)
class FFNLayer(nn.Module):
def __init__(self, input_dim, intermediate_dim, output_dim, dropout, layer_norm=True):
super(FFNLayer, self).__init__()
self.fc1 = nn.Linear(input_dim, intermediate_dim)
if layer_norm:
self.ln = nn.LayerNorm(intermediate_dim)
else:
self.ln = None
self.dropout_func = nn.Dropout(dropout)
self.fc2 = nn.Linear(intermediate_dim, output_dim)
def forward(self, input):
inter = self.fc1(self.dropout_func(input))
inter_act = gelu(inter)
if self.ln:
inter_act = self.ln(inter_act)
return self.fc2(inter_act)
class ArgumentGCN(nn.Module):
def __init__(self, node_dim, extra_factor_dim=0, iteration_steps=1):
super(ArgumentGCN, self).__init__()
self.node_dim = node_dim
self.iteration_steps = iteration_steps
self._node_weight_fc = torch.nn.Linear(node_dim + extra_factor_dim, 1, bias=True)
self._self_node_fc = torch.nn.Linear(node_dim, node_dim, bias=True)
self._node_fc_argument = torch.nn.Linear(node_dim, node_dim,
bias=False)
self._node_fc_punctuation = torch.nn.Linear(node_dim, node_dim,
bias=False)
def forward(self,
node,
node_mask,
argument_graph,
punctuation_graph,
extra_factor=None):
''' '''
'''
Current: 2 relation patterns.
- argument edge. (most of them are causal relations)
- punctuation edges. (including periods and commas)
'''
node_len = node.size(1)
diagmat = torch.diagflat(torch.ones(node.size(1), dtype=torch.long,
device=node.device))
diagmat = diagmat.unsqueeze(0).expand(node.size(0), -1, -1)
dd_graph = node_mask.unsqueeze(1) * node_mask.unsqueeze(-1) * (1 - diagmat)
graph_argument = dd_graph * argument_graph
graph_punctuation = dd_graph * punctuation_graph
node_neighbor_num = graph_argument.sum(-1) + graph_punctuation.sum(-1)
node_neighbor_num_mask = (node_neighbor_num >= 1).long()
node_neighbor_num = util.replace_masked_values(node_neighbor_num.float(), node_neighbor_num_mask, 1)
all_weight = []
for step in range(self.iteration_steps):
''' (1) Node Relatedness Measure '''
if extra_factor is None:
d_node_weight = torch.sigmoid(self._node_weight_fc(node)).squeeze(
-1)
else:
d_node_weight = torch.sigmoid(self._node_weight_fc(torch.cat((node, extra_factor), dim=-1))).squeeze(
-1)
all_weight.append(d_node_weight)
self_node_info = self._self_node_fc(node)
''' (2) Message Propagation (each relation type) '''
node_info_argument = self._node_fc_argument(node)
node_weight = util.replace_masked_values(
d_node_weight.unsqueeze(1).expand(-1, node_len, -1),
graph_argument,
0)
node_info_argument = torch.matmul(node_weight, node_info_argument)
node_info_punctuation = self._node_fc_punctuation(node)
node_weight = util.replace_masked_values(
d_node_weight.unsqueeze(1).expand(-1, node_len, -1),
graph_punctuation,
0)
node_info_punctuation = torch.matmul(node_weight, node_info_punctuation)
agg_node_info = (node_info_argument + node_info_punctuation) / node_neighbor_num.unsqueeze(-1)
''' (3) Node Representation Update '''
node = F.relu(self_node_info + agg_node_info)
all_weight = [weight.unsqueeze(1) for weight in all_weight]
all_weight = torch.cat(all_weight, dim=1)
return node, all_weight
class ArgumentGCN_wreverseedges_double(nn.Module):
def __init__(self, node_dim, extra_factor_dim=0, iteration_steps=1):
super(ArgumentGCN_wreverseedges_double, self).__init__()
self.node_dim = node_dim
self.iteration_steps = iteration_steps
self._node_weight_fc = torch.nn.Linear(node_dim + extra_factor_dim, 1, bias=True)
self._self_node_fc = torch.nn.Linear(node_dim, node_dim, bias=True)
self._node_fc_argument = torch.nn.Linear(node_dim, node_dim,
bias=False)
self._node_fc_punctuation = torch.nn.Linear(node_dim, node_dim,
bias=False)
self._node_fc_argument_prime = torch.nn.Linear(node_dim, node_dim,
bias=False)
self._node_fc_punctuation_prime = torch.nn.Linear(node_dim, node_dim,
bias=False)
self._node_fc_argument_2 = torch.nn.Linear(node_dim, node_dim,
bias=False)
self._node_fc_punctuation_2 = torch.nn.Linear(node_dim, node_dim,
bias=False)
self._node_fc_argument_prime_2 = torch.nn.Linear(node_dim, node_dim,
bias=False)
self._node_fc_punctuation_prime_2 = torch.nn.Linear(node_dim, node_dim,
bias=False)
def forward(self,
node,
node_mask,
argument_graph,
punctuation_graph,
extra_factor=None):
''' '''
'''
Current: 2 relation patterns & reversed directed edges.
- argument edge. (most of them are causal relations)
- punctuation edges. (including periods and commas)
Date: 20/11/2020
Change log:
* add double node_info.
'''
node_len = node.size(1)
diagmat = torch.diagflat(torch.ones(node.size(1), dtype=torch.long,
device=node.device))
diagmat = diagmat.unsqueeze(0).expand(node.size(0), -1, -1)
dd_graph = node_mask.unsqueeze(1) * node_mask.unsqueeze(-1) * (1 - diagmat)
graph_argument = dd_graph * argument_graph
graph_punctuation = dd_graph * punctuation_graph
''' The reverse directed edges. '''
graph_argument_re = dd_graph * graph_argument.permute(0, 2, 1)
graph_punctuation_re = dd_graph * graph_punctuation.permute(0, 2, 1)
node_neighbor_num = graph_argument.sum(-1) + graph_punctuation.sum(-1) + \
graph_argument_re.sum(-1) + graph_punctuation_re.sum(-1)
node_neighbor_num_mask = (node_neighbor_num >= 1).long()
node_neighbor_num = util.replace_masked_values(node_neighbor_num.float(), node_neighbor_num_mask, 1)
all_weight = []
for step in range(self.iteration_steps):
''' (1) Node Relatedness Measure '''
if extra_factor is None:
d_node_weight = torch.sigmoid(self._node_weight_fc(node)).squeeze(
-1)
else:
d_node_weight = torch.sigmoid(self._node_weight_fc(torch.cat((node, extra_factor), dim=-1))).squeeze(
-1)
all_weight.append(d_node_weight)
self_node_info = self._self_node_fc(node)
''' (2) Message Propagation (each relation type) '''
node_info_argument = self._node_fc_argument(node)
node_info_argument_2 = self._node_fc_argument_2(node)
node_weight = util.replace_masked_values(
d_node_weight.unsqueeze(1).expand(-1, node_len, -1),
graph_argument,
0)
node_info_argument = torch.matmul(node_weight, node_info_argument)
node_info_argument_2 = torch.matmul(node_weight, node_info_argument_2)
node_info_argument_prime = self._node_fc_argument_prime(node)
node_info_argument_prime_2 = self._node_fc_argument_prime_2(node)
node_weight = util.replace_masked_values(
d_node_weight.unsqueeze(1).expand(-1, node_len, -1),
graph_argument_re,
0)
node_info_argument_prime = torch.matmul(node_weight, node_info_argument_prime)
node_info_argument_prime_2 = torch.matmul(node_weight, node_info_argument_prime_2)
node_info_punctuation = self._node_fc_punctuation(node)
node_info_punctuation_2 = self._node_fc_punctuation_2(node)
node_weight = util.replace_masked_values(
d_node_weight.unsqueeze(1).expand(-1, node_len, -1),
graph_punctuation,
0)
node_info_punctuation = torch.matmul(node_weight, node_info_punctuation)
node_info_punctuation_2 = torch.matmul(node_weight, node_info_punctuation_2)
node_info_punctuation_prime = self._node_fc_punctuation_prime(node)
node_info_punctuation_prime_2 = self._node_fc_punctuation_prime_2(node)
node_weight = util.replace_masked_values(
d_node_weight.unsqueeze(1).expand(-1, node_len, -1),
graph_punctuation_re,
0)
node_info_punctuation_prime = torch.matmul(node_weight, node_info_punctuation_prime)
node_info_punctuation_prime_2 = torch.matmul(node_weight, node_info_punctuation_prime_2)
agg_node_info = (node_info_argument + node_info_punctuation +
node_info_argument_prime + node_info_punctuation_prime +
node_info_argument_2 + node_info_punctuation_2 +
node_info_argument_prime_2 + node_info_punctuation_prime_2
) / node_neighbor_num.unsqueeze(-1)
''' (3) Node Representation Update '''
node = F.relu(self_node_info + agg_node_info)
all_weight = [weight.unsqueeze(1) for weight in all_weight]
all_weight = torch.cat(all_weight, dim=1)
return node, all_weight