Module minder_utils.models.feature_extractors.simclr.loss
Expand source code
import torch
import numpy as np
class NTXentLoss(torch.nn.Module):
    def __init__(self, device, temperature, use_cosine_similarity):
        super(NTXentLoss, self).__init__()
        self.temperature = temperature
        self.device = device
        self.softmax = torch.nn.Softmax(dim=-1)
        self.similarity_function = self._get_similarity_function(use_cosine_similarity)
        self.criterion = torch.nn.CrossEntropyLoss(reduction="sum")
    def _get_similarity_function(self, use_cosine_similarity):
        if use_cosine_similarity:
            self._cosine_similarity = torch.nn.CosineSimilarity(dim=-1)
            return self._cosine_simililarity
        else:
            return self._dot_simililarity
    def _get_correlated_mask(self, batch_size):
        diag = np.eye(2 * batch_size)
        l1 = np.eye((2 * batch_size), 2 * batch_size, k=-batch_size)
        l2 = np.eye((2 * batch_size), 2 * batch_size, k=batch_size)
        mask = torch.from_numpy((diag + l1 + l2))
        mask = (1 - mask).type(torch.bool)
        return mask.to(self.device)
    @staticmethod
    def _dot_simililarity(x, y):
        v = torch.tensordot(x.unsqueeze(1), y.T.unsqueeze(0), dims=2)
        # x shape: (N, 1, C)
        # y shape: (1, C, 2N)
        # v shape: (N, 2N)
        return v
    def _cosine_simililarity(self, x, y):
        # x shape: (N, 1, C)
        # y shape: (1, 2N, C)
        # v shape: (N, 2N)
        v = self._cosine_similarity(x.unsqueeze(1), y.unsqueeze(0))
        return v
    def forward(self, zis, zjs):
        batch_size = zjs.size(0)
        mask_samples_from_same_repr = self._get_correlated_mask(batch_size).type(torch.bool)
        representations = torch.cat([zjs, zis], dim=0)
        similarity_matrix = self.similarity_function(representations, representations)
        # filter out the scores from the positive samples
        l_pos = torch.diag(similarity_matrix, batch_size)
        r_pos = torch.diag(similarity_matrix, -batch_size)
        positives = torch.cat([l_pos, r_pos]).view(2 * batch_size, 1)
        negatives = similarity_matrix[mask_samples_from_same_repr].view(2 * batch_size, -1)
        logits = torch.cat((positives, negatives), dim=1)
        logits /= self.temperature
        labels = torch.zeros(2 * batch_size).to(self.device).long()
        loss = self.criterion(logits, labels)
        return loss / (2 * batch_size)Classes
- class NTXentLoss (device, temperature, use_cosine_similarity)
- 
Base class for all neural network modules. Your models should also subclass this class. Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes:: import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self): super(Model, self).__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))Submodules assigned in this way will be registered, and will have their parameters converted too when you call :meth: to, etc.:ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool Initializes internal Module state, shared by both nn.Module and ScriptModule. Expand source codeclass NTXentLoss(torch.nn.Module): def __init__(self, device, temperature, use_cosine_similarity): super(NTXentLoss, self).__init__() self.temperature = temperature self.device = device self.softmax = torch.nn.Softmax(dim=-1) self.similarity_function = self._get_similarity_function(use_cosine_similarity) self.criterion = torch.nn.CrossEntropyLoss(reduction="sum") def _get_similarity_function(self, use_cosine_similarity): if use_cosine_similarity: self._cosine_similarity = torch.nn.CosineSimilarity(dim=-1) return self._cosine_simililarity else: return self._dot_simililarity def _get_correlated_mask(self, batch_size): diag = np.eye(2 * batch_size) l1 = np.eye((2 * batch_size), 2 * batch_size, k=-batch_size) l2 = np.eye((2 * batch_size), 2 * batch_size, k=batch_size) mask = torch.from_numpy((diag + l1 + l2)) mask = (1 - mask).type(torch.bool) return mask.to(self.device) @staticmethod def _dot_simililarity(x, y): v = torch.tensordot(x.unsqueeze(1), y.T.unsqueeze(0), dims=2) # x shape: (N, 1, C) # y shape: (1, C, 2N) # v shape: (N, 2N) return v def _cosine_simililarity(self, x, y): # x shape: (N, 1, C) # y shape: (1, 2N, C) # v shape: (N, 2N) v = self._cosine_similarity(x.unsqueeze(1), y.unsqueeze(0)) return v def forward(self, zis, zjs): batch_size = zjs.size(0) mask_samples_from_same_repr = self._get_correlated_mask(batch_size).type(torch.bool) representations = torch.cat([zjs, zis], dim=0) similarity_matrix = self.similarity_function(representations, representations) # filter out the scores from the positive samples l_pos = torch.diag(similarity_matrix, batch_size) r_pos = torch.diag(similarity_matrix, -batch_size) positives = torch.cat([l_pos, r_pos]).view(2 * batch_size, 1) negatives = similarity_matrix[mask_samples_from_same_repr].view(2 * batch_size, -1) logits = torch.cat((positives, negatives), dim=1) logits /= self.temperature labels = torch.zeros(2 * batch_size).to(self.device).long() loss = self.criterion(logits, labels) return loss / (2 * batch_size)Ancestors- torch.nn.modules.module.Module
 Class variables- var dump_patches : bool
- var training : bool
 Methods- def forward(self, zis, zjs) ‑> Callable[..., Any]
- 
Defines the computation performed at every call. Should be overridden by all subclasses. Note Although the recipe for forward pass needs to be defined within this function, one should call the :class: Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.Expand source codedef forward(self, zis, zjs): batch_size = zjs.size(0) mask_samples_from_same_repr = self._get_correlated_mask(batch_size).type(torch.bool) representations = torch.cat([zjs, zis], dim=0) similarity_matrix = self.similarity_function(representations, representations) # filter out the scores from the positive samples l_pos = torch.diag(similarity_matrix, batch_size) r_pos = torch.diag(similarity_matrix, -batch_size) positives = torch.cat([l_pos, r_pos]).view(2 * batch_size, 1) negatives = similarity_matrix[mask_samples_from_same_repr].view(2 * batch_size, -1) logits = torch.cat((positives, negatives), dim=1) logits /= self.temperature labels = torch.zeros(2 * batch_size).to(self.device).long() loss = self.criterion(logits, labels) return loss / (2 * batch_size)