Linear decomposition toolkit for neural network based on pytorch.
Project description
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
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.