Krishnaswamy Lab, Yale University
This is the official implementation of
Due to certain internal policies, we removed the codebase from public access. However, for the benefit of future researchers, we hereby provide the DSE/DSMI functions.
@inproceedings{DSE2023,
title={Assessing Neural Network Representations During Training Using Noise-Resilient Diffusion Spectral Entropy},
author={Liao, Danqi and Liu, Chen and Christensen, Ben and Tong, Alexander and Huguet, Guillaume and Wolf, Guy and Nickel, Maximilian and Adelstein, Ian and Krishnaswamy, Smita},
booktitle={ICML 2023 Workshop on Topology, Algebra and Geometry in Machine Learning (TAG-ML)},
year={2023},
}
@inproceedings{DSE2024,
title={Assessing neural network representations during training using noise-resilient diffusion spectral entropy},
author={Liao, Danqi and Liu, Chen and Christensen, Benjamin W and Tong, Alexander and Huguet, Guillaume and Wolf, Guy and Nickel, Maximilian and Adelstein, Ian and Krishnaswamy, Smita},
booktitle={2024 58th Annual Conference on Information Sciences and Systems (CISS)},
pages={1--6},
year={2024},
organization={IEEE}
}
-
Install this tool by running the following line in your conda-accessible command line.
pip install dse-dsmi --upgrade
-
You can now import it to a Python script.
from dse_dsmi import diffusion_spectral_entropy, diffusion_spectral_mutual_information
-
Simple test scripts.
Test script on DSE
import os import numpy as np from dse_dsmi import diffusion_spectral_entropy, adjacency_spectral_entropy if __name__ == '__main__': print('Testing Diffusion Spectral Entropy.') print('\n1st run, random vecs, without saving eigvals.') embedding_vectors = np.random.uniform(0, 1, (1000, 256)) DSE = diffusion_spectral_entropy(embedding_vectors=embedding_vectors) print('DSE =', DSE) print( '\n2nd run, random vecs, saving eigvals (np.float16). May be slightly off due to float16 saving.' ) tmp_path = './test_dse_eigval.npz' embedding_vectors = np.random.uniform(0, 1, (1000, 256)) DSE = diffusion_spectral_entropy(embedding_vectors=embedding_vectors, eigval_save_path=tmp_path) print('DSE =', DSE) print( '\n3rd run, loading eigvals from 2nd run. May be slightly off due to float16 saving.' ) embedding_vectors = None # does not matter, will be ignored anyways DSE = diffusion_spectral_entropy(embedding_vectors=embedding_vectors, eigval_save_path=tmp_path) print('DSE =', DSE) os.remove(tmp_path) print('\n4th run, random vecs, saving eigvals (np.float64).') embedding_vectors = np.random.uniform(0, 1, (1000, 256)) DSE = diffusion_spectral_entropy(embedding_vectors=embedding_vectors, eigval_save_path=tmp_path, eigval_save_precision=np.float64) print('DSE =', DSE) print('\n5th run, loading eigvals from 4th run. Shall be identical.') embedding_vectors = None # does not matter, will be ignored anyways DSE = diffusion_spectral_entropy(embedding_vectors=embedding_vectors, eigval_save_path=tmp_path) print('DSE =', DSE) os.remove(tmp_path) print('\n6th run, Classic Shannon Entropy.') embedding_vectors = np.random.uniform(0, 1, (1000, 256)) CSE = diffusion_spectral_entropy(embedding_vectors=embedding_vectors, classic_shannon_entropy=True) print('CSE =', CSE) print( '\n7th run, Entropy on diffusion matrix entries rather than eigenvalues.' ) embedding_vectors = np.random.uniform(0, 1, (1000, 256)) DSE_matrix_entry = diffusion_spectral_entropy( embedding_vectors=embedding_vectors, matrix_entry_entropy=True) print('DSE-matrix-entry =', DSE_matrix_entry) print( '\n8th run, Entropy on KNN binarized adjacency matrix.' ) embedding_vectors = np.random.uniform(0, 1, (1000, 256)) knn_binarized_entropy = adjacency_spectral_entropy( embedding_vectors=embedding_vectors, use_knn=True, knn=10, verbose=True) print('KNN binarized adjacency matrix =', knn_binarized_entropy) print( '\n9th run, Entropy on Gaussian adjacency matrix.' ) embedding_vectors = np.random.uniform(0, 1, (1000, 256)) gaussian_adj_entropy = adjacency_spectral_entropy( embedding_vectors=embedding_vectors, anisotropic=False, verbose=True) print('KNN binarized adjacency matrix =', gaussian_adj_entropy) print( '\n10th run, Entropy on Anisotropic Gaussian adjacency matrix.' ) embedding_vectors = np.random.uniform(0, 1, (1000, 256)) aniso_adj_entropy = adjacency_spectral_entropy( embedding_vectors=embedding_vectors, anisotropic=True, verbose=True) print('KNN binarized adjacency matrix =', aniso_adj_entropy)
Test script on DSE
import numpy as np from dse_dsmi import diffusion_spectral_mutual_information, adjacency_spectral_mutual_information if __name__ == '__main__': print('Testing Diffusion Spectral Mutual Information.') print('\n1st run. DSMI, Embeddings vs discrete class labels.') embedding_vectors = np.random.uniform(0, 1, (1000, 256)) class_labels = np.uint8(np.random.uniform(0, 11, (1000, 1))) DSMI, _ = diffusion_spectral_mutual_information( embedding_vectors=embedding_vectors, reference_vectors=class_labels) print('DSMI =', DSMI) print('\n2nd run. DSMI, Embeddings vs continuous scalars') embedding_vectors = np.random.uniform(0, 1, (1000, 256)) continuous_scalars = np.random.uniform(-1, 1, (1000, 1)) DSMI, _ = diffusion_spectral_mutual_information( embedding_vectors=embedding_vectors, reference_vectors=continuous_scalars) print('DSMI =', DSMI) print('\n3rd run. DSMI, Embeddings vs Input Image') embedding_vectors = np.random.uniform(0, 1, (1000, 256)) input_image = np.random.uniform(-1, 1, (1000, 3, 32, 32)) input_image = input_image.reshape(input_image.shape[0], -1) DSMI, _ = diffusion_spectral_mutual_information( embedding_vectors=embedding_vectors, reference_vectors=input_image) print('DSMI =', DSMI) print('\n4th run. DSMI, Classification dataset.') from sklearn.datasets import make_classification embedding_vectors, class_labels = make_classification(n_samples=1000, n_features=5) DSMI, _ = diffusion_spectral_mutual_information( embedding_vectors=embedding_vectors, reference_vectors=class_labels) print('DSMI =', DSMI) print('\n5th run. CSMI, Classification dataset.') embedding_vectors, class_labels = make_classification(n_samples=1000, n_features=5) CSMI, _ = diffusion_spectral_mutual_information( embedding_vectors=embedding_vectors, reference_vectors=class_labels, classic_shannon_entropy=True) print('CSMI =', CSMI) print('\n6th run. DSMI-matrix-entry, Classification dataset.') embedding_vectors, class_labels = make_classification(n_samples=1000, n_features=5) DSMI_matrix_entry, _ = diffusion_spectral_mutual_information( embedding_vectors=embedding_vectors, reference_vectors=class_labels, matrix_entry_entropy=True) print('DSMI-matrix-entry =', DSMI_matrix_entry) print('\n7th run. ASMI-KNN, Classification dataset.') embedding_vectors, class_labels = make_classification(n_samples=1000, n_features=5) ASMI_knn, _ = adjacency_spectral_mutual_information( embedding_vectors=embedding_vectors, reference_vectors=class_labels, use_knn=True) print('ASMI-KNN =', ASMI_knn) print('\n7th run. ASMI-Gaussian, Classification dataset.') embedding_vectors, class_labels = make_classification(n_samples=1000, n_features=5) ASMI_gausadj, _ = adjacency_spectral_mutual_information( embedding_vectors=embedding_vectors, reference_vectors=class_labels) print('ASMI-Gaussian-Adj =', ASMI_gausadj) print('\n8th run. ASMI-Gaussian-Anisotropic, Classification dataset.') embedding_vectors, class_labels = make_classification(n_samples=1000, n_features=5) ASMI_anisotropic, _ = adjacency_spectral_mutual_information( embedding_vectors=embedding_vectors, reference_vectors=class_labels, anisotropic=True) print('ASMI-Anisotropic-Adj =', ASMI_anisotropic)
Here we present the refactored and reorganized APIs for this project.
dse > dse.py > diffusion_spectral_entropy
dse > dsmi.py > diffusion_spectral_mutual_information
You can directly run the following lines for built-in unit tests.
python dse.py
python dsmi.py
We proposed a framework to measure the entropy and mutual information in high dimensional data and thus applicable to modern neural networks.
We can measure, with respect to a given set of data samples, (1) the entropy of the neural representation at a specific layer and (2) the mutual information between a random variable (e.g., model input or output) and the neural representation at a specific layer.
Compared to the classic Shannon formulation using the binning method, e.g. as in the famous paper Deep Learning and the Information Bottleneck Principle [PDF] [Github1] [Github2], our proposed method is more robust and expressive.
No binning and hence no curse of dimensionality. Therefore, it works on modern deep neural networks (e.g., ResNet-50), not just on toy models with double digit layer width. See "Limitations of the Classic Shannon Entropy and Mutual Information" in our paper for details.
Conceptually, we build a data graph from the neural network representations of all data points in a dataset, and compute the diffusion matrix of the data graph. This matrix is a condensed representation of the diffusion geometry of the neural representation manifold. Our proposed Diffusion Spectral Entropy (DSE) and Diffusion Spectral Mutual Information (DSMI) can be computed from this diffusion matrix.
One major statement to make is that the proposed DSE and DSMI are "not conceptually the same as" the classic Shannon counterparts. They are defined differently and while they maintain the gist of "entropy" and "mutual information" measures, they have their own unique properties. For example, DSE is more sensitive to the underlying dimension and structures (e.g., number of branches or clusters) than to the spread or noise in the data itself, which is contracted to the manifold by raising the diffusion operator to the power of $t$.
In the theoretical results, we upper- and lower-bounded the proposed DSE and DSMI. More interestingly, we showed that if a data distribution originates as a single Gaussian blob but later evolves into
We first use toy experiments to showcase that DSE and DSMI "behave properly" as measures of entropy and mutual information. We also demonstrate they are more robust to high dimensions than the classic counterparts.
Then, we also look at how well DSE and DSMI behave at higher dimensions. In the figure below, we show how DSMI outperforms other mutual information estimators when the dimension is high. Besides, the runtime comparison shows DSMI scales better with respect to dimension.
Finally, it's time to put them in practice! We use DSE and DSMI to visualize the training dynamics of classification networks of 6 backbones (3 ConvNets and 3 Transformers) under 3 training conditions and 3 random seeds. We are evaluating the penultimate layer of the neural network --- the second-to-last layer where people believe embeds the rich representation of the data and are often used for visualization, linear-probing evaluation, etc.
DSE(Z) increasese during training. This happens for both generalizable training and overfitting. The former case coincides with our theoretical finding that DSE(Z) shall increase as the model learns to separate data representation into clusters.
DSMI(Z; Y) increases during generalizable training but stays stagnant during overfitting. This is very much expected.
DSMI(Z; X) shows quite intriguing trends. On MNIST, it keeps increasing. On CIFAR-10 and STL-10, it peaks quickly and gradually decreases. Recall that IB [Tishby et al.] suggests that I(Z; X) shall decrease while [Saxe et al. ICLR'18] believes the opposite. We find that both of them could be correct since the trend we observe is dataset-dependent. One possibility is that MNIST features are too easy to learn (and perhaps the models all overfit?) --- and we leave this to future explorations.
One may ask, besides just peeking into the training dynamics of neural networks, how can we REALLY use DSE and DSMI? Here comes the utility studies.
We sought to assess the effects of network initialization in terms of DSE. We were motivated by two observations: (1) the initial DSEs for different models are not always the same despite using the same method for random initialization; (2) if DSE starts low, it grows monotonically; if DSE starts high, it first decreases and then increases.
We found that if we initialize the convolutional layers with weights
By far, we have monitored DSE and DSMI along the training process of the same model. Now we will show how DSMI correlates with downstream classification accuracy across many different pre-trained models. The following result demonstrates the potential in using DSMI for pre-screening potentially competent models for your specialized dataset.
Removed due to internal policies.
We developed the codebase in a miniconda environment. Tested on Python 3.9.13 + PyTorch 1.12.1. How we created the conda environment: Some packages may no longer be required.
conda create --name dse pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.3 -c pytorch
conda activate dse
conda install -c anaconda scikit-image pillow matplotlib seaborn tqdm
python -m pip install -U giotto-tda
python -m pip install POT torch-optimizer
python -m pip install tinyimagenet
python -m pip install natsort
python -m pip install phate
python -m pip install DiffusionEMD
python -m pip install magic-impute
python -m pip install timm
python -m pip install pytorch-lightning