Skip to main content

Linear decomposition toolkit for neural network based on pytorch.

Project description

PyPI Test GitHub Workflow Status Coverage badge 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

Suppose a simple feedforward neural network containing two input tensors and outputting one tensor.

class NN(nn.Module):
    def __init__(self) -> None:...

    def forward(self, x1:Tensor, x2:Tensor) -> Tensor:
        x1 = self.linear1(x1)
        x1 = self.relu(x1)

        x2 = self.linear2(x2)
        x2 = self.relu(x2)

        out = self.linear3(x1+x2)
        return out

In order to keep track of the components of inputs x1 and x2 in each hidden tensor, simply initialize the corresponding compositions and apply the same operation for them.

class NN(nn.Module):
    def __init__(self) -> None:...

    def forward(self, x1: Tensor, x2: Tensor) -> Tensor:
        x1 = self.linear1(x1)
        x1 = self.relu(x1)

        x2 = self.linear2(x2)
        x2 = self.relu(x2)

        out = self.linear3(x1 + x2)

        import pydec
        from pydec import Composition
        # Initialize composition
        c1 = Composition(x1.size(), component_num=2).to(x1)
        c1[0] = x1 # Assign x1 to the first component of c1.

        c2 = Composition(x2.size(), component_num=2).to(x2)
        c2[1] = x2 # Assign x2 to the second component of c2.

        # Apply the same operation for composition
        c1 = pydec.nn.functional.linear(
            c1, weight=self.linear1.weight, bias=self.linear1.bias
        )
        c1 = pydec.nn.functional.relu(c1)

        c2 = pydec.nn.functional.linear(
            c2, weight=self.linear2.weight, bias=self.linear2.bias
        )
        c2 = pydec.nn.functional.relu(c2)
        
        c_out = pydec.nn.functional.linear3(
            c1 + c2, weight=self.linear3.weight, bias=self.linear3.bias
        )
        return out, c_out

In the above example, each composition consists of two components whose sum is always equal to the corresponding tensor being decomposed, e.g., $x_1=c_1[0]+c_1[1]$ and $out=c_{out}[0]+c_{out}[1]$. Usually, you can think of $c_{out}[i]$ as the contribution of $x_i$ to the tensor $out$.

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.1.1.tar.gz (22.8 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.1.1-py3-none-any.whl (23.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydec-0.1.1.tar.gz
  • Upload date:
  • Size: 22.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for pydec-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7120a146d74572107a4fc93836fda66e42da56cbe001d5e8510e37c01e24d0bc
MD5 9cb38cbdb102b08c016568d8e134f008
BLAKE2b-256 b5939f78682cb69b7d3a535d1a713d650f721d7bae5ea398696b3ede58daf14d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydec-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 23.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for pydec-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8be60fef2b9a288f686833df01498d8c2f73cfa36cfc23972138ac36f4aacd52
MD5 9c1f4c886aa58ec48250a25df5494a61
BLAKE2b-256 f70e522ca840c072be407df084d7eec90878998e31dd7d9f690347723dad591e

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