Federated Learning: Complete Guide
Summary
Federated Learning is a distributed machine learning paradigm that enables training machine learning models on data distributed across many devices or servers, without ever centralizing the raw data. Initially proposed by Google in 2017, Federated Learning is based on an elegant principle: instead of moving data to the model, the model is moved to the data. This revolutionary approach solves fundamental privacy and regulatory compliance issues (GDPR, HIPAA) while leveraging the richness of distributed data. In this complete guide, we will explore the mathematical principle of Federated Learning via the FedAvg algorithm, its deep intuition, a complete Python implementation, its critical hyperparameters, as well as its concrete use cases in industry.
Mathematical Principle of Federated Learning
The FedAvg Algorithm (Federated Averaging)
The core of Federated Learning is the FedAvg (Federated Averaging) algorithm, introduced by McMahan et al. in 2017. This algorithm orchestrates collaboration between a central server and multiple clients without exposing each client’s local data.
Basic operation:
Let there be a set of $K$ clients, where each client $k$ has a local dataset $D_k$ containing $n_k$ samples. The global objective is to minimize the cost function:
$$F(w) = \sum_{k=1}^{K} \frac{n_k}{n} F_k(w)$$
where $n = \sum_{k=1}^{K} n_k$ is the total number of samples and $F_k(w)$ is the local cost function of client $k$.
At each communication round $t$:
- Client selection: The server selects a fraction $C$ of available clients (typically 10%).
- Local training: Each selected client $k$ trains the model locally for a fixed number of epochs, starting from the global weights $w_t$ received from the server. After local training, the client obtains updated weights $w_k^{t+1}$:
$$w_k^{t+1} = w_t – \eta \nabla F_k(w_t)$$
where $\eta$ is the local learning rate.
- FedAvg aggregation: The server aggregates the received updates in a weighted manner according to the number of samples from each client:
$$w_{t+1} = \sum_{k=1}^{K} \frac{n_k}{n} \cdot w_k^{t+1}$$
This is the fundamental formula of Federated Learning: the new global model is a weighted average of the local models, where each client’s weight is proportional to its data quantity.
- Repetition: The process repeats over several communication rounds until the global model converges.
The non-IID data challenge
A major problem of Federated Learning is that each client’s data generally does not follow the same distribution (non-IID: non-Independent and Identically Distributed). For example, a Parisian user will take photos of French monuments, while a Tokyo user will photograph Japanese temples. This heterogeneity considerably complicates aggregation, as local gradients converge to different local minima.
Several techniques have been proposed to mitigate this problem:
- FedProx: Adds a proximal regularization term to limit the drift of local weights relative to the global model.
- SCAFFOLD: Uses control variables to correct the bias caused by data heterogeneity.
- Momentum FedAvg: Integrates momentum into server aggregation to stabilize convergence.
Differential Privacy
Even though raw data never leaves the devices, model updates $w_k$ can potentially leak sensitive information through inference attacks. To strengthen protection, differential privacy is applied to Federated Learning by adding Gaussian noise to the gradients before sending them to the server:
$$w_k’ = w_k + \mathcal{N}(0, \sigma^2)$$
where $\mathcal{N}(0, \sigma^2)$ is a normal distribution with zero mean and variance $\sigma^2$. The parameter $\sigma$ controls the fundamental tradeoff between privacy and model performance: too much noise degrades learning quality, while insufficient noise offers inadequate protection.
Communication efficiency
In a real Federated Learning system, available bandwidth is often limited, particularly when clients are mobile devices. Several compression techniques are employed:
- Quantization: Reducing weight precision (e.g., from float32 to float16 or int8).
- Sparsification: Sending only the most significant gradients.
- Encoding: Using coding techniques to represent updates compactly.
Intuition: Understanding Federated Learning Simply
Imagine the following scenario: ten hospitals each possess patient medical records, but for legal and ethical reasons, they cannot share this data with each other. If we wanted to train an artificial intelligence model capable of detecting rare diseases, the classical approach would be to gather all the data in a central repository. But this centralization creates an immense risk to privacy: a data leak, a cyberattack, or even misuse would have catastrophic consequences.
Federated Learning proposes a radically different and much more elegant approach. Instead of gathering the data, the model is sent to each hospital. Concretely:
- An initial model is created on a central server.
- This model is sent to each participating hospital.
- Each hospital trains the model on its own data, locally, without ever exporting it.
- Each hospital sends back only the model updates (adjusted weights), not the data itself.
- The central server aggregates all these updates to create an improved global model.
- The cycle repeats with the new global model.
The student analogy: Imagine a group of ten students preparing together for a difficult exam. Instead of meeting in a common library (which would be equivalent to centralizing data), each student reviews at home with their own notes. Then, instead of sharing their raw notes (their data), they share only what they learned: their summaries, their key formulas, their tips. The professor (the server) compiles all these summaries to create a global study guide, which is then redistributed to each student. Each student then enriches their personal review with the group’s knowledge. This is exactly the principle of Federated Learning: collaborative learning without sharing source data.
This metaphor perfectly illustrates the elegance of Federated Learning: intelligence emerges from collaboration, but data intimacy remains preserved. It is a major advancement that makes it possible to train powerful models on sensitive data — medical, financial, personal — while respecting the privacy of each individual.
Complete Python Implementation
Here is a complete simulation of Federated Learning with the FedAvg algorithm, using 10 clients, a local training loop, FedAvg aggregation, and global evaluation. This implementation is based on PyTorch and simulates a classification scenario on synthetic data.
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
import copy
# ============================================================
# Federated Learning Configuration
# ============================================================
N_CLIENTS = 10
N_ROUNDS = 20
LOCAL_EPOCHS = 5
CLIENT_FRACTION = 0.5 # 50% of clients selected per round
LEARNING_RATE = 0.01
BATCH_SIZE = 32
SEED = 42
torch.manual_seed(SEED)
np.random.seed(SEED)
# ============================================================
# Model: Simple neural network for classification
# ============================================================
class ClientModel(nn.Module):
"""MLP-type model used by each client."""
def __init__(self, input_dim=20, hidden_dim=64, output_dim=4):
super(ClientModel, self).__init__()
self.network = nn.Sequential(
nn.Linear(input_dim, hidden_dim),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, output_dim),
)
def forward(self, x):
return self.network(x)
# ============================================================
# Non-IID synthetic data generation
# ============================================================
def generate_non_iid_data(n_clients, samples_per_client, input_dim, output_dim):
"""
Generates non-IID data: each client has a slightly different
distribution to simulate real data heterogeneity.
"""
client_datasets = []
for k in range(n_clients):
# Each client has a different bias in their data
bias = np.random.randn(input_dim) * 0.5 * (k / n_clients)
X = np.random.randn(samples_per_client, input_dim).astype(np.float32) + bias
y = np.random.randint(0, output_dim, size=samples_per_client)
dataset = TensorDataset(
torch.from_numpy(X), torch.from_numpy(y).long()
)
client_datasets.append(dataset)
return client_datasets
# ============================================================
# Local training on a client
# ============================================================
def train_client(model, dataset, epochs, lr, batch_size):
"""
Trains the model locally on a client's data.
Returns the updated weights after local training.
"""
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=lr, momentum=0.9)
model.train()
for epoch in range(epochs):
epoch_loss = 0.0
for batch_X, batch_y in dataloader:
optimizer.zero_grad()
outputs = model(batch_X)
loss = criterion(outputs, batch_y)
loss.backward()
optimizer.step()
epoch_loss += loss.item()
return model.state_dict()
# ============================================================
# FedAvg Aggregation
# ============================================================
def fedavg_aggregate(weighted_updates):
"""
FedAvg aggregation: weighted average of local weights.
w_global = sum(n_k / n) * w_k
"""
global_state = copy.deepcopy(weighted_updates[0][0])
total_samples = sum(n_k for _, n_k in weighted_updates)
for key in global_state.keys():
global_state[key] = torch.zeros_like(global_state[key])
for client_state, n_k in weighted_updates:
global_state[key] += (n_k / total_samples) * client_state[key]
return global_state
# ============================================================
# Global evaluation
# ============================================================
def evaluate_global(model, test_dataset):
"""Evaluates the global model on a centralized test set."""
dataloader = DataLoader(test_dataset, batch_size=256, shuffle=False)
model.eval()
correct = 0
total = 0
with torch.no_grad():
for batch_X, batch_y in dataloader:
outputs = model(batch_X)
_, predicted = torch.max(outputs, 1)
total += batch_y.size(0)
correct += (predicted == batch_y).sum().item()
accuracy = correct / total
return accuracy
# ============================================================
# Main Federated Learning loop
# ============================================================
def federated_learning():
"""
Main Federated Learning loop with FedAvg.
"""
# Data generation
client_datasets = generate_non_iid_data(
n_clients=N_CLIENTS,
samples_per_client=500,
input_dim=20,
output_dim=4,
)
# Global test set
X_test = np.random.randn(2000, 20).astype(np.float32)
y_test = np.random.randint(0, 4, size=2000)
test_dataset = TensorDataset(
torch.from_numpy(X_test), torch.from_numpy(y_test).long()
)
# Initial global model
global_model = ClientModel(input_dim=20, hidden_dim=64, output_dim=4)
print("=" * 60)
print("Federated Learning — Simulation with FedAvg")
print(f"Clients: {N_CLIENTS} | Rounds: {N_ROUNDS}")
print(f"Local epochs: {LOCAL_EPOCHS} | Client fraction: {CLIENT_FRACTION}")
print(f"Learning rate: {LEARNING_RATE}")
print("=" * 60)
n_clients_per_round = max(1, int(N_CLIENTS * CLIENT_FRACTION))
for round_num in range(N_ROUNDS):
# Random client selection for this round
selected_clients = np.random.choice(
N_CLIENTS, size=n_clients_per_round, replace=False
)
weighted_updates = []
for client_id in selected_clients:
# Copy global model for the client
client_model = ClientModel(input_dim=20, hidden_dim=64, output_dim=4)
client_model.load_state_dict(copy.deepcopy(global_model.state_dict()))
# Local training
local_weights = train_client(
model=client_model,
dataset=client_datasets[client_id],
epochs=LOCAL_EPOCHS,
lr=LEARNING_RATE,
batch_size=BATCH_SIZE,
)
n_k = len(client_datasets[client_id])
weighted_updates.append((local_weights, n_k))
# FedAvg aggregation
global_weights = fedavg_aggregate(weighted_updates)
global_model.load_state_dict(global_weights)
# Global evaluation
accuracy = evaluate_global(global_model, test_dataset)
print(f"Round {round_num + 1:3d} | Global accuracy: {accuracy:.4f} | "
f"Clients selected: {n_clients_per_round}")
print("=" * 60)
final_accuracy = evaluate_global(global_model, test_dataset)
print(f"Final accuracy after {N_ROUNDS} rounds: {final_accuracy:.4f}")
return global_model, final_accuracy
# ============================================================
# Execution
# ============================================================
if __name__ == "__main__":
model, acc = federated_learning()
print(f"\nFederated Learning completed with accuracy of {acc:.2%}")
This implementation covers the entire Federated Learning pipeline: simulated non-IID data generation, independent local training on each client, FedAvg aggregation weighted by number of samples, and iterative global evaluation. It constitutes a solid foundation that can be extended with differential privacy, communication compression, or advanced aggregation algorithms such as FedProx or SCAFFOLD.
Key Federated Learning Hyperparameters
Federated Learning introduces specific hyperparameters that do not exist in classical centralized learning. Each one profoundly influences convergence, privacy, and system efficiency.
| Hyperparameter | Description | Typical values | Impact |
|---|---|---|---|
| n_clients | Total number of participating clients | 10 to 10,000+ | More clients means greater data diversity, but coordination becomes complex |
| n_rounds | Number of communication rounds | 10 to 1,000 | Determines global convergence; too few = under-learning, too many = wasted communication |
| local_epochs | Number of training epochs per client | 1 to 10 | More local epochs reduce needed communication but can cause weight drift |
| client_fraction | Fraction of clients selected each round | 0.05 to 0.5 | Higher fraction accelerates convergence but increases network load |
| learning_rate | Local learning rate | 0.001 to 0.1 | Must be adjusted based on data heterogeneity; non-IID data often needs a lower rate |
| batch_size | Local training batch size | 16 to 128 | Influences gradient variance and training speed |
| noise_scale ($\sigma$) | Differential privacy noise scale | 0.001 to 1.0 | Fundamental tradeoff between privacy and model performance |
| compression_ratio | Update compression ratio | 0.1 to 0.9 | Reduces bandwidth but can degrade accuracy |
Practical recommendations
- Start with:
local_epochs=5,client_fraction=0.2,learning_rate=0.01 - If convergence is slow: Increase
client_fractionorlocal_epochs - If weight drift (model diverges): Reduce
local_epochsor add regularization (FedProx) - For highly non-IID data: Reduce the learning rate and increase the number of rounds
Advantages of Federated Learning
Federated Learning offers considerable advantages over traditional centralized approaches:
- Enhanced privacy: Sensitive data never leaves the client’s device or local server. Only model updates (weights) are transmitted, considerably reducing the risk of data leaks.
- Regulatory compliance: Federated Learning naturally facilitates compliance with GDPR, HIPAA, and other data protection regulations, since personal data is neither collected nor transferred.
- Reduced bandwidth: Transmitting model weights (a few megabytes) is much less costly than transferring gigabytes of raw data to a central server, especially when clients are mobile devices.
- Massive scalability: Federated Learning can work with millions of clients simultaneously, as demonstrated by Google with its Gboard keyboard, which continuously improves thanks to user keystrokes.
- Robustness and resilience: The system is distributed by nature. The failure of several clients does not prevent the global Federated Learning from functioning properly.
Limits and Challenges
Despite its advantages, Federated Learning faces several important challenges:
- Data heterogeneity (non-IID): This is the most fundamental challenge. When data distributions vary greatly between clients, global model convergence can be slow, unstable, or even impossible without specialized techniques such as FedProx or SCAFFOLD.
- Costly communication: Each round requires bidirectional exchange between the server and selected clients. On unstable or low-bandwidth networks, this can become a major bottleneck.
- Device heterogeneity: Clients can have very different computing and storage capabilities. An entry-level smartphone cannot train a model as quickly as a powerful server, creating synchronization problems.
- Potential attacks: Although data is not shared, malicious clients can send poisoned updates to corrupt the global model (Byzantine poisoning attacks). Detection and robustness mechanisms are necessary.
- Complex debugging: Identifying why a Federated Learning model is not converging is particularly difficult, as one cannot directly inspect local data or the intermediate gradients of each client.
4 Concrete Use Cases of Federated Learning
1. Healthcare and Collaborative Medicine
Several hospitals can collaborate to train a medical diagnostic model (e.g., cancer detection on radiological images) without ever sharing patient records. Each hospital trains locally on its own images and the server aggregates the results. Projects like NVIDIA Clara and OpenMined have already demonstrated the viability of this approach. The impact is considerable: small hospitals can benefit from models trained on millions of images, while protecting the privacy of their patients.
2. Smart Keyboards and Text Prediction
Google Gboard has been using Federated Learning since 2017 to improve its typing suggestions. Each local training on your smartphone is based on your own typing habits. Only model updates are sent to Google, not your messages. This means the keyboard learns from your personal vocabulary — proper nouns, familiar expressions, professional jargon — without ever reading your conversations. Billions of devices participate in this large-scale Federated Learning.
3. Finance and Fraud Detection
Banks can collaborate to detect fraud patterns without sharing their sensitive client data. Each bank trains a model on its own suspicious transactions, and the global model benefits from the collective experience of all participating banks. This approach is particularly relevant in the European context, where banking regulations prohibit the direct sharing of client data between competing institutions. Federated Learning therefore enables collective intelligence without compromising regulatory compliance.
4. Autonomous Vehicles and Intelligent Driving
Connected vehicles in a fleet can collectively learn from road conditions, dangerous situations, and optimal driving behaviors. Each vehicle locally trains a perception model from its sensors (cameras, lidars), and the updates are aggregated to improve the perception of the entire fleet. Tesla has explored this approach with its network of vehicles to improve its autonomous driving systems. The benefits are twofold: continuous improvement of perception capabilities and respect for driver privacy, whose personal journeys are never centralized.
See Also
- Two Modules to Properly Secure Your Web Browser
- Testing the Primality of $2n^2 – 1$ in Python: Complete and Optimized Guide

