Classification on imbalanced data

View on TensorFlow.org Run in Google Colab View source on GitHub Download notebook

This tutorial demonstrates how to classify a highly imbalanced dataset in which the number of examples in one class greatly outnumbers the examples in another. You will work with the Credit Card Fraud Detection dataset hosted on Kaggle. The aim is to detect a mere 492 fraudulent transactions from 284,807 transactions in total. You will use Keras to define the model and class weights to help the model learn from the imbalanced data. .

This tutorial contains complete code to:

  • Load a CSV file using Pandas.
  • Create train, validation, and test sets.
  • Define and train a model using Keras (including setting class weights).
  • Evaluate the model using various metrics (including precision and recall).
  • Select a threshold for a probabilistic classifier to get a deterministic classifier.
  • Try and compare with class weighted modelling and oversampling.

Setup

import tensorflow as tf
from tensorflow import keras

import os
import tempfile

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

import sklearn
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
2023-10-27 05:19:05.698235: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2023-10-27 05:19:05.698277: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2023-10-27 05:19:05.699841: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
mpl.rcParams['figure.figsize'] = (12, 10)
colors = plt.rcParams['axes.prop_cycle'].by_key()['color']

Data processing and exploration

Download the Kaggle Credit Card Fraud data set

Pandas is a Python library with many helpful utilities for loading and working with structured data. It can be used to download CSVs into a Pandas DataFrame.

file = tf.keras.utils
raw_df = pd.read_csv('https://storage.googleapis.com/download.tensorflow.org/data/creditcard.csv')
raw_df.head()
raw_df[['Time', 'V1', 'V2', 'V3', 'V4', 'V5', 'V26', 'V27', 'V28', 'Amount', 'Class']].describe()

Examine the class label imbalance

Let's look at the dataset imbalance:

neg, pos = np.bincount(raw_df['Class'])
total = neg + pos
print('Examples:\n    Total: {}\n    Positive: {} ({:.2f}% of total)\n'.format(
    total, pos, 100 * pos / total))
Examples:
    Total: 284807
    Positive: 492 (0.17% of total)

This shows the small fraction of positive samples.

Clean, split and normalize the data

The raw data has a few issues. First the Time and Amount columns are too variable to use directly. Drop the Time column (since it's not clear what it means) and take the log of the Amount column to reduce its range.

cleaned_df = raw_df.copy()

# You don't want the `Time` column.
cleaned_df.pop('Time')

# The `Amount` column covers a huge range. Convert to log-space.
eps = 0.001 # 0 => 0.1¢
cleaned_df['Log Amount'] = np.log(cleaned_df.pop('Amount')+eps)

Split the dataset into train, validation, and test sets. The validation set is used during the model fitting to evaluate the loss and any metrics, however the model is not fit with this data. The test set is completely unused during the training phase and is only used at the end to evaluate how well the model generalizes to new data. This is especially important with imbalanced datasets where overfitting is a significant concern from the lack of training data.

# Use a utility from sklearn to split and shuffle your dataset.
train_df, test_df = train_test_split(cleaned_df, test_size=0.2)
train_df, val_df = train_test_split(train_df, test_size=0.2)

# Form np arrays of labels and features.
train_labels = np.array(train_df.pop('Class'))
bool_train_labels = train_labels != 0
val_labels = np.array(val_df.pop('Class'))
test_labels = np.array(test_df.pop('Class'))

train_features = np.array(train_df)
val_features = np.array(val_df)
test_features = np.array(test_df)

We check whether the distribution of the classes in the three sets is about the same or not.

print(f'Average class probability in training set:   {train_labels.mean():.4f}')
print(f'Average class probability in validation set: {val_labels.mean():.4f}')
print(f'Average class probability in test set:       {test_labels.mean():.4f}')
Average class probability in training set:   0.0018
Average class probability in validation set: 0.0016
Average class probability in test set:       0.0017

Given the small number of positive labels, this seems about right.

Normalize the input features using the sklearn StandardScaler. This will set the mean to 0 and standard deviation to 1.

scaler = StandardScaler()
train_features = scaler.fit_transform(train_features)

val_features = scaler.transform(val_features)
test_features = scaler.transform(test_features)

train_features = np.clip(train_features, -5, 5)
val_features = np.clip(val_features, -5, 5)
test_features = np.clip(test_features, -5, 5)


print('Training labels shape:', train_labels.shape)
print('Validation labels shape:', val_labels.shape)
print('Test labels shape:', test_labels.shape)

print('Training features shape:', train_features.shape)
print('Validation features shape:', val_features.shape)
print('Test features shape:', test_features.shape)
Training labels shape: (182276,)
Validation labels shape: (45569,)
Test labels shape: (56962,)
Training features shape: (182276, 29)
Validation features shape: (45569, 29)
Test features shape: (56962, 29)

Look at the data distribution

Next compare the distributions of the positive and negative examples over a few features. Good questions to ask yourself at this point are:

  • Do these distributions make sense?
    • Yes. You've normalized the input and these are mostly concentrated in the +/- 2 range.
  • Can you see the difference between the distributions?
    • Yes the positive examples contain a much higher rate of extreme values.
pos_df = pd.DataFrame(train_features[ bool_train_labels], columns=train_df.columns)
neg_df = pd.DataFrame(train_features[~bool_train_labels], columns=train_df.columns)

sns.jointplot(x=pos_df['V5'], y=pos_df['V6'],
              kind='hex', xlim=(-5,5), ylim=(-5,5))
plt.suptitle("Positive distribution")

sns.jointplot(x=neg_df['V5'], y=neg_df['V6'],
              kind='hex', xlim=(-5,5), ylim=(-5,5))
_ = plt.suptitle("Negative distribution")

png

png

Define the model and metrics

Define a function that creates a simple neural network with a densly connected hidden layer, a dropout layer to reduce overfitting, and an output sigmoid layer that returns the probability of a transaction being fraudulent:

METRICS = [
      keras.metrics.BinaryCrossentropy(name='cross entropy'),  # same as model's loss
      keras.metrics.MeanSquaredError(name='Brier score'),
      keras.metrics.TruePositives(name='tp'),
      keras.metrics.FalsePositives(name='fp'),
      keras.metrics.TrueNegatives(name='tn'),
      keras.metrics.FalseNegatives(name='fn'), 
      keras.metrics.BinaryAccuracy(name='accuracy'),
      keras.metrics.Precision(name='precision'),
      keras.metrics.Recall(name='recall'),
      keras.metrics.AUC(name='auc'),
      keras.metrics.AUC(name='prc', curve='PR'), # precision-recall curve
]

def make_model(metrics=METRICS, 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=(train_features.shape[-1],)),
      keras.layers.Dropout(0.5),
      keras.layers.Dense(1, activation='sigmoid',
                         bias_initializer=output_bias),
  ])

  model.compile(
      optimizer=keras.optimizers.Adam(learning_rate=1e-3),
      loss=keras.losses.BinaryCrossentropy(),
      metrics=metrics)

  return model

Understanding useful metrics

Notice that there are a few metrics defined above that can be computed by the model that will be helpful when evaluating the performance. These can be divided into three groups.

Metrics for probability predictions

As we train our network with the cross entropy as a loss function, it is fully capable of predicting class probabilities, i.e. it is a probabilistic classifier. Good metrics to assess probabilistic predictions are, in fact, proper scoring rules. Their key property is that predicting the true probability is optimal. We give two well-known examples:

  • cross entropy also known as log loss
  • Mean squared error also known as the Brier score

Metrics for deterministic 0/1 predictions

In the end, one often wants to predict a class label, 0 or 1, no fraud or fraud. This is called a deterministic classifier. To get a label prediction from our probabilistic classifier, one needs to choose a probability threshold \(t\). The default is to predict label 1 (fraud) if the predicted probability is larger than \(t=50\%\) and all the following metrics implicitly use this default.

  • False negatives and false positives are samples that were incorrectly classified
  • True negatives and true positives are samples that were correctly classified
  • Accuracy is the percentage of examples correctly classified > \(\frac{\text{true samples} }{\text{total samples} }\)
  • Precision is the percentage of predicted positives that were correctly classified > \(\frac{\text{true positives} }{\text{true positives + false positives} }\)
  • Recall is the percentage of actual positives that were correctly classified > \(\frac{\text{true positives} }{\text{true positives + false negatives} }\)

Other metrices

The following metrics take into account all possible choices of thresholds \(t\).

  • AUC refers to the Area Under the Curve of a Receiver Operating Characteristic curve (ROC-AUC). This metric is equal to the probability that a classifier will rank a random positive sample higher than a random negative sample.
  • AUPRC refers to Area Under the Curve of the Precision-Recall Curve. This metric computes precision-recall pairs for different probability thresholds.

Read more:

Baseline model

Build the model

Now create and train your model using the function that was defined earlier. Notice that the model is fit using a larger than default batch size of 2048, this is important to ensure that each batch has a decent chance of containing a few positive samples. If the batch size was too small, they would likely have no fraudulent transactions to learn from.

EPOCHS = 100
BATCH_SIZE = 2048

early_stopping = tf.keras.callbacks.EarlyStopping(
    monitor='val_prc', 
    verbose=1,
    patience=10,
    mode='max',
    restore_best_weights=True)
model = make_model()
model.summary()
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense (Dense)               (None, 16)                480       
                                                                 
 dropout (Dropout)           (None, 16)                0         
                                                                 
 dense_1 (Dense)             (None, 1)                 17        
                                                                 
=================================================================
Total params: 497 (1.94 KB)
Trainable params: 497 (1.94 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________

Test run the model:

model.predict(train_features[:10])
1/1 [==============================] - 0s 332ms/step
array([[0.6900619 ],
       [0.51764625],
       [0.67958313],
       [0.01516937],
       [0.5054914 ],
       [0.79354435],
       [0.69832504],
       [0.6598958 ],
       [0.727888  ],
       [0.53301376]], dtype=float32)

Optional: Set the correct initial bias.

These initial guesses are not great. You know the dataset is imbalanced. Set the output layer's bias to reflect that, see A Recipe for Training Neural Networks: "init well". This can help with initial convergence.

With the default bias initialization the loss should be about math.log(2) = 0.69314

results = model.evaluate(train_features, train_labels, batch_size=BATCH_SIZE, verbose=0)
print("Loss: {:0.4f}".format(results[0]))
Loss: 0.6860

The correct bias to set can be derived from:

\[ p_0 = pos/(pos + neg) = 1/(1+e^{-b_0}) \]

\[ b_0 = -log_e(1/p_0 - 1) \]

\[ b_0 = log_e(pos/neg)\]

initial_bias = np.log([pos/neg])
initial_bias
array([-6.35935934])

Set that as the initial bias, and the model will give much more reasonable initial guesses.

It should be near: pos/total = 0.0018

model = make_model(output_bias=initial_bias)
model.predict(train_features[:10])
1/1 [==============================] - 0s 71ms/step
array([[0.00253216],
       [0.00233546],
       [0.00649467],
       [0.00687115],
       [0.00339823],
       [0.00259794],
       [0.00237309],
       [0.00477235],
       [0.00150371],
       [0.00242346]], dtype=float32)

With this initialization the initial loss should be approximately:

\[-p_0log(p_0)-(1-p_0)log(1-p_0) = 0.01317\]

results = model.evaluate(train_features, train_labels, batch_size=BATCH_SIZE, verbose=0)
print("Loss: {:0.4f}".format(results[0]))
Loss: 0.0145

This initial loss is about 50 times less than it would have been with naive initialization.

This way the model doesn't need to spend the first few epochs just learning that positive examples are unlikely. It also makes it easier to read plots of the loss during training.

Checkpoint the initial weights

To make the various training runs more comparable, keep this initial model's weights in a checkpoint file, and load them into each model before training:

initial_weights = os.path.join(tempfile.mkdtemp(), 'initial_weights')
model.save_weights(initial_weights)

Confirm that the bias fix helps

Before moving on, confirm quick that the careful bias initialization actually helped.

Train the model for 20 epochs, with and without this careful initialization, and compare the losses:

model = make_model()
model.load_weights(initial_weights)
model.layers[-1].bias.assign([0.0])
zero_bias_history = model.fit(
    train_features,
    train_labels,
    batch_size=BATCH_SIZE,
    epochs=20,
    validation_data=(val_features, val_labels), 
    verbose=0)
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1698383962.995311  431265 device_compiler.h:186] Compiled cluster using XLA!  This line is logged at most once for the lifetime of the process.
model = make_model()
model.load_weights(initial_weights)
careful_bias_history = model.fit(
    train_features,
    train_labels,
    batch_size=BATCH_SIZE,
    epochs=20,
    validation_data=(val_features, val_labels), 
    verbose=0)
def plot_loss(history, label, n):
  # Use a log scale on y-axis to show the wide range of values.
  plt.semilogy(history.epoch, history.history['loss'],
               color=colors[n], label='Train ' + label)
  plt.semilogy(history.epoch, history.history['val_loss'],
               color=colors[n], label='Val ' + label,
               linestyle="--")
  plt.xlabel('Epoch')
  plt.ylabel('Loss')
  plt.legend()
plot_loss(zero_bias_history, "Zero Bias", 0)
plot_loss(careful_bias_history, "Careful Bias", 1)

png

The above figure makes it clear: In terms of validation loss, on this problem, this careful initialization gives a clear advantage.

Train the model

model = make_model()
model.load_weights(initial_weights)
baseline_history = model.fit(
    train_features,
    train_labels,
    batch_size=BATCH_SIZE,
    epochs=EPOCHS,
    callbacks=[early_stopping],
    validation_data=(val_features, val_labels))
