Differential privacy, federated learning, and homomorphic encryption — compute on data without exposing it.
Privacy-Preserving AI Techniques
# Differential Privacy (DP)
# Add calibrated noise to protect individual data
from diffprivlib.models import GaussianNaiveBayes
import numpy as np
# Train with differential privacy guarantee
dp_model = GaussianNaiveBayes(epsilon=1.0) # privacy budget
dp_model.fit(X_train, y_train)
# TensorFlow Privacy
import tensorflow_privacy
from tensorflow_privacy.privacy.optimizers import dp_optimizer_keras
optimizer = dp_optimizer_keras.DPKerasSGDOptimizer(
l2_norm_clip=1.0,
noise_multiplier=0.5,
num_microbatches=32
)
# Federated Learning
# Train on edge devices, only share gradients
# Data never leaves the device
import flwr as fl # Flower federated learning
class FederatedClient(fl.client.NumPyClient):
def fit(self, parameters, config):
model.set_weights(parameters)
model.fit(local_X, local_y, epochs=1)
return model.get_weights(), len(local_X), {}
# Secure Multi-Party Computation (MPC)
# Multiple parties compute on combined data without seeing each other's data
# Homomorphic Encryption
# Compute on encrypted data without decrypting
import tenseal as ts