Exploring Mixup: A Powerful Data Augmentation Technique in Deep Learning

Moklesur Rahman
4 min readJun 18, 2023

Data augmentation plays a vital role in improving the performance and generalization capabilities of deep learning models. It involves creating variations of the training data by applying transformations such as rotations, translations, and flips. In recent years, a new data augmentation technique called “Mixup” has gained significant attention and has been shown to enhance the robustness and accuracy of deep learning models. In this blog post, we will delve into the concept of Mixup, its underlying principles, and its applications in the field of deep learning.

Photo by Amador Loureiro on Unsplash

Understanding Mixup

Mixup is a data augmentation technique that involves blending pairs of samples and their corresponding labels to create new synthetic training examples. The blending process is performed at the input and output levels simultaneously. Specifically, Mixup takes two samples, xᵢ and xⱼ, and their associated labels, yᵢ and yⱼ, and creates new examples, x̂ and ŷ, as weighted linear combinations:

x̂ = λxᵢ + (1 — λ)xⱼ

ŷ = λyᵢ + (1 — λ)yⱼ

Here, λ is a random value drawn from a beta distribution with a user-defined parameter α, typically set between 0.1 and 0.4. The generated synthetic example x̂ is used as an input during training, while ŷ is used as its corresponding label.


import tensorflow as tf

def mixup_data(x, y, alpha=0.2):
batch_size = tf.shape(x)[0]
weight = tf.random.uniform([batch_size], 0, 1)
weight = tf.maximum(weight, 1 - weight) # Ensure lambda is always between 0.5 and 1

x_mix = x * tf.expand_dims(weight, axis=1) + x[::-1] * tf.expand_dims(1 - weight, axis=1)
y_mix = y * tf.expand_dims(weight, axis=1) + y[::-1] * tf.expand_dims(1 - weight, axis=1)

return x_mix, y_mix

# Create TensorFlow Dataset
train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
train_dataset = train_dataset.shuffle(buffer_size=10000).batch(batch_size)

# Iterate over the dataset using mixup_data function
for batch_x, batch_y in train_dataset:
batch_x_mix, batch_y_mix = mixup_data(batch_x, batch_y, alpha=0.2)

# Perform training using mixed-up data
# ...



Moklesur Rahman

PhD student | Computer Science | University of Milan | Data science | AI in Cardiology | Writer | Researcher