Epoch 1/100
90/90 [==============================] - 2s 11ms/step - loss: 0.0125 - cross entropy: 0.0108 - Brier score: 0.0016 - tp: 95.0000 - fp: 91.0000 - tn: 227361.0000 - fn: 298.0000 - accuracy: 0.9983 - precision: 0.5108 - recall: 0.2417 - auc: 0.8335 - prc: 0.2604 - val_loss: 0.0067 - val_cross entropy: 0.0067 - val_Brier score: 0.0010 - val_tp: 21.0000 - val_fp: 7.0000 - val_tn: 45490.0000 - val_fn: 51.0000 - val_accuracy: 0.9987 - val_precision: 0.7500 - val_recall: 0.2917 - val_auc: 0.8905 - val_prc: 0.6212
Epoch 2/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0073 - cross entropy: 0.0073 - Brier score: 0.0010 - tp: 137.0000 - fp: 29.0000 - tn: 181926.0000 - fn: 184.0000 - accuracy: 0.9988 - precision: 0.8253 - recall: 0.4268 - auc: 0.9083 - prc: 0.5400 - val_loss: 0.0055 - val_cross entropy: 0.0055 - val_Brier score: 8.2362e-04 - val_tp: 35.0000 - val_fp: 8.0000 - val_tn: 45489.0000 - val_fn: 37.0000 - val_accuracy: 0.9990 - val_precision: 0.8140 - val_recall: 0.4861 - val_auc: 0.8805 - val_prc: 0.6500
Epoch 3/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0062 - cross entropy: 0.0062 - Brier score: 8.7017e-04 - tp: 168.0000 - fp: 35.0000 - tn: 181920.0000 - fn: 153.0000 - accuracy: 0.9990 - precision: 0.8276 - recall: 0.5234 - auc: 0.9199 - prc: 0.6322 - val_loss: 0.0051 - val_cross entropy: 0.0051 - val_Brier score: 7.8511e-04 - val_tp: 38.0000 - val_fp: 8.0000 - val_tn: 45489.0000 - val_fn: 34.0000 - val_accuracy: 0.9991 - val_precision: 0.8261 - val_recall: 0.5278 - val_auc: 0.8814 - val_prc: 0.6567
Epoch 4/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0056 - cross entropy: 0.0056 - Brier score: 8.5083e-04 - tp: 172.0000 - fp: 34.0000 - tn: 181921.0000 - fn: 149.0000 - accuracy: 0.9990 - precision: 0.8350 - recall: 0.5358 - auc: 0.9335 - prc: 0.6780 - val_loss: 0.0048 - val_cross entropy: 0.0048 - val_Brier score: 7.4722e-04 - val_tp: 42.0000 - val_fp: 8.0000 - val_tn: 45489.0000 - val_fn: 30.0000 - val_accuracy: 0.9992 - val_precision: 0.8400 - val_recall: 0.5833 - val_auc: 0.8816 - val_prc: 0.6652
Epoch 5/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0054 - cross entropy: 0.0054 - Brier score: 7.8249e-04 - tp: 195.0000 - fp: 35.0000 - tn: 181920.0000 - fn: 126.0000 - accuracy: 0.9991 - precision: 0.8478 - recall: 0.6075 - auc: 0.9137 - prc: 0.6636 - val_loss: 0.0046 - val_cross entropy: 0.0046 - val_Brier score: 7.3179e-04 - val_tp: 42.0000 - val_fp: 8.0000 - val_tn: 45489.0000 - val_fn: 30.0000 - val_accuracy: 0.9992 - val_precision: 0.8400 - val_recall: 0.5833 - val_auc: 0.8886 - val_prc: 0.6768
Epoch 6/100
90/90 [==============================] - 0s 6ms/step - loss: 0.0047 - cross entropy: 0.0047 - Brier score: 7.5780e-04 - tp: 193.0000 - fp: 27.0000 - tn: 181928.0000 - fn: 128.0000 - accuracy: 0.9991 - precision: 0.8773 - recall: 0.6012 - auc: 0.9252 - prc: 0.7130 - val_loss: 0.0045 - val_cross entropy: 0.0045 - val_Brier score: 6.8895e-04 - val_tp: 45.0000 - val_fp: 8.0000 - val_tn: 45489.0000 - val_fn: 27.0000 - val_accuracy: 0.9992 - val_precision: 0.8491 - val_recall: 0.6250 - val_auc: 0.8886 - val_prc: 0.6787
Epoch 7/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0049 - cross entropy: 0.0049 - Brier score: 7.8830e-04 - tp: 182.0000 - fp: 33.0000 - tn: 181922.0000 - fn: 139.0000 - accuracy: 0.9991 - precision: 0.8465 - recall: 0.5670 - auc: 0.9395 - prc: 0.6943 - val_loss: 0.0043 - val_cross entropy: 0.0043 - val_Brier score: 6.9325e-04 - val_tp: 44.0000 - val_fp: 8.0000 - val_tn: 45489.0000 - val_fn: 28.0000 - val_accuracy: 0.9992 - val_precision: 0.8462 - val_recall: 0.6111 - val_auc: 0.8886 - val_prc: 0.6862
Epoch 8/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0043 - cross entropy: 0.0043 - Brier score: 7.1365e-04 - tp: 196.0000 - fp: 31.0000 - tn: 181924.0000 - fn: 125.0000 - accuracy: 0.9991 - precision: 0.8634 - recall: 0.6106 - auc: 0.9334 - prc: 0.7424 - val_loss: 0.0042 - val_cross entropy: 0.0042 - val_Brier score: 6.7840e-04 - val_tp: 45.0000 - val_fp: 8.0000 - val_tn: 45489.0000 - val_fn: 27.0000 - val_accuracy: 0.9992 - val_precision: 0.8491 - val_recall: 0.6250 - val_auc: 0.8886 - val_prc: 0.6907
Epoch 9/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0045 - cross entropy: 0.0045 - Brier score: 7.1057e-04 - tp: 202.0000 - fp: 30.0000 - tn: 181925.0000 - fn: 119.0000 - accuracy: 0.9992 - precision: 0.8707 - recall: 0.6293 - auc: 0.9272 - prc: 0.7217 - val_loss: 0.0041 - val_cross entropy: 0.0041 - val_Brier score: 6.7617e-04 - val_tp: 44.0000 - val_fp: 8.0000 - val_tn: 45489.0000 - val_fn: 28.0000 - val_accuracy: 0.9992 - val_precision: 0.8462 - val_recall: 0.6111 - val_auc: 0.8886 - val_prc: 0.6972
Epoch 10/100
90/90 [==============================] - 1s 6ms/step - loss: 0.0042 - cross entropy: 0.0042 - Brier score: 7.0678e-04 - tp: 199.0000 - fp: 29.0000 - tn: 181926.0000 - fn: 122.0000 - accuracy: 0.9992 - precision: 0.8728 - recall: 0.6199 - auc: 0.9351 - prc: 0.7309 - val_loss: 0.0041 - val_cross entropy: 0.0041 - val_Brier score: 6.5973e-04 - val_tp: 45.0000 - val_fp: 8.0000 - val_tn: 45489.0000 - val_fn: 27.0000 - val_accuracy: 0.9992 - val_precision: 0.8491 - val_recall: 0.6250 - val_auc: 0.8886 - val_prc: 0.6971
Epoch 11/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0041 - cross entropy: 0.0041 - Brier score: 6.9724e-04 - tp: 204.0000 - fp: 29.0000 - tn: 181926.0000 - fn: 117.0000 - accuracy: 0.9992 - precision: 0.8755 - recall: 0.6355 - auc: 0.9351 - prc: 0.7249 - val_loss: 0.0040 - val_cross entropy: 0.0040 - val_Brier score: 6.4722e-04 - val_tp: 45.0000 - val_fp: 8.0000 - val_tn: 45489.0000 - val_fn: 27.0000 - val_accuracy: 0.9992 - val_precision: 0.8491 - val_recall: 0.6250 - val_auc: 0.8886 - val_prc: 0.7108
Epoch 12/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0040 - cross entropy: 0.0040 - Brier score: 6.8904e-04 - tp: 205.0000 - fp: 33.0000 - tn: 181922.0000 - fn: 116.0000 - accuracy: 0.9992 - precision: 0.8613 - recall: 0.6386 - auc: 0.9306 - prc: 0.7481 - val_loss: 0.0039 - val_cross entropy: 0.0039 - val_Brier score: 6.3793e-04 - val_tp: 48.0000 - val_fp: 8.0000 - val_tn: 45489.0000 - val_fn: 24.0000 - val_accuracy: 0.9993 - val_precision: 0.8571 - val_recall: 0.6667 - val_auc: 0.8886 - val_prc: 0.7094
Epoch 13/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0040 - cross entropy: 0.0040 - Brier score: 6.9995e-04 - tp: 204.0000 - fp: 37.0000 - tn: 181918.0000 - fn: 117.0000 - accuracy: 0.9992 - precision: 0.8465 - recall: 0.6355 - auc: 0.9399 - prc: 0.7348 - val_loss: 0.0039 - val_cross entropy: 0.0039 - val_Brier score: 6.3256e-04 - val_tp: 46.0000 - val_fp: 8.0000 - val_tn: 45489.0000 - val_fn: 26.0000 - val_accuracy: 0.9993 - val_precision: 0.8519 - val_recall: 0.6389 - val_auc: 0.8886 - val_prc: 0.7168
Epoch 14/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0038 - cross entropy: 0.0038 - Brier score: 6.9020e-04 - tp: 201.0000 - fp: 37.0000 - tn: 181918.0000 - fn: 120.0000 - accuracy: 0.9991 - precision: 0.8445 - recall: 0.6262 - auc: 0.9354 - prc: 0.7674 - val_loss: 0.0038 - val_cross entropy: 0.0038 - val_Brier score: 6.1821e-04 - val_tp: 47.0000 - val_fp: 8.0000 - val_tn: 45489.0000 - val_fn: 25.0000 - val_accuracy: 0.9993 - val_precision: 0.8545 - val_recall: 0.6528 - val_auc: 0.8886 - val_prc: 0.7173
Epoch 15/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0039 - cross entropy: 0.0039 - Brier score: 7.0191e-04 - tp: 201.0000 - fp: 28.0000 - tn: 181927.0000 - fn: 120.0000 - accuracy: 0.9992 - precision: 0.8777 - recall: 0.6262 - auc: 0.9399 - prc: 0.7455 - val_loss: 0.0038 - val_cross entropy: 0.0038 - val_Brier score: 6.0862e-04 - val_tp: 50.0000 - val_fp: 8.0000 - val_tn: 45489.0000 - val_fn: 22.0000 - val_accuracy: 0.9993 - val_precision: 0.8621 - val_recall: 0.6944 - val_auc: 0.8955 - val_prc: 0.7302
Epoch 16/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0038 - cross entropy: 0.0038 - Brier score: 6.8627e-04 - tp: 208.0000 - fp: 31.0000 - tn: 181924.0000 - fn: 113.0000 - accuracy: 0.9992 - precision: 0.8703 - recall: 0.6480 - auc: 0.9384 - prc: 0.7501 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 6.0433e-04 - val_tp: 45.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 27.0000 - val_accuracy: 0.9993 - val_precision: 0.9000 - val_recall: 0.6250 - val_auc: 0.8955 - val_prc: 0.7373
Epoch 17/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0038 - cross entropy: 0.0038 - Brier score: 7.0074e-04 - tp: 198.0000 - fp: 33.0000 - tn: 181922.0000 - fn: 123.0000 - accuracy: 0.9991 - precision: 0.8571 - recall: 0.6168 - auc: 0.9400 - prc: 0.7478 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.9457e-04 - val_tp: 51.0000 - val_fp: 6.0000 - val_tn: 45491.0000 - val_fn: 21.0000 - val_accuracy: 0.9994 - val_precision: 0.8947 - val_recall: 0.7083 - val_auc: 0.8955 - val_prc: 0.7377
Epoch 18/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0038 - cross entropy: 0.0038 - Brier score: 6.8327e-04 - tp: 208.0000 - fp: 37.0000 - tn: 181918.0000 - fn: 113.0000 - accuracy: 0.9992 - precision: 0.8490 - recall: 0.6480 - auc: 0.9384 - prc: 0.7400 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.9802e-04 - val_tp: 49.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 23.0000 - val_accuracy: 0.9994 - val_precision: 0.9074 - val_recall: 0.6806 - val_auc: 0.8956 - val_prc: 0.7375
Epoch 19/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0039 - cross entropy: 0.0039 - Brier score: 7.0597e-04 - tp: 202.0000 - fp: 33.0000 - tn: 181922.0000 - fn: 119.0000 - accuracy: 0.9992 - precision: 0.8596 - recall: 0.6293 - auc: 0.9337 - prc: 0.7354 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.9616e-04 - val_tp: 49.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 23.0000 - val_accuracy: 0.9994 - val_precision: 0.9074 - val_recall: 0.6806 - val_auc: 0.8955 - val_prc: 0.7394
Epoch 20/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - cross entropy: 0.0035 - Brier score: 6.2861e-04 - tp: 214.0000 - fp: 29.0000 - tn: 181926.0000 - fn: 107.0000 - accuracy: 0.9993 - precision: 0.8807 - recall: 0.6667 - auc: 0.9432 - prc: 0.7760 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.9749e-04 - val_tp: 51.0000 - val_fp: 8.0000 - val_tn: 45489.0000 - val_fn: 21.0000 - val_accuracy: 0.9994 - val_precision: 0.8644 - val_recall: 0.7083 - val_auc: 0.9025 - val_prc: 0.7405
Epoch 21/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0037 - cross entropy: 0.0037 - Brier score: 6.8296e-04 - tp: 206.0000 - fp: 34.0000 - tn: 181921.0000 - fn: 115.0000 - accuracy: 0.9992 - precision: 0.8583 - recall: 0.6417 - auc: 0.9416 - prc: 0.7534 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.9550e-04 - val_tp: 51.0000 - val_fp: 6.0000 - val_tn: 45491.0000 - val_fn: 21.0000 - val_accuracy: 0.9994 - val_precision: 0.8947 - val_recall: 0.7083 - val_auc: 0.9025 - val_prc: 0.7431
Epoch 22/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0038 - cross entropy: 0.0038 - Brier score: 6.6048e-04 - tp: 217.0000 - fp: 31.0000 - tn: 181924.0000 - fn: 104.0000 - accuracy: 0.9993 - precision: 0.8750 - recall: 0.6760 - auc: 0.9370 - prc: 0.7494 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.9666e-04 - val_tp: 51.0000 - val_fp: 8.0000 - val_tn: 45489.0000 - val_fn: 21.0000 - val_accuracy: 0.9994 - val_precision: 0.8644 - val_recall: 0.7083 - val_auc: 0.9024 - val_prc: 0.7390
Epoch 23/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - cross entropy: 0.0033 - Brier score: 5.9352e-04 - tp: 218.0000 - fp: 21.0000 - tn: 181934.0000 - fn: 103.0000 - accuracy: 0.9993 - precision: 0.9121 - recall: 0.6791 - auc: 0.9402 - prc: 0.7894 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.9910e-04 - val_tp: 51.0000 - val_fp: 8.0000 - val_tn: 45489.0000 - val_fn: 21.0000 - val_accuracy: 0.9994 - val_precision: 0.8644 - val_recall: 0.7083 - val_auc: 0.9024 - val_prc: 0.7385
Epoch 24/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0036 - cross entropy: 0.0036 - Brier score: 6.8033e-04 - tp: 212.0000 - fp: 39.0000 - tn: 181916.0000 - fn: 109.0000 - accuracy: 0.9992 - precision: 0.8446 - recall: 0.6604 - auc: 0.9339 - prc: 0.7641 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.9233e-04 - val_tp: 51.0000 - val_fp: 8.0000 - val_tn: 45489.0000 - val_fn: 21.0000 - val_accuracy: 0.9994 - val_precision: 0.8644 - val_recall: 0.7083 - val_auc: 0.9024 - val_prc: 0.7418
Epoch 25/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - cross entropy: 0.0035 - Brier score: 6.9125e-04 - tp: 209.0000 - fp: 40.0000 - tn: 181915.0000 - fn: 112.0000 - accuracy: 0.9992 - precision: 0.8394 - recall: 0.6511 - auc: 0.9448 - prc: 0.7683 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.9311e-04 - val_tp: 51.0000 - val_fp: 8.0000 - val_tn: 45489.0000 - val_fn: 21.0000 - val_accuracy: 0.9994 - val_precision: 0.8644 - val_recall: 0.7083 - val_auc: 0.9024 - val_prc: 0.7426
Epoch 26/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0037 - cross entropy: 0.0037 - Brier score: 6.8222e-04 - tp: 206.0000 - fp: 31.0000 - tn: 181924.0000 - fn: 115.0000 - accuracy: 0.9992 - precision: 0.8692 - recall: 0.6417 - auc: 0.9370 - prc: 0.7574 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.8773e-04 - val_tp: 51.0000 - val_fp: 6.0000 - val_tn: 45491.0000 - val_fn: 21.0000 - val_accuracy: 0.9994 - val_precision: 0.8947 - val_recall: 0.7083 - val_auc: 0.9024 - val_prc: 0.7453
Epoch 27/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - cross entropy: 0.0035 - Brier score: 6.3431e-04 - tp: 216.0000 - fp: 35.0000 - tn: 181920.0000 - fn: 105.0000 - accuracy: 0.9992 - precision: 0.8606 - recall: 0.6729 - auc: 0.9371 - prc: 0.7770 - val_loss: 0.0036 - val_cross entropy: 0.0036 - val_Brier score: 5.8623e-04 - val_tp: 49.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 23.0000 - val_accuracy: 0.9994 - val_precision: 0.9074 - val_recall: 0.6806 - val_auc: 0.9025 - val_prc: 0.7486
Epoch 28/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0036 - cross entropy: 0.0036 - Brier score: 6.6918e-04 - tp: 212.0000 - fp: 38.0000 - tn: 181917.0000 - fn: 109.0000 - accuracy: 0.9992 - precision: 0.8480 - recall: 0.6604 - auc: 0.9356 - prc: 0.7619 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.8949e-04 - val_tp: 49.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 23.0000 - val_accuracy: 0.9994 - val_precision: 0.9074 - val_recall: 0.6806 - val_auc: 0.9025 - val_prc: 0.7480
Epoch 29/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0037 - cross entropy: 0.0037 - Brier score: 6.8371e-04 - tp: 204.0000 - fp: 31.0000 - tn: 181924.0000 - fn: 117.0000 - accuracy: 0.9992 - precision: 0.8681 - recall: 0.6355 - auc: 0.9371 - prc: 0.7616 - val_loss: 0.0036 - val_cross entropy: 0.0036 - val_Brier score: 5.8821e-04 - val_tp: 49.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 23.0000 - val_accuracy: 0.9994 - val_precision: 0.9074 - val_recall: 0.6806 - val_auc: 0.9024 - val_prc: 0.7507
Epoch 30/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - cross entropy: 0.0033 - Brier score: 6.0280e-04 - tp: 225.0000 - fp: 24.0000 - tn: 181931.0000 - fn: 96.0000 - accuracy: 0.9993 - precision: 0.9036 - recall: 0.7009 - auc: 0.9449 - prc: 0.7877 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.8531e-04 - val_tp: 51.0000 - val_fp: 6.0000 - val_tn: 45491.0000 - val_fn: 21.0000 - val_accuracy: 0.9994 - val_precision: 0.8947 - val_recall: 0.7083 - val_auc: 0.9024 - val_prc: 0.7479
Epoch 31/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0034 - cross entropy: 0.0034 - Brier score: 6.2222e-04 - tp: 217.0000 - fp: 32.0000 - tn: 181923.0000 - fn: 104.0000 - accuracy: 0.9993 - precision: 0.8715 - recall: 0.6760 - auc: 0.9434 - prc: 0.7903 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.8940e-04 - val_tp: 51.0000 - val_fp: 6.0000 - val_tn: 45491.0000 - val_fn: 21.0000 - val_accuracy: 0.9994 - val_precision: 0.8947 - val_recall: 0.7083 - val_auc: 0.9024 - val_prc: 0.7472
Epoch 32/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - cross entropy: 0.0033 - Brier score: 6.2715e-04 - tp: 213.0000 - fp: 28.0000 - tn: 181927.0000 - fn: 108.0000 - accuracy: 0.9993 - precision: 0.8838 - recall: 0.6636 - auc: 0.9418 - prc: 0.7944 - val_loss: 0.0036 - val_cross entropy: 0.0036 - val_Brier score: 5.9011e-04 - val_tp: 49.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 23.0000 - val_accuracy: 0.9994 - val_precision: 0.9074 - val_recall: 0.6806 - val_auc: 0.9024 - val_prc: 0.7490
Epoch 33/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0034 - cross entropy: 0.0034 - Brier score: 6.0579e-04 - tp: 220.0000 - fp: 29.0000 - tn: 181926.0000 - fn: 101.0000 - accuracy: 0.9993 - precision: 0.8835 - recall: 0.6854 - auc: 0.9433 - prc: 0.7874 - val_loss: 0.0036 - val_cross entropy: 0.0036 - val_Brier score: 5.8673e-04 - val_tp: 50.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 22.0000 - val_accuracy: 0.9994 - val_precision: 0.9091 - val_recall: 0.6944 - val_auc: 0.9024 - val_prc: 0.7494
Epoch 34/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - cross entropy: 0.0035 - Brier score: 6.6404e-04 - tp: 214.0000 - fp: 36.0000 - tn: 181919.0000 - fn: 107.0000 - accuracy: 0.9992 - precision: 0.8560 - recall: 0.6667 - auc: 0.9402 - prc: 0.7687 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.8826e-04 - val_tp: 49.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 23.0000 - val_accuracy: 0.9994 - val_precision: 0.9074 - val_recall: 0.6806 - val_auc: 0.9024 - val_prc: 0.7506
Epoch 35/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - cross entropy: 0.0035 - Brier score: 6.4552e-04 - tp: 211.0000 - fp: 31.0000 - tn: 181924.0000 - fn: 110.0000 - accuracy: 0.9992 - precision: 0.8719 - recall: 0.6573 - auc: 0.9433 - prc: 0.7873 - val_loss: 0.0036 - val_cross entropy: 0.0036 - val_Brier score: 5.8679e-04 - val_tp: 50.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 22.0000 - val_accuracy: 0.9994 - val_precision: 0.9091 - val_recall: 0.6944 - val_auc: 0.9024 - val_prc: 0.7483
Epoch 36/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0034 - cross entropy: 0.0034 - Brier score: 6.2420e-04 - tp: 218.0000 - fp: 25.0000 - tn: 181930.0000 - fn: 103.0000 - accuracy: 0.9993 - precision: 0.8971 - recall: 0.6791 - auc: 0.9450 - prc: 0.7851 - val_loss: 0.0036 - val_cross entropy: 0.0036 - val_Brier score: 5.8602e-04 - val_tp: 51.0000 - val_fp: 6.0000 - val_tn: 45491.0000 - val_fn: 21.0000 - val_accuracy: 0.9994 - val_precision: 0.8947 - val_recall: 0.7083 - val_auc: 0.9024 - val_prc: 0.7485
Epoch 37/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - cross entropy: 0.0035 - Brier score: 6.4125e-04 - tp: 217.0000 - fp: 34.0000 - tn: 181921.0000 - fn: 104.0000 - accuracy: 0.9992 - precision: 0.8645 - recall: 0.6760 - auc: 0.9449 - prc: 0.7812 - val_loss: 0.0036 - val_cross entropy: 0.0036 - val_Brier score: 5.8659e-04 - val_tp: 51.0000 - val_fp: 6.0000 - val_tn: 45491.0000 - val_fn: 21.0000 - val_accuracy: 0.9994 - val_precision: 0.8947 - val_recall: 0.7083 - val_auc: 0.9024 - val_prc: 0.7485
Epoch 38/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - cross entropy: 0.0035 - Brier score: 6.6541e-04 - tp: 216.0000 - fp: 33.0000 - tn: 181922.0000 - fn: 105.0000 - accuracy: 0.9992 - precision: 0.8675 - recall: 0.6729 - auc: 0.9433 - prc: 0.7759 - val_loss: 0.0036 - val_cross entropy: 0.0036 - val_Brier score: 5.8565e-04 - val_tp: 49.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 23.0000 - val_accuracy: 0.9994 - val_precision: 0.9074 - val_recall: 0.6806 - val_auc: 0.9024 - val_prc: 0.7524
Epoch 39/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - cross entropy: 0.0033 - Brier score: 6.4174e-04 - tp: 212.0000 - fp: 34.0000 - tn: 181921.0000 - fn: 109.0000 - accuracy: 0.9992 - precision: 0.8618 - recall: 0.6604 - auc: 0.9511 - prc: 0.7910 - val_loss: 0.0036 - val_cross entropy: 0.0036 - val_Brier score: 5.8433e-04 - val_tp: 50.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 22.0000 - val_accuracy: 0.9994 - val_precision: 0.9091 - val_recall: 0.6944 - val_auc: 0.9024 - val_prc: 0.7512
Epoch 40/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0034 - cross entropy: 0.0034 - Brier score: 6.4466e-04 - tp: 216.0000 - fp: 35.0000 - tn: 181920.0000 - fn: 105.0000 - accuracy: 0.9992 - precision: 0.8606 - recall: 0.6729 - auc: 0.9432 - prc: 0.7736 - val_loss: 0.0036 - val_cross entropy: 0.0036 - val_Brier score: 5.9291e-04 - val_tp: 48.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 24.0000 - val_accuracy: 0.9994 - val_precision: 0.9057 - val_recall: 0.6667 - val_auc: 0.9024 - val_prc: 0.7513
Epoch 41/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0036 - cross entropy: 0.0036 - Brier score: 6.6270e-04 - tp: 212.0000 - fp: 29.0000 - tn: 181926.0000 - fn: 109.0000 - accuracy: 0.9992 - precision: 0.8797 - recall: 0.6604 - auc: 0.9402 - prc: 0.7690 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.9778e-04 - val_tp: 47.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 25.0000 - val_accuracy: 0.9993 - val_precision: 0.9038 - val_recall: 0.6528 - val_auc: 0.9024 - val_prc: 0.7508
Epoch 42/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - cross entropy: 0.0032 - Brier score: 6.2712e-04 - tp: 214.0000 - fp: 29.0000 - tn: 181926.0000 - fn: 107.0000 - accuracy: 0.9993 - precision: 0.8807 - recall: 0.6667 - auc: 0.9496 - prc: 0.7964 - val_loss: 0.0036 - val_cross entropy: 0.0036 - val_Brier score: 5.8292e-04 - val_tp: 51.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 21.0000 - val_accuracy: 0.9994 - val_precision: 0.9107 - val_recall: 0.7083 - val_auc: 0.9024 - val_prc: 0.7535
Epoch 43/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - cross entropy: 0.0035 - Brier score: 6.3098e-04 - tp: 218.0000 - fp: 32.0000 - tn: 181923.0000 - fn: 103.0000 - accuracy: 0.9993 - precision: 0.8720 - recall: 0.6791 - auc: 0.9433 - prc: 0.7773 - val_loss: 0.0036 - val_cross entropy: 0.0036 - val_Brier score: 5.9061e-04 - val_tp: 47.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 25.0000 - val_accuracy: 0.9993 - val_precision: 0.9038 - val_recall: 0.6528 - val_auc: 0.9024 - val_prc: 0.7540
Epoch 44/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - cross entropy: 0.0033 - Brier score: 6.0973e-04 - tp: 215.0000 - fp: 30.0000 - tn: 181925.0000 - fn: 106.0000 - accuracy: 0.9993 - precision: 0.8776 - recall: 0.6698 - auc: 0.9386 - prc: 0.7859 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.8743e-04 - val_tp: 50.0000 - val_fp: 6.0000 - val_tn: 45491.0000 - val_fn: 22.0000 - val_accuracy: 0.9994 - val_precision: 0.8929 - val_recall: 0.6944 - val_auc: 0.9024 - val_prc: 0.7514
Epoch 45/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0034 - cross entropy: 0.0034 - Brier score: 6.0192e-04 - tp: 223.0000 - fp: 29.0000 - tn: 181926.0000 - fn: 98.0000 - accuracy: 0.9993 - precision: 0.8849 - recall: 0.6947 - auc: 0.9402 - prc: 0.7826 - val_loss: 0.0036 - val_cross entropy: 0.0036 - val_Brier score: 5.8868e-04 - val_tp: 50.0000 - val_fp: 6.0000 - val_tn: 45491.0000 - val_fn: 22.0000 - val_accuracy: 0.9994 - val_precision: 0.8929 - val_recall: 0.6944 - val_auc: 0.9024 - val_prc: 0.7497
Epoch 46/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - cross entropy: 0.0033 - Brier score: 6.1774e-04 - tp: 220.0000 - fp: 32.0000 - tn: 181923.0000 - fn: 101.0000 - accuracy: 0.9993 - precision: 0.8730 - recall: 0.6854 - auc: 0.9434 - prc: 0.7956 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.8731e-04 - val_tp: 47.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 25.0000 - val_accuracy: 0.9993 - val_precision: 0.9038 - val_recall: 0.6528 - val_auc: 0.9024 - val_prc: 0.7515
Epoch 47/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0034 - cross entropy: 0.0034 - Brier score: 6.3799e-04 - tp: 214.0000 - fp: 32.0000 - tn: 181923.0000 - fn: 107.0000 - accuracy: 0.9992 - precision: 0.8699 - recall: 0.6667 - auc: 0.9418 - prc: 0.7733 - val_loss: 0.0036 - val_cross entropy: 0.0036 - val_Brier score: 5.8810e-04 - val_tp: 47.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 25.0000 - val_accuracy: 0.9993 - val_precision: 0.9038 - val_recall: 0.6528 - val_auc: 0.9024 - val_prc: 0.7531
Epoch 48/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - cross entropy: 0.0035 - Brier score: 6.4644e-04 - tp: 213.0000 - fp: 31.0000 - tn: 181924.0000 - fn: 108.0000 - accuracy: 0.9992 - precision: 0.8730 - recall: 0.6636 - auc: 0.9402 - prc: 0.7714 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.8606e-04 - val_tp: 50.0000 - val_fp: 6.0000 - val_tn: 45491.0000 - val_fn: 22.0000 - val_accuracy: 0.9994 - val_precision: 0.8929 - val_recall: 0.6944 - val_auc: 0.9024 - val_prc: 0.7526
Epoch 49/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - cross entropy: 0.0032 - Brier score: 6.0215e-04 - tp: 220.0000 - fp: 27.0000 - tn: 181928.0000 - fn: 101.0000 - accuracy: 0.9993 - precision: 0.8907 - recall: 0.6854 - auc: 0.9449 - prc: 0.7882 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.8496e-04 - val_tp: 50.0000 - val_fp: 6.0000 - val_tn: 45491.0000 - val_fn: 22.0000 - val_accuracy: 0.9994 - val_precision: 0.8929 - val_recall: 0.6944 - val_auc: 0.9024 - val_prc: 0.7520
Epoch 50/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - cross entropy: 0.0033 - Brier score: 6.0097e-04 - tp: 224.0000 - fp: 30.0000 - tn: 181925.0000 - fn: 97.0000 - accuracy: 0.9993 - precision: 0.8819 - recall: 0.6978 - auc: 0.9419 - prc: 0.7840 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.9023e-04 - val_tp: 49.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 23.0000 - val_accuracy: 0.9994 - val_precision: 0.9074 - val_recall: 0.6806 - val_auc: 0.9024 - val_prc: 0.7506
Epoch 51/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - cross entropy: 0.0033 - Brier score: 5.8703e-04 - tp: 223.0000 - fp: 28.0000 - tn: 181927.0000 - fn: 98.0000 - accuracy: 0.9993 - precision: 0.8884 - recall: 0.6947 - auc: 0.9449 - prc: 0.7910 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.8905e-04 - val_tp: 50.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 22.0000 - val_accuracy: 0.9994 - val_precision: 0.9091 - val_recall: 0.6944 - val_auc: 0.9093 - val_prc: 0.7543
Epoch 52/100
90/90 [==============================] - 1s 6ms/step - loss: 0.0030 - cross entropy: 0.0030 - Brier score: 5.5838e-04 - tp: 224.0000 - fp: 25.0000 - tn: 181930.0000 - fn: 97.0000 - accuracy: 0.9993 - precision: 0.8996 - recall: 0.6978 - auc: 0.9496 - prc: 0.8046 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.8398e-04 - val_tp: 50.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 22.0000 - val_accuracy: 0.9994 - val_precision: 0.9091 - val_recall: 0.6944 - val_auc: 0.9023 - val_prc: 0.7485
Epoch 53/100
90/90 [==============================] - 1s 6ms/step - loss: 0.0032 - cross entropy: 0.0032 - Brier score: 6.0789e-04 - tp: 220.0000 - fp: 33.0000 - tn: 181922.0000 - fn: 101.0000 - accuracy: 0.9993 - precision: 0.8696 - recall: 0.6854 - auc: 0.9480 - prc: 0.7919 - val_loss: 0.0036 - val_cross entropy: 0.0036 - val_Brier score: 5.9127e-04 - val_tp: 47.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 25.0000 - val_accuracy: 0.9993 - val_precision: 0.9038 - val_recall: 0.6528 - val_auc: 0.9024 - val_prc: 0.7526
Epoch 54/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - cross entropy: 0.0033 - Brier score: 6.0180e-04 - tp: 220.0000 - fp: 28.0000 - tn: 181927.0000 - fn: 101.0000 - accuracy: 0.9993 - precision: 0.8871 - recall: 0.6854 - auc: 0.9480 - prc: 0.7979 - val_loss: 0.0036 - val_cross entropy: 0.0036 - val_Brier score: 5.8662e-04 - val_tp: 48.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 24.0000 - val_accuracy: 0.9994 - val_precision: 0.9057 - val_recall: 0.6667 - val_auc: 0.9024 - val_prc: 0.7564
Epoch 55/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - cross entropy: 0.0032 - Brier score: 5.9976e-04 - tp: 218.0000 - fp: 28.0000 - tn: 181927.0000 - fn: 103.0000 - accuracy: 0.9993 - precision: 0.8862 - recall: 0.6791 - auc: 0.9450 - prc: 0.7952 - val_loss: 0.0036 - val_cross entropy: 0.0036 - val_Brier score: 5.7930e-04 - val_tp: 51.0000 - val_fp: 6.0000 - val_tn: 45491.0000 - val_fn: 21.0000 - val_accuracy: 0.9994 - val_precision: 0.8947 - val_recall: 0.7083 - val_auc: 0.9093 - val_prc: 0.7605
Epoch 56/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0031 - cross entropy: 0.0031 - Brier score: 5.7106e-04 - tp: 226.0000 - fp: 24.0000 - tn: 181931.0000 - fn: 95.0000 - accuracy: 0.9993 - precision: 0.9040 - recall: 0.7040 - auc: 0.9450 - prc: 0.8061 - val_loss: 0.0036 - val_cross entropy: 0.0036 - val_Brier score: 5.8611e-04 - val_tp: 49.0000 - val_fp: 6.0000 - val_tn: 45491.0000 - val_fn: 23.0000 - val_accuracy: 0.9994 - val_precision: 0.8909 - val_recall: 0.6806 - val_auc: 0.9093 - val_prc: 0.7572
Epoch 57/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0034 - cross entropy: 0.0034 - Brier score: 6.1932e-04 - tp: 220.0000 - fp: 37.0000 - tn: 181918.0000 - fn: 101.0000 - accuracy: 0.9992 - precision: 0.8560 - recall: 0.6854 - auc: 0.9434 - prc: 0.7738 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.9228e-04 - val_tp: 47.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 25.0000 - val_accuracy: 0.9993 - val_precision: 0.9038 - val_recall: 0.6528 - val_auc: 0.9093 - val_prc: 0.7592
Epoch 58/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0030 - cross entropy: 0.0030 - Brier score: 5.6991e-04 - tp: 223.0000 - fp: 28.0000 - tn: 181927.0000 - fn: 98.0000 - accuracy: 0.9993 - precision: 0.8884 - recall: 0.6947 - auc: 0.9513 - prc: 0.8244 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.9132e-04 - val_tp: 47.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 25.0000 - val_accuracy: 0.9993 - val_precision: 0.9038 - val_recall: 0.6528 - val_auc: 0.9024 - val_prc: 0.7532
Epoch 59/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - cross entropy: 0.0033 - Brier score: 6.1934e-04 - tp: 218.0000 - fp: 33.0000 - tn: 181922.0000 - fn: 103.0000 - accuracy: 0.9993 - precision: 0.8685 - recall: 0.6791 - auc: 0.9419 - prc: 0.7862 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.8682e-04 - val_tp: 49.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 23.0000 - val_accuracy: 0.9994 - val_precision: 0.9074 - val_recall: 0.6806 - val_auc: 0.9024 - val_prc: 0.7505
Epoch 60/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0029 - cross entropy: 0.0029 - Brier score: 5.5058e-04 - tp: 228.0000 - fp: 25.0000 - tn: 181930.0000 - fn: 93.0000 - accuracy: 0.9994 - precision: 0.9012 - recall: 0.7103 - auc: 0.9435 - prc: 0.8239 - val_loss: 0.0036 - val_cross entropy: 0.0036 - val_Brier score: 5.8324e-04 - val_tp: 50.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 22.0000 - val_accuracy: 0.9994 - val_precision: 0.9091 - val_recall: 0.6944 - val_auc: 0.9024 - val_prc: 0.7554
Epoch 61/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - cross entropy: 0.0032 - Brier score: 6.2829e-04 - tp: 216.0000 - fp: 35.0000 - tn: 181920.0000 - fn: 105.0000 - accuracy: 0.9992 - precision: 0.8606 - recall: 0.6729 - auc: 0.9512 - prc: 0.8056 - val_loss: 0.0036 - val_cross entropy: 0.0036 - val_Brier score: 5.8949e-04 - val_tp: 47.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 25.0000 - val_accuracy: 0.9993 - val_precision: 0.9038 - val_recall: 0.6528 - val_auc: 0.9024 - val_prc: 0.7542
Epoch 62/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - cross entropy: 0.0032 - Brier score: 6.0151e-04 - tp: 216.0000 - fp: 23.0000 - tn: 181932.0000 - fn: 105.0000 - accuracy: 0.9993 - precision: 0.9038 - recall: 0.6729 - auc: 0.9496 - prc: 0.7963 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.8616e-04 - val_tp: 50.0000 - val_fp: 6.0000 - val_tn: 45491.0000 - val_fn: 22.0000 - val_accuracy: 0.9994 - val_precision: 0.8929 - val_recall: 0.6944 - val_auc: 0.9024 - val_prc: 0.7521
Epoch 63/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - cross entropy: 0.0033 - Brier score: 6.3906e-04 - tp: 219.0000 - fp: 30.0000 - tn: 181925.0000 - fn: 102.0000 - accuracy: 0.9993 - precision: 0.8795 - recall: 0.6822 - auc: 0.9434 - prc: 0.7898 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.8911e-04 - val_tp: 49.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 23.0000 - val_accuracy: 0.9994 - val_precision: 0.9074 - val_recall: 0.6806 - val_auc: 0.9024 - val_prc: 0.7528
Epoch 64/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - cross entropy: 0.0032 - Brier score: 6.0072e-04 - tp: 224.0000 - fp: 30.0000 - tn: 181925.0000 - fn: 97.0000 - accuracy: 0.9993 - precision: 0.8819 - recall: 0.6978 - auc: 0.9434 - prc: 0.7951 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.9173e-04 - val_tp: 49.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 23.0000 - val_accuracy: 0.9994 - val_precision: 0.9074 - val_recall: 0.6806 - val_auc: 0.9024 - val_prc: 0.7513
Epoch 65/100
84/90 [===========================>..] - ETA: 0s - loss: 0.0031 - cross entropy: 0.0031 - Brier score: 5.6877e-04 - tp: 212.0000 - fp: 22.0000 - tn: 171706.0000 - fn: 92.0000 - accuracy: 0.9993 - precision: 0.9060 - recall: 0.6974 - auc: 0.9469 - prc: 0.8153Restoring model weights from the end of the best epoch: 55.
90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - cross entropy: 0.0032 - Brier score: 5.7537e-04 - tp: 224.0000 - fp: 25.0000 - tn: 181930.0000 - fn: 97.0000 - accuracy: 0.9993 - precision: 0.8996 - recall: 0.6978 - auc: 0.9450 - prc: 0.8067 - val_loss: 0.0037 - val_cross entropy: 0.0037 - val_Brier score: 5.8603e-04 - val_tp: 50.0000 - val_fp: 5.0000 - val_tn: 45492.0000 - val_fn: 22.0000 - val_accuracy: 0.9994 - val_precision: 0.9091 - val_recall: 0.6944 - val_auc: 0.9024 - val_prc: 0.7505
Epoch 65: early stopping

