Neural Additive Models (NAM) library for interpretable machine learning with shape function visualization
Project description
NAM - Neural Additive Models
A PyTorch library for building and visualizing Neural Additive Models (NAMs) - interpretable machine learning models that decompose predictions into individual feature contributions.
Features
- Interpretable ML: Build Neural Additive Models that decompose predictions into individual feature contributions
- Shape Function Visualization: Plot and analyze how each feature affects predictions
- Architecture Diagrams: Generate visual explanations of the NAM structure
- PyTorch-based: Efficient training with automatic differentiation
- Model Persistence: Save and load trained models
- Easy to Use: Simple API for training and inference
Installation
pip install nam
Quick Start
Training a NAM
import torch
from nam import NAM, train_nam
# Prepare your data
X = torch.randn(1000, 5) # 1000 samples, 5 features
y = X[:, 0] * 2 + X[:, 1] ** 2 - X[:, 2] # Some non-linear relationship
# Train the model
model = train_nam(X, y, num_features=5, hidden_dim=32, depth=5, epochs=1000)
# Make predictions
predictions = model(X)
Using the Model
from nam import NAM
# Create model
model = NAM(num_features=5, hidden_dim=32, depth=5)
# Forward pass
output = model(X)
# Save model
model.save_model('my_nam_model.pth')
# Load model
loaded_model = NAM.load_model('my_nam_model.pth')
Visualizing Shape Functions
Use the built-in visualization tools to understand feature contributions:
from nam import plot_shape_functions, get_shape_function_values
import matplotlib.pyplot as plt
# Plot all shape functions
figures = plot_shape_functions(model, X, feature_names=['f1', 'f2', 'f3', 'f4', 'f5'])
# Or get the raw values for custom plotting
values = get_shape_function_values(model, X, feature_names=['f1', 'f2', 'f3', 'f4', 'f5'])
for feature_name, (x_range, y_values) in values.items():
plt.figure()
plt.plot(x_range, y_values)
plt.title(f'Shape Function: {feature_name}')
plt.show()
Architecture Visualization
Generate publication-ready architecture diagrams:
from nam import make_nam_architecture_figure
fig = make_nam_architecture_figure(
feature_names=['feature1', 'feature2', 'feature3'],
example_inputs=[0.5, -1.2, 0.8],
example_outputs=[0.3, -0.5, 0.2]
)
fig.savefig('nam_architecture.png', dpi=300, bbox_inches='tight')
What are Neural Additive Models?
Neural Additive Models (NAMs) are interpretable machine learning models that learn a separate neural network for each input feature. The final prediction is the sum of all feature contributions:
y = f₁(x₁) + f₂(x₂) + ... + fₙ(xₙ)
This additive structure makes it easy to understand how each feature affects predictions, while still capturing non-linear relationships.
Examples
See the experiments/ directory for complete examples including:
- Housing price prediction with interactive Gradio visualization
- Shape function plotting
- Model training and evaluation
API Reference
NAM
Main model class implementing a Neural Additive Model.
Parameters:
num_features(int): Number of input featureshidden_dim(int): Hidden dimension for shape function networksdepth(int): Depth of each shape function network
Methods:
forward(x): Forward pass returning predictionssave_model(path): Save model to diskload_model(path): Load model from disk (classmethod)
train_nam
Train a NAM model on your data.
Parameters:
X(torch.Tensor): Input features (n_samples, n_features)y(torch.Tensor): Target values (n_samples,)num_features(int): Number of featureshidden_dim(int): Hidden dimension (default: 32)depth(int): Network depth (default: 5)epochs(int): Training epochs (default: 1000)lr(float): Learning rate (default: 0.01)verbose(bool): Print progress (default: True)
Returns:
- Trained NAM model
Contributing
Contributions are welcome! Please open issues or pull requests at the GitHub repository.
License
This project is licensed under the MIT License. See LICENSE.md for details.
References
Based on the Neural Additive Models paper:
- Agarwal, R., Melnick, L., Frosst, N., Zhang, X., Lengerich, B., Caruana, R., & Hinton, G. E. (2021). Neural additive models: Interpretable machine learning with neural nets. NeurIPS.
Project details
Release history Release notifications | RSS feed
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 pytorch_nam-0.1.0.tar.gz.
File metadata
- Download URL: pytorch_nam-0.1.0.tar.gz
- Upload date:
- Size: 748.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fcf3f2a9f5103071eaaed9ee168f34458b619e2f9e5c967b08647e45c0d0922c
|
|
| MD5 |
4726c53ba561dab6cad77bab2c00187d
|
|
| BLAKE2b-256 |
b4d57e1b9225fffe4a6e6579632d651dbec0c9fc80a55cceff2e12b85ea0d6db
|
File details
Details for the file pytorch_nam-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pytorch_nam-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b745847152a7bfd6f12aa7850088d540868b1eaa98efcca561041696644d3b1c
|
|
| MD5 |
46760ca40c7741cdc8a111f8091d5767
|
|
| BLAKE2b-256 |
51a82c72fda4533e27f87e49a474c1db9a6e9cd71df8b64d8c6a9e639c4e18a6
|