-
Notifications
You must be signed in to change notification settings - Fork 286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added directclr loss #963
base: master
Are you sure you want to change the base?
Added directclr loss #963
Changes from 1 commit
e3a19e2
2cdcbf8
c536318
9480157
5bf1eba
05dab52
1dc252d
1928cbc
35a7bb9
9f2421c
1f97ef5
d48afe2
543cf9b
2dc7804
535a2f5
95d419a
2d8b25e
20b71f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from cProfile import label | ||
import torch | ||
import torch.nn as nn | ||
|
||
#Adapted from https://github.com/facebookresearch/directclr/blob/main/directclr/main.py | ||
class InfoNCELoss(nn.Module): | ||
"""Implementation of InfoNCELoss as required for DIRECTCLR""" | ||
def __init__(self, dim:int ,temprature:float = 0.1): | ||
Atharva-Phatak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Parameters | ||
Args: | ||
Atharva-Phatak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
dim : Dimension of subvector to be used to compute InfoNCELoss. | ||
temprature: The value used to scale logits. | ||
""" | ||
self.temprature = temprature | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo, it's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry :( |
||
#dimension of subvector sent to infoNCE | ||
self.dim = dim | ||
|
||
def normalize(self, x:torch.Tensor) -> torch.Tensor: | ||
Atharva-Phatak marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would raise the question if it's necessary to put this in its own function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well technically not but I would avoid writing |
||
"""Function to normalize the tensor | ||
Args: | ||
x : The torch tensor to be normalized. | ||
Atharva-Phatak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
return nn.functional.normalize(x, dim = 1) | ||
|
||
def compute_loss(self, z1:torch.Tensor, z2:torch.Tensor) -> torch.Tensor: | ||
"""Method to compute InfoNCELoss | ||
Args: | ||
z1,z2 : The representations from the encoder. | ||
Atharva-Phatak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
z1 = self.normalize(z1) | ||
z2 = self.normalize(z2) | ||
#DDP step | ||
logits = z1 @ z2.T | ||
logits = logits/self.temprature | ||
labels = torch.arange(0, z2.shape[0]).type_as(logits) | ||
loss = torch.nn.functional.cross_entropy(logits, labels) | ||
return loss | ||
|
||
def forward(self, z1:torch.Tensor, z2:torch.Tensor) -> torch.Tensor: | ||
"""Forward Pass for InfoNCE computation""" | ||
z1 = z1[:, :self.dim] | ||
z2 = z2[:, :self.dim] | ||
loss = self.compute_loss(z1, z2) + self.compute_loss(z2, z1) | ||
return loss / 2 | ||
|
||
__all__ = ["InfoNCELoss"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can leave the reference to
DIRECTCLR
away here.