Check training history

In this section, you will produce plots of your model's accuracy and loss on the training and validation set. These are useful to check for overfitting, which you can learn more about in the Overfit and underfit tutorial.

Additionally, you can produce these plots for any of the metrics you created above. False negatives are included as an example.

def plot_metrics(history):
  metrics = ['loss', 'prc', 'precision', 'recall']
  for n, metric in enumerate(metrics):
    name = metric.replace("_"," ").capitalize()
    plt.subplot(2,2,n+1)
    plt.plot(history.epoch, history.history[metric], color=colors[0], label='Train')
    plt.plot(history.epoch, history.history['val_'+metric],
             color=colors[0], linestyle="--", label='Val')
    plt.xlabel('Epoch')
    plt.ylabel(name)
    if metric == 'loss':
      plt.ylim([0, plt.ylim()[1]])
    elif metric == 'auc':
      plt.ylim([0.8,1])
    else:
      plt.ylim([0,1])

    plt.legend()
plot_metrics(baseline_history)

png

Evaluate metrics

You can use a confusion matrix to summarize the actual vs. predicted labels, where the X axis is the predicted label and the Y axis is the actual label:

train_predictions_baseline = model.predict(train_features, batch_size=BATCH_SIZE)
test_predictions_baseline = model.predict(test_features, batch_size=BATCH_SIZE)
90/90 [==============================] - 0s 1ms/step
28/28 [==============================] - 0s 1ms/step
def plot_cm(labels, predictions, threshold=0.5):
  cm = confusion_matrix(labels, predictions > threshold)
  plt.figure(figsize=(5,5))
  sns.heatmap(cm, annot=True, fmt="d")
  plt.title('Confusion matrix @{:.2f}'.format(threshold))
  plt.ylabel('Actual label')
  plt.xlabel('Predicted label')

  print('Legitimate Transactions Detected (True Negatives): ', cm[0][0])
  print('Legitimate Transactions Incorrectly Detected (False Positives): ', cm[0][1])
  print('Fraudulent Transactions Missed (False Negatives): ', cm[1][0])
  print('Fraudulent Transactions Detected (True Positives): ', cm[1][1])
  print('Total Fraudulent Transactions: ', np.sum(cm[1]))

