Skip to main content

Pytorch-based InfoNCE loss for self-supervised learning

Project description

InfoNCE Loss - PyTorch Implementation

This repository contains a PyTorch implementation of the InfoNCE loss. The implementation supports both unsupervised and supervised InfoNCE loss.

Classes

  • InfoNCE: Positive pairs are generated by using two independent augmentations of the same batch.
  • SupervisedInfoNCE: Positive pairs are defined using class labels, ensuring that negative pairs are not from the same class.

Installation

To install the package , run:

pip install infonce

Usage

Unsupervised InfoNCE

from infonce import InfoNCELoss

# Example usage
loss_fn = InfoNCELoss(temperature=0.07)
# augmentation1 and augmentation2 are two different augmentations of the same batch
features1 = model(augmentations1) 
features2 = model(augmentations2) 
features = torch.stack([features1, features2], dim=0)
loss = loss_fn(features)

Supervised InfoNCE

from infonce import SupervisedInfoNCELoss

# Example usage
loss_fn = SupervisedInfoNCELoss(temperature=0.07)
loss = loss_fn(features, labels)

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

This project is licensed under the MIT License.

Acknowledgements

This implementation is inspired by the original InfoNCE loss paper and various open-source implementations.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

infonce-0.2.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

infonce-0.2-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file infonce-0.2.tar.gz.

File metadata

  • Download URL: infonce-0.2.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.5

File hashes

Hashes for infonce-0.2.tar.gz
Algorithm Hash digest
SHA256 81f64efa103392333ba5bb887662ebd5b3d5b647a982db9f3ba7ca711c499628
MD5 704a93497a591730a77d4d0841434099
BLAKE2b-256 56d98c7d2ac7de3027b1ed06e0bd3a911cdf7e4dc1f985f77a0842887f868637

See more details on using hashes here.

File details

Details for the file infonce-0.2-py3-none-any.whl.

File metadata

  • Download URL: infonce-0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.5

File hashes

Hashes for infonce-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9d183309ec06ea08918d875f07cf676a22024f9ccd4cafeb2dfbf4709932372c
MD5 811cb64a44c7214c35f5dce52d017b2f
BLAKE2b-256 ba2db467b654e90d78c9a77c4cec00f0f46f5ac240cceded99ee794a443b5f0f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page