Skip to main content

Neural Network Gradient Metrics with PyTorch

Project description

PyPI GitHub Workflow Status (branch) PyPI - License PyPI - Downloads

This package implements utilities for computing gradient metrics for measuring uncertainties in neural networks based on the paper "Classification Uncertainty of Deep Neural Networks Based on Gradient Information, Oberdiek et al., 2018".
An application of this can also be found in "On the Importance of Gradients for Detecting Distributional Shifts in the Wild, Huang et al., 2021"

Documentation and examples can be found on GitHub pages.

Table of Contents

Installation

pip install gradient-metrics

Usage

Example of computing the maximum, minimum, mean and standard deviation of gradient entries as in Classification Uncertainty of Deep Neural Networks Based on Gradient Information:

from gradient_metrics import GradientMetricCollector
from gradient_metrics.metrics import Max, Min, MeanStd
import torch.nn.functional as tfunc

# Initialize a network
mynet = MyNeuralNetwork()

# Initialize the GradientMetricCollector
mcollector = GradientMetricCollector(
    [
        Max(mynet),
        Min(mynet),
        MeanStd(mynet),
    ]
)

# Predict your data
out = mynet(x)

# Construct pseudo labels
y_pred = out.argmax(1).clone().detach()

# Construct the sample wise loss for backpropagation
sample_loss = tfunc.cross_entropy(out, y_pred, reduction="none")

# Compute the gradient metrics
metrics = mcollector(sample_loss)

Example of computing the L1-Norm from On the Importance of Gradients for Detecting Distributional Shifts in the Wild:

from gradient_metrics import GradientMetricCollector
from gradient_metrics.metrics import PNorm
import torch
import torch.nn.functional as tfunc

# Initialize a network
mynet = MyNeuralNetwork()

# Initialize the GradientMetricCollector
mcollector = GradientMetricCollector(PNorm(mynet))

# Predict your data
out = mynet(x)

# Construct the sample wise loss for backpropagation
sample_loss = torch.log(tfunc.softmax(out, dim=1)).mean(1).neg()

# Compute the gradient metrics
metrics = mcollector(sample_loss)

Contributing

Requirements:

Contributions in the form of PRs or issues are welcome. To install the development environment run

make setup

Before you open your pull-request, make sure that all tests are passing in your local copy by running make test.

Citing

@inproceedings{OberdiekRG18,  
  author    = {Philipp Oberdiek and  
               Matthias Rottmann and  
               Hanno Gottschalk},  
  editor    = {Luca Pancioni and  
               Friedhelm Schwenker and  
               Edmondo Trentin},  
  title     = {Classification Uncertainty of Deep Neural Networks Based on Gradient  
               Information},  
  booktitle = {Artificial Neural Networks in Pattern Recognition - 8th {IAPR} {TC3}  
               Workshop, {ANNPR} 2018, Siena, Italy, September 19-21, 2018, Proceedings},  
  series    = {Lecture Notes in Computer Science},  
  volume    = {11081},  
  pages     = {113--125},  
  publisher = {Springer},  
  year      = {2018},  
  url       = { https://doi.org/10.1007/978-3-319-99978-4_9 },  
  doi       = { 10.1007/978-3-319-99978-4\_9 },  
}

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

gradient_metrics-0.5.0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

gradient_metrics-0.5.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file gradient_metrics-0.5.0.tar.gz.

File metadata

  • Download URL: gradient_metrics-0.5.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.7 Linux/6.2.0-1019-azure

File hashes

Hashes for gradient_metrics-0.5.0.tar.gz
Algorithm Hash digest
SHA256 346b5e68b9e533547d6d2406a03d8197ab55788a0e416bf3f89e6eaa651ca18d
MD5 9019af28d3a3c317b19f0945b1fe31f4
BLAKE2b-256 0d541504280ee02192824b20bdd2a8e39fa62c8519f6ad4b77bdc58cd33e58dd

See more details on using hashes here.

File details

Details for the file gradient_metrics-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: gradient_metrics-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.7 Linux/6.2.0-1019-azure

File hashes

Hashes for gradient_metrics-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c353d3fb2e5479a8ac9abe1d4e03cd8e28f4f631f28ed56bf35f632f39b516de
MD5 924551c0de1403f324557dcb860aef24
BLAKE2b-256 e80d1614fab694fb094e4cd49c97f61835a906ed9688949de8a46bccd29ce690

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page