Evaluate your model on the test dataset and display the results for the metrics you created above:

baseline_results = model.evaluate(test_features, test_labels,
                                  batch_size=BATCH_SIZE, verbose=0)
for name, value in zip(model.metrics_names, baseline_results):
  print(name, ': ', value)
print()

plot_cm(test_labels, test_predictions_baseline)
loss :  0.0028144640382379293
cross entropy :  0.0028144640382379293
Brier score :  0.0004354231059551239
tp :  79.0
fp :  6.0
tn :  56857.0
fn :  20.0
accuracy :  0.9995435476303101
precision :  0.929411768913269
recall :  0.7979797720909119
auc :  0.9239726662635803
prc :  0.8202375769615173

Legitimate Transactions Detected (True Negatives):  56857
Legitimate Transactions Incorrectly Detected (False Positives):  6
Fraudulent Transactions Missed (False Negatives):  20
Fraudulent Transactions Detected (True Positives):  79
Total Fraudulent Transactions:  99

png

If the model had predicted everything perfectly (impossible with true randomness), this would be a diagonal matrix where values off the main diagonal, indicating incorrect predictions, would be zero. In this case, the matrix shows that you have relatively few false positives, meaning that there were relatively few legitimate transactions that were incorrectly flagged.

Changing the threshold

The default threshold of \(t=50\%\) corresponds to equal costs of false negatives and false positives. In the case of fraud detection, however, you would likely associate higher costs to false negatives than to false positives. This trade off may be preferable because false negatives would allow fraudulent transactions to go through, whereas false positives may cause an email to be sent to a customer to ask them to verify their card activity.

By decreasing the threshold, we attribute higher cost to false negatives, thereby increasing missed transactions at the price of more false positives. We test thresholds at 10% and at 1%.

plot_cm(test_labels, test_predictions_baseline, threshold=0.1)
plot_cm(test_labels, test_predictions_baseline, threshold=0.01)
Legitimate Transactions Detected (True Negatives):  56847
Legitimate Transactions Incorrectly Detected (False Positives):  16
Fraudulent Transactions Missed (False Negatives):  17
Fraudulent Transactions Detected (True Positives):  82
Total Fraudulent Transactions:  99
Legitimate Transactions Detected (True Negatives):  56787
Legitimate Transactions Incorrectly Detected (False Positives):  76
Fraudulent Transactions Missed (False Negatives):  15
Fraudulent Transactions Detected (True Positives):  84
Total Fraudulent Transactions:  99

png

png

Plot the ROC

Now plot the ROC. This plot is useful because it shows, at a glance, the range of performance the model can reach by tuning the output threshold over its full range (0 to 1). So each point corresponds to a single value of the threshold.

def plot_roc(name, labels, predictions, **kwargs):
  fp, tp, _ = sklearn.metrics.roc_curve(labels, predictions)

  plt.plot(100*fp, 100*tp, label=name, linewidth=2, **kwargs)
  plt.xlabel('False positives [%]')
  plt.ylabel('True positives [%]')
  plt.xlim([-0.5,20])
  plt.ylim([80,100.5])
  plt.grid(True)
  ax = plt.gca()
  ax.set_aspect('equal')
plot_roc("Train Baseline", train_labels, train_predictions_baseline, color=colors[0])
plot_roc("Test Baseline", test_labels, test_predictions_baseline, color=colors[0], linestyle='--')
plt.legend(loc='lower right');

png

Plot the PRC

Now plot the AUPRC. Area under the interpolated precision-recall curve, obtained by plotting (recall, precision) points for different values of the classification threshold. Depending on how it's calculated, PR AUC may be equivalent to the average precision of the model.

def plot_prc(name, labels, predictions, **kwargs):
    precision, recall, _ = sklearn.metrics.precision_recall_curve(labels, predictions)

    plt.plot(precision, recall, label=name, linewidth=2, **kwargs)
    plt.xlabel('Precision')
    plt.ylabel('Recall')
    plt.grid(True)
    ax = plt.gca()
    ax.set_aspect('equal')
