Skip to content
Snippets Groups Projects

Constrainable Neural Additive Models - CNAM

immagine 1Structure of CNAM

Description

Constrainable Neural Additive Model (CNAM) allows for building an interpretable neural network for classification or regression tasks. The balance between performance and interpretability can be explicitly enforced with a set of constraints.

Installation

We recommend working in a clean environment with conda:

conda create --name cnamenv
conda activate cnamenv

Then install the following dependencies:

pip3 install matplotlib numpy pytorch_lightning torch torchmetrics interpret scikit-learn xgboost

Then for using the model just download CNAM.py and put it in your project folder.

Usage

This package follow the Scikit-learn API, implementing fit, predict and predict_proba methods.

The usage is described with the following two examples (taken from sectios 4.2 and 4.3 of the manuscript)

California Housing (Regression)

Example usage on a Regression dataset

Prepare the data:

from sklearn.model_selection import train_test_split
from sklearn.datasets import fetch_california_housing

cali = fetch_california_housing()

x_full = cali['data']
y_full = cali['target']

x_tr, x_te, y_tr, y_te = train_test_split(x_full, y_full)

Fit the model:

from CNAM import CNAMSystem
cnam = CNAMSystem(num_steps=50,
                  init_as_ebm=True,
                  task='regression',
                  batch_size=1000
                  )
cnam.fit(x_tr, y_tr, max_epochs=100, lr=1e-2)

Evaluate the model:

from sklearn.metrics import r2_score

y_preds = cnam.predict(x_te)
score = r2_score(y_te, y_preds)
print(score)
0.7395

visualise the model:

cnam.plot_shape_functions(x_te, feature_names=cali['feature_names'])

cali shape functions

MAGIC Telescopes (Classification)

Prepare the data:

from sklearn.datasets import fetch_openml
from sklearn.preprocessing import LabelEncoder

magic_dataset = fetch_openml('MagicTelescope')

x_full = magic_dataset['data']
y_full = LabelEncoder().fit_transform(magic_dataset['target'])

x_tr, x_te, y_tr, y_te = train_test_split(x_full, y_full)

fit the model:

cnam = CNAMSystem(num_steps=50,
                  init_as_ebm=True,
                  task='classification',
                  batch_size=1000
                  )
cnam.fit(x_tr, y_tr, max_epochs=100, lr=1e-2)

evaluate the model:

from sklearn.metrics import roc_auc_score

y_probas_preds = cnam.predict_proba(x_te)
score = roc_auc_score(y_te, y_preds[:, 1])
print(score)
0.8991

visualise the model:

cnam.plot_shape_functions(x_te.values, feature_names=magic_dataset['feature_names'])

magic shape functions

Use constraints for simplifying the model

The novelty of CNAM lies in the possibility of constraining the model in such a way that the final result is more interpretable. The API offers two main parameters:

  • linear_layer_constraint_coefficient: which specifies a L1 regularisation on the final linear layer, which encourages sparsity (the shape functions are more often to 0 so the model relies less on some features)
  • smoothness_incentive: encourage the shape functions to be smooth

A possible practice is to sweep through various combinations of these two parameters and then measure the performance/interpretability scores. Such an approach is described in sections 4.2 and 4.3 of the paper and is shown in cnam-classification-magic-sweep.py for the MAGIC dataset.

Support

For any doubt contact the author at the email: ettore.mariotti@usc.es

Reference Paper

LINK to be added

Authors and acknowledgment

This research is carried out in the framework of the NL4XAI project which has received funding from the European Union's Horizon 2020 research and innovation programme under the Marie Skłodowska-Curie grant agreement No 860621.