Skip to main content

Pytorch-based InfoNCE loss for self-supervised learning

Project description

InfoNCE Loss - PyTorch Implementation

The InfoNCE loss (Information Noise-Contrastive Estimation) is commonly used in contrastive learning to maximize the similarity between positive pairs while minimizing it between negative pairs. This repository provides a PyTorch implementation supporting both unsupervised and supervised modes.

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.3.tar.gz (3.3 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.3-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for infonce-0.3.tar.gz
Algorithm Hash digest
SHA256 212d50db6a665bc062a3ffd22e445716f47aaea11768b712507ef5ace5b8767b
MD5 31bbac58c35b48faa60c9fa509afdcd7
BLAKE2b-256 fe52ec3247e684536a7ee39d42352d3e57ffa626ec70d837a72c6592a2eeb806

See more details on using hashes here.

File details

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

File metadata

  • Download URL: infonce-0.3-py3-none-any.whl
  • Upload date:
  • Size: 4.2 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 cf7b488c59f4b3a8282ca33827fd04aa9599199ea50a3f2f50559c6602681745
MD5 f546d96d98affba0556edca7c543a989
BLAKE2b-256 c189408037884f3587abb79bb4bf33146407a68b2441be18844d445fe7832c7f

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