plot_prc("Train Baseline", train_labels, train_predictions_baseline, color=colors[0])
plot_prc("Test Baseline", test_labels, test_predictions_baseline, color=colors[0], linestyle='--')
plt.legend(loc='lower right');

png

It looks like the precision is relatively high, but the recall and the area under the ROC curve (AUC) aren't as high as you might like. Classifiers often face challenges when trying to maximize both precision and recall, which is especially true when working with imbalanced datasets. It is important to consider the costs of different types of errors in the context of the problem you care about. In this example, a false negative (a fraudulent transaction is missed) may have a financial cost, while a false positive (a transaction is incorrectly flagged as fraudulent) may decrease user happiness.

Class weights

Calculate class weights

The goal is to identify fraudulent transactions, but you don't have very many of those positive samples to work with, so you would want to have the classifier heavily weight the few examples that are available. You can do this by passing Keras weights for each class through a parameter. These will cause the model to "pay more attention" to examples from an under-represented class. Note, however, that this does not increase in any way the amount of information of your dataset. In the end, using class weights is more or less equivalent to changing the output bias or to changing the threshold. Let's see how it works out.

# Scaling by total/2 helps keep the loss to a similar magnitude.
# The sum of the weights of all examples stays the same.
weight_for_0 = (1 / neg) * (total / 2.0)
weight_for_1 = (1 / pos) * (total / 2.0)

class_weight = {0: weight_for_0, 1: weight_for_1}

print('Weight for class 0: {:.2f}'.format(weight_for_0))
print('Weight for class 1: {:.2f}'.format(weight_for_1))
Weight for class 0: 0.50
Weight for class 1: 289.44

Train a model with class weights

Now try re-training and evaluating the model with class weights to see how that affects the predictions.

weighted_model = make_model()
weighted_model.load_weights(initial_weights)

weighted_history = weighted_model.fit(
    train_features,
    train_labels,
    batch_size=BATCH_SIZE,
    epochs=EPOCHS,
    callbacks=[early_stopping],
    validation_data=(val_features, val_labels),
    # The class weights go here
    class_weight=class_weight)
Epoch 1/100
90/90 [==============================] - 2s 11ms/step - loss: 1.4571 - cross entropy: 0.0188 - Brier score: 0.0035 - tp: 167.0000 - fp: 730.0000 - tn: 238088.0000 - fn: 253.0000 - accuracy: 0.9959 - precision: 0.1862 - recall: 0.3976 - auc: 0.8525 - prc: 0.2485 - val_loss: 0.0143 - val_cross entropy: 0.0143 - val_Brier score: 0.0016 - val_tp: 46.0000 - val_fp: 41.0000 - val_tn: 45456.0000 - val_fn: 26.0000 - val_accuracy: 0.9985 - val_precision: 0.5287 - val_recall: 0.6389 - val_auc: 0.9319 - val_prc: 0.4959
Epoch 2/100
90/90 [==============================] - 1s 6ms/step - loss: 0.6082 - cross entropy: 0.0372 - Brier score: 0.0077 - tp: 213.0000 - fp: 1568.0000 - tn: 180387.0000 - fn: 108.0000 - accuracy: 0.9908 - precision: 0.1196 - recall: 0.6636 - auc: 0.9243 - prc: 0.3774 - val_loss: 0.0211 - val_cross entropy: 0.0211 - val_Brier score: 0.0027 - val_tp: 53.0000 - val_fp: 96.0000 - val_tn: 45401.0000 - val_fn: 19.0000 - val_accuracy: 0.9975 - val_precision: 0.3557 - val_recall: 0.7361 - val_auc: 0.9348 - val_prc: 0.5913
Epoch 3/100
90/90 [==============================] - 1s 6ms/step - loss: 0.3994 - cross entropy: 0.0520 - Brier score: 0.0112 - tp: 246.0000 - fp: 2388.0000 - tn: 179567.0000 - fn: 75.0000 - accuracy: 0.9865 - precision: 0.0934 - recall: 0.7664 - auc: 0.9441 - prc: 0.4198 - val_loss: 0.0292 - val_cross entropy: 0.0292 - val_Brier score: 0.0045 - val_tp: 54.0000 - val_fp: 213.0000 - val_tn: 45284.0000 - val_fn: 18.0000 - val_accuracy: 0.9949 - val_precision: 0.2022 - val_recall: 0.7500 - val_auc: 0.9393 - val_prc: 0.6181
Epoch 4/100
90/90 [==============================] - 1s 6ms/step - loss: 0.3518 - cross entropy: 0.0662 - Brier score: 0.0146 - tp: 268.0000 - fp: 3188.0000 - tn: 178767.0000 - fn: 53.0000 - accuracy: 0.9822 - precision: 0.0775 - recall: 0.8349 - auc: 0.9456 - prc: 0.4281 - val_loss: 0.0360 - val_cross entropy: 0.0360 - val_Brier score: 0.0062 - val_tp: 55.0000 - val_fp: 324.0000 - val_tn: 45173.0000 - val_fn: 17.0000 - val_accuracy: 0.9925 - val_precision: 0.1451 - val_recall: 0.7639 - val_auc: 0.9445 - val_prc: 0.5998
Epoch 5/100
90/90 [==============================] - 1s 6ms/step - loss: 0.2970 - cross entropy: 0.0799 - Brier score: 0.0177 - tp: 276.0000 - fp: 3840.0000 - tn: 178115.0000 - fn: 45.0000 - accuracy: 0.9787 - precision: 0.0671 - recall: 0.8598 - auc: 0.9563 - prc: 0.3691 - val_loss: 0.0447 - val_cross entropy: 0.0447 - val_Brier score: 0.0084 - val_tp: 55.0000 - val_fp: 450.0000 - val_tn: 45047.0000 - val_fn: 17.0000 - val_accuracy: 0.9898 - val_precision: 0.1089 - val_recall: 0.7639 - val_auc: 0.9500 - val_prc: 0.5852
Epoch 6/100
90/90 [==============================] - 1s 6ms/step - loss: 0.2825 - cross entropy: 0.0907 - Brier score: 0.0200 - tp: 277.0000 - fp: 4320.0000 - tn: 177635.0000 - fn: 44.0000 - accuracy: 0.9761 - precision: 0.0603 - recall: 0.8629 - auc: 0.9594 - prc: 0.3160 - val_loss: 0.0522 - val_cross entropy: 0.0522 - val_Brier score: 0.0102 - val_tp: 56.0000 - val_fp: 527.0000 - val_tn: 44970.0000 - val_fn: 16.0000 - val_accuracy: 0.9881 - val_precision: 0.0961 - val_recall: 0.7778 - val_auc: 0.9512 - val_prc: 0.5693
Epoch 7/100
90/90 [==============================] - 1s 6ms/step - loss: 0.2610 - cross entropy: 0.1010 - Brier score: 0.0222 - tp: 283.0000 - fp: 4837.0000 - tn: 177118.0000 - fn: 38.0000 - accuracy: 0.9733 - precision: 0.0553 - recall: 0.8816 - auc: 0.9594 - prc: 0.2988 - val_loss: 0.0578 - val_cross entropy: 0.0578 - val_Brier score: 0.0115 - val_tp: 56.0000 - val_fp: 582.0000 - val_tn: 44915.0000 - val_fn: 16.0000 - val_accuracy: 0.9869 - val_precision: 0.0878 - val_recall: 0.7778 - val_auc: 0.9534 - val_prc: 0.5649
Epoch 8/100
90/90 [==============================] - 1s 6ms/step - loss: 0.2476 - cross entropy: 0.1093 - Brier score: 0.0243 - tp: 281.0000 - fp: 5331.0000 - tn: 176624.0000 - fn: 40.0000 - accuracy: 0.9705 - precision: 0.0501 - recall: 0.8754 - auc: 0.9638 - prc: 0.2717 - val_loss: 0.0651 - val_cross entropy: 0.0651 - val_Brier score: 0.0133 - val_tp: 57.0000 - val_fp: 656.0000 - val_tn: 44841.0000 - val_fn: 15.0000 - val_accuracy: 0.9853 - val_precision: 0.0799 - val_recall: 0.7917 - val_auc: 0.9539 - val_prc: 0.5202
Epoch 9/100
90/90 [==============================] - 1s 6ms/step - loss: 0.2398 - cross entropy: 0.1218 - Brier score: 0.0265 - tp: 286.0000 - fp: 5684.0000 - tn: 176271.0000 - fn: 35.0000 - accuracy: 0.9686 - precision: 0.0479 - recall: 0.8910 - auc: 0.9646 - prc: 0.2462 - val_loss: 0.0739 - val_cross entropy: 0.0739 - val_Brier score: 0.0153 - val_tp: 58.0000 - val_fp: 800.0000 - val_tn: 44697.0000 - val_fn: 14.0000 - val_accuracy: 0.9821 - val_precision: 0.0676 - val_recall: 0.8056 - val_auc: 0.9593 - val_prc: 0.5023
Epoch 10/100
90/90 [==============================] - 1s 6ms/step - loss: 0.2345 - cross entropy: 0.1294 - Brier score: 0.0283 - tp: 285.0000 - fp: 6156.0000 - tn: 175799.0000 - fn: 36.0000 - accuracy: 0.9660 - precision: 0.0442 - recall: 0.8879 - auc: 0.9683 - prc: 0.2293 - val_loss: 0.0808 - val_cross entropy: 0.0808 - val_Brier score: 0.0168 - val_tp: 59.0000 - val_fp: 885.0000 - val_tn: 44612.0000 - val_fn: 13.0000 - val_accuracy: 0.9803 - val_precision: 0.0625 - val_recall: 0.8194 - val_auc: 0.9594 - val_prc: 0.4804
Epoch 11/100
90/90 [==============================] - 1s 6ms/step - loss: 0.2064 - cross entropy: 0.1316 - Brier score: 0.0287 - tp: 288.0000 - fp: 6178.0000 - tn: 175777.0000 - fn: 33.0000 - accuracy: 0.9659 - precision: 0.0445 - recall: 0.8972 - auc: 0.9735 - prc: 0.2334 - val_loss: 0.0794 - val_cross entropy: 0.0794 - val_Brier score: 0.0164 - val_tp: 59.0000 - val_fp: 842.0000 - val_tn: 44655.0000 - val_fn: 13.0000 - val_accuracy: 0.9812 - val_precision: 0.0655 - val_recall: 0.8194 - val_auc: 0.9599 - val_prc: 0.4822
Epoch 12/100
90/90 [==============================] - 1s 6ms/step - loss: 0.2009 - cross entropy: 0.1330 - Brier score: 0.0287 - tp: 286.0000 - fp: 6295.0000 - tn: 175660.0000 - fn: 35.0000 - accuracy: 0.9653 - precision: 0.0435 - recall: 0.8910 - auc: 0.9772 - prc: 0.2258 - val_loss: 0.0838 - val_cross entropy: 0.0838 - val_Brier score: 0.0174 - val_tp: 59.0000 - val_fp: 891.0000 - val_tn: 44606.0000 - val_fn: 13.0000 - val_accuracy: 0.9802 - val_precision: 0.0621 - val_recall: 0.8194 - val_auc: 0.9614 - val_prc: 0.4667
Epoch 13/100
78/90 [=========================>....] - ETA: 0s - loss: 0.2087 - cross entropy: 0.1328 - Brier score: 0.0290 - tp: 248.0000 - fp: 5509.0000 - tn: 153962.0000 - fn: 25.0000 - accuracy: 0.9654 - precision: 0.0431 - recall: 0.9084 - auc: 0.9711 - prc: 0.2191Restoring model weights from the end of the best epoch: 3.
90/90 [==============================] - 1s 6ms/step - loss: 0.1999 - cross entropy: 0.1335 - Brier score: 0.0293 - tp: 294.0000 - fp: 6339.0000 - tn: 175616.0000 - fn: 27.0000 - accuracy: 0.9651 - precision: 0.0443 - recall: 0.9159 - auc: 0.9740 - prc: 0.2328 - val_loss: 0.0826 - val_cross entropy: 0.0826 - val_Brier score: 0.0170 - val_tp: 59.0000 - val_fp: 867.0000 - val_tn: 44630.0000 - val_fn: 13.0000 - val_accuracy: 0.9807 - val_precision: 0.0637 - val_recall: 0.8194 - val_auc: 0.9620 - val_prc: 0.4767
Epoch 13: early stopping

Check training history

plot_metrics(weighted_history)

png

Evaluate metrics

train_predictions_weighted = weighted_model.predict(train_features, batch_size=BATCH_SIZE)
test_predictions_weighted = weighted_model.predict(test_features, batch_size=BATCH_SIZE)
90/90 [==============================] - 0s 1ms/step
28/28 [==============================] - 0s 1ms/step
weighted_results = weighted_model.evaluate(test_features, test_labels,
                                           batch_size=BATCH_SIZE, verbose=0)
for name, value in zip(weighted_model.metrics_names, weighted_results):
  print(name, ': ', value)
print()

plot_cm(test_labels, test_predictions_weighted)
loss :  0.027200935408473015
cross entropy :  0.027200935408473015
Brier score :  0.0043372539803385735
tp :  84.0
fp :  271.0
tn :  56592.0
fn :  15.0
accuracy :  0.9949790835380554
precision :  0.2366197109222412
recall :  0.8484848737716675
auc :  0.967598021030426
prc :  0.7439466714859009

Legitimate Transactions Detected (True Negatives):  56592
Legitimate Transactions Incorrectly Detected (False Positives):  271
Fraudulent Transactions Missed (False Negatives):  15
Fraudulent Transactions Detected (True Positives):  84
Total Fraudulent Transactions:  99

png

