-
Notifications
You must be signed in to change notification settings - Fork 5
/
model.py
207 lines (176 loc) · 9.87 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
from typing import Union
import torch
from torch import Tensor
import torch.nn as nn
import torch.nn.functional as torch_f
from src.model.news_encoder import NewsEncoder
from src.utils import pairwise_cosine_similarity
class Miner(nn.Module):
r"""
Implementation of Multi-interest matching network for news recommendation. Please see the paper in
https://aclanthology.org/2022.findings-acl.29.pdf.
"""
def __init__(self, news_encoder: NewsEncoder, use_category_bias: bool, num_context_codes: int,
context_code_dim: int, score_type: str, dropout: float, num_category: Union[int, None] = None,
category_embed_dim: Union[int, None] = None, category_pad_token_id: Union[int, None] = None,
category_embed: Union[Tensor, None] = None):
r"""
Initialization
Args:
news_encoder: NewsEncoder object.
use_category_bias: whether to use Category-aware attention weighting.
num_context_codes: the number of attention vectors ``K``.
context_code_dim: the number of features in a context code.
score_type: the ways to aggregate the ``K`` matching scores as a final user click score ('max', 'mean' or
'weighted').
dropout: dropout value.
num_category: the size of the dictionary of categories.
category_embed_dim: the size of each category embedding vector.
category_pad_token_id: ID of the padding token type in the category vocabulary.
category_embed: pre-trained category embedding.
"""
super().__init__()
self.news_encoder = news_encoder
self.news_embed_dim = self.news_encoder.embed_dim
self.use_category_bias = use_category_bias
if self.use_category_bias:
self.category_dropout = nn.Dropout(dropout)
if category_embed is not None:
self.category_embedding = nn.Embedding.from_pretrained(category_embed, freeze=False,
padding_idx=category_pad_token_id)
self.category_embed_dim = category_embed.shape[1]
else:
assert num_category is not None
self.category_embedding = nn.Embedding(num_embeddings=num_category, embedding_dim=category_embed_dim,
padding_idx=category_pad_token_id)
self.category_embed_dim = category_embed_dim
self.poly_attn = PolyAttention(in_embed_dim=self.news_embed_dim, num_context_codes=num_context_codes,
context_code_dim=context_code_dim)
self.score_type = score_type
if self.score_type == 'weighted':
self.target_aware_attn = TargetAwareAttention(self.news_embed_dim)
self.dropout = nn.Dropout(dropout)
def forward(self, title: Tensor, title_mask: Tensor, his_title: Tensor, his_title_mask: Tensor,
his_mask: Tensor, sapo: Union[Tensor, None] = None, sapo_mask: Union[Tensor, None] = None,
his_sapo: Union[Tensor, None] = None, his_sapo_mask: Union[Tensor, None] = None,
category: Union[Tensor, None] = None, his_category: Union[Tensor, None] = None):
r"""
Forward propagation
Args:
title: tensor of shape ``(batch_size, num_candidates, title_length)``.
title_mask: tensor of shape ``(batch_size, num_candidates, title_length)``.
his_title: tensor of shape ``(batch_size, num_clicked_news, title_length)``.
his_title_mask: tensor of shape ``(batch_size, num_clicked_news, title_length)``.
his_mask: tensor of shape ``(batch_size, num_clicked_news)``.
sapo: tensor of shape ``(batch_size, num_candidates, sapo_length)``.
sapo_mask: tensor of shape ``(batch_size, num_candidates, sapo_length)``.
his_sapo: tensor of shape ``(batch_size, num_clicked_news, sapo_length)``.
his_sapo_mask: tensor of shape ``(batch_size, num_clicked_news, sapo_length)``.
category: tensor of shape ``(batch_size, num_candidates)``.
his_category: tensor of shape ``(batch_size, num_clicked_news)``.
Returns:
tuple
- multi_user_interest: tensor of shape ``(batch_size, num_context_codes, embed_dim)``
- matching_scores: tensor of shape ``(batch_size, num_candidates)``
"""
batch_size = title.shape[0]
num_candidates = title.shape[1]
his_length = his_title.shape[1]
# Representation of candidate news
title = title.view(batch_size * num_candidates, -1)
title_mask = title_mask.view(batch_size * num_candidates, -1)
sapo = sapo.view(batch_size * num_candidates, -1)
sapo_mask = sapo_mask.view(batch_size * num_candidates, -1)
candidate_repr = self.news_encoder(title_encoding=title, title_attn_mask=title_mask, sapo_encoding=sapo,
sapo_attn_mask=sapo_mask)
candidate_repr = candidate_repr.view(batch_size, num_candidates, -1)
# Representation of history clicked news
his_title = his_title.view(batch_size * his_length, -1)
his_title_mask = his_title_mask.view(batch_size * his_length, -1)
his_sapo = his_sapo.view(batch_size * his_length, -1)
his_sapo_mask = his_sapo_mask.view(batch_size * his_length, -1)
history_repr = self.news_encoder(title_encoding=his_title, title_attn_mask=his_title_mask,
sapo_encoding=his_sapo, sapo_attn_mask=his_sapo_mask)
history_repr = history_repr.view(batch_size, his_length, -1)
if self.use_category_bias:
his_category_embed = self.category_embedding(his_category)
his_category_embed = self.category_dropout(his_category_embed)
candidate_category_embed = self.category_embedding(category)
candidate_category_embed = self.category_dropout(candidate_category_embed)
category_bias = pairwise_cosine_similarity(his_category_embed, candidate_category_embed)
multi_user_interest = self.poly_attn(embeddings=history_repr, attn_mask=his_mask, bias=category_bias)
else:
multi_user_interest = self.poly_attn(embeddings=history_repr, attn_mask=his_mask, bias=None)
# Click predictor
matching_scores = torch.matmul(candidate_repr, multi_user_interest.permute(0, 2, 1))
if self.score_type == 'max':
matching_scores = matching_scores.max(dim=2)[0]
elif self.score_type == 'mean':
matching_scores = matching_scores.mean(dim=2)
elif self.score_type == 'weighted':
matching_scores = self.target_aware_attn(query=multi_user_interest, key=candidate_repr,
value=matching_scores)
else:
raise ValueError('Invalid method of aggregating matching score')
return multi_user_interest, matching_scores
class PolyAttention(nn.Module):
r"""
Implementation of Poly attention scheme that extracts `K` attention vectors through `K` additive attentions
"""
def __init__(self, in_embed_dim: int, num_context_codes: int, context_code_dim: int):
r"""
Initialization
Args:
in_embed_dim: The number of expected features in the input ``embeddings``
num_context_codes: The number of attention vectors ``K``
context_code_dim: The number of features in a context code
"""
super().__init__()
self.linear = nn.Linear(in_features=in_embed_dim, out_features=context_code_dim, bias=False)
self.context_codes = nn.Parameter(nn.init.xavier_uniform_(torch.empty(num_context_codes, context_code_dim),
gain=nn.init.calculate_gain('tanh')))
def forward(self, embeddings: Tensor, attn_mask: Tensor, bias: Tensor = None):
r"""
Forward propagation
Args:
embeddings: tensor of shape ``(batch_size, his_length, embed_dim)``
attn_mask: tensor of shape ``(batch_size, his_length)``
bias: tensor of shape ``(batch_size, his_length, num_candidates)``
Returns:
A tensor of shape ``(batch_size, num_context_codes, embed_dim)``
"""
proj = torch.tanh(self.linear(embeddings))
if bias is None:
weights = torch.matmul(proj, self.context_codes.T)
else:
bias = bias.mean(dim=2).unsqueeze(dim=2)
weights = torch.matmul(proj, self.context_codes.T) + bias
weights = weights.permute(0, 2, 1)
weights = weights.masked_fill_(~attn_mask.unsqueeze(dim=1), 1e-30)
weights = torch_f.softmax(weights, dim=2)
poly_repr = torch.matmul(weights, embeddings)
return poly_repr
class TargetAwareAttention(nn.Module):
"""Implementation of target-aware attention network"""
def __init__(self, embed_dim: int):
r"""
Initialization
Args:
embed_dim: The number of features in query and key vectors
"""
super().__init__()
self.linear = nn.Linear(in_features=embed_dim, out_features=embed_dim, bias=False)
def forward(self, query: Tensor, key: Tensor, value: Tensor):
r"""
Forward propagation
Args:
query: tensor of shape ``(batch_size, num_context_codes, embed_dim)``
key: tensor of shape ``(batch_size, num_candidates, embed_dim)``
value: tensor of shape ``(batch_size, num_candidates, num_context_codes)``
Returns:
tensor of shape ``(batch_size, num_candidates)``
"""
proj = torch_f.gelu(self.linear(query))
weights = torch_f.softmax(torch.matmul(key, proj.permute(0, 2, 1)), dim=2)
outputs = torch.mul(weights, value).sum(dim=2)
return outputs