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
| #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 |
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
| # 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) |
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
| # 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 |
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
| # 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) |
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
| # 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( |
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
| #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]) |
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
| # 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( |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
| 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): |
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_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) |