Skip to content

Instantly share code, notes, and snippets.

@DN6
Last active July 20, 2020 17:25
Show Gist options
  • Select an option

  • Save DN6/99f4532137615d23cb28eace9c4d2ae2 to your computer and use it in GitHub Desktop.

Select an option

Save DN6/99f4532137615d23cb28eace9c4d2ae2 to your computer and use it in GitHub Desktop.
Snippet to create a confusion matrix using Comet ML
from comet_ml import Experiment
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.callbacks import Callback
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
WORKSPACE = "cometpublic"
PROJECT_NAME = "confusion-matrix"
class ConfusionMatrixCallback(Callback):
def __init__(self, experiment, inputs, targets, cutoff=0.5):
self.experiment = experiment
self.inputs = inputs
self.cutoff = cutoff
self.targets = targets
self.targets_reshaped = keras.utils.to_categorical(self.targets)
def on_epoch_end(self, epoch, logs={}):
predicted = self.model.predict(self.inputs)
predicted = np.where(predicted < self.cutoff, 0, 1)
predicted_reshaped = keras.utils.to_categorical(predicted)
self.experiment.log_confusion_matrix(
self.targets_reshaped,
predicted_reshaped,
title="Confusion Matrix, Epoch #%d" % (epoch + 1),
file_name="confusion-matrix-%03d.json" % (epoch + 1),
)
def load_data():
raw_df = pd.read_csv(
"https://storage.googleapis.com/download.tensorflow.org/data/creditcard.csv"
)
return raw_df
def preprocess(raw_df):
df = raw_df.copy()
eps = 0.01
df.pop("Time")
df["Log Ammount"] = np.log(df.pop("Amount") + eps)
train_df, val_df = train_test_split(df, test_size=0.2)
train_labels = np.array(train_df.pop("Class"))
val_labels = np.array(val_df.pop("Class"))
train_features = np.array(train_df)
val_features = np.array(val_df)
scaler = StandardScaler()
train_features = scaler.fit_transform(train_features)
val_features = scaler.transform(val_features)
train_features = np.clip(train_features, -5, 5)
val_features = np.clip(val_features, -5, 5)
return train_features, val_features, train_labels, val_labels
def build_model(input_shape, output_bias=None):
if output_bias is not None:
output_bias = tf.keras.initializers.Constant(output_bias)
model = keras.Sequential(
[
keras.layers.Dense(16, activation="relu", input_shape=(input_shape,)),
keras.layers.Dropout(0.5),
keras.layers.Dense(1, activation="sigmoid", bias_initializer=output_bias),
]
)
model.compile(
optimizer=keras.optimizers.Adam(lr=1e-3),
loss=keras.losses.BinaryCrossentropy(),
metrics=["accuracy"],
)
return model
def main():
experiment = Experiment(workspace=WORKSPACE, project_name=PROJECT_NAME)
df = load_data()
X_train, X_val, y_train, y_val = preprocess(df)
confmat = ConfusionMatrixCallback(experiment, X_val, y_val)
model = build_model(input_shape=X_train.shape[1])
model.fit(
X_train,
y_train,
validation_data=(X_val, y_val),
epochs=5,
batch_size=64,
callbacks=[confmat],
)
return
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment