Skip to content

added VICReg losses#12

Open
drscotthawley wants to merge 2 commits into
Harmonai-org:mainfrom
drscotthawley:main
Open

added VICReg losses#12
drscotthawley wants to merge 2 commits into
Harmonai-org:mainfrom
drscotthawley:main

Conversation

@drscotthawley

Copy link
Copy Markdown
Collaborator

This adds a few very simple functions that implement VICReg.

If the style of writing these are not to your liking, let me know how I can be more compliant with the style

-- e.g. I didn't find it necessary to create nn.Module Classes for these since they are so simple, but I could so.

Comment thread oobleck/losses.py
# These two losses offer a sort of "repulsion" regularization, across the batch dimension, to keep the
# space from collapse. Intended to be connected to the latents in the middle of the autoencoder..

def vicreg_var_loss(z, gamma=1, eps=1e-4):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These need to made into instantiable "loss modules" like the ones above. Also please add a test for them.

@drscotthawley

Copy link
Copy Markdown
Collaborator Author

@zqevans take a look at the new version; not sure I wrote the module class correctly.

Comment thread oobleck/losses.py
def forward(self, inputs: TensorDict) -> TensorDict:
z = inputs['latent']
# writing with self.... "hooks" to enable extraction of individual losses for logging
self.var_loss = vicreg_var_loss(inputs, gamma=self.gamma, eps=self.eps)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't store values in instance variables like this, doesn't make sense semantically.

If you want to return the individual losses, you can do so in the inputs.update() call on line 217.

Comment thread oobleck/losses.py
inputs = accumulate_value(
inputs,
{
"vicreg_loss": self.weight * loss

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be "generator_loss". This line adds the vic_reg loss to the overall accumulated loss while the inputs.update() line below is used to return the individual named losses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants