Skip to main content

PyTorch implementation for k-Lipschitz layers

Project description

DEEL-TORCHLIP Banner


deel-torchlip is an open source Python API to build and train Lipschitz neural networks. It is built on top of PyTorch.
Explore deel-torchlip docs »

deel-torchlip provides:

  • Easy-to-use Lipschitz layers -- deel-torchlip layers are custom PyTorch layers and are very user-friendly. No need to be an expert in Lipschitz networks!
  • Custom losses for robustness -- The provided losses help improving adversarial robustness in classification tasks by increasing margins between outputs of the network (see our paper for more information).
  • Certified robustness -- One main advantage of Lipschitz networks is the costless computation of certificates ensuring that there is no adversarial attacks smaller than these certified radii of robustness.

For TensorFlow/Keras users, we released the deel-lip package offering a similar implementation based on Keras.

📚 Table of contents

🔥 Tutorials

We propose some tutorials to get familiar with the library and its API:

Tutorial Description Link
Wasserstein Tutorial Get started with the basics of torchlip to compute wasserstein distance. Open In Github Open In Colab
Binary classification Learning binary robust classifier with deel-torchlip's API. Open In Github Open In Colab
Tutorial Multiclass classification Learning multiclass robust classifier with deel-torchlip's API. Open In Github Open In Colab

🚀 Quick Start

The latest release can be installed using pip. The torch package will also be installed as a dependency. If torch is already present, be sure that the version is compatible with the deel-torchlip version.

$ pip install deel-torchlip

Usage

Creating a Lipschitz network is similar to building a PyTorch model: standard layers are replaced with their Lipschitz counterparts from deel-torchlip. PyTorch layers that are already Lipschitz can still be used in Lipschitz networks, such as torch.nn.ReLU() or torch.nn.Flatten().

import torch
from deel import torchlip

# Build a Lipschitz network with 4 layers, that can be used in a training loop,
# like any torch.nn.Sequential network
model = torchlip.Sequential(
    torchlip.SpectralConv2d(
        in_channels=3, out_channels=16, kernel_size=(3, 3), padding="same"
    ),
    torchlip.GroupSort2(),
    torch.nn.Flatten(),
    torchlip.SpectralLinear(15544, 64)
)

📦 What's Included

The deel-torchlip library proposes a list of 1-Lipschitz layers equivalent to torch.nn ones.

torch.nn 1-Lipschitz? deel-torchlip equivalent comments
torch.nn.Linear no .SpectralLinear
.FrobeniusLinear
.SpectralLinear and .FrobeniusLinear are similar when there is a single output.
torch.nn.Conv2d no .SpectralConv2d
.FrobeniusConv2d
.SpectralConv2d also implements Björck normalization.
torch.nn.Conv1d no .SpectralConv1d .SpectralConv1d also implements Björck normalization.
MaxPooling
GlobalMaxPooling
yes n/a
torch.nn.AvgPool2d
torch.nn.AdaptiveAvgPool2d
no .ScaledAvgPool2d
.ScaledAdaptiveAvgPool2d
.ScaledL2NormPool2d
.ScaledAdaptativeL2NormPool2d
The Lipschitz constant is bounded by sqrt(pool_h * pool_w).
Flatten yes n/a
torch.nn.ConvTranspose2d no .SpectralConvTranspose2d .SpectralConvTranspose2d also implements Björck normalization.
torch.nn.BatchNorm1d
torch.nn.BatchNorm2d
torch.nn.BatchNorm3d
no .BatchCentering This layer apply a bias based on statistics on batch, but no normalization factor (1-Lipschitz).
torch.nn.LayerNorm no .LayerCentering This layer apply a bias based on statistics on each sample, but no normalization factor (1-Lipschitz).
Residual connections no .LipResidual Learn a factor for mixing residual and a 1-Lipschitz branch.
torch.nn.Dropout no None The Lipschitz constant is bounded by the dropout factor.

The deel-torchlip library proposes a list of classification losses

Type torch.nn deel-torchlip equivalent comments
Binary classification torch.nn.BCEWithLogitsLoss .HKRLoss alpha: Regularization factor ([0,1]) between the hinge and the KR loss; min_margin: Minimal margin for the hinge loss.
Multiclass classification torch.nn.CrossEntropyLoss .HKRMulticlassLoss
.SoftHKRMulticlassLoss
.LseHKRMulticlassLoss
alpha: Regularization factor ([0,1]) between the hinge and the KR loss; min_margin: Minimal margin for the hinge loss.
temperature for the softmax calculation

👍 Contributing

Contributions are welcome! You can open an issue or fork this repository and propose a pull-request. The development environment with all required dependencies should be installed by running:

$ make prepare-dev

Code formatting and linting are performed with black and flake8. Tests are run with pytest. These three commands are gathered in:

$ make test

Finally, commits should respect pre-commit hooks. To be sure that your code changes are accepted, you can run the following target:

$ make check_all

👀 See Also

More from the DEEL project:

  • Xplique a Python library exclusively dedicated to explaining neural networks.
  • deel-lip a Python library for training k-Lipschitz neural networks on TF.
  • Influenciae Python toolkit dedicated to computing influence values for the discovery of potentially problematic samples in a dataset.
  • oodeel a Python library for post-hoc deep OOD (Out-of-Distribution) detection on already trained neural network image classifiers
  • DEEL White paper a summary of the DEEL team on the challenges of certifiable AI and the role of data quality, representativity and explainability for this purpose.

🙏 Acknowledgments

DEEL Logo
This project received funding from the French ”Investing for the Future – PIA3” program within the Artificial and Natural Intelligence Toulouse Institute (ANITI). The authors gratefully acknowledge the support of the DEEL project.

👨‍🎓 Creators

Main contributors of the deel-torchlip library are:

🗞️ Citation

This library was built to support the work presented in our CVPR 2021 paper Achieving robustness in classification using optimal transport with Hinge regularization. If you use our library for your work, please cite our paper :wink:

@misc{2006.06520,
Author = {Mathieu Serrurier and Franck Mamalet and Alberto González-Sanz and Thibaut Boissin and Jean-Michel Loubes and Eustasio del Barrio},
Title = {Achieving robustness in classification using optimal transport with hinge regularization},
Year = {2020},
Eprint = {arXiv:2006.06520},
}

📝 License

The package is released under MIT license.

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

deel_torchlip-1.0.4.tar.gz (67.9 kB view details)

Uploaded Source

Built Distribution

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

deel_torchlip-1.0.4-py3-none-any.whl (58.0 kB view details)

Uploaded Python 3

File details

Details for the file deel_torchlip-1.0.4.tar.gz.

File metadata

  • Download URL: deel_torchlip-1.0.4.tar.gz
  • Upload date:
  • Size: 67.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for deel_torchlip-1.0.4.tar.gz
Algorithm Hash digest
SHA256 87f8620abf85f2a5ac98e7918b60185408e8c8a36fb79460a0ebccbcaa47fcf7
MD5 32f4567acc76df6b6295b6f1ea573f5b
BLAKE2b-256 2131fbbcd595110260e0c233261b247da5d84c087b4d68097aaaab163e510f81

See more details on using hashes here.

File details

Details for the file deel_torchlip-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: deel_torchlip-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 58.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for deel_torchlip-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 62df4d6246b6f41d0e87e786c503241b9c6c977837ac5216a1a1b2fe407bb25a
MD5 30acaa91390b9e306b4d10318c916e57
BLAKE2b-256 11314cc91b059914bfa6673fb865e557777bb71a55e515a092036b780ef299be

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