Skip to content

Instantly share code, notes, and snippets.

View analyticsindiamagazine's full-sized avatar
:octocat:

Analytics India Magazine analyticsindiamagazine

:octocat:
View GitHub Profile
#Importing tensorflow 2
try:
%tensorflow_version 2.x #gpu
except Exception:
pass
import tensorflow as tf
#Installing the What-If Tool widget
try:
!pip install --upgrade witwidget
# Creates a tf feature spec from the dataframe and columns specified.
def create_feature_spec(df, columns=None):
feature_spec = {}
if columns == None:
columns = df.columns.values.tolist()
for f in columns:
if df[f].dtype is np.dtype(np.int64):
feature_spec[f] = tf.compat.v1.FixedLenFeature(shape=(), dtype=tf.int64)
elif df[f].dtype is np.dtype(np.float64):
feature_spec[f] = tf.compat.v1.FixedLenFeature(shape=(), dtype=tf.float32)
# Specify the dataframe
df = train
# Setting the categorical feature to predict or classify (Target)
label_column = 'Delivery_Time'
#List the uique classes in the target column
classes = list(df[label_column].unique())
#Encode the categories in the target column using the unique classes
# Convert dataset to tf.Example protos
examples = df_to_examples(df)
#Create feature spec for the data
feature_spec = create_feature_spec(df, features_and_labels)
#Creating input features from training set
train_inpf = functools.partial(tfexamples_input_fn, examples, feature_spec, label_column)
# LINEAR CLASSIFIER
# Set the number of training steps
num_steps = 1500
# A logger for logging the training data
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.DEBUG)
# Defining the classifier
classifier = tf.estimator.LinearClassifier(
#Setting the number of datapoints to choose
num_datapoints = len(val)
# Preprocessing The validation data
make_label_column_numeric(val, label_column, classes)
test_examples = df_to_examples(val[0:num_datapoints])
# Importing The WIT essentials
from witwidget.notebook.visualization import WitConfigBuilder
from witwidget.notebook.visualization import WitWidget
#Setting the WIT tool box height in pixels
tool_height_in_px = 600
# Configuring the WIT tool with the test examples and the trained classifier
config_builder = WitConfigBuilder(test_examples[0:num_datapoints]).set_estimator_and_feature_spec(
classifier, feature_spec).set_compare_estimator_and_feature_spec(
@analyticsindiamagazine
analyticsindiamagazine / XAI_WHAT_IF.ipynb
Created November 27, 2019 11:20
How To Implement Explainable AI With What-If Tool For Model Comparison
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
def get_masks(tokens, max_seq_length):
"""Mask for padding"""
if len(tokens)>max_seq_length:
#Cutting down the excess length
tokens = tokens[0:max_seq_length]
return [1]*len(tokens)
else :
return [1]*len(tokens) + [0] * (max_seq_length - len(tokens))
def get_segments(tokens, max_seq_length):
input_word_ids = tf.keras.layers.Input(shape=(max_seq_length,), dtype=tf.int32,
name="input_word_ids")
input_mask = tf.keras.layers.Input(shape=(max_seq_length,), dtype=tf.int32,
name="input_mask")
segment_ids = tf.keras.layers.Input(shape=(max_seq_length,), dtype=tf.int32,
name="segment_ids")
bert_inputs = [input_word_ids, input_mask, segment_ids]
pooled_output, _ = bert_layer(bert_inputs)