CHAPTER 10
Data Privacy: Differential Privacy and Anonymization
Overview
The principle of privacy dictates that developers should protect the personal and sensitive information used to train and operate AI systems. Data breaches and the ability to infer sensitive information from model outputs are significant ethical and legal risks. Ethical developers employ techniques to ensure that individual data points cannot be reconstructed or identified, even when the model is publicly available.
Anonymization techniques: Limitations and risks
Traditional anonymization techniques, such as removing direct identifiers (names, addresses), are often insufficient. Research has shown that seemingly anonymous datasets can be easily reidentified by linking them with publicly available information.
K-anonymity: Requires that, for every combination of quasi-identifiers (such as age, gender, zip code), at least k individuals share that combination.
L-diversity: An extension of k-anonymity that ensures the sensitive attribute (such as disease) has at least distinct values within each group of k individuals, preventing inference attacks. While useful, these techniques are brittle and can fail under sophisticated attacks. The L doesn’t stand for anything; it’s just the letter after K.
Differential privacy: The gold standard
Differential privacy (DP) is a rigorous, mathematical definition of privacy that guarantees that the output of an algorithm is essentially the same, whether or not any single individual's data is included in the input dataset.
This is achieved by carefully injecting a controlled amount of random noise into the data or the model's training process. Adding noise to training data usually reduces model accuracy on the training data but can sometimes improve generalizability to new, previously unseen data.
The developer's ethical choice is to find the optimal trade-off between privacy and utility.
Conceptual differential privacy with Opacus
Implementing differential privacy (DP) from scratch is complex. Ethical developers leverage specialized libraries like Opacus (for PyTorch) or TensorFlow Privacy.
The following conceptual Python code illustrates the steps involved in training a model with DP, ensuring that the model cannot be used to infer the presence of any single training data point.
Code Listing 10-a: Differential privacy with Opacus (differential-privacy.py)
# pip install torch torchvision torchaudio import torch from torch import nn, optim from torch.utils.data import DataLoader, TensorDataset # pip install opacus from opacus import PrivacyEngine # --- Training with Differential Privacy (Opacus) --- # 1. Simulate a simple model and data. class SimpleModel(nn.Module): def __init__(self): super().__init__() self.fc = nn.Linear(10, 1) def forward(self, x): return self.fc(x) device = "cuda" if torch.cuda.is_available() else "cpu" model = SimpleModel().to(device) optimizer = optim.SGD(model.parameters(), lr=0.05) criterion = nn.BCEWithLogitsLoss() # Simulate a DataLoader. torch.manual_seed(42) X_data = torch.randn(1000, 10) y_data = torch.randint(0, 2, (1000, 1)).float() # shape (N, 1) for BCEWithLogitsLoss dataset = TensorDataset(X_data, y_data) data_loader = DataLoader(dataset, batch_size=32, shuffle=True, drop_last=True) # 2. Ethical Step: Initialize the Privacy Engine (DP-SGD). # Key knobs: # - noise_multiplier: higher => more privacy (but potentially lower accuracy) # - max_grad_norm: gradient clipping bound per-sample privacy_engine = PrivacyEngine() model, optimizer, data_loader = privacy_engine.make_private( module=model, optimizer=optimizer, data_loader=data_loader, noise_multiplier=1.1, max_grad_norm=1.0, ) # 3. Training Loop (DP). def train_with_dp(model, optimizer, data_loader, epochs=1): model.train() for epoch in range(epochs): running_loss = 0.0 for data, target in data_loader: data = data.to(device) target = target.to(device) optimizer.zero_grad(set_to_none=True) logits = model(data) # shape (batch, 1) loss = criterion(logits, target) # target shape (batch, 1) loss.backward() optimizer.step() running_loss += loss.item() avg_loss = running_loss / len(data_loader) print(f"Epoch {epoch+1}/{epochs} - loss: {avg_loss:.4f}") # 4. Ethical Step: Report the final privacy budget (epsilon). delta = 1e-5 epsilon = privacy_engine.get_epsilon(delta=delta) print(f"\n[PRIVACY]: Trained with ε = {epsilon:.2f}, δ = {delta}") train_with_dp(model, optimizer, data_loader, epochs=3) print( "\n[ETHICAL NOTE]: Differential Privacy (DP-SGD) clips per-sample gradients and adds noise, " "limiting how much any single person's data can influence the trained model. " "This reduces privacy risks such as membership inference and data reconstruction." ) |
This code shows how to train a neural network with formal DP guarantees using Opacus, Meta’s privacy library for PyTorch.
First, it defines a simple neural network (SimpleModel) with a single linear layer. This keeps the focus on the privacy mechanism rather than model complexity.
The code then selects a CPU or GPU device, initializes the model, an SGD optimizer, and a binary cross-entropy with logits loss (BCEWithLogitsLoss).
Next, it simulates a dataset of 1,000 samples with 10 features and binary labels, wraps the data in a DataLoader, and enables shuffling. This mimics a realistic training setup without relying on sensitive real-world data.
The key ethical step is to initialize the Opacus PrivacyEngine and call make_private. This transforms the model, optimizer, and data loader so training uses DP-SGD:
· Per-sample gradients are clipped (max_grad_norm=1.0) to prevent any single record from dominating learning.
· Random noise is added to gradients (noise_multiplier=1.1) to obscure individual data contributions.
Together, these steps provide mathematical privacy guarantees.
The training loop then runs normally, but all gradient updates are now privacy-preserving. After training, the code reports the privacy budget (ε, δ), which quantifies the total privacy loss incurred during training.
Finally, the ethical note summarizes the benefit. By limiting each individual’s influence on the model, differential privacy reduces the risk of membership inference and data reconstruction attacks, making the training process safer for sensitive user data.
Now, let’s run the code with the command py differential-privacy.py from the terminal in VS Code.
Code Listing 10-b: Differential privacy with Opacus execution
C:\PERSONAL-DATA\Projects\Books\Ethical AI Code\venv\Lib\site-packages\opacus\privacy_engine.py:96: UserWarning: Secure RNG turned off. This is perfectly fine for experimentation as it allows for much faster training performance, but remember to turn it on and retrain one last time before production with ``secure_mode`` turned on. warnings.warn( 01/16/2026 18:26:01:WARNING:Ignoring drop_last as it is not compatible with DPDataLoader. C:\PERSONAL-DATA\Projects\Books\Ethical AI Code\differential-privacy.py:61: UserWarning: Full backward hook is firing when gradients are computed with respect to module outputs since no inputs require gradients. See https://docs.pytorch.org/docs/main/generated/torch.nn.Module.html#torch.nn.Module.register_full_backward_hook for more details. loss.backward() Epoch 1/3 - loss: 0.7272 Epoch 2/3 - loss: 0.7570 Epoch 3/3 - loss: 0.7302 [PRIVACY]: Trained with ε = 1.80, δ = 1e-05 [ETHICAL NOTE]: Differential Privacy (DP-SGD) clips per-sample gradients and adds noise, limiting how much any single person's data can influence the trained model. This reduces privacy risks such as membership inference and data reconstruction. |
This output shows a successful, differentially private training run, along with several essential warnings and their implications. Opacus is using a noncryptographically secure random number generator to add noise. This is:
· Fine for experimentation and learning.
· Not sufficient for production-grade privacy guarantees.
The privacy math (ε, δ) is still computed, but the randomness source is weaker. For real deployments, secure_mode=True must be enabled to ensure cryptographically rigorous noise.
Opacus needs to know the exact sampling rate to compute privacy loss correctly. Dropping the last batch would make that ambiguous. Opacus overrides drop_last=True to preserve correct privacy accounting. This is expected and safe.
The “Full backward hook is firing...” message is a PyTorch internal warning, not an error. It happens because:
· The model is simple.
· Gradients are computed only with respect to outputs.
· Opacus attaches hooks for a per-sample gradient.
No action required. It does not affect correctness or privacy.
Epoch 1/3 - loss: 0.7272
Epoch 2/3 - loss: 0.7570
Epoch 3/3 - loss: 0.7302
The loss fluctuates and does not decrease. This is normal under DP-SGD because:
· Noise is injected into gradients.
· Gradient clipping limits optimization strength.
The model is learning under privacy constraints, trading some accuracy and stability for privacy protection.
[PRIVACY]: Trained with ε = 1.80, δ = 1e-05
This is the most important result:
· ε = 1.80: Strong privacy (lower ε = better privacy).
· δ = 1e-05: Very low probability of privacy failure.
An attacker cannot reliably infer whether any individual record was used during training. This is considered a reasonable and conservative privacy budget for many applications.
The final note correctly summarizes the outcome:
· Per-sample gradients were clipped.
· Random noise was added.
· Individual influence on the model is mathematically bound.
Recap
This execution demonstrates a proper, working implementation of DP. Despite warnings meant to guide production hardening, the model was trained with formal privacy guarantees, substantially reducing risks like:
· Membership inference attacks.
· Training data reconstruction.
· Leakage of sensitive individual information.
In short, the system behaved as expected: privacy was enforced, learning occurred, and trade-offs were transparent and measurable.
- 1800+ high-performance UI components.
- Includes popular controls such as Grid, Chart, Scheduler, and more.
- 24x5 unlimited support by developers.