-
Notifications
You must be signed in to change notification settings - Fork 6
/
Model_routing.py
358 lines (275 loc) · 13.2 KB
/
Model_routing.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import math
import time
from tqdm import tqdm
import numpy as np
import torch
import torch.nn as nn
from torch.nn import Parameter
import torch.nn.functional as F
from SAGEConv import SAGEConv
from GATConv import GATConv
from torch_geometric.utils import add_self_loops, dropout_adj, scatter_
# from torch.utils.checkpoint import checkpoint
##########################################################################
class EGCN(torch.nn.Module):
def __init__(self, num_user, num_item, dim_E, aggr_mode, has_act, has_norm):
super(EGCN, self).__init__()
self.num_user = num_user
self.num_item = num_item
self.dim_E = dim_E
self.aggr_mode = aggr_mode
self.has_act = has_act
self.has_norm = has_norm
self.id_embedding = nn.Parameter( nn.init.xavier_normal_(torch.rand((num_user+num_item, dim_E))))
self.conv_embed_1 = SAGEConv(dim_E, dim_E, aggr=aggr_mode)
self.conv_embed_2 = SAGEConv(dim_E, dim_E, aggr=aggr_mode)
def forward(self, edge_index, weight_vector):
x = self.id_embedding
edge_index = torch.cat((edge_index, edge_index[[1,0]]), dim=1)
if self.has_norm:
x = F.normalize(x)
x_hat_1 = self.conv_embed_1(x, edge_index, weight_vector)
if self.has_act:
x_hat_1 = F.leaky_relu_(x_hat_1)
x_hat_2 = self.conv_embed_2(x_hat_1, edge_index, weight_vector)
if self.has_act:
x_hat_2 = F.leaky_relu_(x_hat_2)
return x + x_hat_1 + x_hat_2
class CGCN(torch.nn.Module):
def __init__(self, features, num_user, num_item, dim_C, aggr_mode, num_routing, has_act, has_norm, is_word=False):
super(CGCN, self).__init__()
self.num_user = num_user
self.num_item = num_item
self.aggr_mode = aggr_mode
self.num_routing = num_routing
self.has_act = has_act
self.has_norm = has_norm
self.dim_C = dim_C
self.preference = nn.Parameter(nn.init.xavier_normal_(torch.rand((num_user, dim_C))))
self.conv_embed_1 = GATConv(self.dim_C, self.dim_C)
self.is_word = is_word
if is_word:
self.word_tensor = torch.LongTensor(features).cuda()
self.features = nn.Embedding(torch.max(features[1])+1, dim_C)
nn.init.xavier_normal_(self.features.weight)
else:
self.dim_feat = features.size(1)
self.features = features
self.MLP = nn.Linear(self.dim_feat, self.dim_C)
def forward(self, edge_index):
if self.is_word:
features = torch.tensor(scatter_('mean', self.features(self.word_tensor[1]), self.word_tensor[0])).cuda()
else:
features = F.leaky_relu(self.MLP(self.features))
if self.has_norm:
preference = F.normalize(self.preference)
features = F.normalize(features)
for i in range(self.num_routing):
x = torch.cat((preference, features), dim=0)
x_hat_1 = self.conv_embed_1(x, edge_index)
preference = preference + x_hat_1[:self.num_user]
if self.has_norm:
preference = F.normalize(preference)
x = torch.cat((preference, features), dim=0)
edge_index = torch.cat((edge_index, edge_index[[1,0]]), dim=1)
x_hat_1 = self.conv_embed_1(x, edge_index)
if self.has_act:
x_hat_1 = F.leaky_relu_(x_hat_1)
return x + x_hat_1, self.conv_embed_1.alpha.view(-1, 1)
class Net(torch.nn.Module):
def __init__(self, num_user, num_item, edge_index, user_item_dict, reg_weight,
v_feat, a_feat, t_feat,
aggr_mode, weight_mode, fusion_mode,
num_routing, dropout,
has_act, has_norm, has_entropy_loss, has_weight_loss,
is_word,
dim_E, dim_C,
pruning):
super(Net, self).__init__()
self.num_user = num_user
self.num_item = num_item
self.user_item_dict = user_item_dict
self.weight_mode = weight_mode
self.fusion_mode = fusion_mode
self.weight = torch.tensor([[1.0],[-1.0]]).cuda()
self.reg_weight = reg_weight
self.dropout = dropout
self.edge_index = torch.tensor(edge_index).t().contiguous().cuda()
self.id_gcn = EGCN(num_user, num_item, dim_E, aggr_mode, has_act, has_norm)
self.v_feat = v_feat
self.a_feat = a_feat
self.t_feat = t_feat
self.has_entropy_loss = has_entropy_loss
self.has_weight_loss = has_weight_loss
self.pruning = pruning
num_model = 0
if v_feat is not None:
self.v_gcn = CGCN(self.v_feat, num_user, num_item, dim_C, aggr_mode, num_routing, has_act, has_norm)
num_model += 1
if a_feat is not None:
self.a_gcn = CGCN(self.a_feat, num_user, num_item, dim_C, aggr_mode, num_routing, has_act, has_norm)
num_model += 1
if t_feat is not None:
self.t_gcn = CGCN(self.t_feat, num_user, num_item, dim_C, aggr_mode, num_routing, has_act, has_norm, is_word)
num_model += 1
self.model_specific_conf = nn.Parameter(nn.init.xavier_normal_(torch.rand((num_user+num_item, num_model))))
self.result = nn.init.xavier_normal_(torch.rand((num_user+num_item, dim_E))).cuda()
def forward(self):
weight = None
content_rep = None
num_modal = 0
edge_index, _ = dropout_adj(self.edge_index, p=self.dropout)
if self.v_feat is not None:
num_modal += 1
v_rep, weight_v = self.v_gcn(edge_index)
weight = weight_v
content_rep = v_rep
if self.a_feat is not None:
num_modal += 1
a_rep, weight_a = self.a_gcn(edge_index)
if weight is None:
weight = weight_a
content_rep = a_rep
else:
content_rep = torch.cat((content_rep,a_rep),dim=1)
if self.weight_mode == 'mean':
weight = weight+ weight_a
else:
weight = torch.cat((weight, weight_a), dim=1)
if self.t_feat is not None:
num_modal += 1
t_rep, weight_t = self.t_gcn(edge_index)
if weight is None:
weight = weight_t
conetent_rep = t_rep
else:
content_rep = torch.cat((content_rep,t_rep),dim=1)
if self.weight_mode == 'mean':
weight = weight+ weight_t
else:
weight = torch.cat((weight, weight_t), dim=1)
if self.weight_mode == 'mean':
weight = weight/num_modal
elif self.weight_mode == 'max':
weight, _ = torch.max(weight, dim=1)
weight = weight.view(-1, 1)
elif self.weight_mode == 'confid':
confidence = torch.cat((self.model_specific_conf[edge_index[0]], self.model_specific_conf[edge_index[1]]), dim=0)
weight = weight * confidence
weight, _ = torch.max(weight, dim=1)
weight = weight.view(-1, 1)
if self.pruning:
weight = torch.relu(weight)
id_rep = self.id_gcn(edge_index, weight)
if self.fusion_mode == 'concat':
representation = torch.cat((id_rep, content_rep), dim=1)
elif self.fusion_mode == 'id':
representation = id_rep
elif self.fusion_mode == 'mean':
representation = (id_rep+v_rep+a_rep+t_rep)/4
self.result = representation
return representation
def loss(self, user_tensor, item_tensor):
user_tensor = user_tensor.view(-1)
item_tensor = item_tensor.view(-1)
out = self.forward()
user_score = out[user_tensor]
item_score = out[item_tensor]
score = torch.sum(user_score*item_score, dim=1).view(-1, 2)
loss = -torch.mean(torch.log(torch.sigmoid(torch.matmul(score, self.weight))))
reg_embedding_loss = (self.id_gcn.id_embedding[user_tensor]**2 + self.id_gcn.id_embedding[item_tensor]**2).mean()
reg_content_loss = torch.zeros(1).cuda()
if self.v_feat is not None:
reg_content_loss = reg_content_loss + (self.v_gcn.preference[user_tensor]**2).mean()
if self.a_feat is not None:
reg_content_loss = reg_content_loss + (self.a_gcn.preference[user_tensor]**2).mean()
if self.t_feat is not None:
reg_content_loss = reg_content_loss + (self.t_gcn.preference[user_tensor]**2).mean()
reg_confid_loss = (self.model_specific_conf**2).mean()
reg_loss = reg_embedding_loss + reg_content_loss
reg_loss = self.reg_weight * reg_loss
return loss+reg_loss, loss, reg_embedding_loss+reg_content_loss, reg_embedding_loss, reg_content_loss
def accuracy(self, step=2000, topk=10):
user_tensor = self.result[:self.num_user]
item_tensor = self.result[self.num_user:]
start_index = 0
end_index = self.num_user if step==None else step
all_index_of_rank_list = torch.LongTensor([])
while end_index <= self.num_user and start_index < end_index:
temp_user_tensor = user_tensor[start_index:end_index]
score_matrix = torch.matmul(temp_user_tensor, item_tensor.t())
_, index_of_rank_list = torch.topk(score_matrix, topk)
all_index_of_rank_list = torch.cat((all_index_of_rank_list, index_of_rank_list.cpu()+self.num_user), dim=0)
start_index = end_index
if end_index+step < self.num_user:
end_index += step
else:
end_index = self.num_user
length = self.num_user
precision = recall = ndcg = 0.0
for row, col in self.user_item_dict.items():
user = row
pos_items = set(col)
num_pos = len(pos_items)
items_list = all_index_of_rank_list[user].tolist()
items = set(items_list)
num_hit = len(pos_items.intersection(items))
precision += float(num_hit / topk)
recall += float(num_hit / num_pos)
ndcg_score = 0.0
max_ndcg_score = 0.0
for i in range(min(num_hit, topk)):
max_ndcg_score += 1 / math.log2(i+2)
if max_ndcg_score == 0:
continue
for i, temp_item in enumerate(items_list):
if temp_item in pos_items:
ndcg_score += 1 / math.log2(i+2)
ndcg += ndcg_score/max_ndcg_score
return precision/length, recall/length, ndcg/length
def full_accuracy(self, val_data, step=2000, topk=10):
user_tensor = self.result[:self.num_user]
item_tensor = self.result[self.num_user:]
start_index = 0
end_index = self.num_user if step==None else step
all_index_of_rank_list = torch.LongTensor([])
while end_index <= self.num_user and start_index < end_index:
temp_user_tensor = user_tensor[start_index:end_index]
score_matrix = torch.matmul(temp_user_tensor, item_tensor.t())
for row, col in self.user_item_dict.items():
if row >= start_index and row < end_index:
row -= start_index
col = torch.LongTensor(list(col))-self.num_user
score_matrix[row][col] = 1e-5
_, index_of_rank_list = torch.topk(score_matrix, topk)
all_index_of_rank_list = torch.cat((all_index_of_rank_list, index_of_rank_list.cpu()+self.num_user), dim=0)
start_index = end_index
if end_index+step < self.num_user:
end_index += step
else:
end_index = self.num_user
length = 0#len(val_data)
precision = recall = ndcg = 0.0
for data in val_data:
user = data[0]
pos_items = set(data[1:])
num_pos = len(pos_items)
if num_pos == 0:
continue
length += 1
items_list = all_index_of_rank_list[user].tolist()
items = set(items_list)
num_hit = len(pos_items.intersection(items))
precision += float(num_hit / topk)
recall += float(num_hit / num_pos)
ndcg_score = 0.0
max_ndcg_score = 0.0
for i in range(min(num_hit, topk)):
max_ndcg_score += 1 / math.log2(i+2)
if max_ndcg_score == 0:
continue
for i, temp_item in enumerate(items_list):
if temp_item in pos_items:
ndcg_score += 1 / math.log2(i+2)
ndcg += ndcg_score/max_ndcg_score
return precision/length, recall/length, ndcg/length