Skip to main content

Linear decomposition toolkit for neural network based on pytorch.

Project description

PyPI Test GitHub Workflow Status codecov License: MIT

PyDec

PyDec is a linear decomposition toolkit for neural network based on PyTorch, which can decompose the tensor in the forward process into given components with a small amount of code. The result of decomposition can be applied to tasks such as attribution analysis.

Features:

  • Fast. Compute decomposition in foward process and benefit from GPU acceleration.
  • Real-time. Outputs attribution along with the model output results.
  • Applicable to networks such as Transformer, CNN and RNN.

Examples

Attribution

Contribution Heat maps of the Roberta model (fine-tuned on SST-2). Warm colors indicate high contribution while cool colors indicate low contribution. The outputs of the model were positive, negative and positive, but the latter two samples did not match the labels.

Data flow visualization

demo2

demo2

Requirements and Installation

  • PyTorch version >= 1.11.0
  • Python version >= 3.7
  • To install PyDec and develop locally:
git clone https://github.com/DoubleVII/pydec
cd pydec
pip install --editable ./
  • To install the latest stable release:
pip install pydec

Getting Started

Example: deompose a tiny network

As a simple example, here’s a very simple model with two linear layers and an activation function. We’ll create an instance of it andand get the decomposition of the output by autotracing:

import torch

class TinyModel(torch.nn.Module):
    def __init__(self):
        super(TinyModel, self).__init__()

        self.linear1 = torch.nn.Linear(4, 10)
        self.activation = torch.nn.ReLU()
        self.linear2 = torch.nn.Linear(10, 2)

    def forward(self, x):
        x = self.linear1(x)
        x = self.activation(x)
        x = self.linear2(x)
        return x

tinymodel = TinyModel()

Given an input x, the output of the model is:

x = torch.rand(4)

print("Input tensor:")
print(x)

print("\n\nOutput tensor:")
print(tinymodel(x))

Out:

Input tensor:
tensor([0.7023, 0.3492, 0.7771, 0.0157])


Output tensor:
tensor([0.2751, 0.3626], grad_fn=<AddBackward0>)

To decompose the output, first wrap the model using pydec.autotracing.compile and then input the Composition initialized from x:

tinymodel = pydec.autotracing.compile(tinymodel)

c = pydec.zeros(x.size(), c_num=x.size(0))
c = pydec.diagonal_init(c, src=x, dim=0)

print("Input composition:")
print(c)

c_out = tinymodel(c)

print("\n\nOutput composition:")
print(c_out)

Out:

Input composition:
composition{
  components:
    tensor([0.7023, 0.0000, 0.0000, 0.0000]),
    tensor([0.0000, 0.3492, 0.0000, 0.0000]),
    tensor([0.0000, 0.0000, 0.7771, 0.0000]),
    tensor([0.0000, 0.0000, 0.0000, 0.0157]),
  residual:
    tensor([0., 0., 0., 0.])}


Output composition:
composition{
  components:
    tensor([-0.0418, -0.0296]),
    tensor([0.0566, 0.0332]),
    tensor([0.1093, 0.1147]),
    tensor([ 0.0015, -0.0018]),
  residual:
    tensor([0.1497, 0.2461]),
  grad_fn=<AddBackward0>}

Each component of the output composition represents the contribution of each feature in x to the output. Summing each component yields the tensor of original output:

print("Sum of each component:")
print(c_out.c_sum())

Out:

Sum of each component:
tensor([0.2751, 0.3626], grad_fn=<AddBackward0>)

To restore the ability of the model to forward tensor, use trace() to turn off autotracing:

tinymodel.trace(False)

print("Output tensor:")
print(tinymodel(x))

Out:

Output tensor:
tensor([0.2751, 0.3626], grad_fn=<AddBackward0>)

Documentation

The full documentation contains examples of implementations on realistic models, tutorials, notes and Python API.

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

pydec-0.2.0.tar.gz (34.4 kB view details)

Uploaded Source

Built Distribution

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

pydec-0.2.0-py3-none-any.whl (38.5 kB view details)

Uploaded Python 3

File details

Details for the file pydec-0.2.0.tar.gz.

File metadata

  • Download URL: pydec-0.2.0.tar.gz
  • Upload date:
  • Size: 34.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for pydec-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1c127a60870f04c59b1b6672791bfbe7850520b2ca11e50bae87ae0538172c4f
MD5 d1909643ba02d3d4c8c5c44f4af64298
BLAKE2b-256 3e10619b59e6dbd87583d8a842545dcb81eda4fb6a09aa3552cf80555a42c124

See more details on using hashes here.

File details

Details for the file pydec-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pydec-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 38.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for pydec-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9a288cf23f2a4c97accaec62a0d7b69e3a9781b2ebefa9c21d9e05c30071212b
MD5 7c76a00e2fb726aa71075ed217850e34
BLAKE2b-256 49065d2a15b7ad1891f137bee6cb3725a0c338c6f1bea3acfe9c690ae7713d79

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