Selected topic

ML in Federated Systems

Ml In Federated Systems

Prefer practical output? Use related tools below while reading.

Key Characteristics:

  1. Distributed data: Each organization has its own dataset, which remains on-premises.
  2. No data sharing: The organizations do not exchange their raw data with each other or with the cloud.
  3. Model updates: Each organization trains a local model on its private data and shares only the updated model parameters with the central server (or coordinator).
  4. Decentralized architecture: The system is designed to be decentralized, allowing for scalability and flexibility.

Benefits:

  1. Data privacy: Organizations maintain control over their sensitive data.
  2. Improved accuracy: Models are trained on diverse datasets, leading to better performance.
  3. Increased collaboration: Federated learning enables multiple organizations to collaborate on a shared goal without sharing sensitive data.

Example: Federated Learning for Image Classification

Suppose we have three hospitals (A, B, and C) that want to develop a deep learning model for diagnosing medical images. Each hospital has its own dataset of patient images, but they don't want to share these images with each other or the cloud due to privacy concerns.
  1. Local Model Training: Each hospital trains a local image classification model on their private data using a variant of the stochastic gradient descent (SGD) algorithm.
  2. Model Updates: After training, each hospital sends only the updated model parameters (weights and biases) to the central server, without sharing any raw images or labels.
  3. Central Server Update: The central server aggregates the received model updates from all hospitals, combines them, and generates a global model update.
  4. Global Model Update: The global model update is sent back to each hospital, which incorporates it into its local model.

Mathematical Representation

Let's denote:
  • m as the number of hospitals (or organizations)
  • D as the local dataset at each hospital
  • w as the shared model weights
  • θ_i as the local model parameters at hospital i
  • u_i as the update sent from hospital i to the central server
The federated learning process can be represented mathematically as:
  1. Local Model Training: Each hospital trains its own model on D using SGD, with objective function:
* Loss function: L(θ_i, D) * Optimizer: SGD
  1. Model Updates: Hospital i sends update u_i to the central server:
* Update rule: u_i = ∇L(θ_i)
  1. Central Server Update: The central server aggregates updates from all hospitals and combines them into a global model update:
* Aggregate update: ∑_i=1^m u_i
  1. Global Model Update: The global model update is sent back to each hospital:
* Global update rule: w = w + ∑_i=1^m u_i

This is a simplified example, and actual federated learning systems may involve more complex optimization algorithms, such as Federated Averaging (FedAvg) or Local Stochastic Gradient Descent (LSGD).

Code Example


Here's an example code snippet in PyTorch for federated image classification:
python
import torch
import torch.nn as nn

# Define the shared model architecture
class SharedModel(nn.Module):
def __init__(self):
super(SharedModel, self).__init__()
self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)

# Define the local model architecture
class LocalModel(nn.Module):
def __init__(self):
super(LocalModel, self).__init__()
self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)

# Define the federated learning process
class FederatedLearning:
def __init__(self):
self.shared_model = SharedModel()
self.local_models = [LocalModel() for _ in range(3)] # 3 hospitals

def train_local(self, dataset):
local_model = self.local_models[dataset]
optimizer = torch.optim.SGD(local_model.parameters(), lr=0.01)
loss_fn = nn.CrossEntropyLoss()
for epoch in range(10): # Train for 10 epochs
inputs, labels = dataset
outputs = local_model(inputs)
loss = loss_fn(outputs, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()

def update_global(self):
global_update = torch.zeros_like(self.shared_model.conv1.weight.data)
for i in range(3): # Aggregate updates from all hospitals
local_update = self.local_models[i].conv1.weight.data.clone()
global_update += local_update
self.shared_model.conv1.weight.data = global_update

# Initialize the federated learning process
fl = FederatedLearning()

# Train each hospital locally
for i in range(3):
fl.train_local(i) # i is the dataset index for hospital i

# Update the global model
fl.update_global()


Note that this code snippet is a simplified example and does not include all the necessary components of a real-world federated learning system.