-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel.py
289 lines (222 loc) · 12.3 KB
/
model.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
# coding=utf-8
"""
@author: Yantong Lai
@paper: [24 SIGIR] Disentangled Contrastive Hypergraph Learning for Next POI Recommendation
"""
import torch.nn as nn
import torch
import torch.nn.functional as F
class MultiViewHyperConvLayer(nn.Module):
"""
Multi-view Hypergraph Convolutional Layer
"""
def __init__(self, emb_dim, device):
super(MultiViewHyperConvLayer, self).__init__()
# self.fc_seq = nn.Linear(2 * emb_dim, emb_dim, bias=True, device=device)
self.fc_fusion = nn.Linear(2 * emb_dim, emb_dim, device=device)
self.dropout = nn.Dropout(0.3)
self.emb_dim = emb_dim
self.device = device
def forward(self, pois_embs, pad_all_train_sessions, HG_up, HG_pu):
# pois_embs = [L, d]
# H_pu = [L, U]
# H_up = [U, L]
# pad_all_train_session = [U, MAX_SESS_LEN]
# 1. node -> hyperedge message
# 1) poi node aggregation
msg_poi_agg = torch.sparse.mm(HG_up, pois_embs) # [U, d]
# 2. propagation: hyperedge -> node
# propag_pois_embs = torch.sparse.mm(HG_poi_session, msg_emb) # [L, d]
propag_pois_embs = torch.sparse.mm(HG_pu, msg_poi_agg) # [L, d]
# propag_pois_embs = self.dropout(propag_pois_embs)
return propag_pois_embs
class DirectedHyperConvLayer(nn.Module):
"""Directed hypergraph convolutional layer"""
def __init__(self):
super(DirectedHyperConvLayer, self).__init__()
def forward(self, pois_embs, HG_poi_src, HG_poi_tar):
msg_tar = torch.sparse.mm(HG_poi_tar, pois_embs)
msg_src = torch.sparse.mm(HG_poi_src, msg_tar)
return msg_src
class MultiViewHyperConvNetwork(nn.Module):
"""
Multi-view Hypergraph Convolutional Network
"""
def __init__(self, num_layers, emb_dim, dropout, device):
super(MultiViewHyperConvNetwork, self).__init__()
self.num_layers = num_layers
self.device = device
self.mv_hconv_layer = MultiViewHyperConvLayer(emb_dim, device)
self.dropout = dropout
def forward(self, pois_embs, pad_all_train_sessions, HG_up, HG_pu):
final_pois_embs = [pois_embs]
for layer_idx in range(self.num_layers):
pois_embs = self.mv_hconv_layer(pois_embs, pad_all_train_sessions, HG_up, HG_pu) # [L, d]
# add residual connection to alleviate over-smoothing issue
pois_embs = pois_embs + final_pois_embs[-1]
pois_embs = F.dropout(pois_embs, self.dropout)
final_pois_embs.append(pois_embs)
final_pois_embs = torch.mean(torch.stack(final_pois_embs), dim=0) # [L, d]
return final_pois_embs
class DirectedHyperConvNetwork(nn.Module):
def __init__(self, num_layers, device, dropout=0.3):
super(DirectedHyperConvNetwork, self).__init__()
self.num_layers = num_layers
self.device = device
self.dropout = dropout
self.di_hconv_layer = DirectedHyperConvLayer()
def forward(self, pois_embs, HG_poi_src, HG_poi_tar):
final_pois_embs = [pois_embs]
for layer_idx in range(self.num_layers):
pois_embs = self.di_hconv_layer(pois_embs, HG_poi_src, HG_poi_tar)
# add residual connection
pois_embs = pois_embs + final_pois_embs[-1]
pois_embs = F.dropout(pois_embs, self.dropout)
final_pois_embs.append(pois_embs)
final_pois_embs = torch.mean(torch.stack(final_pois_embs), dim=0) # [L, d]
return final_pois_embs
class GeoConvNetwork(nn.Module):
def __init__(self, num_layers, dropout):
super(GeoConvNetwork, self).__init__()
self.num_layers = num_layers
self.dropout = dropout
def forward(self, pois_embs, geo_graph):
final_pois_embs = [pois_embs]
for _ in range(self.num_layers):
# pois_embs = geo_graph @ pois_embs
pois_embs = torch.sparse.mm(geo_graph, pois_embs)
pois_embs = pois_embs + final_pois_embs[-1]
# pois_embs = F.dropout(pois_embs, self.dropout)
final_pois_embs.append(pois_embs)
output_pois_embs = torch.mean(torch.stack(final_pois_embs), dim=0) # [L, d]
return output_pois_embs
class DCHL(nn.Module):
def __init__(self, num_users, num_pois, args, device):
super(DCHL, self).__init__()
# definition
self.num_users = num_users
self.num_pois = num_pois
self.args = args
self.device = device
self.emb_dim = args.emb_dim
self.ssl_temp = args.temperature
# embedding
self.user_embedding = nn.Embedding(num_users, self.emb_dim)
self.poi_embedding = nn.Embedding(num_pois + 1, self.emb_dim, padding_idx=num_pois)
# embedding init
nn.init.xavier_uniform_(self.user_embedding.weight)
nn.init.xavier_uniform_(self.poi_embedding.weight)
# network
self.mv_hconv_network = MultiViewHyperConvNetwork(args.num_mv_layers, args.emb_dim, 0, device)
self.geo_conv_network = GeoConvNetwork(args.num_geo_layers, args.dropout)
self.di_hconv_network = DirectedHyperConvNetwork(args.num_di_layers, device, args.dropout)
# gate for adaptive fusion with pois embeddings
self.hyper_gate = nn.Sequential(nn.Linear(args.emb_dim, 1), nn.Sigmoid())
self.gcn_gate = nn.Sequential(nn.Linear(args.emb_dim, 1), nn.Sigmoid())
self.trans_gate = nn.Sequential(nn.Linear(args.emb_dim, 1), nn.Sigmoid())
# gate for adaptive fusion with users embeddings
self.user_hyper_gate = nn.Sequential(nn.Linear(args.emb_dim, 1), nn.Sigmoid())
self.user_gcn_gate = nn.Sequential(nn.Linear(args.emb_dim, 1), nn.Sigmoid())
# temporal-augmentation
self.pos_embeddings = nn.Embedding(1500, self.emb_dim, padding_idx=0)
self.w_1 = nn.Linear(2 * self.emb_dim, self.emb_dim)
self.w_2 = nn.Parameter(torch.Tensor(self.emb_dim, 1))
self.glu1 = nn.Linear(self.emb_dim, self.emb_dim)
self.glu2 = nn.Linear(self.emb_dim, self.emb_dim, bias=False)
# gating before disentangled learning
self.w_gate_geo = nn.Parameter(torch.FloatTensor(args.emb_dim, args.emb_dim))
self.b_gate_geo = nn.Parameter(torch.FloatTensor(1, args.emb_dim))
self.w_gate_seq = nn.Parameter(torch.FloatTensor(args.emb_dim, args.emb_dim))
self.b_gate_seq = nn.Parameter(torch.FloatTensor(1, args.emb_dim))
self.w_gate_col = nn.Parameter(torch.FloatTensor(args.emb_dim, args.emb_dim))
self.b_gate_col = nn.Parameter(torch.FloatTensor(1, args.emb_dim))
nn.init.xavier_normal_(self.w_gate_geo.data)
nn.init.xavier_normal_(self.b_gate_geo.data)
nn.init.xavier_normal_(self.w_gate_seq.data)
nn.init.xavier_normal_(self.b_gate_seq.data)
nn.init.xavier_normal_(self.w_gate_col.data)
nn.init.xavier_normal_(self.b_gate_col.data)
# dropout
self.dropout = nn.Dropout(args.dropout)
@staticmethod
def row_shuffle(embedding):
corrupted_embedding = embedding[torch.randperm(embedding.size()[0])]
return corrupted_embedding
def cal_loss_infonce(self, emb1, emb2):
pos_score = torch.exp(torch.sum(emb1 * emb2, dim=1) / self.ssl_temp)
neg_score = torch.sum(torch.exp(torch.mm(emb1, emb2.T) / self.ssl_temp), axis=1)
loss = torch.sum(-torch.log(pos_score / (neg_score + 1e-8) + 1e-8))
loss /= pos_score.shape[0]
return loss
def cal_loss_cl_pois(self, hg_pois_embs, geo_pois_embs, trans_pois_embs):
# projection
# proj_hg_pois_embs = self.proj_hg(hg_pois_embs)
# proj_geo_pois_embs = self.proj_geo(geo_pois_embs)
# proj_trans_pois_embs = self.proj_trans(trans_pois_embs)
# normalization
norm_hg_pois_embs = F.normalize(hg_pois_embs, p=2, dim=1)
norm_geo_pois_embs = F.normalize(geo_pois_embs, p=2, dim=1)
norm_trans_pois_embs = F.normalize(trans_pois_embs, p=2, dim=1)
# calculate loss
loss_cl_pois = 0.0
loss_cl_pois += self.cal_loss_infonce(norm_hg_pois_embs, norm_geo_pois_embs)
loss_cl_pois += self.cal_loss_infonce(norm_hg_pois_embs, norm_trans_pois_embs)
loss_cl_pois += self.cal_loss_infonce(norm_geo_pois_embs, norm_trans_pois_embs)
return loss_cl_pois
def cal_loss_cl_users(self, hg_batch_users_embs, geo_batch_users_embs, trans_batch_users_embs):
# normalization
norm_hg_batch_users_embs = F.normalize(hg_batch_users_embs, p=2, dim=1)
norm_geo_batch_users_embs = F.normalize(geo_batch_users_embs, p=2, dim=1)
norm_trans_batch_users_embs = F.normalize(trans_batch_users_embs, p=2, dim=1)
# calculate loss
loss_cl_users = 0.0
loss_cl_users += self.cal_loss_infonce(norm_hg_batch_users_embs, norm_geo_batch_users_embs)
loss_cl_users += self.cal_loss_infonce(norm_hg_batch_users_embs, norm_trans_batch_users_embs)
loss_cl_users += self.cal_loss_infonce(norm_geo_batch_users_embs, norm_trans_batch_users_embs)
return loss_cl_users
def forward(self, dataset, batch):
# self-gating input
geo_gate_pois_embs = torch.multiply(self.poi_embedding.weight[:-1],
torch.sigmoid(torch.matmul(self.poi_embedding.weight[:-1],
self.w_gate_geo) + self.b_gate_geo))
seq_gate_pois_embs = torch.multiply(self.poi_embedding.weight[:-1],
torch.sigmoid(torch.matmul(self.poi_embedding.weight[:-1],
self.w_gate_seq) + self.b_gate_seq))
col_gate_pois_embs = torch.multiply(self.poi_embedding.weight[:-1],
torch.sigmoid(torch.matmul(self.poi_embedding.weight[:-1],
self.w_gate_col) + self.b_gate_col))
# multi-view hypergraph convolutional network
hg_pois_embs = self.mv_hconv_network(col_gate_pois_embs, dataset.pad_all_train_sessions, dataset.HG_up, dataset.HG_pu)
# hypergraph structure aware users embeddings
hg_structural_users_embs = torch.sparse.mm(dataset.HG_up, hg_pois_embs) # [U, d]
hg_batch_users_embs = hg_structural_users_embs[batch["user_idx"]] # [BS, d]
# poi-poi geographical graph convolutional network
geo_pois_embs = self.geo_conv_network(geo_gate_pois_embs, dataset.poi_geo_graph) # [L, d]
# geo-aware user embeddings
geo_structural_users_embs = torch.sparse.mm(dataset.HG_up, geo_pois_embs)
geo_batch_users_embs = geo_structural_users_embs[batch["user_idx"]] # [BS, d]
# poi-poi directed hypergraph
trans_pois_embs = self.di_hconv_network(seq_gate_pois_embs, dataset.HG_poi_src, dataset.HG_poi_tar)
# transition-aware user embeddings
trans_structural_users_embs = torch.sparse.mm(dataset.HG_up, trans_pois_embs)
trans_batch_users_embs = trans_structural_users_embs[batch["user_idx"]] # [BS, d]
# cross view contrastive learning
loss_cl_poi = self.cal_loss_cl_pois(hg_pois_embs, geo_pois_embs, trans_pois_embs)
loss_cl_user = self.cal_loss_cl_users(hg_batch_users_embs, geo_batch_users_embs, trans_batch_users_embs)
# normalization
norm_hg_pois_embs = F.normalize(hg_pois_embs, p=2, dim=1)
norm_geo_pois_embs = F.normalize(geo_pois_embs, p=2, dim=1)
norm_trans_pois_embs = F.normalize(trans_pois_embs, p=2, dim=1)
norm_hg_batch_users_embs = F.normalize(hg_batch_users_embs, p=2, dim=1)
norm_geo_batch_users_embs = F.normalize(geo_batch_users_embs, p=2, dim=1)
norm_trans_batch_users_embs = F.normalize(trans_batch_users_embs, p=2, dim=1)
# adaptive fusion for user embeddings
hyper_coef = self.hyper_gate(norm_hg_batch_users_embs)
geo_coef = self.gcn_gate(norm_geo_batch_users_embs)
trans_coef = self.trans_gate(norm_trans_batch_users_embs)
# final fusion for user and poi embeddings
fusion_batch_users_embs = hyper_coef * norm_hg_batch_users_embs + geo_coef * norm_geo_batch_users_embs + trans_coef * norm_trans_batch_users_embs
fusion_pois_embs = norm_hg_pois_embs + norm_geo_pois_embs + norm_trans_pois_embs
# prediction
prediction = fusion_batch_users_embs @ fusion_pois_embs.T
return prediction, loss_cl_user, loss_cl_poi