DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Architecting AI-Native Cloud Platforms: Signals to Insights to Actions
  • Scaling ML Models Efficiently With Shared Neural Networks
  • Navigating the Complexities of Text Summarization With NLP
  • Implementing Fraud Detection Systems Using Machine Learning Models

Trending

  • Architecting Zero-Trust AI Agents: How to Handle Data Safely
  • Rethinking Java CRUDs With Event Sourcing and CQRS Patterns
  • Mocking Kafka for Local Spring Development
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Two-Tower Model for Fraud Detection: A Comprehensive Guide

Two-Tower Model for Fraud Detection: A Comprehensive Guide

Boost fraud detection with the two-tower model, a powerful architecture capturing complex relationships between transaction and user data.

By 
Gaurav Puri user avatar
Gaurav Puri
·
Harsh Daiya user avatar
Harsh Daiya
DZone Core CORE ·
Jul. 31, 24 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
4.7K Views

Join the DZone community and get the full member experience.

Join For Free

Fraud detection is a very important task in most industries, especially in finance and e-commerce. The classic machine learning models struggle to handle the subtlety and complexity of patterns rooted in fraudulent behavior. The dual-tower or Siamese network model provides a powerful architecture to handle the complex tasks of parallel processing with two different sets of inputs, thus helping capture the intricate relationships between them. This article presents how to apply the two-tower model to fraud detection, with detailed explanations, code snippets, and practical illustrations.

What Is a Two-Tower Model?

It is an architecture that comprises two independent neural networks: one dealing with one type of input data and the second dealing with another. These two towers may work independently, although their output results are combined to make a single unified prediction. This architecture works very nicely in tasks involving finding relationships or similarities between two diverse sources of data.

Key Components

  1. Two separate neural networks (towers): Every tower is a neural network processing one type of input; for example, user features and transaction features.
  2. Input data: This varies for both towers depending on the use case. This is treated as input, holding separate views to capture different patterns and relationships.
  3. Combined layer: The separate outputs of the two towers are combined into one final prediction.

Two intricate towers unite to form the iconic Eiffel Tower

Two intricate towers unite to form the iconic Eiffel Tower

Why Two-Tower Model for Fraud Detection?

Fraud detection is a complex task that requires analyzing distinct types of data with different distributions. For illustration purposes, we would be considering transaction and user data. 

Transaction Data

This type of data includes detailed information about individual transactions, such as:

  • Transaction amount
  • Timestamp
  • Location
  • Merchant information
  • Transaction category 

User Data

This type of data includes information about the user making the transaction, such as:

  • Demographics
  • Browsing history
  • Purchase history
  • Device information

Traditional machine learning struggles to combine the transaction and user data since they represent different distributions and different processing techniques are required in processing them. Two-tower architecture provides an effective solution to this challenge. The processing for each data type can be done with techniques and architecture tailored for the specific data type, and then the insights of each tower can be compacted into a unified view over the transaction and user for much better fraud behavior detection.

Implementation: Two-Tower Model for Fraud Detection

Below we will implement a two-tower model using TensorFlow and Keras on a synthetically generated toy dataset.

Step 1: Data Preparation

We'll generate synthetic transaction and user data for this example.

Python
 
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense, Concatenate

# Generate synthetic transaction data
num_samples = 10000
transaction_data = np.random.rand(num_samples, 10)  # 10 transaction features

# Generate synthetic user data
user_data = np.random.rand(num_samples, 5)  # 5 user features

# Generate labels (0 for non-fraud, 1 for fraud)
labels = np.random.randint(2, size=num_samples)


Step 2: Define the Two Towers

We define two separate neural networks to process transaction and user data.

Python
 
# Define the transaction tower
transaction_input = Input(shape=(10,), name='transaction_input')
transaction_dense = Dense(64, activation='relu')(transaction_input)
transaction_output = Dense(32, activation='relu')(transaction_dense)

# Define the user tower
user_input = Input(shape=(5,), name='user_input')
user_dense = Dense(32, activation='relu')(user_input)
user_output = Dense(16, activation='relu')(user_dense)


Step 3: Combine the Towers

Combine the outputs of the two towers and add additional layers for the final prediction.

Python
 
# Combine the outputs of the two towers
combined = Concatenate()([transaction_output, user_output])

# Add additional dense layers
combined_dense = Dense(32, activation='relu')(combined)
final_output = Dense(1, activation='sigmoid')(combined_dense)

# Define the model
model = Model(inputs=[transaction_input, user_input], outputs=final_output)

# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Print the model summary
model.summary()


Step 4: Train the Model

Train the model using the synthetic data.

Python
 
# Train the model
model.fit([transaction_data, user_data], labels, epochs=10, batch_size=32, validation_split=0.2)


Results and Evaluation

After training, we can evaluate the model's performance on a validation set to see how well it detects fraudulent transactions.

Python
 
# Evaluate the model
loss, accuracy = model.evaluate([transaction_data, user_data], labels)
print(f'Validation Accuracy: {accuracy * 100:.2f}%')


Conclusion and Ideas for Further Exploration

The two-tower model has proven to be highly effective by leveraging the strengths of each tower to mine complex patterns within each input data set. These patterns are combined to produce embeddings that can be further fine-tuned for specific use cases. I recommend the developer community think outside the box and experiment with a wide range of input types for models, such as graph-based inputs that capture network relationships, hierarchical inputs that model user behavior, and multi-modal inputs that utilize text, images, and other data sources.

Architecture Machine learning Transaction data Data (computing) neural network

Opinions expressed by DZone contributors are their own.

Related

  • Architecting AI-Native Cloud Platforms: Signals to Insights to Actions
  • Scaling ML Models Efficiently With Shared Neural Networks
  • Navigating the Complexities of Text Summarization With NLP
  • Implementing Fraud Detection Systems Using Machine Learning Models

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook