/ ExNN Public
forked from ZebinYang/exnn

Enhanced Explainable Neural Network

You must be signed in to change notification settings

# SelfExplainML/ExNN

## Folders and files

NameName
Last commit message
Last commit date

# ExNN: Enhanced Explainable Neural Networks

## Installation

The following environments are required:

• Python 3.7 (anaconda is preferable)
• tensorflow 2.0
pip install git+https://github.com/SelfExplainML/exnn.git

## Usage

import numpy as np
import tensorflow as tf
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split

from exnn import ExNN

def data_generator1(datanum, testnum=10000, noise_sigma=1, rand_seed=0):

corr = 0.5
np.random.seed(rand_seed)
proj_matrix = np.zeros((10, 4))
proj_matrix[:7, 0] = np.array([1,0,0,0,0,0,0])
proj_matrix[:7, 1] = np.array([0,1,0,0,0,0,0])
proj_matrix[:7, 2] = np.array([0,0,0.5,0.5,0,0,0])
proj_matrix[:7, 3] = np.array([0,0,0,0,0.2,0.3,0.5])
u = np.random.uniform(-1, 1, [datanum + testnum, 1])
t = np.sqrt(corr / (1 - corr))
x = np.zeros((datanum + testnum, 10))
for i in range(10):
x[:, i:i + 1] = (np.random.uniform(-1, 1, [datanum + testnum, 1]) + t * u) / (1 + t)

y = np.reshape(2 * np.dot(x, proj_matrix[:, 0]) + 0.2 * np.exp(-4 * np.dot(x, proj_matrix[:, 1])) + \
3 * (np.dot(x, proj_matrix[:, 2]))**2 + 2.5 * np.sin(np.pi * np.dot(x, proj_matrix[:, 3])), [-1, 1]) + \
noise_sigma * np.random.normal(0, 1, [datanum + testnum, 1])

meta_info = {"X1":{"type":"continuous"},
"X2":{"type":"continuous"},
"X3":{"type":"continuous"},
"X4":{"type":"continuous"},
"X5":{"type":"continuous"},
"X6":{"type":"continuous"},
"X7":{"type":"continuous"},
"X8":{"type":"continuous"},
"X9":{"type":"continuous"},
"X10":{"type":"continuous"},
"Y":{"type":"target"}}
for i, (key, item) in enumerate(meta_info.items()):
if item['type'] == "target":
sy = MinMaxScaler((-1, 1))
y = sy.fit_transform(y)
meta_info[key]["scaler"] = sy
elif item['type'] == "categorical":
enc = OrdinalEncoder()
enc.fit(x[:,[i]])
ordinal_feature = enc.transform(x[:,[i]])
x[:,[i]] = ordinal_feature
meta_info[key]["values"] = enc.categories_[0].tolist()
else:
sx = MinMaxScaler((-1, 1))
x[:,[i]] = sx.fit_transform(x[:,[i]])
meta_info[key]["scaler"] = sx

train_x, test_x, train_y, test_y = train_test_split(x, y, test_size=testnum, random_state=rand_seed)
return train_x, test_x, train_y, test_y, task_type, meta_info

train_x, test_x, train_y, test_y, task_type, meta_info = data_generator1(datanum=10000, testnum=10000, noise_sigma=1, rand_seed=0)
model = ExNN(meta_info=meta_info,
subnet_num=10,
subnet_arch=[10, 6],
activation_func=tf.tanh,
batch_size=min(1000, int(train_x.shape[0] * 0.2)),
training_epochs=10000,
lr_bp=0.001,
lr_cl=0.1,
beta_threshold=0.05,
tuning_epochs=100,
l1_proj=0.0001,
l1_subnet=0.00316,
l2_smooth=10**(-6),
verbose=True,
val_ratio=0.2,
early_stop_thres=500)

model.fit(train_x, train_y)
model.visualize("./", "exnn_demo")

## Citations

Yang, Zebin, Zhang, Aijun and Sudjianto, Agus, 2020. Enhancing explainability of neural networks through architecture constraints. IEEE Transactions on Neural Networks and Learning Systems. DOI: 10.1109/TNNLS.2020.3007259

@article{yang2020enhancing,
title={Enhancing explainability of neural networks through architecture constraints},
author={Yang, Zebin and Zhang, Aijun and Sudjianto, Agus},
journal={IEEE Transactions on Neural Networks and Learning Systems},
year={2020},
publisher={IEEE}
}

Enhanced Explainable Neural Network

## Releases

No releases published

## Languages

• Python 100.0%