liveBook · Manning

Reconstruction loss

As explained in Financial AI in Practice

A reconstruction error is the difference between an input and the output produced when a model attempts to reconstruct that input.

A fraud team needs a way to spot unusual card transactions, including patterns absent from past fraud cases. An autoencoder trained only on legitimate transactions learns to reproduce familiar inputs. Reconstruction loss measures the difference between a transaction’s original features and the model’s reconstructed features. A large difference becomes an anomaly score: a reason to investigate, not proof of fraud. section 8.4.1 section 8.4.2

Turn reconstruction loss into a testable fraud signal:

  • Start with scaled transaction features. Filter the training set to legitimate transactions; use labels to select training data, not as targets for the autoencoder. section 8.4.2
  • Train the autoencoder to reconstruct those transactions. Check training and validation loss, using early stopping when validation loss stops improving. section 8.4.3
  • Reconstruct every transaction in a separate test set. Compute one error per transaction using mean squared error (MSE) or mean absolute error (MAE), then compare the distributions for legitimate and fraudulent cases. section 8.4.4

Calculate a per-transaction score

With `X_test` and its model output `test_reconstructions`, calculate `mse = np.mean((X_test - test_reconstructions) ** 2, axis=1)` or `mae = np.mean(np.abs(X_test - test_reconstructions), axis=1)`. Group these scores by the known test labels for evaluation. In the credit-card example, fraudulent transactions have substantially higher mean and median errors; scatter plots make the separation visible. section 8.4.4 section 8.4.5

Listing 8.12 Calculating reconstruction errors on the full test set
test_reconstructions = autoencoder_model.predict(X_test)
mse_per_instance_test = np.mean(np.power(X_test - test_reconstructions, 2), axis=1)    #1
mae_per_instance_test = np.mean(np.abs(X_test - test_reconstructions), axis=1)

reconstruction_error_df_test = pd.DataFrame({
    'mse': mse_per_instance_test,
    'mae': mae_per_instance_test,
    'true_class': y_test    #2
})

print("\nReconstruction errors calculated for the test set.")
print("First 5 rows of reconstruction errors with true classes:")
display(reconstruction_error_df_test.head())
print("\nSummary statistics of reconstruction errors, grouped by true class:")
display(reconstruction_error_df_test.groupby('true_class')[['mse', 'mae']].describe())    #3

Inspect the full calculation: model predictions, per-row MSE and MAE, and descriptive statistics grouped by the true test class. section 8.4.4

Live transactions arrive without fraud labels, so the plotted class separation cannot set an operational cutoff by itself. Choose a threshold against trusted normal-error data—for example, a high percentile—and test its alert volume against review capacity and customer-friction limits. Use shadow-mode testing and analyst feedback before relying on alerts. The threshold is a business decision as well as a statistical one. section 8.4.6 section 8.5

How Financial AI in Practice covers it

How deep it goes
Works through legitimate-only training, per-transaction MSE and MAE, distribution checks, and operational threshold selection.
Examples
Uses credit-card transactions, Python error calculations, class-grouped summary statistics, and scatter plots of legitimate and fraudulent scores.
What it assumes
Assumes scaled training and test features, then teaches how to restrict autoencoder training to legitimate transactions.
What you can do after
Apply it via an autoencoder to score transactions and evaluate thresholds against fraud separation and review capacity.
How it connects
Connects autoencoders, unsupervised anomaly detection, mean squared error, mean absolute error, and fraud-alert thresholds.

Also explained in

Compare how 10 books explain reconstruction loss →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Financial AI in Practice. The section links point to the parts of the book it draws on.