-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrrgcn.py
262 lines (227 loc) · 11.9 KB
/
rrgcn.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
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
# from rgcn.layers import RGCNBlockLayer as RGCNLayer
from rgcn.layers import UnionRGCNLayer, RGCNBlockLayer
from model import BaseRGCN
from decoder import ConvTransE, ConvTransR
class RGCNCell(BaseRGCN):
def build_hidden_layer(self, idx):
act = F.rrelu
if idx:
self.num_basis = 0
print("activate function: {}".format(act))
if self.skip_connect:
sc = False if idx == 0 else True
else:
sc = False
if self.encoder_name == "uvrgcn":
return UnionRGCNLayer(self.h_dim, self.h_dim, self.num_rels, self.num_bases,
activation=act, dropout=self.dropout, self_loop=self.self_loop, skip_connect=sc, rel_emb=self.rel_emb)
else:
raise NotImplementedError
def forward(self, g, init_ent_emb, init_rel_emb, method=0):
if self.encoder_name == "uvrgcn":
if method == 0:
node_id = g.ndata['id'].squeeze()
g.ndata['h'] = init_ent_emb[node_id]
elif method == 1:
g.ndata['h'] = init_ent_emb
x, r = init_ent_emb, init_rel_emb
for i, layer in enumerate(self.layers):
layer(g, [], r[i])
return g.ndata.pop('h')
else:
if self.features is not None:
print("----------------Feature is not None, Attention ------------")
g.ndata['id'] = self.features
node_id = g.ndata['id'].squeeze()
g.ndata['h'] = init_ent_emb[node_id]
if self.skip_connect:
prev_h = []
for layer in self.layers:
prev_h = layer(g, prev_h)
else:
for layer in self.layers:
layer(g, [])
return g.ndata.pop('h')
class RecurrentRGCN(nn.Module):
def __init__(self, decoder, encoder, gnn, num_ents, num_rels, num_static_rels, num_words, h_dim, opn, sequence_len, num_bases=-1, num_basis=-1,
num_hidden_layers=1, dropout=0, self_loop=False, skip_connect=False, layer_norm=True, input_dropout=0,
hidden_dropout=0, feat_dropout=0, aggregation='cat', weight=1, discount=0, angle=0, use_static=False,
entity_prediction=False, relation_prediction=False, sequence='rnn', use_cuda=False, analysis=False):
super(RecurrentRGCN, self).__init__()
self.decoder_name = decoder
self.encoder_name = encoder
self.gnn = gnn
self.num_rels = num_rels
self.num_ents = num_ents
self.opn = opn
self.num_words = num_words
self.num_static_rels = num_static_rels
self.sequence_len = sequence_len
self.h_dim = h_dim
self.layer_norm = layer_norm
self.h = None
self.run_analysis = analysis
self.aggregation = aggregation
self.relation_evolve = False
self.weight = weight
self.discount = discount
self.use_static = use_static
self.angle = angle
self.relation_prediction = relation_prediction
self.entity_prediction = entity_prediction
self.sequence = sequence
self.emb_rel = None
self.w1 = torch.nn.Parameter(torch.Tensor(self.h_dim, self.h_dim), requires_grad=True).float()
torch.nn.init.xavier_normal_(self.w1)
self.w2 = torch.nn.Parameter(torch.Tensor(self.h_dim, self.h_dim), requires_grad=True).float()
torch.nn.init.xavier_normal_(self.w2)
# self.emb_rel = torch.nn.Parameter(torch.Tensor(self.num_rels * 2, self.h_dim), requires_grad=True).float()
# torch.nn.init.xavier_normal_(self.emb_rel)
# self.dynamic_emb = torch.nn.Parameter(torch.Tensor(num_ents, h_dim), requires_grad=True).float()
# torch.nn.init.normal_(self.dynamic_emb)
self.dynamic_emb = None
if self.use_static:
self.words_emb = torch.nn.Parameter(torch.Tensor(self.num_words, h_dim), requires_grad=True).float()
torch.nn.init.xavier_normal_(self.words_emb)
self.statci_rgcn_layer = RGCNBlockLayer(self.h_dim, self.h_dim, self.num_static_rels*2, num_bases,
activation=F.rrelu, dropout=dropout, self_loop=False, skip_connect=False)
self.static_loss = torch.nn.MSELoss()
self.loss_r = torch.nn.CrossEntropyLoss()
self.loss_e = torch.nn.CrossEntropyLoss()
self.rgcn = None
# self.rgcn = RGCNCell(num_ents,
# h_dim,
# h_dim,
# num_rels * 2,
# num_bases,
# num_basis,
# num_hidden_layers,
# dropout,
# self_loop,
# skip_connect,
# encoder,
# self.opn,
# self.emb_rel,
# use_cuda,
# analysis)
if self.sequence == 'regcn':
self.time_gate_weight = nn.Parameter(torch.Tensor(h_dim, h_dim))
nn.init.xavier_uniform_(self.time_gate_weight, gain=nn.init.calculate_gain('relu'))
self.time_gate_bias = nn.Parameter(torch.Tensor(h_dim))
nn.init.zeros_(self.time_gate_bias)
elif self.sequence == 'rnn':
self.rnn_cell = nn.GRUCell(self.h_dim, self.h_dim)
# GRU cell for relation evolving
self.relation_cell_1 = nn.GRUCell(self.h_dim*2, self.h_dim)
# decoder
# if decoder == "convtranse":
# self.decoder_ob = ConvTransE(num_ents, h_dim, input_dropout, hidden_dropout, feat_dropout)
# self.rdecoder = ConvTransR(num_rels, h_dim, input_dropout, hidden_dropout, feat_dropout)
# else:
# raise NotImplementedError
def forward(self, g_list, static_graph=None, device=None):
gate_list = []
degree_list = []
if self.use_static:
static_graph = static_graph.to(device)
static_graph.ndata['h'] = torch.cat((self.dynamic_emb, self.words_emb), dim=0) # 演化得到的表示,和wordemb满足静态图约束
self.statci_rgcn_layer(static_graph, [])
static_emb = static_graph.ndata.pop('h')[:self.num_ents, :]
static_emb = F.normalize(static_emb) if self.layer_norm else static_emb
self.h = static_emb
else:
self.h = F.normalize(self.dynamic_emb) if self.layer_norm else self.dynamic_emb[:, :]
static_emb = None
history_embs = []
for i, g in enumerate(g_list):
g = g.to(device)
temp_e = self.h[g.r_to_e]
x_input = torch.zeros(self.num_rels * 2, self.h_dim).float().to(device)
for span, r_idx in zip(g.r_len, g.uniq_r):
x = temp_e[span[0]:span[1],:]
x_mean = torch.mean(x, dim=0, keepdim=True)
x_input[r_idx] = x_mean
if i == 0:
x_input = torch.cat((self.emb_rel[0:self.num_rels *2], x_input), dim=1)
self.h_0 = self.relation_cell_1(x_input, self.emb_rel[0:self.num_rels *2]) # 第1层输入
self.h_0 = F.normalize(self.h_0) if self.layer_norm else self.h_0
else:
x_input = torch.cat((self.emb_rel[0:self.num_rels *2], x_input), dim=1)
self.h_0 = self.relation_cell_1(x_input, self.h_0) # 第2层输出==下一时刻第一层输入
self.h_0 = F.normalize(self.h_0) if self.layer_norm else self.h_0
if self.gnn == 'regcn':
current_h = self.rgcn.forward(g, self.h, [self.h_0, self.h_0])
elif self.gnn == 'rgat':
g.edata['r_h'] = self.h_0[g.edata['etype']]
#g.edata['r_h'] = self.emb_rel[g.edata['etype']]
current_h = self.rgcn(g, self.h)
if self.sequence == 'regcn':
current_h = F.normalize(current_h) if self.layer_norm else current_h
time_weight = torch.sigmoid(torch.mm(self.h, self.time_gate_weight) + self.time_gate_bias)
self.h = time_weight * current_h + (1-time_weight) * self.h
elif self.sequence == 'rnn':
self.h = self.rnn_cell(current_h, self.h)
history_embs.append(self.h)
return history_embs, static_emb, self.h_0, gate_list, degree_list
def predict(self, test_graph, num_rels, static_graph, test_triplets, use_cuda):
with torch.no_grad():
inverse_test_triplets = test_triplets[:, [2, 1, 0]]
inverse_test_triplets[:, 1] = inverse_test_triplets[:, 1] + num_rels # 将逆关系换成逆关系的id
all_triples = torch.cat((test_triplets, inverse_test_triplets))
evolve_embs, _, r_emb, _, _ = self.forward(test_graph, static_graph, use_cuda)
embedding = F.normalize(evolve_embs[-1]) if self.layer_norm else evolve_embs[-1]
score = self.decoder_ob.forward(embedding, r_emb, all_triples, mode="test")
score_rel = self.rdecoder.forward(embedding, r_emb, all_triples, mode="test")
return all_triples, score, score_rel
def get_loss(self, glist, triples, static_graph, device):
"""
:param glist:
:param triplets:
:param static_graph:
:param use_cuda:
:return:
"""
loss_ent = torch.zeros(1).to(device)
loss_rel = torch.zeros(1).to(device)
loss_static = torch.zeros(1).to(device)
inverse_triples = triples[:, [2, 1, 0]]
inverse_triples[:, 1] = inverse_triples[:, 1] + self.num_rels
all_triples = torch.cat([triples, inverse_triples])
all_triples = all_triples.to(device)
evolve_embs, static_emb, r_emb, _, _ = self.forward(glist, static_graph, device)
pre_emb = F.normalize(evolve_embs[-1]) if self.layer_norm else evolve_embs[-1]
if self.entity_prediction:
scores_ob = self.decoder_ob.forward(pre_emb, r_emb, all_triples).view(-1, self.num_ents)
loss_ent += self.loss_e(scores_ob, all_triples[:, 2])
if self.relation_prediction:
score_rel = self.rdecoder.forward(pre_emb, r_emb, all_triples, mode="train").view(-1, 2 * self.num_rels)
loss_rel += self.loss_r(score_rel, all_triples[:, 1])
if self.use_static:
if self.discount == 1:
for time_step, evolve_emb in enumerate(evolve_embs):
step = (self.angle * math.pi / 180) * (time_step + 1)
if self.layer_norm:
sim_matrix = torch.sum(static_emb * F.normalize(evolve_emb), dim=1)
else:
sim_matrix = torch.sum(static_emb * evolve_emb, dim=1)
c = torch.norm(static_emb, p=2, dim=1) * torch.norm(evolve_emb, p=2, dim=1)
sim_matrix = sim_matrix / c
mask = (math.cos(step) - sim_matrix) > 0
loss_static += self.weight * torch.sum(torch.masked_select(math.cos(step) - sim_matrix, mask))
elif self.discount == 0:
for time_step, evolve_emb in enumerate(evolve_embs):
step = (self.angle * math.pi / 180)
if self.layer_norm:
sim_matrix = torch.sum(static_emb * F.normalize(evolve_emb), dim=1)
else:
sim_matrix = torch.sum(static_emb * evolve_emb, dim=1)
c = torch.norm(static_emb, p=2, dim=1) * torch.norm(evolve_emb, p=2, dim=1)
sim_matrix = sim_matrix / c
mask = (math.cos(step) - sim_matrix) > 0
loss_static += self.weight * torch.sum(torch.masked_select(math.cos(step) - sim_matrix, mask))
return loss_ent, loss_rel, loss_static