Skip to main content

This is the code of the paper Stochastic smoothing of the top-K calibrated hinge loss for deep imbalanced classification published at ICML2022. If you use this code for your work, please cite the paper:

@inproceedings{pmlr-v162-garcin22a,
  title = 	 {Stochastic smoothing of the top-K calibrated hinge loss for deep imbalanced classification},
  author =       {Garcin, Camille and Servajean, Maximilien and Joly, Alexis and Salmon, Joseph},
  booktitle = 	 {Proceedings of the 39th International Conference on Machine Learning},
  pages = 	 {7208--7222},
  year = 	 {2022},
  volume = 	 {162},
  series = 	 {Proceedings of Machine Learning Research},
  month = 	 {17--23 Jul},
  publisher =    {PMLR},
}

The pytopk package

The pytopk package contains the code for the balanced and imbalanced top-k losses as well as a differentiable top-k function.

Installation

It can be installed as follows:

pip install pytopk

Top-k losses

Our losses can be used as standard pytorch loss functions:

import torch
from pytopk import BalNoisedTopK, ImbalNoisedTopK

scores = torch.tensor([[2.0, 1.5, -3.0],
                       [7.5, 4.0, -1.5]])
labels = torch.tensor([0, 2])

k = 2

criteria_bal = BalNoisedTopK(k=k, epsilon=1.0)
criteria_imbal = ImbalNoisedTopK(k=k, epsilon=0.01, max_m=0.3, cls_num_list=[17, 23, 55])

loss_batch_bal = criteria_bal(scores, labels)
loss_batch_imbal = criteria_imbal(scores, labels)

Smooth top-k function

We also provide a differentiable top-k function for tensors of any size that can be plugged into any neural network architecture:

import torch
from pytopk import NoisedTopK

smooth_topk = NoisedTopK(k=3, dim=-1)
x = torch.tensor([[-1.5, 2.0, 0.7, 3.8],
                  [-1.1, -5.4, 0.1, 2.3]], requires_grad=True)
out = smooth_topk(x)
print(out)

>> tensor([ 0.4823, -1.4710], grad_fn=<_NoisedTopKBackward>)

out.sum().backward()
print(x.grad)

>> tensor([[0.0000, 0.4000, 0.6000, 0.0000],
          [0.8000, 0.0000, 0.2000, 0.0000]])

Metadata

Release files for pytopk 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytopk 0.1.0
File Size Uploaded Provenance Actions
pytopk-0.1.0.tar.gz 5.7 kB Without provenance Details for pytopk-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pytopk 0.1.0
File Interpreter ABI Platform Actions
pytopk-0.1.0-py3-none-any.whl Python 3 none any Details for pytopk-0.1.0-py3-none-any.whl

Total release size: 12.6 kB

Release files / pytopk-0.1.0.tar.gz

Download URL pytopk-0.1.0.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
606b39278281139b25016275219fb3f1c65ac22a0c6d9312c63e77029539ab10
BLAKE2b-256 checksum
How to use checksums
d95e1d4cc247572706d70d8c0457deb4016d27ebbbff2dc49be480d9dbbc9369
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.2

Release files / pytopk-0.1.0-py3-none-any.whl

Download URL pytopk-0.1.0-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
41baec45ec321c2bb99c9e9a1d12a597432f077d1f0c9c63579b6d9f3a92ac24
BLAKE2b-256 checksum
How to use checksums
d785704f37680d310bec09bdb0f7f08d68feeed6da6799371028435aa915fac2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.2

Security for pytopk 0.1.0

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page