sparsemax
A PyTorch implementation of SparseMax (https://arxiv.org/pdf/1602.02068.pdf) with gradients checked and tested
Sparsemax is an alternative to softmax when one wants to generate hard probability distributions. It has been used to great effect in recent papers like ProtoAttend (https://arxiv.org/pdf/1902.06292v4.pdf).
Installation
pip install -U sparsemax
Usage
Use as if it was nn.Softmax()! Nice and simple.
from sparsemax import Sparsemax
import torch
import torch.nn as nn
sparsemax = Sparsemax(dim=-1)
softmax = torch.nn.Softmax(dim=-1)
logits = torch.randn(2, 3, 5)
logits.requires_grad = True
print("\nLogits")
print(logits)
softmax_probs = softmax(logits)
print("\nSoftmax probabilities")
print(softmax_probs)
sparsemax_probs = sparsemax(logits)
print("\nSparsemax probabilities")
print(sparsemax_probs)
Advantages over existing implementations
This repo borrows heavily from: https://github.com/KrisKorrel/sparsemax-pytorch
However, there are a few key advantages:
Backward pass equations implemented natively as a
torch.autograd.Function, resulting in 30% speedup, compared to the above repository.The package is easily pip-installable (no need to copy the code).
The package works for multi-dimensional tensors, operating over any axis.
The operator forward and backward passes are tested (backward-pass check due to
torch.autograd.gradcheck
Check that gradients are computed correctly
from torch.autograd import gradcheck
from sparsemax import Sparsemax
input = (torch.randn(6, 3, 20,dtype=torch.double,requires_grad=True))
test = gradcheck(sparsemax, input, eps=1e-6, atol=1e-4)
print(test)
Credits
This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.
History
0.1.0 (2020-05-25)
First release on PyPI.
Metadata
Release files for sparsemax 0.1.9
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 | |
|---|---|---|---|---|
| sparsemax-0.1.9.tar.gz | 12.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sparsemax-0.1.9-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 17.6 kB
Release files / sparsemax-0.1.9.tar.gz
| Download URL | sparsemax-0.1.9.tar.gz |
|---|---|
| Size | 12.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
85fe08d08900cbf2a0259e7925f88f59e7fe725e8981236c8b14e239b47f0f17
|
|
BLAKE2b-256 checksum How to use checksums |
d44afe026840c0b6a7dca0741d9bdadc9c86fa132e21573679c3544bc35c0812
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.0
|
Release files / sparsemax-0.1.9-py2.py3-none-any.whl
| Download URL | sparsemax-0.1.9-py2.py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
2e7191933652dea3df223079efdd871e871440fd74d7edd7ba318c34f707e0bd
|
|
BLAKE2b-256 checksum How to use checksums |
1cf8e56723d8279ff156dea120c67afde88be80448958bb88d5307426390794f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.0
|
Security for sparsemax 0.1.9
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.