-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathloss_functions.py
More file actions
189 lines (166 loc) · 6.98 KB
/
Copy pathloss_functions.py
File metadata and controls
189 lines (166 loc) · 6.98 KB
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
# -*- coding: utf-8 -*-
import torch
import torch.nn as nn
import numpy as np
l2_loss = nn.MSELoss(reduce=False)
cross_entropy_func = nn.CrossEntropyLoss()
cross_entropy_func_none = nn.CrossEntropyLoss(reduction='none')
class Similarity(nn.Module):
"""
Dot product or cosine similarity
"""
def __init__(self, temp):
super().__init__()
self.temp = temp
assert temp > 0
self.cos = nn.CosineSimilarity(dim=-1)
def forward(self, x, y):
return self.cos(x, y) / self.temp
def l2_similarity(embed1, embed2, mask):
"""
Args:
embed1: shape [N, n_domain, latent_dim]
embed2: shape [N, n_domain, latent_dim]
mask: domain-level mask: [N, n_domain]
return:
l2 reconstruction loss between embedding a and b
"""
loss = l2_loss(embed1, embed2)
loss = loss.sum(axis=-1)
if mask.sum() == 0: # if no mask then reconstruction loss is meaningless.
loss = (loss * mask).sum() / (mask.sum() + 1)
else:
loss = (loss * mask).sum() / mask.sum()
return loss
def contrastive_loss_domain(embed_org, embed_aug, mask, sim, device):
"""
Domain-level contrastive loss
Args:
embed_org (tensor [N, 5, latent_dim]), original user embedding in all 5 domains
embed_aug (tensor [N, 5, latent_dim]), generated user embedding
(masked domains are replaced with generated embedding)
mask (tensor [N, 5]), 0-1 valued, where 1 represents the corresponding domain is masked.
sim (Similarity), similarity function
device
"""
embed_org_splits = [torch.squeeze(val) for val in torch.split(embed_org, 1, dim=1)]
embed_aug_splits = [torch.squeeze(val) for val in torch.split(embed_aug, 1, dim=1)]
loss = 0
for i in range(len(embed_aug_splits)): # for each domain
loss = loss + contrastive_loss_sample_N(embed_org_splits[i], embed_aug_splits[i], mask[:, i], sim, device)
loss = loss / len(embed_aug_splits)
return loss
def contrastive_loss_sample_2N(embed_org, embed_aug, mask, sim, device):
"""
Sample-level contrastive loss
Args:
embed_org (tensor [N, latent_dim]), original user embedding in all 5 domains
embed_aug (tensor [N, latent_dim]), generated user embedding
(masked domains are replaced with generated embedding)
mask (tensor [N, 5]), 0-1 valued, where 1 represents the corresponding domain is masked.
sim (Similarity), similarity function
device
"""
# 1. concat embed_org and embed_aug --> embed_all
embed_all = torch.cat((embed_org, embed_aug), dim=0) # [2N, dim]
# 2. cosine_similarity: mat_mul(embed_all, transpose(embed_all)) --> [bs * 2, bs*2]
sim_scores = sim(embed_all.unsqueeze(1), embed_all.clone().unsqueeze(0))
# 3. remove diagonal values pair(i,i) (make it -1e9 or other equivalents)
sim_scores.fill_diagonal_(-1e9) # before cross-entropy
# 4. ground truth pair: if index < batch_size: (index; index + batch_size) else: (index, index-batch_size)
labels = torch.from_numpy((np.arange(embed_all.size(0)) + embed_org.size(0)) % embed_all.size(0)).long().to(device)
# only sample and its mask -> generated sample are positive pairs.
# 6. cross-entropy loss with logits, remove samples are have no mask
loss = cross_entropy_func_none(sim_scores, labels) # [2N]
if len(mask.size()) > 1: # 2D mask
mask = (mask.sum(dim=1) > 0).to(float)
else:
mask = mask.to(float)
if mask.sum() > 0:
loss = (loss * mask).sum() / mask.sum()
else: # there is no mask in the whole batch.
loss = 0
return loss
def contrastive_loss_sample_N(embed_org, embed_aug, mask, sim, device='cuda'):
"""
Sample-level contrastive loss
Args:
embed_org (tensor [N, latent_dim]), original user embedding in all 5 domains
embed_aug (tensor [N, latent_dim]), generated user embedding
(masked domains are replaced with generated embedding)
mask (tensor [N, 5]), 0-1 valued, where 1 represents the corresponding domain is masked.
sim (Similarity), similarity function
device:
"""
sim_scores_1 = sim(embed_org.unsqueeze(1), embed_aug.unsqueeze(0))
sim_scores_2 = sim(embed_aug.unsqueeze(1), embed_org.unsqueeze(0))
labels = torch.arange(embed_org.size(0)).long().to(device)
# cross-entropy loss with logits, remove samples are have no mask
loss_1 = cross_entropy_func_none(sim_scores_1, labels) # [N]
loss_2 = cross_entropy_func_none(sim_scores_2, labels) # [N]
loss = (loss_1 + loss_2) / 2 # positional averaging
if len(mask.size()) > 1: # 2D mask
mask = (mask.sum(dim=1) > 0).to(float)
else:
mask = mask.to(float)
if mask.sum() > 0:
loss = (loss * mask).sum() / mask.sum()
else: # there is no mask in the whole batch.
loss = 0
return loss
########################
# Recommendation Metrics
########################
def getLabel(test_data, pred_data):
r = []
for i in range(len(test_data)):
groundTrue = test_data[i]
predictTopK = pred_data[i]
pred = list(map(lambda x: x in groundTrue, predictTopK))
pred = np.array(pred).astype("float")
r.append(pred)
return np.array(r).astype('float')
def RecallPrecision_ATk(test_data, r, k):
"""
test_data should be a list? cause users may have different amount of pos items. shape (test_batch, k)
pred_data : shape (test_batch, k) NOTE: pred_data should be pre-sorted
k : top-k
"""
right_pred = r[:, :k].sum(1)
precis_n = k
recall_n = np.array([len(test_data[i]) for i in range(len(test_data))])
recall = np.sum(right_pred / recall_n)
precis = np.sum(right_pred) / precis_n
return {'recall': recall, 'precision': precis}
def NDCGatK_r(test_data, r, k):
"""
Normalized Discounted Cumulative Gain
rel_i = 1 or 0, so 2^{rel_i} - 1 = 1 or 0
"""
assert len(r) == len(test_data)
pred_data = r[:, :k]
test_matrix = np.zeros((len(pred_data), k))
for i, items in enumerate(test_data):
length = k if k <= len(items) else len(items)
test_matrix[i, :length] = 1
max_r = test_matrix
idcg = np.sum(max_r * 1. / np.log2(np.arange(2, k + 2)), axis=1)
dcg = pred_data * (1. / np.log2(np.arange(2, k + 2)))
dcg = np.sum(dcg, axis=1)
idcg[idcg == 0.] = 1.
ndcg = dcg / idcg
ndcg[np.isnan(ndcg)] = 0.
return np.sum(ndcg)
def metrics_batch(batch_result):
sorted_items = batch_result[0].numpy()
groundTrue = batch_result[1]
r = getLabel(groundTrue, sorted_items)
pre, recall, ndcg = [], [], []
for k in [5, 10, 20, 50]:
ret = RecallPrecision_ATk(groundTrue, r, k)
pre.append(ret['precision'])
recall.append(ret['recall'])
ndcg.append(NDCGatK_r(groundTrue, r, k))
return {'recall': np.array(recall),
'precision': np.array(pre),
'ndcg': np.array(ndcg)}