Skip to main content

Neural Network Signal Processing

nsp is a Python package for extracting and visualizing activation patterns of PyTorch neural networks. It can

  • extract the computational graph of a neural network as a directed graph.
  • extract the activation pattern generated by an input of a neural network.
  • compute the Fourier transform of an activation pattern (signal) in a neural network (directed graph) based on [1] and [2].
  • visualize activation patterns and their Fourier coefficients.
Visualization of the activation pattern

activation

Visualization of the Fourier coefficients

spectrum

Dependencies

Installation requires pytorch, networkx, and numpy. Some functions will use scipy and/or matplotlib.

Installation

Use the package manager pip to install nsp.

pip install nsp

Usage

Get your network as torch.nn.Module and a valid input.

import nsp
import torch
import torch.nn as nn

class Network(nn.Module):
    def __init__(self):
        super(Network, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=1, out_channels=2, kernel_size=2)
        self.conv2 = nn.Conv2d(in_channels=2, out_channels=8, kernel_size=2)
        self.fc1 = nn.Linear(1*4*8, 8)

    def forward(self, x):
        x = torch.sigmoid(self.conv1(x))
        x = torch.sigmoid(self.conv2(x))
        x = x.view(-1, 1*4*8)
        x = self.fc1(x)
        return x

network = Networks()
image = torch.tensor([[[[-6, -1, -2,  5],
                        [-3, -6,  5,  4],
                        [ 2,  5, -6,  3],
                        [ 5,  0,  1, -6]]]], dtype = torch.float)

Extract the activation pattern.

activations = nsp.Activations(network, image)

Extract the graph of your neural network. NNGraph extends networkx.DiGraph.

graph = nsp.NNGraph(activations)

Transform the activation pattern into its spectrum.

spectrum = graph.transform(activations)

Visualize the activation pattern and the spectrum. Pick your favorite cmap_style from matplotlib colormaps.

nsp.Visualizer.visualize_pattern(activations, pdf_filepath='activations.pdf', scale='layernorm', cmap_style='viridis')

nsp.Visualizer.visualize_pattern(spectrum, pdf_filepath='spectrum.pdf', scale='layernorm', cmap_style='viridis')
Visualization of the activation pattern

activation

Visualization of the spectrum

spectrum

For more details check out the tutorials and read the documentation.

License

MIT

Developed by Felipa Schwarz (c) 2021

References

[1] Markus Püschel, Bastian Seifert, and Chris Wendler. Discrete signal processing on meet/join lattices. IEEE Transactions on Signal Processing, 2021.

[2] Bastian Seifert, Chris Wendler and Markus Püschel. A Causal Shift and Fourier Transform for Directed Acyclic Graphs. Submitted for publication, 2021.

Metadata

Release files for nsp 0.0.16

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nsp 0.0.16
File Size Uploaded Provenance
nsp-0.0.16.tar.gz 9.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for nsp 0.0.16
File Interpreter ABI Platform
nsp-0.0.16-py3-none-any.whl Python 3 none any Details

Total release size: 20.2 kB

Release files / nsp-0.0.16.tar.gz

Download URL nsp-0.0.16.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ad133bf7334c48a3f99ec506931b6a25f4b9d3eb5d9658e065d3de424da807dc
BLAKE2b-256 checksum
How to use checksums
b94a481fe2533d274c3cdb5d41f5f2296edf0b1d18d0ff15e6682787398c4a3f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/3.7.3 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.10

Release files / nsp-0.0.16-py3-none-any.whl

Download URL nsp-0.0.16-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
51fdde37a9168190c438b4441719955b6ef8fe2f3231c90ce4703096b2e6ec1b
BLAKE2b-256 checksum
How to use checksums
506d69344129291cec49a0bd9c69f546993272e3d5991d8166a9da0b0ac38ad8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/3.7.3 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.10

Security for nsp 0.0.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page