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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Scaling ML Models Efficiently With Shared Neural Networks
  • Navigating the Complexities of Text Summarization With NLP
  • Implementing Fraud Detection Systems Using Machine Learning Models
  • The Perils of AI Hallucination: Unraveling the Challenges and Implications

Trending

  • AI Speaks for the World... But Whose Humanity Does It Learn From?
  • Navigating and Modernizing Legacy Codebases: A Developer's Guide to AI-Assisted Code Understanding
  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  • Advancing Your Software Engineering Career in 2025
  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
3.8K 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

  • Scaling ML Models Efficiently With Shared Neural Networks
  • Navigating the Complexities of Text Summarization With NLP
  • Implementing Fraud Detection Systems Using Machine Learning Models
  • The Perils of AI Hallucination: Unraveling the Challenges and Implications

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!