Here you can see that with class weights the accuracy and precision are lower because there are more false positives, but conversely the recall and AUC are higher because the model also found more true positives. Despite having lower accuracy, this model has higher recall (and identifies more fraudulent transactions than the baseline model at threshold 50%). Of course, there is a cost to both types of error (you wouldn't want to bug users by flagging too many legitimate transactions as fraudulent, either). Carefully consider the trade-offs between these different types of errors for your application.

Compared to the baseline model with changed threshold, the class weighted model is clearly inferior. The superiority of the baseline model is further confirmed by the lower test loss value (cross entropy and mean squared error) and additionally can be seen by plotting the ROC curves of both models together.

Plot the ROC

plot_roc("Train Baseline", train_labels, train_predictions_baseline, color=colors[0])
plot_roc("Test Baseline", test_labels, test_predictions_baseline, color=colors[0], linestyle='--')

plot_roc("Train Weighted", train_labels, train_predictions_weighted, color=colors[1])
plot_roc("Test Weighted", test_labels, test_predictions_weighted, color=colors[1], linestyle='--')


plt.legend(loc='lower right');

png

Plot the PRC

plot_prc("Train Baseline", train_labels, train_predictions_baseline, color=colors[0])
plot_prc("Test Baseline", test_labels, test_predictions_baseline, color=colors[0], linestyle='--')

plot_prc("Train Weighted", train_labels, train_predictions_weighted, color=colors[1])
plot_prc("Test Weighted", test_labels, test_predictions_weighted, color=colors[1], linestyle='--')


plt.legend(loc='lower right');

png

Oversampling

Oversample the minority class

A related approach would be to resample the dataset by oversampling the minority class.

pos_features = train_features[bool_train_labels]
neg_features = train_features[~bool_train_labels]

pos_labels = train_labels[bool_train_labels]
neg_labels = train_labels[~bool_train_labels]

Using NumPy

You can balance the dataset manually by choosing the right number of random indices from the positive examples:

ids = np.arange(len(pos_features))
choices = np.random.choice(ids, len(neg_features))

res_pos_features = pos_features[choices]
res_pos_labels = pos_labels[choices]

res_pos_features.shape
(181955, 29)
resampled_features = np.concatenate([res_pos_features, neg_features], axis=0)
resampled_labels = np.concatenate([res_pos_labels, neg_labels], axis=0)

order = np.arange(len(resampled_labels))
np.random.shuffle(order)
resampled_features = resampled_features[order]
resampled_labels = resampled_labels[order]

resampled_features.shape
(363910, 29)

Using tf.data

If you're using tf.data the easiest way to produce balanced examples is to start with a positive and a negative dataset, and merge them. See the tf.data guide for more examples.

BUFFER_SIZE = 100000

def make_ds(features, labels):
  ds = tf.data.Dataset.from_tensor_slices((features, labels))#.cache()
  ds = ds.shuffle(BUFFER_SIZE).repeat()
  return ds

pos_ds = make_ds(pos_features, pos_labels)
neg_ds = make_ds(neg_features, neg_labels)

Each dataset provides (feature, label) pairs:

for features, label in pos_ds.take(1):
  print("Features:\n", features.numpy())
  print()
  print("Label: ", label.numpy())
Features:
 [ 0.55064943  0.66667268 -0.8940899   1.42499457  0.51412155 -0.60658785
  0.1253631   0.13071879  0.00959075 -1.76170621  2.54795275 -0.30755193
 -1.77601643 -4.5976438  -0.18793709  1.1986865   4.60814304  2.09707224
 -1.08331714 -0.17246582 -0.44168984 -1.01104226 -0.17364212 -0.37019947
  1.09319705 -0.69660701  0.21815265  0.34618153 -1.45888394]

Label:  1

Merge the two together using tf.data.Dataset.sample_from_datasets:

resampled_ds = tf.data.Dataset.sample_from_datasets([pos_ds, neg_ds], weights=[0.5, 0.5])
resampled_ds = resampled_ds.batch(BATCH_SIZE).prefetch(2)
for features, label in resampled_ds.take(1):
  print(label.numpy().mean())
0.50732421875

To use this dataset, you'll need the number of steps per epoch.

The definition of "epoch" in this case is less clear. Say it's the number of batches required to see each negative example once:

resampled_steps_per_epoch = np.ceil(2.0*neg/BATCH_SIZE)
resampled_steps_per_epoch
278.0

Train on the oversampled data

Now try training the model with the resampled data set instead of using class weights to see how these methods compare.

resampled_model = make_model()
resampled_model.load_weights(initial_weights)

# Reset the bias to zero, since this dataset is balanced.
output_layer = resampled_model.layers[-1] 
output_layer.bias.assign([0])

val_ds = tf.data.Dataset.from_tensor_slices((val_features, val_labels)).cache()
val_ds = val_ds.batch(BATCH_SIZE).prefetch(2) 

resampled_history = resampled_model.fit(
    resampled_ds,
    epochs=EPOCHS,
    steps_per_epoch=resampled_steps_per_epoch,
    callbacks=[early_stopping],
    validation_data=val_ds)
Epoch 1/100
278/278 [==============================] - 8s 22ms/step - loss: 0.4214 - cross entropy: 0.3855 - Brier score: 0.1294 - tp: 261180.0000 - fp: 99585.0000 - tn: 241602.0000 - fn: 23939.0000 - accuracy: 0.8028 - precision: 0.7240 - recall: 0.9160 - auc: 0.9354 - prc: 0.9440 - val_loss: 0.2816 - val_cross entropy: 0.2816 - val_Brier score: 0.0689 - val_tp: 59.0000 - val_fp: 1593.0000 - val_tn: 43904.0000 - val_fn: 13.0000 - val_accuracy: 0.9648 - val_precision: 0.0357 - val_recall: 0.8194 - val_auc: 0.9363 - val_prc: 0.6488
Epoch 2/100
278/278 [==============================] - 6s 21ms/step - loss: 0.2042 - cross entropy: 0.2042 - Brier score: 0.0608 - tp: 263967.0000 - fp: 19492.0000 - tn: 265875.0000 - fn: 20010.0000 - accuracy: 0.9306 - precision: 0.9312 - recall: 0.9295 - auc: 0.9754 - prc: 0.9813 - val_loss: 0.1414 - val_cross entropy: 0.1414 - val_Brier score: 0.0289 - val_tp: 59.0000 - val_fp: 958.0000 - val_tn: 44539.0000 - val_fn: 13.0000 - val_accuracy: 0.9787 - val_precision: 0.0580 - val_recall: 0.8194 - val_auc: 0.9617 - val_prc: 0.6593
Epoch 3/100
278/278 [==============================] - 6s 20ms/step - loss: 0.1592 - cross entropy: 0.1592 - Brier score: 0.0466 - tp: 266838.0000 - fp: 11626.0000 - tn: 272852.0000 - fn: 18028.0000 - accuracy: 0.9479 - precision: 0.9582 - recall: 0.9367 - auc: 0.9840 - prc: 0.9871 - val_loss: 0.1014 - val_cross entropy: 0.1014 - val_Brier score: 0.0211 - val_tp: 59.0000 - val_fp: 860.0000 - val_tn: 44637.0000 - val_fn: 13.0000 - val_accuracy: 0.9808 - val_precision: 0.0642 - val_recall: 0.8194 - val_auc: 0.9651 - val_prc: 0.6399
Epoch 4/100
278/278 [==============================] - 6s 21ms/step - loss: 0.1379 - cross entropy: 0.1379 - Brier score: 0.0404 - tp: 267860.0000 - fp: 9722.0000 - tn: 274900.0000 - fn: 16862.0000 - accuracy: 0.9533 - precision: 0.9650 - recall: 0.9408 - auc: 0.9880 - prc: 0.9898 - val_loss: 0.0830 - val_cross entropy: 0.0830 - val_Brier score: 0.0180 - val_tp: 60.0000 - val_fp: 829.0000 - val_tn: 44668.0000 - val_fn: 12.0000 - val_accuracy: 0.9815 - val_precision: 0.0675 - val_recall: 0.8333 - val_auc: 0.9669 - val_prc: 0.6307
Epoch 5/100
278/278 [==============================] - 6s 21ms/step - loss: 0.1245 - cross entropy: 0.1245 - Brier score: 0.0366 - tp: 268894.0000 - fp: 8642.0000 - tn: 275511.0000 - fn: 16297.0000 - accuracy: 0.9562 - precision: 0.9689 - recall: 0.9429 - auc: 0.9903 - prc: 0.9916 - val_loss: 0.0746 - val_cross entropy: 0.0746 - val_Brier score: 0.0168 - val_tp: 60.0000 - val_fp: 810.0000 - val_tn: 44687.0000 - val_fn: 12.0000 - val_accuracy: 0.9820 - val_precision: 0.0690 - val_recall: 0.8333 - val_auc: 0.9674 - val_prc: 0.6211
Epoch 6/100
278/278 [==============================] - 6s 21ms/step - loss: 0.1131 - cross entropy: 0.1131 - Brier score: 0.0333 - tp: 269995.0000 - fp: 8058.0000 - tn: 276068.0000 - fn: 15223.0000 - accuracy: 0.9591 - precision: 0.9710 - recall: 0.9466 - auc: 0.9924 - prc: 0.9931 - val_loss: 0.0680 - val_cross entropy: 0.0680 - val_Brier score: 0.0158 - val_tp: 60.0000 - val_fp: 800.0000 - val_tn: 44697.0000 - val_fn: 12.0000 - val_accuracy: 0.9822 - val_precision: 0.0698 - val_recall: 0.8333 - val_auc: 0.9640 - val_prc: 0.6195
Epoch 7/100
278/278 [==============================] - 6s 21ms/step - loss: 0.1060 - cross entropy: 0.1060 - Brier score: 0.0312 - tp: 269801.0000 - fp: 7531.0000 - tn: 277138.0000 - fn: 14874.0000 - accuracy: 0.9606 - precision: 0.9728 - recall: 0.9478 - auc: 0.9936 - prc: 0.9940 - val_loss: 0.0627 - val_cross entropy: 0.0627 - val_Brier score: 0.0148 - val_tp: 60.0000 - val_fp: 739.0000 - val_tn: 44758.0000 - val_fn: 12.0000 - val_accuracy: 0.9835 - val_precision: 0.0751 - val_recall: 0.8333 - val_auc: 0.9632 - val_prc: 0.6195
Epoch 8/100
278/278 [==============================] - 6s 21ms/step - loss: 0.0999 - cross entropy: 0.0999 - Brier score: 0.0295 - tp: 270426.0000 - fp: 7176.0000 - tn: 277357.0000 - fn: 14385.0000 - accuracy: 0.9621 - precision: 0.9742 - recall: 0.9495 - auc: 0.9945 - prc: 0.9947 - val_loss: 0.0589 - val_cross entropy: 0.0589 - val_Brier score: 0.0142 - val_tp: 60.0000 - val_fp: 711.0000 - val_tn: 44786.0000 - val_fn: 12.0000 - val_accuracy: 0.9841 - val_precision: 0.0778 - val_recall: 0.8333 - val_auc: 0.9572 - val_prc: 0.6103
Epoch 9/100
278/278 [==============================] - 6s 21ms/step - loss: 0.0950 - cross entropy: 0.0950 - Brier score: 0.0281 - tp: 270894.0000 - fp: 6860.0000 - tn: 277789.0000 - fn: 13801.0000 - accuracy: 0.9637 - precision: 0.9753 - recall: 0.9515 - auc: 0.9951 - prc: 0.9952 - val_loss: 0.0543 - val_cross entropy: 0.0543 - val_Brier score: 0.0131 - val_tp: 60.0000 - val_fp: 639.0000 - val_tn: 44858.0000 - val_fn: 12.0000 - val_accuracy: 0.9857 - val_precision: 0.0858 - val_recall: 0.8333 - val_auc: 0.9537 - val_prc: 0.6102
Epoch 10/100
278/278 [==============================] - 6s 22ms/step - loss: 0.0911 - cross entropy: 0.0911 - Brier score: 0.0270 - tp: 270968.0000 - fp: 6510.0000 - tn: 278109.0000 - fn: 13757.0000 - accuracy: 0.9644 - precision: 0.9765 - recall: 0.9517 - auc: 0.9955 - prc: 0.9955 - val_loss: 0.0504 - val_cross entropy: 0.0504 - val_Brier score: 0.0122 - val_tp: 59.0000 - val_fp: 610.0000 - val_tn: 44887.0000 - val_fn: 13.0000 - val_accuracy: 0.9863 - val_precision: 0.0882 - val_recall: 0.8194 - val_auc: 0.9547 - val_prc: 0.6115
Epoch 11/100
278/278 [==============================] - 6s 21ms/step - loss: 0.0862 - cross entropy: 0.0862 - Brier score: 0.0256 - tp: 271597.0000 - fp: 6354.0000 - tn: 278121.0000 - fn: 13272.0000 - accuracy: 0.9655 - precision: 0.9771 - recall: 0.9534 - auc: 0.9961 - prc: 0.9959 - val_loss: 0.0475 - val_cross entropy: 0.0475 - val_Brier score: 0.0116 - val_tp: 59.0000 - val_fp: 591.0000 - val_tn: 44906.0000 - val_fn: 13.0000 - val_accuracy: 0.9867 - val_precision: 0.0908 - val_recall: 0.8194 - val_auc: 0.9509 - val_prc: 0.6116
Epoch 12/100
278/278 [==============================] - ETA: 0s - loss: 0.0833 - cross entropy: 0.0833 - Brier score: 0.0246 - tp: 272212.0000 - fp: 6195.0000 - tn: 278209.0000 - fn: 12728.0000 - accuracy: 0.9668 - precision: 0.9777 - recall: 0.9553 - auc: 0.9964 - prc: 0.9961Restoring model weights from the end of the best epoch: 2.
278/278 [==============================] - 6s 21ms/step - loss: 0.0833 - cross entropy: 0.0833 - Brier score: 0.0246 - tp: 272212.0000 - fp: 6195.0000 - tn: 278209.0000 - fn: 12728.0000 - accuracy: 0.9668 - precision: 0.9777 - recall: 0.9553 - auc: 0.9964 - prc: 0.9961 - val_loss: 0.0441 - val_cross entropy: 0.0441 - val_Brier score: 0.0108 - val_tp: 59.0000 - val_fp: 544.0000 - val_tn: 44953.0000 - val_fn: 13.0000 - val_accuracy: 0.9878 - val_precision: 0.0978 - val_recall: 0.8194 - val_auc: 0.9465 - val_prc: 0.6118
Epoch 12: early stopping

If the training process were considering the whole dataset on each gradient update, this oversampling would be basically identical to the class weighting.

But when training the model batch-wise, as you did here, the oversampled data provides a smoother gradient signal: Instead of each positive example being shown in one batch with a large weight, they're shown in many different batches each time with a small weight.

This smoother gradient signal makes it easier to train the model.

Check training history

Note that the distributions of metrics will be different here, because the training data has a totally different distribution from the validation and test data.

plot_metrics(resampled_history)

png

Re-train

Because training is easier on the balanced data, the above training procedure may overfit quickly.

So break up the epochs to give the tf.keras.callbacks.EarlyStopping finer control over when to stop training.

resampled_model = make_model()
resampled_model.load_weights(initial_weights)

# Reset the bias to zero, since this dataset is balanced.
output_layer = resampled_model.layers[-1] 
output_layer.bias.assign([0])

resampled_history = resampled_model.fit(
    resampled_ds,
    # These are not real epochs
    steps_per_epoch=20,
    epochs=10*EPOCHS,
    callbacks=[early_stopping],
    validation_data=(val_ds))
Epoch 1/1000
20/20 [==============================] - 3s 49ms/step - loss: 0.8337 - cross entropy: 0.4179 - Brier score: 0.1416 - tp: 16462.0000 - fp: 15557.0000 - tn: 50236.0000 - fn: 4274.0000 - accuracy: 0.7708 - precision: 0.5141 - recall: 0.7939 - auc: 0.8927 - prc: 0.7518 - val_loss: 1.0856 - val_cross entropy: 1.0856 - val_Brier score: 0.4168 - val_tp: 69.0000 - val_fp: 39154.0000 - val_tn: 6343.0000 - val_fn: 3.0000 - val_accuracy: 0.1407 - val_precision: 0.0018 - val_recall: 0.9583 - val_auc: 0.8951 - val_prc: 0.1270
Epoch 2/1000
20/20 [==============================] - 1s 30ms/step - loss: 0.6566 - cross entropy: 0.6566 - Brier score: 0.2313 - tp: 18697.0000 - fp: 14164.0000 - tn: 6176.0000 - fn: 1923.0000 - accuracy: 0.6073 - precision: 0.5690 - recall: 0.9067 - auc: 0.8383 - prc: 0.8802 - val_loss: 0.9722 - val_cross entropy: 0.9722 - val_Brier score: 0.3710 - val_tp: 69.0000 - val_fp: 35747.0000 - val_tn: 9750.0000 - val_fn: 3.0000 - val_accuracy: 0.2155 - val_precision: 0.0019 - val_recall: 0.9583 - val_auc: 0.9087 - val_prc: 0.4256
Epoch 3/1000
20/20 [==============================] - 1s 30ms/step - loss: 0.5645 - cross entropy: 0.5645 - Brier score: 0.2003 - tp: 19180.0000 - fp: 12662.0000 - tn: 7675.0000 - fn: 1443.0000 - accuracy: 0.6556 - precision: 0.6023 - recall: 0.9300 - auc: 0.8927 - prc: 0.9237 - val_loss: 0.8554 - val_cross entropy: 0.8554 - val_Brier score: 0.3206 - val_tp: 68.0000 - val_fp: 30175.0000 - val_tn: 15322.0000 - val_fn: 4.0000 - val_accuracy: 0.3377 - val_precision: 0.0022 - val_recall: 0.9444 - val_auc: 0.9093 - val_prc: 0.5325
Epoch 4/1000
20/20 [==============================] - 1s 27ms/step - loss: 0.5053 - cross entropy: 0.5053 - Brier score: 0.1785 - tp: 19073.0000 - fp: 11080.0000 - tn: 9507.0000 - fn: 1300.0000 - accuracy: 0.6978 - precision: 0.6325 - recall: 0.9362 - auc: 0.9155 - prc: 0.9400 - val_loss: 0.7480 - val_cross entropy: 0.7480 - val_Brier score: 0.2721 - val_tp: 67.0000 - val_fp: 22970.0000 - val_tn: 22527.0000 - val_fn: 5.0000 - val_accuracy: 0.4958 - val_precision: 0.0029 - val_recall: 0.9306 - val_auc: 0.9087 - val_prc: 0.5706
Epoch 5/1000
20/20 [==============================] - 1s 26ms/step - loss: 0.4485 - cross entropy: 0.4485 - Brier score: 0.1561 - tp: 19164.0000 - fp: 9061.0000 - tn: 11377.0000 - fn: 1358.0000 - accuracy: 0.7456 - precision: 0.6790 - recall: 0.9338 - auc: 0.9270 - prc: 0.9492 - val_loss: 0.6579 - val_cross entropy: 0.6579 - val_Brier score: 0.2304 - val_tp: 65.0000 - val_fp: 15933.0000 - val_tn: 29564.0000 - val_fn: 7.0000 - val_accuracy: 0.6502 - val_precision: 0.0041 - val_recall: 0.9028 - val_auc: 0.9083 - val_prc: 0.5999
Epoch 6/1000
20/20 [==============================] - 0s 26ms/step - loss: 0.4142 - cross entropy: 0.4142 - Brier score: 0.1418 - tp: 18889.0000 - fp: 7657.0000 - tn: 12944.0000 - fn: 1470.0000 - accuracy: 0.7772 - precision: 0.7116 - recall: 0.9278 - auc: 0.9333 - prc: 0.9534 - val_loss: 0.5824 - val_cross entropy: 0.5824 - val_Brier score: 0.1955 - val_tp: 63.0000 - val_fp: 10310.0000 - val_tn: 35187.0000 - val_fn: 9.0000 - val_accuracy: 0.7736 - val_precision: 0.0061 - val_recall: 0.8750 - val_auc: 0.9075 - val_prc: 0.6148
Epoch 7/1000
20/20 [==============================] - 1s 27ms/step - loss: 0.3758 - cross entropy: 0.3758 - Brier score: 0.1263 - tp: 19132.0000 - fp: 6111.0000 - tn: 14226.0000 - fn: 1491.0000 - accuracy: 0.8144 - precision: 0.7579 - recall: 0.9277 - auc: 0.9408 - prc: 0.9594 - val_loss: 0.5207 - val_cross entropy: 0.5207 - val_Brier score: 0.1672 - val_tp: 62.0000 - val_fp: 6890.0000 - val_tn: 38607.0000 - val_fn: 10.0000 - val_accuracy: 0.8486 - val_precision: 0.0089 - val_recall: 0.8611 - val_auc: 0.9083 - val_prc: 0.6217
Epoch 8/1000
20/20 [==============================] - 0s 26ms/step - loss: 0.3532 - cross entropy: 0.3532 - Brier score: 0.1166 - tp: 18771.0000 - fp: 5173.0000 - tn: 15500.0000 - fn: 1516.0000 - accuracy: 0.8367 - precision: 0.7840 - recall: 0.9253 - auc: 0.9455 - prc: 0.9618 - val_loss: 0.4679 - val_cross entropy: 0.4679 - val_Brier score: 0.1436 - val_tp: 59.0000 - val_fp: 4902.0000 - val_tn: 40595.0000 - val_fn: 13.0000 - val_accuracy: 0.8921 - val_precision: 0.0119 - val_recall: 0.8194 - val_auc: 0.9099 - val_prc: 0.6272
Epoch 9/1000
20/20 [==============================] - 1s 27ms/step - loss: 0.3237 - cross entropy: 0.3237 - Brier score: 0.1048 - tp: 18942.0000 - fp: 4215.0000 - tn: 16271.0000 - fn: 1532.0000 - accuracy: 0.8597 - precision: 0.8180 - recall: 0.9252 - auc: 0.9519 - prc: 0.9663 - val_loss: 0.4229 - val_cross entropy: 0.4229 - val_Brier score: 0.1241 - val_tp: 59.0000 - val_fp: 3648.0000 - val_tn: 41849.0000 - val_fn: 13.0000 - val_accuracy: 0.9197 - val_precision: 0.0159 - val_recall: 0.8194 - val_auc: 0.9122 - val_prc: 0.6306
Epoch 10/1000
20/20 [==============================] - 0s 25ms/step - loss: 0.3093 - cross entropy: 0.3093 - Brier score: 0.0987 - tp: 18899.0000 - fp: 3723.0000 - tn: 16753.0000 - fn: 1585.0000 - accuracy: 0.8704 - precision: 0.8354 - recall: 0.9226 - auc: 0.9539 - prc: 0.9672 - val_loss: 0.3849 - val_cross entropy: 0.3849 - val_Brier score: 0.1082 - val_tp: 59.0000 - val_fp: 2837.0000 - val_tn: 42660.0000 - val_fn: 13.0000 - val_accuracy: 0.9375 - val_precision: 0.0204 - val_recall: 0.8194 - val_auc: 0.9148 - val_prc: 0.6333
Epoch 11/1000
20/20 [==============================] - 0s 26ms/step - loss: 0.2907 - cross entropy: 0.2907 - Brier score: 0.0916 - tp: 19000.0000 - fp: 3037.0000 - tn: 17316.0000 - fn: 1607.0000 - accuracy: 0.8866 - precision: 0.8622 - recall: 0.9220 - auc: 0.9563 - prc: 0.9699 - val_loss: 0.3539 - val_cross entropy: 0.3539 - val_Brier score: 0.0958 - val_tp: 59.0000 - val_fp: 2380.0000 - val_tn: 43117.0000 - val_fn: 13.0000 - val_accuracy: 0.9475 - val_precision: 0.0242 - val_recall: 0.8194 - val_auc: 0.9217 - val_prc: 0.6383
Epoch 12/1000
20/20 [==============================] - 1s 27ms/step - loss: 0.2794 - cross entropy: 0.2794 - Brier score: 0.0874 - tp: 18879.0000 - fp: 2871.0000 - tn: 17633.0000 - fn: 1577.0000 - accuracy: 0.8914 - precision: 0.8680 - recall: 0.9229 - auc: 0.9602 - prc: 0.9714 - val_loss: 0.3262 - val_cross entropy: 0.3262 - val_Brier score: 0.0851 - val_tp: 59.0000 - val_fp: 2030.0000 - val_tn: 43467.0000 - val_fn: 13.0000 - val_accuracy: 0.9552 - val_precision: 0.0282 - val_recall: 0.8194 - val_auc: 0.9278 - val_prc: 0.6419
Epoch 13/1000
20/20 [==============================] - 1s 26ms/step - loss: 0.2648 - cross entropy: 0.2648 - Brier score: 0.0820 - tp: 18832.0000 - fp: 2436.0000 - tn: 18061.0000 - fn: 1631.0000 - accuracy: 0.9007 - precision: 0.8855 - recall: 0.9203 - auc: 0.9621 - prc: 0.9727 - val_loss: 0.3018 - val_cross entropy: 0.3018 - val_Brier score: 0.0760 - val_tp: 59.0000 - val_fp: 1762.0000 - val_tn: 43735.0000 - val_fn: 13.0000 - val_accuracy: 0.9610 - val_precision: 0.0324 - val_recall: 0.8194 - val_auc: 0.9333 - val_prc: 0.6453
Epoch 14/1000
20/20 [==============================] - 1s 26ms/step - loss: 0.2544 - cross entropy: 0.2544 - Brier score: 0.0779 - tp: 18934.0000 - fp: 2270.0000 - tn: 18183.0000 - fn: 1573.0000 - accuracy: 0.9062 - precision: 0.8929 - recall: 0.9233 - auc: 0.9648 - prc: 0.9744 - val_loss: 0.2801 - val_cross entropy: 0.2801 - val_Brier score: 0.0684 - val_tp: 59.0000 - val_fp: 1582.0000 - val_tn: 43915.0000 - val_fn: 13.0000 - val_accuracy: 0.9650 - val_precision: 0.0360 - val_recall: 0.8194 - val_auc: 0.9377 - val_prc: 0.6475
Epoch 15/1000
20/20 [==============================] - 0s 26ms/step - loss: 0.2427 - cross entropy: 0.2427 - Brier score: 0.0738 - tp: 18870.0000 - fp: 1994.0000 - tn: 18591.0000 - fn: 1505.0000 - accuracy: 0.9146 - precision: 0.9044 - recall: 0.9261 - auc: 0.9677 - prc: 0.9763 - val_loss: 0.2616 - val_cross entropy: 0.2616 - val_Brier score: 0.0621 - val_tp: 59.0000 - val_fp: 1470.0000 - val_tn: 44027.0000 - val_fn: 13.0000 - val_accuracy: 0.9675 - val_precision: 0.0386 - val_recall: 0.8194 - val_auc: 0.9419 - val_prc: 0.6496
Epoch 16/1000
20/20 [==============================] - 1s 26ms/step - loss: 0.2333 - cross entropy: 0.2333 - Brier score: 0.0706 - tp: 18954.0000 - fp: 1884.0000 - tn: 18634.0000 - fn: 1488.0000 - accuracy: 0.9177 - precision: 0.9096 - recall: 0.9272 - auc: 0.9700 - prc: 0.9777 - val_loss: 0.2453 - val_cross entropy: 0.2453 - val_Brier score: 0.0569 - val_tp: 59.0000 - val_fp: 1390.0000 - val_tn: 44107.0000 - val_fn: 13.0000 - val_accuracy: 0.9692 - val_precision: 0.0407 - val_recall: 0.8194 - val_auc: 0.9446 - val_prc: 0.6517
Epoch 17/1000
20/20 [==============================] - 1s 27ms/step - loss: 0.2248 - cross entropy: 0.2248 - Brier score: 0.0678 - tp: 19021.0000 - fp: 1736.0000 - tn: 18720.0000 - fn: 1483.0000 - accuracy: 0.9214 - precision: 0.9164 - recall: 0.9277 - auc: 0.9717 - prc: 0.9790 - val_loss: 0.2303 - val_cross entropy: 0.2303 - val_Brier score: 0.0522 - val_tp: 58.0000 - val_fp: 1317.0000 - val_tn: 44180.0000 - val_fn: 14.0000 - val_accuracy: 0.9708 - val_precision: 0.0422 - val_recall: 0.8056 - val_auc: 0.9475 - val_prc: 0.6540
Epoch 18/1000
20/20 [==============================] - 1s 26ms/step - loss: 0.2192 - cross entropy: 0.2192 - Brier score: 0.0657 - tp: 18905.0000 - fp: 1619.0000 - tn: 18997.0000 - fn: 1439.0000 - accuracy: 0.9253 - precision: 0.9211 - recall: 0.9293 - auc: 0.9725 - prc: 0.9793 - val_loss: 0.2164 - val_cross entropy: 0.2164 - val_Brier score: 0.0480 - val_tp: 58.0000 - val_fp: 1237.0000 - val_tn: 44260.0000 - val_fn: 14.0000 - val_accuracy: 0.9725 - val_precision: 0.0448 - val_recall: 0.8056 - val_auc: 0.9497 - val_prc: 0.6559
Epoch 19/1000
20/20 [==============================] - 0s 26ms/step - loss: 0.2111 - cross entropy: 0.2111 - Brier score: 0.0630 - tp: 19098.0000 - fp: 1449.0000 - tn: 18934.0000 - fn: 1479.0000 - accuracy: 0.9285 - precision: 0.9295 - recall: 0.9281 - auc: 0.9737 - prc: 0.9803 - val_loss: 0.2050 - val_cross entropy: 0.2050 - val_Brier score: 0.0448 - val_tp: 58.0000 - val_fp: 1203.0000 - val_tn: 44294.0000 - val_fn: 14.0000 - val_accuracy: 0.9733 - val_precision: 0.0460 - val_recall: 0.8056 - val_auc: 0.9522 - val_prc: 0.6579
Epoch 20/1000
20/20 [==============================] - 1s 26ms/step - loss: 0.2097 - cross entropy: 0.2097 - Brier score: 0.0627 - tp: 18934.0000 - fp: 1483.0000 - tn: 19076.0000 - fn: 1467.0000 - accuracy: 0.9280 - precision: 0.9274 - recall: 0.9281 - auc: 0.9740 - prc: 0.9802 - val_loss: 0.1947 - val_cross entropy: 0.1947 - val_Brier score: 0.0420 - val_tp: 58.0000 - val_fp: 1162.0000 - val_tn: 44335.0000 - val_fn: 14.0000 - val_accuracy: 0.9742 - val_precision: 0.0475 - val_recall: 0.8056 - val_auc: 0.9546 - val_prc: 0.6599
Epoch 21/1000
20/20 [==============================] - 1s 26ms/step - loss: 0.2047 - cross entropy: 0.2047 - Brier score: 0.0610 - tp: 18842.0000 - fp: 1412.0000 - tn: 19243.0000 - fn: 1463.0000 - accuracy: 0.9298 - precision: 0.9303 - recall: 0.9279 - auc: 0.9749 - prc: 0.9807 - val_loss: 0.1845 - val_cross entropy: 0.1845 - val_Brier score: 0.0392 - val_tp: 58.0000 - val_fp: 1103.0000 - val_tn: 44394.0000 - val_fn: 14.0000 - val_accuracy: 0.9755 - val_precision: 0.0500 - val_recall: 0.8056 - val_auc: 0.9563 - val_prc: 0.6594
Epoch 22/1000
20/20 [==============================] - 0s 25ms/step - loss: 0.1969 - cross entropy: 0.1969 - Brier score: 0.0582 - tp: 19049.0000 - fp: 1263.0000 - tn: 19224.0000 - fn: 1424.0000 - accuracy: 0.9344 - precision: 0.9378 - recall: 0.9304 - auc: 0.9770 - prc: 0.9822 - val_loss: 0.1760 - val_cross entropy: 0.1760 - val_Brier score: 0.0370 - val_tp: 58.0000 - val_fp: 1060.0000 - val_tn: 44437.0000 - val_fn: 14.0000 - val_accuracy: 0.9764 - val_precision: 0.0519 - val_recall: 0.8056 - val_auc: 0.9572 - val_prc: 0.6598
Epoch 23/1000
20/20 [==============================] - 0s 26ms/step - loss: 0.1915 - cross entropy: 0.1915 - Brier score: 0.0565 - tp: 19016.0000 - fp: 1198.0000 - tn: 19313.0000 - fn: 1433.0000 - accuracy: 0.9358 - precision: 0.9407 - recall: 0.9299 - auc: 0.9780 - prc: 0.9829 - val_loss: 0.1689 - val_cross entropy: 0.1689 - val_Brier score: 0.0353 - val_tp: 58.0000 - val_fp: 1052.0000 - val_tn: 44445.0000 - val_fn: 14.0000 - val_accuracy: 0.9766 - val_precision: 0.0523 - val_recall: 0.8056 - val_auc: 0.9582 - val_prc: 0.6609
Epoch 24/1000
20/20 [==============================] - 1s 27ms/step - loss: 0.1891 - cross entropy: 0.1891 - Brier score: 0.0558 - tp: 19083.0000 - fp: 1167.0000 - tn: 19360.0000 - fn: 1350.0000 - accuracy: 0.9385 - precision: 0.9424 - recall: 0.9339 - auc: 0.9785 - prc: 0.9834 - val_loss: 0.1620 - val_cross entropy: 0.1620 - val_Brier score: 0.0336 - val_tp: 58.0000 - val_fp: 1032.0000 - val_tn: 44465.0000 - val_fn: 14.0000 - val_accuracy: 0.9770 - val_precision: 0.0532 - val_recall: 0.8056 - val_auc: 0.9587 - val_prc: 0.6617
Epoch 25/1000
20/20 [==============================] - 1s 27ms/step - loss: 0.1891 - cross entropy: 0.1891 - Brier score: 0.0556 - tp: 19055.0000 - fp: 1154.0000 - tn: 19371.0000 - fn: 1380.0000 - accuracy: 0.9381 - precision: 0.9429 - recall: 0.9325 - auc: 0.9784 - prc: 0.9831 - val_loss: 0.1552 - val_cross entropy: 0.1552 - val_Brier score: 0.0319 - val_tp: 58.0000 - val_fp: 1004.0000 - val_tn: 44493.0000 - val_fn: 14.0000 - val_accuracy: 0.9777 - val_precision: 0.0546 - val_recall: 0.8056 - val_auc: 0.9596 - val_prc: 0.6631
Epoch 26/1000
20/20 [==============================] - 1s 27ms/step - loss: 0.1815 - cross entropy: 0.1815 - Brier score: 0.0536 - tp: 19093.0000 - fp: 1089.0000 - tn: 19407.0000 - fn: 1371.0000 - accuracy: 0.9399 - precision: 0.9460 - recall: 0.9330 - auc: 0.9797 - prc: 0.9843 - val_loss: 0.1500 - val_cross entropy: 0.1500 - val_Brier score: 0.0308 - val_tp: 58.0000 - val_fp: 995.0000 - val_tn: 44502.0000 - val_fn: 14.0000 - val_accuracy: 0.9779 - val_precision: 0.0551 - val_recall: 0.8056 - val_auc: 0.9608 - val_prc: 0.6649
Epoch 27/1000
20/20 [==============================] - 1s 26ms/step - loss: 0.1798 - cross entropy: 0.1798 - Brier score: 0.0529 - tp: 19057.0000 - fp: 1048.0000 - tn: 19520.0000 - fn: 1335.0000 - accuracy: 0.9418 - precision: 0.9479 - recall: 0.9345 - auc: 0.9799 - prc: 0.9842 - val_loss: 0.1449 - val_cross entropy: 0.1449 - val_Brier score: 0.0297 - val_tp: 58.0000 - val_fp: 977.0000 - val_tn: 44520.0000 - val_fn: 14.0000 - val_accuracy: 0.9783 - val_precision: 0.0560 - val_recall: 0.8056 - val_auc: 0.9611 - val_prc: 0.6652
Epoch 28/1000
20/20 [==============================] - 1s 27ms/step - loss: 0.1747 - cross entropy: 0.1747 - Brier score: 0.0516 - tp: 19079.0000 - fp: 1007.0000 - tn: 19496.0000 - fn: 1378.0000 - accuracy: 0.9418 - precision: 0.9499 - recall: 0.9326 - auc: 0.9811 - prc: 0.9851 - val_loss: 0.1406 - val_cross entropy: 0.1406 - val_Brier score: 0.0288 - val_tp: 58.0000 - val_fp: 967.0000 - val_tn: 44530.0000 - val_fn: 14.0000 - val_accuracy: 0.9785 - val_precision: 0.0566 - val_recall: 0.8056 - val_auc: 0.9611 - val_prc: 0.6498
Epoch 29/1000
20/20 [==============================] - 1s 28ms/step - loss: 0.1728 - cross entropy: 0.1728 - Brier score: 0.0504 - tp: 19050.0000 - fp: 984.0000 - tn: 19591.0000 - fn: 1335.0000 - accuracy: 0.9434 - precision: 0.9509 - recall: 0.9345 - auc: 0.9817 - prc: 0.9853 - val_loss: 0.1361 - val_cross entropy: 0.1361 - val_Brier score: 0.0278 - val_tp: 59.0000 - val_fp: 961.0000 - val_tn: 44536.0000 - val_fn: 13.0000 - val_accuracy: 0.9786 - val_precision: 0.0578 - val_recall: 0.8194 - val_auc: 0.9619 - val_prc: 0.6500
Epoch 30/1000
20/20 [==============================] - 1s 26ms/step - loss: 0.1695 - cross entropy: 0.1695 - Brier score: 0.0500 - tp: 19271.0000 - fp: 980.0000 - tn: 19382.0000 - fn: 1327.0000 - accuracy: 0.9437 - precision: 0.9516 - recall: 0.9356 - auc: 0.9823 - prc: 0.9861 - val_loss: 0.1315 - val_cross entropy: 0.1315 - val_Brier score: 0.0267 - val_tp: 59.0000 - val_fp: 942.0000 - val_tn: 44555.0000 - val_fn: 13.0000 - val_accuracy: 0.9790 - val_precision: 0.0589 - val_recall: 0.8194 - val_auc: 0.9624 - val_prc: 0.6505
Epoch 31/1000
20/20 [==============================] - 0s 26ms/step - loss: 0.1680 - cross entropy: 0.1680 - Brier score: 0.0493 - tp: 19299.0000 - fp: 915.0000 - tn: 19366.0000 - fn: 1380.0000 - accuracy: 0.9440 - precision: 0.9547 - recall: 0.9333 - auc: 0.9823 - prc: 0.9862 - val_loss: 0.1278 - val_cross entropy: 0.1278 - val_Brier score: 0.0260 - val_tp: 59.0000 - val_fp: 934.0000 - val_tn: 44563.0000 - val_fn: 13.0000 - val_accuracy: 0.9792 - val_precision: 0.0594 - val_recall: 0.8194 - val_auc: 0.9621 - val_prc: 0.6513
Epoch 32/1000
20/20 [==============================] - 0s 26ms/step - loss: 0.1665 - cross entropy: 0.1665 - Brier score: 0.0487 - tp: 18949.0000 - fp: 942.0000 - tn: 19770.0000 - fn: 1299.0000 - accuracy: 0.9453 - precision: 0.9526 - recall: 0.9358 - auc: 0.9828 - prc: 0.9860 - val_loss: 0.1237 - val_cross entropy: 0.1237 - val_Brier score: 0.0251 - val_tp: 59.0000 - val_fp: 915.0000 - val_tn: 44582.0000 - val_fn: 13.0000 - val_accuracy: 0.9796 - val_precision: 0.0606 - val_recall: 0.8194 - val_auc: 0.9620 - val_prc: 0.6325
Epoch 33/1000
20/20 [==============================] - 0s 26ms/step - loss: 0.1620 - cross entropy: 0.1620 - Brier score: 0.0474 - tp: 19313.0000 - fp: 860.0000 - tn: 19468.0000 - fn: 1319.0000 - accuracy: 0.9468 - precision: 0.9574 - recall: 0.9361 - auc: 0.9837 - prc: 0.9871 - val_loss: 0.1201 - val_cross entropy: 0.1201 - val_Brier score: 0.0243 - val_tp: 59.0000 - val_fp: 882.0000 - val_tn: 44615.0000 - val_fn: 13.0000 - val_accuracy: 0.9804 - val_precision: 0.0627 - val_recall: 0.8194 - val_auc: 0.9624 - val_prc: 0.6328
Epoch 34/1000
20/20 [==============================] - 0s 26ms/step - loss: 0.1620 - cross entropy: 0.1620 - Brier score: 0.0474 - tp: 19167.0000 - fp: 855.0000 - tn: 19631.0000 - fn: 1307.0000 - accuracy: 0.9472 - precision: 0.9573 - recall: 0.9362 - auc: 0.9834 - prc: 0.9868 - val_loss: 0.1174 - val_cross entropy: 0.1174 - val_Brier score: 0.0238 - val_tp: 59.0000 - val_fp: 873.0000 - val_tn: 44624.0000 - val_fn: 13.0000 - val_accuracy: 0.9806 - val_precision: 0.0633 - val_recall: 0.8194 - val_auc: 0.9624 - val_prc: 0.6348
Epoch 35/1000
20/20 [==============================] - 1s 26ms/step - loss: 0.1592 - cross entropy: 0.1592 - Brier score: 0.0468 - tp: 19335.0000 - fp: 879.0000 - tn: 19439.0000 - fn: 1307.0000 - accuracy: 0.9466 - precision: 0.9565 - recall: 0.9367 - auc: 0.9840 - prc: 0.9872 - val_loss: 0.1142 - val_cross entropy: 0.1142 - val_Brier score: 0.0232 - val_tp: 59.0000 - val_fp: 852.0000 - val_tn: 44645.0000 - val_fn: 13.0000 - val_accuracy: 0.9810 - val_precision: 0.0648 - val_recall: 0.8194 - val_auc: 0.9627 - val_prc: 0.6350
Epoch 36/1000
20/20 [==============================] - 0s 25ms/step - loss: 0.1568 - cross entropy: 0.1568 - Brier score: 0.0458 - tp: 19215.0000 - fp: 825.0000 - tn: 19613.0000 - fn: 1307.0000 - accuracy: 0.9479 - precision: 0.9588 - recall: 0.9363 - auc: 0.9844 - prc: 0.9874 - val_loss: 0.1117 - val_cross entropy: 0.1117 - val_Brier score: 0.0227 - val_tp: 59.0000 - val_fp: 852.0000 - val_tn: 44645.0000 - val_fn: 13.0000 - val_accuracy: 0.9810 - val_precision: 0.0648 - val_recall: 0.8194 - val_auc: 0.9632 - val_prc: 0.6350
Epoch 37/1000
20/20 [==============================] - ETA: 0s - loss: 0.1547 - cross entropy: 0.1547 - Brier score: 0.0452 - tp: 19037.0000 - fp: 802.0000 - tn: 19810.0000 - fn: 1311.0000 - accuracy: 0.9484 - precision: 0.9596 - recall: 0.9356 - auc: 0.9850 - prc: 0.9875Restoring model weights from the end of the best epoch: 27.
20/20 [==============================] - 1s 26ms/step - loss: 0.1547 - cross entropy: 0.1547 - Brier score: 0.0452 - tp: 19037.0000 - fp: 802.0000 - tn: 19810.0000 - fn: 1311.0000 - accuracy: 0.9484 - precision: 0.9596 - recall: 0.9356 - auc: 0.9850 - prc: 0.9875 - val_loss: 0.1094 - val_cross entropy: 0.1094 - val_Brier score: 0.0223 - val_tp: 59.0000 - val_fp: 849.0000 - val_tn: 44648.0000 - val_fn: 13.0000 - val_accuracy: 0.9811 - val_precision: 0.0650 - val_recall: 0.8194 - val_auc: 0.9639 - val_prc: 0.6373
Epoch 37: early stopping

Re-check training history

plot_metrics(resampled_history)

png

Evaluate metrics

train_predictions_resampled = resampled_model.predict(train_features, batch_size=BATCH_SIZE)
test_predictions_resampled = resampled_model.predict(test_features, batch_size=BATCH_SIZE)
90/90 [==============================] - 0s 1ms/step
28/28 [==============================] - 0s 1ms/step
resampled_results = resampled_model.evaluate(test_features, test_labels,
                                             batch_size=BATCH_SIZE, verbose=0)
for name, value in zip(resampled_model.metrics_names, resampled_results):
  print(name, ': ', value)
print()
plot_cm(test_labels, test_predictions_resampled)
loss :  0.14393876492977142
cross entropy :  0.14393876492977142
Brier score :  0.02951517514884472
tp :  91.0
fp :  1181.0
tn :  55682.0
fn :  8.0
accuracy :  0.9791264533996582
precision :  0.07154087722301483
recall :  0.9191918969154358
auc :  0.9757788777351379
prc :  0.7805911898612976

Legitimate Transactions Detected (True Negatives):  55682
Legitimate Transactions Incorrectly Detected (False Positives):  1181
Fraudulent Transactions Missed (False Negatives):  8
Fraudulent Transactions Detected (True Positives):  91
Total Fraudulent Transactions:  99

png

Plot the ROC

plot_roc("Train Baseline", train_labels, train_predictions_baseline, color=colors[0])
plot_roc("Test Baseline", test_labels, test_predictions_baseline, color=colors[0], linestyle='--')
plot_roc("Train Weighted", train_labels, train_predictions_weighted, color=colors[1])
plot_roc("Test Weighted", test_labels, test_predictions_weighted, color=colors[1], linestyle='--')
plot_roc("Train Resampled", train_labels, train_predictions_resampled, color=colors[2])
plot_roc("Test Resampled", test_labels, test_predictions_resampled, color=colors[2], linestyle='--')
plt.legend(loc='lower right');

png

Plot the AUPRC

plot_prc("Train Baseline", train_labels, train_predictions_baseline, color=colors[0])
plot_prc("Test Baseline", test_labels, test_predictions_baseline, color=colors[0], linestyle='--')

plot_prc("Train Weighted", train_labels, train_predictions_weighted, color=colors[1])
plot_prc("Test Weighted", test_labels, test_predictions_weighted, color=colors[1], linestyle='--')

plot_prc("Train Resampled", train_labels, train_predictions_resampled, color=colors[2])
plot_prc("Test Resampled", test_labels, test_predictions_resampled, color=colors[2], linestyle='--')
plt.legend(loc='lower right');

png

Applying this tutorial to your problem

Imbalanced data classification is an inherently difficult task since there are so few samples to learn from. You should always start with the data first and do your best to collect as many samples as possible and give substantial thought to what features may be relevant so the model can get the most out of your minority class. At some point your model may struggle to improve and yield the results you want, so it is important to keep in mind the context of your problem and the trade offs between different types of errors.