Introduction
AI red teaming is a critical practice for ensuring the safety and robustness of artificial intelligence systems. In this tutorial, you'll learn how to implement a basic AI red teaming framework using Python and common machine learning libraries. This hands-on approach will help you understand how adversarial testing can reveal vulnerabilities in AI models before deployment.
Prerequisites
- Basic Python programming knowledge
- Familiarity with machine learning concepts
- Installed libraries: scikit-learn, numpy, matplotlib, tensorflow/keras
- Basic understanding of neural networks and model evaluation
Step-by-Step Instructions
1. Setting Up Your Environment
1.1 Install Required Libraries
First, create a virtual environment and install the necessary packages:
pip install scikit-learn numpy matplotlib tensorflow
Why this step: Having a clean environment ensures consistent results and avoids dependency conflicts when testing adversarial examples.
1.2 Import Required Modules
Set up your Python environment with essential imports:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import tensorflow as tf
from tensorflow import keras
Why this step: These libraries provide the foundation for creating, training, and testing machine learning models while also supporting adversarial example generation.
2. Creating a Baseline Model
2.1 Load and Prepare Dataset
Load the Iris dataset and split it into training and testing sets:
# Load dataset
iris = load_iris()
X, y = iris.data, iris.target
# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
print(f"Training set size: {X_train.shape}")
print(f"Test set size: {X_test.shape}")
Why this step: Using a well-known dataset like Iris allows us to focus on adversarial testing concepts without getting bogged down in data preparation.
2.2 Train a Simple Classifier
Create and train a Random Forest classifier as our baseline model:
# Train a classifier
clf = RandomForestClassifier(n_estimators=100, random_state=42)
clf.fit(X_train, y_train)
# Evaluate baseline performance
y_pred = clf.predict(X_test)
baseline_accuracy = accuracy_score(y_test, y_pred)
print(f"Baseline accuracy: {baseline_accuracy:.2f}")
Why this step: Establishing a baseline performance metric is crucial for understanding how adversarial examples affect model behavior.
3. Implementing Adversarial Testing
3.1 Generate Simple Adversarial Examples
Create a basic adversarial example generator using the Fast Gradient Sign Method (FGSM):
def generate_adversarial_example(model, x, y, epsilon=0.01):
"""
Generate adversarial example using FGSM
"""
x = tf.convert_to_tensor(x, dtype=tf.float32)
y = tf.convert_to_tensor(y, dtype=tf.float32)
with tf.GradientTape() as tape:
tape.watch(x)
prediction = model(x)
loss = tf.keras.losses.sparse_categorical_crossentropy(y, prediction)
gradient = tape.gradient(loss, x)
signed_gradient = tf.sign(gradient)
# Create adversarial example
adversarial_example = x + epsilon * signed_gradient
return adversarial_example.numpy()
Why this step: FGSM is one of the simplest yet most effective methods for generating adversarial examples, helping us understand model vulnerabilities.
3.2 Test Model Robustness
Apply adversarial examples to test model robustness:
# Test robustness
adversarial_examples = []
original_predictions = []
adversarial_predictions = []
for i in range(10): # Test first 10 samples
x = X_test[i:i+1]
y = y_test[i:i+1]
# Generate adversarial example
adv_example = generate_adversarial_example(clf, x, y, epsilon=0.1)
adversarial_examples.append(adv_example)
# Make predictions
original_pred = clf.predict(x)
adversarial_pred = clf.predict(adv_example)
original_predictions.append(original_pred)
adversarial_predictions.append(adversarial_pred)
print(f"Original: {original_pred[0]}, Adversarial: {adversarial_pred[0]}")
Why this step: By comparing predictions on original and adversarial inputs, we can quantify how vulnerable our model is to small perturbations.
4. Analyzing Results
4.1 Visualize Perturbations
Create a visualization of the adversarial perturbations:
# Visualize the perturbations
fig, axes = plt.subplots(2, 5, figsize=(15, 6))
axes = axes.ravel()
for i in range(10):
original = X_test[i]
adversarial = adversarial_examples[i]
# Plot original and adversarial
axes[i].bar(range(len(original)), original, alpha=0.7, label='Original')
axes[i].bar(range(len(adversarial)), adversarial, alpha=0.7, label='Adversarial')
axes[i].set_title(f'Sample {i+1}')
axes[i].legend()
plt.tight_layout()
plt.show()
Why this step: Visualizing the perturbations helps us understand the magnitude and nature of changes that can fool our model.
4.2 Calculate Robustness Metrics
Measure how well the model performs on adversarial examples:
# Calculate robustness metrics
original_correct = 0
adversarial_correct = 0
for i in range(len(X_test)):
x = X_test[i:i+1]
y = y_test[i:i+1]
# Original prediction
original_pred = clf.predict(x)[0]
if original_pred == y[0]:
original_correct += 1
# Adversarial prediction
adv_example = generate_adversarial_example(clf, x, y, epsilon=0.1)
adversarial_pred = clf.predict(adv_example)[0]
if adversarial_pred == y[0]:
adversarial_correct += 1
print(f"Original accuracy: {original_correct/len(X_test):.2f}")
print(f"Adversarial accuracy: {adversarial_correct/len(X_test):.2f}")
Why this step: Quantifying the drop in accuracy on adversarial inputs gives us concrete metrics for evaluating model robustness.
5. Improving Model Robustness
5.1 Implement Adversarial Training
Train a model with adversarial examples to improve robustness:
# Create adversarial training dataset
adversarial_train_data = []
adversarial_train_labels = []
for i in range(50): # Use first 50 samples for adversarial training
x = X_train[i:i+1]
y = y_train[i:i+1]
# Generate adversarial example
adv_example = generate_adversarial_example(clf, x, y, epsilon=0.05)
adversarial_train_data.append(adv_example[0])
adversarial_train_labels.append(y[0])
# Combine original and adversarial data
combined_X = np.vstack([X_train, adversarial_train_data])
combined_y = np.hstack([y_train, adversarial_train_labels])
# Retrain model with combined data
improved_clf = RandomForestClassifier(n_estimators=100, random_state=42)
improved_clf.fit(combined_X, combined_y)
Why this step: Adversarial training is a key technique for improving model robustness against adversarial attacks.
5.2 Compare Model Performance
Evaluate how adversarial training improved model robustness:
# Compare performance
improved_predictions = improved_clf.predict(X_test)
improved_accuracy = accuracy_score(y_test, improved_predictions)
print(f"Baseline accuracy: {baseline_accuracy:.2f}")
print(f"Improved accuracy: {improved_accuracy:.2f}")
Why this step: Comparing pre- and post-adversarial training performance demonstrates the effectiveness of robustness improvements.
Summary
In this tutorial, you've implemented a basic AI red teaming framework that demonstrates how adversarial testing can reveal vulnerabilities in machine learning models. You learned to generate adversarial examples using the Fast Gradient Sign Method, tested model robustness, and implemented adversarial training to improve model resilience. This hands-on approach gives you practical experience in identifying and mitigating AI system vulnerabilities, which is essential for deploying safe and reliable AI systems in real-world applications.



