InfoNCE
PyTorch implementation of the InfoNCE loss from “Representation Learning with Contrastive Predictive Coding”. In contrastive learning, we want to learn how to map high dimensional data to a lower dimensional embedding space. This mapping should place semantically similar samples close together in the embedding space, whilst placing semantically distinct samples further apart. The InfoNCE loss function can be used for the purpose of contrastive learning.
This package is available on PyPI and can be installed via:
pip install info-nce-pytorch
Example usage
Can be used without explicit negative keys, whereby each sample is compared with the other samples in the batch.
loss = InfoNCE()
batch_size, embedding_size = 32, 128
query = torch.randn(batch_size, embedding_size)
positive_key = torch.randn(batch_size, embedding_size)
output = loss(query, positive_key)
Can be used with negative keys, whereby every combination between query and negative key is compared.
loss = InfoNCE(negative_mode='unpaired') # negative_mode='unpaired' is the default value
batch_size, num_negative, embedding_size = 32, 48, 128
query = torch.randn(batch_size, embedding_size)
positive_key = torch.randn(batch_size, embedding_size)
negative_keys = torch.randn(num_negative, embedding_size)
output = loss(query, positive_key, negative_keys)
Can be used with negative keys, whereby each query sample is compared with only the negative keys it is paired with.
loss = InfoNCE(negative_mode='paired')
batch_size, num_negative, embedding_size = 32, 6, 128
query = torch.randn(batch_size, embedding_size)
positive_key = torch.randn(batch_size, embedding_size)
negative_keys = torch.randn(batch_size, num_negative, embedding_size)
output = loss(query, positive_key, negative_keys)
Loss graph
Suppose we have some initial mean vectors µ_q, µ_p, µ_n and a covariance matrix Σ = I/10, then we can plot the value of the InfoNCE loss by sampling from distributions with interpolated mean vectors. Given interpolation weights α and β, we define the distribution Q ~ N(µ_q, Σ) for the query samples, the distribution P_α ~ N(αµ_q + (1-α)µ_p, Σ) for the positive samples and the distribution N_β ~ N(βµ_q + (1-β)µ_n, Σ) for the negative samples. Shown below is the value of the loss with inputs sampled from the distributions defined above for different values of α and β.
Metadata
Release files for info-nce-pytorch 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| info-nce-pytorch-0.1.4.tar.gz | 4.3 kB | Without provenance | Details for info-nce-pytorch-0.1.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| info_nce_pytorch-0.1.4-py3-none-any.whl | Python 3 | none | any | Details for info_nce_pytorch-0.1.4-py3-none-any.whl |
Total release size: 9.1 kB
Release files / info-nce-pytorch-0.1.4.tar.gz
| Download URL | info-nce-pytorch-0.1.4.tar.gz |
|---|---|
| Size | 4.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e1c3019be1238dd42cb2a6f71825483869a78340585a6772b154fd134d2b48c5
|
|
BLAKE2b-256 checksum How to use checksums |
1a6139bc456f50428f74a95f37c020dfb2ee70954dbec81cc7e9b97f50305a62
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
|
Release files / info_nce_pytorch-0.1.4-py3-none-any.whl
| Download URL | info_nce_pytorch-0.1.4-py3-none-any.whl |
|---|---|
| Size | 4.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1dd232e7503828bdee1bd7ea29d92ac4b3c6c5d15e53adbdb1830574d992ba79
|
|
BLAKE2b-256 checksum How to use checksums |
8ec6435455387a6c32fa10be9311affd5dc9bc6208cb80f1f9cf5b84ac5c58a5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
|
Security for info-nce-pytorch 0.1.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.