forked from obananas/GMAE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloss.py
More file actions
39 lines (34 loc) · 1.85 KB
/
Copy pathloss.py
File metadata and controls
39 lines (34 loc) · 1.85 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
import torch
def contrastive_loss(args, hidden, nbr_idx, neg_idx, idx):
if not args.do_contrast:
return 0
loss_con = 0
# 遍历计算正负样本对的对比损失
for i in range(len(idx)):
index = idx[i]
if int(index) < len(idx) - 1:
# 获取正样本表示
hidden_positive = hidden[nbr_idx[index]]
# 计算正样本和当前样本的余弦相似度,并取其指数值
positive = torch.exp(torch.cosine_similarity(hidden[i].unsqueeze(0), hidden_positive.detach()))
# 获取负样本表示
hidden_negative = hidden[neg_idx[index]]
# 计算负样本和当前样本的余弦相似度,并取其指数值
negative = torch.exp(torch.cosine_similarity(hidden[i].unsqueeze(0), hidden_negative.detach())).sum()
# 计算对比损失:对比正样本和负样本的相似度,最大化正样本与负样本的差异
loss_con -= torch.log((positive / negative)).sum()
# 清除 GPU 缓存,避免内存溢出
torch.cuda.empty_cache()
# 返回平均对比损失(取决于样本数量 idx 的长度)
return loss_con / len(idx)
# 正交损失函数(Orthogonal Loss)用于使共享潜在表示与每个视图的特定潜在表示之间的相关性最小化
def orthogonal_loss(shared, specific):
# 计算共享潜在表示的均值并减去均值
_shared = shared.detach()
_shared = _shared - _shared.mean(dim=0)
# 计算共享表示和特定表示之间的相关性矩阵
correlation_matrix = _shared.t().matmul(specific)
# 计算相关性矩阵的 L1 范数(即矩阵元素的绝对值之和)
norm = torch.norm(correlation_matrix, p=1)
# 返回正交损失,鼓励共享表示和特定表示之间的正交性
return norm