Skip to main content

The Python Data Valuation Library

Project description

pyDVL Logo

A library for data valuation.

Build Status
PyPI - License

Docs

pyDVL collects algorithms for Data Valuation and Influence Function computation.

Data Valuation is the task of estimating the intrinsic value of a data point wrt. the training set, the model and a scoring function. We currently implement methods from the following papers:

Influence Functions compute the effect that single points have on an estimator / model. We implement methods from the following papers:

Installation

To install the latest release use:

$ pip install pyDVL

You can also install the latest development version from TestPyPI:

pip install pyDVL --index-url https://test.pypi.org/simple/

For more instructions and information refer to Installing pyDVL in the documentation.

Usage

Influence Functions

For influence computation, follow these steps:

  1. Wrap your model and loss in a TorchTwiceDifferential object
  2. Compute influence factors by providing training data and inversion method

Using the conjugate gradient algorithm, this would look like:

import torch
from torch import nn
from torch.utils.data import DataLoader, TensorDataset

from pydvl.influence import TorchTwiceDifferentiable, compute_influences, InversionMethod

nn_architecture = nn.Sequential(
    nn.Conv2d(in_channels=5, out_channels=3, kernel_size=3),
    nn.Flatten(),
    nn.Linear(27, 3),
)
loss = nn.MSELoss()
model = TorchTwiceDifferentiable(nn_architecture, loss)

input_dim = (5, 5, 5)
output_dim = 3

train_data_loader = DataLoader(
    TensorDataset(torch.rand((10, *input_dim)), torch.rand((10, output_dim))),
    batch_size=2,
)
test_data_loader = DataLoader(
    TensorDataset(torch.rand((5, *input_dim)), torch.rand((5, output_dim))),
    batch_size=1,
)

influences = compute_influences(
    model,
    training_data=train_data_loader,
    test_data=test_data_loader,
    progress=True,
    inversion_method=InversionMethod.Cg,
    hessian_regularization=1e-1,
    maxiter=200,
)

Shapley Values

The steps required to compute values for your samples are:

  1. Create a Dataset object with your train and test splits.
  2. Create an instance of a SupervisedModel (basically any sklearn compatible predictor)
  3. Create a Utility object to wrap the Dataset, the model and a scoring function.
  4. Use one of the methods defined in the library to compute the values.

This is how it looks for Truncated Montecarlo Shapley, an efficient method for Data Shapley values:

from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from pydvl.value import *

data = Dataset.from_sklearn(load_breast_cancer(), train_size=0.7)
model = LogisticRegression()
u = Utility(model, data, Scorer("accuracy", default=0.0))
values = compute_shapley_values(
    u,
    mode=ShapleyMode.TruncatedMontecarlo,
    done=MaxUpdates(100) | AbsoluteStandardError(threshold=0.01),
    truncation=RelativeTruncation(u, rtol=0.01),
)

For more instructions and information refer to Getting Started in the documentation. We provide several examples with details on the algorithms and their applications.

Caching

pyDVL offers the possibility to cache certain results and speed up computation. It uses Memcached For that.

You can run it either locally or, using Docker:

docker container run --rm -p 11211:11211 --name pydvl-cache -d memcached:latest

You can read more in the documentation.

Contributing

Please open new issues for bugs, feature requests and extensions. You can read about the structure of the project, the toolchain and workflow in the guide for contributions.

License

pyDVL is distributed under LGPL-3.0. A complete version can be found in two files: here and here.

All contributions will be distributed under this 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

pyDVL-0.7.0.tar.gz (2.1 MB view details)

Uploaded Source

Built Distribution

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

pyDVL-0.7.0-py3-none-any.whl (143.1 kB view details)

Uploaded Python 3

File details

Details for the file pyDVL-0.7.0.tar.gz.

File metadata

  • Download URL: pyDVL-0.7.0.tar.gz
  • Upload date:
  • Size: 2.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.18

File hashes

Hashes for pyDVL-0.7.0.tar.gz
Algorithm Hash digest
SHA256 64e72023d3cfc2b5e52e471e7a23f7f0b542ae6c44e6cedb2c42a41198d2f5b5
MD5 d138327b0aea94fc418781cbacb3d459
BLAKE2b-256 2527deda6a4b82be6a1fbf1fd11b9944719f264caf57f1ae3b9831f6ba6e55de

See more details on using hashes here.

File details

Details for the file pyDVL-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: pyDVL-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 143.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.18

File hashes

Hashes for pyDVL-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4f277e593fdf3de5c2c4860f46de530321759c00c0ca01346c89c3599eaeeef5
MD5 5908fddbe1c5c9de7c338acbb1f12060
BLAKE2b-256 0ae729b6006cc80e2d9f6649cf30a80d365ff60d27f5f9ba8dcfb1f92f682b4a

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