注意
您正在阅读 MMSelfSup 0.x 版本的文档,而 MMSelfSup 0.x 版本将会在 2022 年末 开始逐步停止维护。我们建议您及时升级到 MMSelfSup 1.0.0rc 版本,享受由 OpenMMLab 2.0 带来的更多新特性和更佳的性能表现。阅读 MMSelfSup 1.0.0rc 的 发版日志, 代码 和 文档 获取更多信息。
mmselfsup.models.heads.contrastive_head 源代码
# Copyright (c) OpenMMLab. All rights reserved.
import torch
import torch.nn as nn
from mmcv.runner import BaseModule
from ..builder import HEADS
[文档]@HEADS.register_module()
class ContrastiveHead(BaseModule):
"""Head for contrastive learning.
The contrastive loss is implemented in this head and is used in SimCLR,
MoCo, DenseCL, etc.
Args:
temperature (float): The temperature hyper-parameter that
controls the concentration level of the distribution.
Defaults to 0.1.
"""
def __init__(self, temperature=0.1):
super(ContrastiveHead, self).__init__()
self.criterion = nn.CrossEntropyLoss()
self.temperature = temperature
[文档] def forward(self, pos, neg):
"""Forward function to compute contrastive loss.
Args:
pos (Tensor): Nx1 positive similarity.
neg (Tensor): Nxk negative similarity.
Returns:
dict[str, Tensor]: A dictionary of loss components.
"""
N = pos.size(0)
logits = torch.cat((pos, neg), dim=1)
logits /= self.temperature
labels = torch.zeros((N, ), dtype=torch.long).to(pos.device)
losses = dict()
losses['loss'] = self.criterion(logits, labels)
return losses