Linearity measure applied to fashion MNIST
General definition
The model linearity module in alibi provides metric to measure how linear an ML model is. Linearity is defined based on how much the linear superposition of the model's outputs differs from the output of the same linear superposition of the inputs.
Given $N$ input vectors $v_i$, $N$ real coefficients $\alpha_i$ and a predict function $\text{M}(v_i)$, the linearity of the predict function is defined as
Note that a lower value of $L$ means that the model $M$ is more linear.
Alibi implementation
Based on the general definition above, alibi calculates the linearity of a model in the neighboorhood of a given instance $v_0$.
Fashion MNIST data set
We train a convolutional neural network to classify the images in the fashion MNIST dataset.
We investigate the correlation between the model's linearity associated to a certain instance and the class the instance belong to.
We also calculate the linearity measure for each internal layer of the CNN and show how linearity propagates through the model.
import pandas as pd
import numpy as np
import matplotlib
%matplotlib inline
import matplotlib.pyplot as plt
from time import time
import tensorflow as tf
from alibi.confidence import linearity_measure, LinearityMeasure
from alibi.confidence.model_linearity import infer_feature_range
from tensorflow.keras.layers import Conv2D, Dense, Dropout, Flatten, MaxPooling2D, Input, Activation
from tensorflow.keras.models import Model
from tensorflow.keras.utils import to_categorical
from tensorflow.keras import backend as KLoad data fashion mnist
The fashion MNIST data set consists of 60000 images of shape $28 \times 28$ divided in 10 categories. Each category corresponds to a different type of clothing piece, such as "boots", "t-shirts", etc

Convolutional neural network
Here we define and train a 2 layer convolutional neural network on the fashion MNIST data set.
Define model
Training
Linearity of each Layer
Here we calculate the linearity of the model considering each layer as the output in turn. The values are averaged over 100 random instances sampled from the training set.
Extract layers
Calculate linearity

Linearity measure in the locality of a given instance calculated taking as output each layer of a convolutional neural network trained on the fashion MNIST data set.
The linearity measure of the first convolutional layer conv_1 is 0, as expected since convolutions are linear operations.
The relu activation introduces non-linearity, which is increased by maxpooling. Dropout layers and flatten layers do no change the output at inference time so the linearity doesn't change.
The second convolutional layer conv_2 and the dense layers change the linearity even though they are linear operations.
The softmax layer in red is obtained by inverting the softmax function.
For more details see arxiv reference.
Linearity and categories
Here we calculate the linearity averaged over all instances belonging to the same class, for each class.


Last updated
Was this helpful?

