Attributionpriors
Tools for training explainable models using attribution priors.
Install / Use
/learn @suinleelab/AttributionpriorsREADME
Attribution Priors
A repository for training explainable models using attribution priors.
This repository contains tools for connecting the machine learning topics of model priors and model explanations with a new method called attribution priors, discussed in our paper "Learning Explainable Models Using Attribution Priors". This package contains:
- A differentiable axiomatic feature attribution method called expected gradients.
- Tensorflow and PyTorch operations to directly regularize expected gradients attributions during training.
- Examples of how arbitrary differentiable functions of expected gradient attributions can be regularized during training to encode prior knowledge about a modeling task.
For more guidance about how to use this repository/how to train with attribution priors, one of the quickest demos for PyTorch is available in the
Convergence Demo.ipynb notebook. For quick demos in Tensorflow, see the example_usage.ipynb notebook
(for older versions of TensorFlow) and the example_usage_tf2.ipynb notebook (for TensorFlow 2.0 and above)
in the top level directory of this repository, and the Installation and Usage Section of this README.
Compatability
The code in this repository was written to support TensorFlow versions r1.8 and up, and works with both Python 2 and 3. If you are using TensorFlow with eager execution/TensorFlow 2.0 and above, see Training with Eager Execution. If you are training with TensorFlow Sessions (old-school TensorFlow), see Training with TensorFlow Sessions. We also now have support for PyTorch, which has been tested with Python 3 - see Training with PyTorch.
Code has been tested for GPU compatability on a Lambda Blade GPU Server running CentOS 7.8, and for CPU compatibility on a MacBook Pro running macOS Catalina 10.15.4.
Installation
The easiest way to install this package is by cloning the repository:
git clone https://github.com/suinleelab/attributionpriors.git
Installation should take less than a minute.
Demo
The quickest demo to run the code is the Convergence Demo.ipynb notebook, available in the main directory. This notebook should run in a matter of minutes using only cpu, and also illustrates the benefits of expected gradients feature attributions over other feature attribution methods for attribution priors. For quick demos in Tensorflow, see the example_usage.ipynb notebook
(for older versions of TensorFlow) and the example_usage_tf2.ipynb notebook (for TensorFlow 2.0 and above)
in the top level directory of this repository, and the Installation and Usage Section of this README.
Examples
So what exactly are attribution priors and why would you want to use them? The examples here provide three ways in which you can use attribution priors to improve network performance and interpretability. We use these examples in our paper. However, attribution priors are not limited to the examples here.
Image Data (mnist)
In the mnist folder, we give examples about how to train models that have smoother attributions over pixels, which in turn
leads to better performance on noisy test data. Click through the notebooks in that folder to see more.
Gene Expression Data (graph)
In the graph folder, the notebook shows how penalizing differences between the attributions of neighbors in an arbitrary graph
connecting the features can be used to incorporate prior biological knowledge about the relationships between genes,
yield more biologically plausible explanations of drug response predictions, and improve test error.
Tabular Data (sparsity)
In the sparsity folder, the notebook shows how encouraging inequality in the distribution of feature attributions
can build sparser models that can perform more accurately when training data is limited.
Usage: Training with Eager Execution
This code provides an API for users who are using TensorFlow with eager execution, which is the default in TensorFlow 2.0 and above. The API change is rather simple in eager exceution and follows the following steps:
1: Importing
#Other import statements...
from attributionpriors import eager_ops
2: Manually writing the train_step
Where normally you would write code like this:
@tf.function
def train_step(inputs, labels, model):
with tf.GradientTape() as tape:
tape.watch(inputs)
predictions = model(inputs, training=True)
pred_loss = loss_fn(labels, predictions)
total_loss = pred_loss
if len(model.losses) > 0:
regularization_loss = tf.math.add_n(model.losses)
total_loss = total_loss + regularization_loss
gradients = tape.gradient(total_loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
Now you should add the following lines:
@tf.function
def train_step(inputs, labels, model):
with tf.GradientTape() as tape:
tape.watch(inputs)
predictions = model(inputs, training=True)
pred_loss = loss_fn(labels, predictions)
total_loss = pred_loss
if len(model.losses) > 0:
regularization_loss = tf.math.add_n(model.losses)
total_loss = total_loss + regularization_loss
+ attributions = eager_ops.expected_gradients(inputs, labels, model)
+ attribution_loss = ap_loss_func(attributions, model)
+ total_loss = total_loss + lamb * attribution_loss
gradients = tape.gradient(total_loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
where ap_loss_func is some loss function on top of your attributions, and lamb is a scalar
penalty controlling the trade-off between penalizing attributions and your standard training loss.
In the image case, we use tf.reduce_mean(tf.image.total_variation()), the mean of the total variation across the attributions.
And that's it! If you want a more in-depth example, see the example_usage_tf2.ipynb notebook.
Why can't we use the fit function?
Overloading the fit function is, well, more difficult and makes it harder to specify complex penalties on your attributions. It would require sub-classing the tf.keras.model API, which requires handling a variety of edge cases. If you want to take on this project, feel free to do so, but we don't have plans to support it as of right now.
Usage: Training with TensorFlow Sessions
This package provides a simple API that can be used to define attribution priors over neural networks that can be dropped in to existing TensorFlow code. In order to train using our implementation of attribution priors in your own code, you need to follow the following four steps:
1: Importing
#Other import statements...
from attributionpriors.ops import AttributionPriorExplainer
2: Wrapping your input tensor using an AttributionPriorExplainer object
Where normally you would write code like this:
input_op = get_input() #Returns some tensor of shape [batch_size, feature_dim0, feature_dim1, ...] that you feed into a TensorFlow model
Add the following lines in green:
+ explainer = AttributionPriorExplainer()
input_op = get_input()
+ input_op, train_eg = explainer.input_to_samples_delta(input_op)
input_op can be a placeholder, a constant, or any other TensorFlow tensor that you use to feed data, like images or feature vectors, into a model.
The above code modifications create operations that alternate between feeding your model normal input and input interpolated between a sample and a reference.
This is used to compute expected gradients.
3: Defining the Expected Gradients Tensor
The code for getting your expected gradients tensor depends on whether your model outputs a single number (e.g. in regression), or multiple (e.g. in classification tasks).
Single-output Tasks (like regression)
If your code to define your model normally looks like this:
output_op = model(input_op) #output_op is a [batch_size] shaped tensor of floats
Add the following line in green:
output_op = model(input_op)
+ expected_grads_op = explainer.shap_value_op(y_pred, cond_input_op)
Multi-output Tasks (like classification)
If instead, you are predicting multiple classes, you need to define which class you want to take expected gradients with respect to. Although our repository supports taking expected gradients with respect to ALL output classes (simply by using the code above in the single-output tasks), it is more computationally efficient and more intuitive to take expected gradients with respect to the true class. That is, if you have an image of, say, a car, you should take attributions with respect to the car output class. Our repository supports this as follows:
label_op = get_labels() #label_op should be a [batch_size] shaped tensor of integers specifying which class each input in input_op belongs to
output_op = model(input_op) #output_op is a [batch_size] shaped tensor of floats
+ expected_grads_op = explainer.shap_value_op(y_pred, cond_input_op, label_op)
This will return a tensor operation that represents attributions with respect to the true class of each example.
4: Training
Related Skills
proje
Interactive vocabulary learning platform with smart flashcards and spaced repetition for effective language acquisition.
YC-Killer
2.7kA library of enterprise-grade AI agents designed to democratize artificial intelligence and provide free, open-source alternatives to overvalued Y Combinator startups. If you are excited about democratizing AI access & AI agents, please star ⭐️ this repository and use the link in the readme to join our open source AI research team.
best-practices-researcher
The most comprehensive Claude Code skills registry | Web Search: https://skills-registry-web.vercel.app
groundhog
400Groundhog's primary purpose is to teach people how Cursor and all these other coding agents work under the hood. If you understand how these coding assistants work from first principles, then you can drive these tools harder (or perhaps make your own!).
