Icon

01_​Fraud_​Detection_​Training_​Exercise

Keras Autoencoder for Fraud Detection - Training

Exercise of the L4-DL Deep Learning Specialization course.

The goal of this exercise is to train an autoencoder model to detect fraudulent transactions.

URL: L4-DL course slides https://www.knime.com/form/material-download-registration

Step 2. Keras Autoencoder Architecture

  1. Layer 1 (input layer): Keras Input Layer node with Shape 30

  2. Layer 2 (hidden layer): Keras Dense Layer node with Shape 40, Sigmoid Activation function

  3. Layer 3 (hidden layer): Keras Dense Layer node with Shape 20, Sigmoid Activation function

  4. Layer 4 (bottleneck hidden layer): Keras Dense Layer node with Shape 8, Sigmoid Activation function

  5. Layer 5 (hidden layer): Keras Dense Layer node with Shape 20, Sigmoid Activation function

  6. Layer 6 (hidden layer): Keras Dense Layer node with Shape 40, Sigmoid Activation function

  7. Layer 7 (output layer): Keras Dense Layer node with Shape 30, Sigmoid Activation function



Step 1. Data Preparation

The dataset is initially separated into legitimate (Class=0) and fraudulent transactions (Class=1). Of all legitimate transactions, 2/3 are used to train the autoencoder. 10% of the dataset is reserved as the validation set. The remaining 1/3 of legitimate transactions are combined with all fraudulent transactions. The training set is normalized to [0,1], and the same normalization is applied to the validation and testing sets. The normalization model is written to the data folder as normalizer_new.model.



Session 2 - Design Components of Deep Learning

Exercise 01 Keras Autoencoder for Fraud Detection - Training

Learning objective: In this exercise, you'll learn how to train an autoencoder model to detect fraudulent transactions.


Workflow description: The dataset contains 228K credit card transactions. While most of the transactions are legitimate, a small number (~400) of transactions are fraudulent. The goal of this exercise is to detect fraudulent transactions by building an autoencoder. The information regarding the transactions has been transformed to 28 numerical features. Two additional numerical features (Time and Amount) are also included.


You'll find the instructions to the exercises in the yellow annotations.

Step 3. Training the Autoencoder

  1. Train the network model with the Keras Network Lerner node

    • 30 numerical features are the Input and the Target

    • Loss function is Mean squared error

    • Options: Batch size 300 for both training and validation. 50 Epochs. Adam Optimizer

  2. Save the trained network model with the Keras Network Writer node, as creditcard_autoencoder.h5 in the data folder

  3. Apply the trained data to the testing set with the Keras Network Executor node

    • Keep input columns in the output table



Step 4. Optimizing threshold K

  1. MSE calculation with the MSE Calculation component, found in components folder

    • The input is the output from the Keras Network Executor node

  2. Optimize K with the Threshold Optimization component, found in the components folder

    • Use the Threshold Optimization component, found in the Session 2 folder. The input is the output from the MSE calculation component



Step 5. Final Performance

  1. Save the optimized threshold K

    • Use the Variable to Table Row node to convert the output from the Threshold Optimization component to a table. Then use the Table Writer node to save the threshold table as threshold.table in the data folder

  2. Classify transactions based on their MSE and the threshold K

    • Use the output from the MSE Calculation component, and the flow variable from the Threshold Optimization component in an Expression node

    • If the MSE (reconstruction_error) is lower than the flow variable Threshold, then the sample is classified normal (“0”), otherwise classified as fraudulent (“1”)

    • Append the classification results as a new column “prediction”

  3. Convert the target variable “class” by the Number to String node

  4. Evaluate the performance of the autoencoder fraud detector with the Scorer (Java Script) node



Read creditcard.csv
CSV Reader
Partition data into2/3 training1/3 testParitioning the training to90% Training10% validationNormalize all featuresto [0,1]
Partitioning Normalization

Nodes

Extensions

Links