This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # to get the output value and base value | |
| record = 1 # this is just to pick one record in the dataset | |
| base_value = explainer_2.expected_value | |
| output= base_value + np.sum(shap_values[0][0][record]) | |
| print('base value: ',base_value) | |
| print('output value: ',output) | |
| #sanity check that the ouput value is equal to the actual prediction | |
| print(np.round(output,decimals=1) == np.round(model.predict(X_train.values)[record],decimals=1)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # now let's inspect some individual explanations inferred by DeepSHAP | |
| shap.force_plot(explainer_shap.expected_value, | |
| shap_values[0][0], | |
| feature_names=X_train.columns) | |
| shap.force_plot(explainer_shap.expected_value, | |
| shap_values[0][0][1], | |
| X_train.values[:500][0], | |
| feature_names=X_train.columns,) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import shap | |
| #initialize js methods for visualization | |
| shap.initjs() | |
| # create an instance of the DeepSHAP which is called DeepExplainer | |
| explainer_shap = shap.DeepExplainer(model=model, | |
| data=X_train) | |
| # Fit the explainer on a subset of the data (you can try all but then gets slower) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from keras.layers import Input, Dense, BatchNormalization | |
| from keras.models import Model | |
| from keras.callbacks import EarlyStopping | |
| # set input layer | |
| inputs = Input(shape=(X_train.shape[1],), name='input') | |
| # normalized the batches | |
| x = BatchNormalization(name='input_bn')(inputs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # separate train/test set | |
| import sklearn | |
| X_train, X_test, Y_train, Y_test = model_selection.train_test_split(X, Y, test_size = 0.33, random_state = 5) | |
| print('Y_train: ', Y_train.shape) | |
| print('Y_test: ', Y_test.shape) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # get target and feature variables | |
| X = df.drop('price',axis=1) | |
| Y = df.price | |
| #set them as categorical | |
| X[['cut','color','clarity']] = X[['cut','color','clarity']].astype('category') | |
| # get the maps to the encoding | |
| cut_cat_map = [x for x in zip(X.cut.cat.categories,X.cut.cat.codes.unique() )] | |
| color_cat_map = [x for x in zip(X.color.cat.categories,X.cut.cat.codes.unique() )] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| import seaborn as sns | |
| import keras | |
| import shap | |
| #let's load the diamonds dataset | |
| df=sns.load_dataset(name='diamonds') | |
| print(df.head()) | |
| print(df.describe()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| n_epoch = 1000 #example only | |
| n_batch = 3000 #example only | |
| X=[] # set to appropiate feature space dimmensions | |
| y=[] # set the target | |
| #method 2 for alternating training: use callback | |
| class alternate_trainable(keras.callbacks.Callback): | |
| def __init__(self): | |
| self.counter = 0 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| n_epoch = 1000 #example only | |
| n_batch = 3000 # example only | |
| X=[] #set up your feature space dimensions | |
| y=[] # set your target dimesion | |
| #method 1 for alternating training of the the added transfered layer weight | |
| for i in range(n_epoch): | |
| model_mix_2.fit(X, y, epochs=1, batch_size=n_batch, verbose=1, shuffle=False) | |
| if not self.counter % 2: | |
| model_mix_2.get_layer(name='layer_11').trainable = True | |
| else: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| input_shape = (500,) | |
| #old model | |
| input_1 = keras.layers.Input(shape=input_shape, name='input_1') | |
| nn = keras.layers.Dense(input_shape[0]*3, activation='relu',name='layer_11')(input_1) | |
| nn = keras.layers.Dropout(0.75)(nn) | |
| nn = keras.layers.normalization.BatchNormalization()(nn) | |
| nn = keras.layers.Dense(input_shape[0]*2, activation='relu',name='layer_12')(nn) | |
| nn = keras.layers.Dropout(0.75)(nn) | |
| nn = keras.layers.normalization.BatchNormalization()(nn) |