UI interface for gradient flow visualization
Project description
vnittest
Gradient Flow Visualization with Interactive Debugging
Table of Contents
- 1. Overview
- 2. Main Features
- 3. Architecture
- 4. Usage Workflow with the Adapter
- 5. Usage Instructions
- 6. Future Development
- 7. Contribution & Maintenance
1. Overview
A tool for visually debugging gradient flows between convolutional layers of neural networks.
2. Main Features
What are supported?
2.1. Gradient Flow Visualization
- Display Gradient Flow:
- Specify a z value in the "Z:" textbox.
- Uses a Sankey diagram to represent gradient flows between layers.
- Interactive UI for visual gradient debugging:
- Use heatmaps for displaying gradient values.
- Highlights points that have gradients to a selected node in the convolutional layer (input).
- Support padding correction via the Offset Adjustments feature.
2.2. Interactive Debugging
- Input-Editing Mode:
- Users can click on individual heatmap cells to toggle values between 0 and z, which serves as a visual debugging tool for gradients.
- Save Input:
- The Save button allows users to store the modified input values in
flow_info.pkl, which will later be processed by Adapters to generate gradient flow information.
- The Save button allows users to store the modified input values in
- Gradient-Debugging Workflow:
- Users run an external neural network (with the Adapter attached) in the
DEBUG_FOLDERto compute new gradient flows using the saved input. - Clicking the Render button reloads the updated gradient information, reflecting the new computed gradients.
- Users run an external neural network (with the Adapter attached) in the
- State Preservation:
- After debugging, the previously saved heatmap values remain visible, allowing further adjustments and iterative debugging.
3. Architecture
3.1. Data Processing
-
Gradient Extraction:
- Computes gradients for each layer and stores them in a Python dict, which is eventually saved to a pickle file with the (same) name
flow_info.pkl, of the following format:
{ 'activation_gradients': { 'conv2': array([[15.879998, 15.899999], [15.899999, 15.929999]], dtype=float32), 'conv3': array([[4.]], dtype=float32), 'conv1': array([[3.97, 3.98, 3.97, 3.97], [3.98, 4. , 3.98, 3.98], [3.97, 3.98, 3.97, 3.97], [3.97, 3.98, 3.97, 3.97]], dtype=float32) }, 'gradient_flows': { ('conv2', 'conv3'): array([[1.], [1.], [1.], [1.]], dtype=float32), ('conv1', 'conv2'): array([ [1. , 0.99, 0.99, 0.99], [1. , 1. , 0.99, 0.99], [0.99, 1. , 0.99, 0.99], [0.99, 1. , 0.99, 0.99], [1. , 0.99, 1. , 0.99], [1. , 1. , 1. , 1. ], [0.99, 1. , 0.99, 1. ], [0.99, 1. , 0.99, 1. ], [0.99, 0.99, 1. , 0.99], [0.99, 0.99, 1. , 1. ], [0.99, 0.99, 0.99, 1. ], [0.99, 0.99, 0.99, 1. ], [0.99, 0.99, 1. , 0.99], [0.99, 0.99, 1. , 1. ], [0.99, 0.99, 0.99, 1. ], [0.99, 0.99, 0.99, 1. ] ], dtype=float32) } }
Accordingly,
flow_info['activation_gradients']: A Pythondictobject where each key ("conv1", "conv2", ...) is the name you give to the neural network's layers and its content is a 2d tensor of the gradients at that layer (results of user-defined backward hook procedures or through accessing.gradproperty of pytorch tensors).flow_info['gradient_flows']: Adictobject where each key is a tuple, respectively, of the names of input and output layers representing a set of gradient flows, for node pairs connecting two consecutive layers, and its content is a 2-dimensional tensor whose first dimension is the number of nodes in the input layer and the second dimension is the number of nodes in the output layer. - Computes gradients for each layer and stores them in a Python dict, which is eventually saved to a pickle file with the (same) name
-
Flow Matrix Computation:
- Uses values from
gradient_flows. - If a flow is undefined (set to
None), it is interpolated using the available gradient data fromactivation_gradientsto maintain consistency with chain rule calculations.
- Uses values from
3.2. Visualization
- Sankey Diagram:
- Each node represents a pixel or neuron, and each link represents the gradient flow between layers.
- Utilizes Plotly for rendering with fixed properties like positions, colors, and dynamic thresholds for gradient filtering.
- Heatmaps:
- Displays gradient values for selected layers.
- In Input Editing mode, the heatmap allows direct interaction to edit the underlying input values.
3.3. User Interface (UI)
- ipywidgets:
- Provides interactive components such as dropdowns (for layer selection), sliders (for node selection and threshold adjustment), and buttons (for Save and Render).
- Event Handling:
- Any change in the UI (layer, node, threshold, or heatmap edits) triggers an update of the Sankey diagram and heatmaps.
3.4. Adapter Mechanism
- Standardized Adapter Interface:
- Users must implement an Adapter to load the saved input from
flow_info.pkl, process it using their neural network, and store the computed gradient flow back intoflow_info.pkl. - The Adapter ensures the debugging tool remains independent of specific network architectures.
- Users must implement an Adapter to load the saved input from
Example Adapter Implementation:
import torch
class DebugAdapter:
def __init__(self, model, device="cpu"):
self.model = model.to(device)
self.device = device
def compute_gradients(self, input_path, output_path):
# Load input from file
input_data = torch.load(input_path).to(self.device)
# Ensure gradients are tracked
input_data.requires_grad = True
# Forward pass
output = self.model(input_data)
# Compute gradients
output.backward(torch.ones_like(output))
# Extract and save gradients
gradients = input_data.grad.cpu().detach().numpy()
torch.save(gradients, output_path)
4. Usage Workflow with the Adapter
-
Edit and Save Input:
Use the debug tool to modify the input heatmap and click Save to store the current input representation into theflow_info.pklfile. -
External Neural Network Execution:
In a separate notebook or process within theDEBUG_FOLDER, run your neural network (with the Adapter attached in a similar way to setting traditional breakpoints as you can see in vinittest files, definied below, in the${{ github.workspace }}/testsfolder) so that it processes the saved input, computes updated gradient flows, and writes the new data toflow_info.pkl. -
Render Updated Data:
In the debug tool, click the Render button to reload the updated gradient information from theDEBUG_FOLDER. The visualizations (Sankey Diagram and Heatmaps) will then refresh to display the new data.
5. Usage Instructions
Environment Setup
-
Install from PyPI
You can install smap directly from PyPI using pip:
pip install vnittest
-
Configure DEBUG_FOLDER:
Set theDEBUG_FOLDERvariable (e.g.,"../tests/test_data/test_") to point to your data directory. -
Prepare Data Files:
Ensure thatflow_info.pkl,input_representation.npyandtarget_representation.npyare located in the DEBUG_FOLDER.
Running the Tool
-
Launch the Debug Notebook:
vnittest-app
The UI displays:
- Sankey Diagram
- Control Widgets: Layer Dropdown, Node Slider, and Threshold Slider.
- Heatmaps: For Conv1, Conv2, and Target.
- Input Section: Input Heatmap along with Render and Save buttons.
-
Modify the Input:
Click on the Input Heatmap to toggle cell values (0 ↔ 1). The Save button becomes enabled upon modification. -
Save Input Changes:
Click the Save button to save the modified input representation toflow_info.pkl. (This action only updates the file; it does not trigger gradient computation.) -
Run the Neural Network Externally:
In a separate notebook or process within the DEBUG_FOLDER, run your neural network (with the Adapter attached) so that it processes the saved input and writes updated gradient information to file. -
Render Updated Data:
Click the Render button in the debug tool to reload the updated gradient information from the DEBUG_FOLDER. The Sankey Diagram and Heatmaps will refresh accordingly. After rendering, the Save button is disabled until new input modifications occur. -
Interact and Inspect:
Use the control widgets to select different layers, nodes, and thresholds. The visualizations update automatically.
6. Future Development
-
Extended Debugging Capabilities:
- Integration of real-time gradient feedback during training.
-
Enhanced Adapter Interface:
- Further standardize the Adapter to support various network architectures seamlessly.
7. Contribution & Maintenance
-
Contribution:
Contributions, feedback, and bug reports are welcome. Please submit pull requests or open issues on the repository. -
Maintenance:
The modular design facilitates easy updates and extensions for future debugging needs.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file vnittest-0.1.0.tar.gz.
File metadata
- Download URL: vnittest-0.1.0.tar.gz
- Upload date:
- Size: 23.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6bb6d405e8edf014dda32563338476d6b7f3819074187b37f5d277a9513a50d9
|
|
| MD5 |
7e2e5a1059f97578a1712cb2b2b5d8ce
|
|
| BLAKE2b-256 |
37f546ee852c2f0f4a18ae9414691170468ff5a7d6def352b5f13f5045c4de71
|
File details
Details for the file vnittest-0.1.0-py3-none-any.whl.
File metadata
- Download URL: vnittest-0.1.0-py3-none-any.whl
- Upload date:
- Size: 21.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
01da3b5c363435be6fe6032b7d6e1aedcc7753580e88d7c0a27c1e3dcb7c0bf5
|
|
| MD5 |
531a336292c2db8ad195f8f861bfc208
|
|
| BLAKE2b-256 |
6161785800218239a21be2f40e6278702e0474b28d6583728b22eba141e1ee0f
|