-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
244 lines (188 loc) · 7.44 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
import torch
import torch.nn as nn
import torch.nn.functional as F
class UserAttention(nn.Module):
""" Basic Item Attention for HLRM """
def __init__(self, emb_size,num_relations):
super().__init__()
self.emb_size = emb_size
self.num_relations = num_relations
self.key_ = nn.Embedding(self.num_relations,self.emb_size)
self.val_ = nn.Embedding(self.num_relations,self.emb_size)
def get_i2i_inter_rel(self,item,inter):
"""
Input:
- Item: (b,emb)
- Inter: (b,num_inter,emb)
Output:
- Rel_Vecs : (b,num_inter,emb)
"""
w = torch.einsum('ri,ri -> ri', self.key_.weight,self.val_.weight)
return torch.einsum('mbj,ij -> bmj', (item*inter.transpose(0,1)), w)
def get_u2i_attn_weights(self,user, rel_vecs):
"""
Input:
- User: (b,emb)
- Rel_vecs: (b,emb)
Output:
- Attn_weights : (b, emb)
"""
bef = torch.einsum('bji,bi -> bj', rel_vecs, user)
return F.softmax(bef, dim = 1)
def forward(self,user,item, inter):
"""
Input:
- User: (b,emb)
- Item: (b,emb)
- Inter: (b, num_inter, emb)
Output:
- Weighted_rel_vecs : (b, emb)
"""
rel_vecs = self.get_i2i_inter_rel(item, inter)
attn = self.get_u2i_attn_weights(user, rel_vecs)
return torch.einsum('bi,bij -> bj', attn, rel_vecs)
class ItemAttention(nn.Module):
""" Additional User Attention for HLRM ++ """
def __init__(self, emb_size,num_relations):
super().__init__()
self.emb_size = emb_size
self.num_relations = num_relations
self.key_ = nn.Embedding(self.num_relations,self.emb_size)
self.val_ = nn.Embedding(self.num_relations,self.emb_size)
def get_u2i_inter_rel(self,user,inter):
"""
Input:
- User: (b,emb)
- Inter: (b,num_inter,emb)
Output:
- Rel_Vecs : (b,num_inter,emb)
"""
w = torch.einsum('ri,ri -> ri', self.key_.weight,self.val_.weight)
return torch.einsum('mbj,ij -> bmj', (user*inter.transpose(0,1)), w)
def get_i2i_attn_weights(self,item, rel_vecs):
"""
Input:
- Item: (b,emb)
- Rel_vecs: (b,emb)
Output:
- Attn_weights : (b, emb)
"""
bef = torch.einsum('bji,bi -> bj', rel_vecs, item)
return F.softmax(bef, dim = 1)
def forward(self,user,item, inter):
"""
Input:
- User: (b,emb)
- Item: (b,emb)
- Inter: (b, num_inter, emb)
Output:
- Weighted_rel_vecs : (b, emb)
"""
rel_vecs = self.get_u2i_inter_rel(user, inter)
attn = self.get_i2i_attn_weights(item, rel_vecs)
return torch.einsum('bi,bij -> bj', attn, rel_vecs)
class BaseModel(nn.Module):
def __init__(self):
super().__init__()
self.global_step = 0
def determine_mode(self, split):
if split == 'train':
self.train()
else:
self.eval()
class HLRM(BaseModel):
""" Hierarhical Latent Relation Modeling """
def __init__(self,
emb_size,
num_relations,
user_num, item_num,
pretrained_embs = False,
user_embs = None,
item_embs = None,
model_type = 'base'):
super().__init__()
self.emb_size = emb_size
self.num_relations = num_relations
self.user_num = user_num
self.item_num = item_num
self.pretrained_embs = pretrained_embs
self.user_embs_pretrained = user_embs
self.item_embs_pretrained = item_embs
self.userattn = UserAttention(self.emb_size, self.num_relations)
if model_type == 'plus':
self.itemattn = ItemAttention(self.emb_size, self.num_relations)
if self.pretrained_embs:
self.user_emb = nn.Embedding.from_pretrained(self.user_embs_pretrained,max_norm = 1)
self.item_emb = nn.Embedding.from_pretrained(self.item_embs_pretrained,max_norm = 1)
else :
self.user_emb = nn.Embedding(self.user_num,self.emb_size,max_norm = 1)
self.item_emb = nn.Embedding(self.item_num,self.emb_size, max_norm = 1)
self.model_type = model_type
def inference(self, user_id,item_id, inter_id):
""" Inference for a single {user,item,interaction} triplet """
user_feat = self.user_emb(user_id)
item_feat = self.item_emb(item_id)
inter_feat = self.item_emb(inter_id)
rel = self.userattn(user_feat,item_feat,inter_feat)
if self.model_type == 'plus':
rel += self.itemattn(user_feat,item_feat,inter_feat)
return user_feat + rel
def forward(self, user_id, item_id_p, item_id_n, inter_id):
"""
Forward to retrieve relational vector of pos_item * neg_item for triplet loss
"""
user_feat = self.user_emb(user_id) # (b, emb)
item_feat_p = self.item_emb(item_id_p) # (b, emb)
item_feat_n = self.item_emb(item_id_n) # (b, emb)
inter_feat = self.item_emb(inter_id) # (b, num_inter, emb)
# retrieve relation vectors from user-attention
rel_p = self.userattn(user_feat,item_feat_p,inter_feat) # (b,emb)
rel_n = self.userattn(user_feat,item_feat_n,inter_feat) # (b,emb)
# retrieve relation vectors from item-attention if HLRM++
if self.model_type == 'plus':
rel_p += self.itemattn(user_feat,item_feat_p,inter_feat) # (b,emb)
rel_n += self.itemattn(user_feat,item_feat_n,inter_feat) # (b,emb)
return user_feat,item_feat_p,item_feat_n, rel_p, rel_n
class ItemAttention_Extended(nn.Module):
"""
Extended Item Attention for HLRM_Aug
Different from the original ItemAttention as utilizing the item's previous user interaction
"""
def __init__(self, emb_size,num_relations):
super().__init__()
self.emb_size = emb_size
self.num_relations = num_relations
self.key_ = nn.Embedding(self.num_relations,self.emb_size)
self.val_ = nn.Embedding(self.num_relations,self.emb_size)
def get_u2u_inter_rel(self,user,i2u_inter):
"""
Input:
- User: (b,emb)
- Inter: (b,num_inter,emb)
Output:
- Rel_Vecs : (b,num_inter,emb)
"""
w = torch.einsum('ri,ri -> ri', self.key_.weight,self.val_.weight)
return torch.einsum('mbj,ij -> bmj', (user*i2u_inter.transpose(0,1)), w)
def get_u2i_attn_weights(self,item, rel_vecs):
"""
Input:
- User: (b,emb)
- Rel_vecs: (b,emb)
Output:
- Attn_weights : (b, emb)
"""
bef = torch.einsum('bji,bi -> bj', rel_vecs, item)
return F.softmax(bef, dim = 1)
def forward(self,user,item, i2u_inter):
"""
Input:
- User: (b,emb)
- Item: (b,emb)
- Inter: (b, num_inter, emb)
Output:
- Weighted_rel_vecs : (b, emb)
"""
rel_vecs = self.get_u2u_inter_rel(user, i2u_inter)
attn = self.get_u2i_attn_weights(item, rel_vecs)
return torch.einsum('bi,bij -> bj', attn, rel_vecs)