forked from anonymousCML/CML
-
Notifications
You must be signed in to change notification settings - Fork 16
/
BGNN.py
212 lines (153 loc) · 8.12 KB
/
BGNN.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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import init
from torch.autograd import Variable
from Params import args
def to_var(x, requires_grad=True):
if torch.cuda.is_available():
x = x.cuda()
return Variable(x, requires_grad=requires_grad)
class myModel(nn.Module):
def __init__(self, userNum, itemNum, behavior, behavior_mats):
super(myModel, self).__init__()
self.userNum = userNum
self.itemNum = itemNum
self.behavior = behavior
self.behavior_mats = behavior_mats
self.embedding_dict = self.init_embedding()
self.weight_dict = self.init_weight()
self.gcn = GCN(self.userNum, self.itemNum, self.behavior, self.behavior_mats)
def init_embedding(self):
embedding_dict = {
'user_embedding': None,
'item_embedding': None,
'user_embeddings': None,
'item_embeddings': None,
}
return embedding_dict
def init_weight(self):
initializer = nn.init.xavier_uniform_
weight_dict = nn.ParameterDict({
'w_self_attention_item': nn.Parameter(initializer(torch.empty([args.hidden_dim, args.hidden_dim]))),
'w_self_attention_user': nn.Parameter(initializer(torch.empty([args.hidden_dim, args.hidden_dim]))),
'w_self_attention_cat': nn.Parameter(initializer(torch.empty([args.head_num*args.hidden_dim, args.hidden_dim]))),
'alpha': nn.Parameter(torch.ones(2)),
})
return weight_dict
def forward(self):
user_embed, item_embed, user_embeds, item_embeds = self.gcn()
return user_embed, item_embed, user_embeds, item_embeds
def para_dict_to_tenser(self, para_dict):
tensors = []
for beh in para_dict.keys():
tensors.append(para_dict[beh])
tensors = torch.stack(tensors, dim=0)
return tensors.float()
def update_params(self, lr_inner, first_order=False, source_params=None, detach=False):
if source_params is not None:
for tgt, src in zip(self.named_parameters(), source_params):
name_t, param_t = tgt
grad = src
if first_order:
grad = to_var(grad.detach().data)
tmp = param_t - lr_inner * grad
self.set_param(self, name_t, tmp)
else:
for name, param in self.named_parameters()(self):
if not detach:
grad = param.grad
if first_order:
grad = to_var(grad.detach().data)
tmp = param - lr_inner * grad
self.set_param(self, name, tmp)
else:
param = param.detach_()
self.set_param(self, name, param)
class GCN(nn.Module):
def __init__(self, userNum, itemNum, behavior, behavior_mats):
super(GCN, self).__init__()
self.userNum = userNum
self.itemNum = itemNum
self.hidden_dim = args.hidden_dim
self.behavior = behavior
self.behavior_mats = behavior_mats
self.user_embedding, self.item_embedding = self.init_embedding()
self.alpha, self.i_concatenation_w, self.u_concatenation_w, self.i_input_w, self.u_input_w = self.init_weight()
self.sigmoid = torch.nn.Sigmoid()
self.act = torch.nn.PReLU()
self.dropout = torch.nn.Dropout(args.drop_rate)
self.gnn_layer = eval(args.gnn_layer)
self.layers = nn.ModuleList()
for i in range(0, len(self.gnn_layer)):
self.layers.append(GCNLayer(args.hidden_dim, args.hidden_dim, self.userNum, self.itemNum, self.behavior, self.behavior_mats))
def init_embedding(self):
user_embedding = torch.nn.Embedding(self.userNum, args.hidden_dim)
item_embedding = torch.nn.Embedding(self.itemNum, args.hidden_dim)
nn.init.xavier_uniform_(user_embedding.weight)
nn.init.xavier_uniform_(item_embedding.weight)
return user_embedding, item_embedding
def init_weight(self):
alpha = nn.Parameter(torch.ones(2))
i_concatenation_w = nn.Parameter(torch.Tensor(len(eval(args.gnn_layer))*args.hidden_dim, args.hidden_dim))
u_concatenation_w = nn.Parameter(torch.Tensor(len(eval(args.gnn_layer))*args.hidden_dim, args.hidden_dim))
i_input_w = nn.Parameter(torch.Tensor(args.hidden_dim, args.hidden_dim))
u_input_w = nn.Parameter(torch.Tensor(args.hidden_dim, args.hidden_dim))
init.xavier_uniform_(i_concatenation_w)
init.xavier_uniform_(u_concatenation_w)
init.xavier_uniform_(i_input_w)
init.xavier_uniform_(u_input_w)
# init.xavier_uniform_(alpha)
return alpha, i_concatenation_w, u_concatenation_w, i_input_w, u_input_w
def forward(self, user_embedding_input=None, item_embedding_input=None):
all_user_embeddings = []
all_item_embeddings = []
all_user_embeddingss = []
all_item_embeddingss = []
user_embedding = self.user_embedding.weight
item_embedding = self.item_embedding.weight
for i, layer in enumerate(self.layers):
user_embedding, item_embedding, user_embeddings, item_embeddings = layer(user_embedding, item_embedding)
norm_user_embeddings = F.normalize(user_embedding, p=2, dim=1)
norm_item_embeddings = F.normalize(item_embedding, p=2, dim=1)
all_user_embeddings.append(user_embedding)
all_item_embeddings.append(item_embedding)
all_user_embeddingss.append(user_embeddings)
all_item_embeddingss.append(item_embeddings)
user_embedding = torch.cat(all_user_embeddings, dim=1)
item_embedding = torch.cat(all_item_embeddings, dim=1)
user_embeddings = torch.cat(all_user_embeddingss, dim=2)
item_embeddings = torch.cat(all_item_embeddingss, dim=2)
user_embedding = torch.matmul(user_embedding , self.u_concatenation_w)
item_embedding = torch.matmul(item_embedding , self.i_concatenation_w)
user_embeddings = torch.matmul(user_embeddings , self.u_concatenation_w)
item_embeddings = torch.matmul(item_embeddings , self.i_concatenation_w)
return user_embedding, item_embedding, user_embeddings, item_embeddings #[31882, 16], [31882, 16], [4, 31882, 16], [4, 31882, 16]
class GCNLayer(nn.Module):
def __init__(self, in_dim, out_dim, userNum, itemNum, behavior, behavior_mats):
super(GCNLayer, self).__init__()
self.behavior = behavior
self.behavior_mats = behavior_mats
self.userNum = userNum
self.itemNum = itemNum
self.act = torch.nn.Sigmoid()
self.i_w = nn.Parameter(torch.Tensor(in_dim, out_dim))
self.u_w = nn.Parameter(torch.Tensor(in_dim, out_dim))
self.ii_w = nn.Parameter(torch.Tensor(in_dim, out_dim))
init.xavier_uniform_(self.i_w)
init.xavier_uniform_(self.u_w)
def forward(self, user_embedding, item_embedding):
user_embedding_list = [None]*len(self.behavior)
item_embedding_list = [None]*len(self.behavior)
for i in range(len(self.behavior)):
user_embedding_list[i] = torch.spmm(self.behavior_mats[i]['A'], item_embedding)
item_embedding_list[i] = torch.spmm(self.behavior_mats[i]['AT'], user_embedding)
user_embeddings = torch.stack(user_embedding_list, dim=0)
item_embeddings = torch.stack(item_embedding_list, dim=0)
user_embedding = self.act(torch.matmul(torch.mean(user_embeddings, dim=0), self.u_w))
item_embedding = self.act(torch.matmul(torch.mean(item_embeddings, dim=0), self.i_w))
user_embeddings = self.act(torch.matmul(user_embeddings, self.u_w))
item_embeddings = self.act(torch.matmul(item_embeddings, self.i_w))
return user_embedding, item_embedding, user_embeddings, item_embeddings
#------------------------------------------------------------------------------------------------------------------------------------------------