{
“cells”: [
{
“cell_type”: “markdown”,
“metadata”: {
“id”: “cz71fPGrpRiQ”
},
“source”: [
“# Strategies in Federated Learning - Moving Beyond FedAvg with Flower Strategiesn”,
“n”,
“Welcome to the next part of the federated learning tutorial. In this notebook, we’ll begin to customize the federated learning system we built in the introductory notebook (again, using Flower and PyTorch).”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“id”: “mBu1HRRY6bwX”
},
“source”: [
“## Preparationn”,
“n”,
“Before we begin with the actual code, let’s make sure that we have everything we need.”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“id”: “D4KiTMTpiort”
},
“source”: [
“### Installing dependenciesn”,
“n”,
“Next, we install and import the necessary packages:”
]
},
{
“cell_type”: “code”,
“execution_count”: null,
“metadata”: {
“id”: “9PAxbNFt6in6”
},
“outputs”: [],
“source”: [
“!pip install flwr[“simulation”]==0.18.0 torch torchvision”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“id”: “abtAKdBl6in6”
},
“source”: [
“Now that we have the necessary packages installed, we can import everything we need for this tutorial:”
]
},
{
“cell_type”: “code”,
“execution_count”: null,
“metadata”: {
“id”: “eTrCL2FmC5U5”
},
“outputs”: [],
“source”: [
“from collections import OrderedDictn”,
“from typing import Dict, List, Optional, Tuplen”,
“n”,
“import flwr as fln”,
“import numpy as npn”,
“import torchn”,
“import torch.nn as nnn”,
“import torch.nn.functional as Fn”,
“import torchvision.transforms as transformsn”,
“from torch.utils.data import DataLoader, random_splitn”,
“from torchvision.datasets import CIFAR10n”,
“n”,
“DEVICE = torch.device(“cpu”)n”,
“# DEVICE = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)n”,
“print(f"Training on {DEVICE}”)"
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“id”: “8D2bnPKG58Gx”
},
“source”: [
“It is possible to switch to a runtime that has GPU acceleration enabled (on Google Colab: Runtime > Change runtime type > Hardware acclerator: GPU > Save). Note, however, that Google Colab is not always able to offer GPU acceleration. If you see an error related to GPU availability in one of the following sections, consider switching back to CPU-based execution by setting DEVICE = torch.device("cpu"). If the runtime has GPU acceleration enabled, you should see the output Training on cuda:0, otherwise it’ll say Training on cpu.”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“id”: “JVcgAAiaihnx”
},
“source”: [
“### Data loadingn”,
“n”,
“Let’s now load the CIFAR-10 training and test set, partition them into ten smaller datasets (each split into training and validation set), and wrap everything in their own DataLoader. We introduce a new parameter num_clients which allows us to call load_datasets with different numbers of clients.”
]
},
{
“cell_type”: “code”,
“execution_count”: null,
“metadata”: {
“id”: “J4Em7BPNTXeX”
},
“outputs”: [],
“source”: [
“NUM_CLIENTS = 10n”,
“n”,
“def load_datasets(num_clients: int):n”,
" # Download and transform CIFAR-10 (train and test)n",
" transform = transforms.Compose(n",
" [transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]n",
" )n",
" trainset = CIFAR10(“./dataset”, train=True, download=True, transform=transform)n",
" testset = CIFAR10(“./dataset”, train=False, download=True, transform=transform)n",
“n”,
" # Split training set into num_clients partitions to simulate different local datasetsn",
" partition_size = len(trainset) // num_clientsn",
" lengths = [partition_size] * num_clientsn",
" datasets = random_split(trainset, lengths, torch.Generator().manual_seed(42))n",
“n”,
" # Split each partition into train/val and create DataLoadern",
" trainloaders = []n",
" valloaders = []n",
" for ds in datasets:n",
" len_val = len(ds) // 10 # 10 % validation setn",
" len_train = len(ds) - len_valn",
" lengths = [len_train, len_val]n",
" ds_train, ds_val = random_split(ds, lengths, torch.Generator().manual_seed(42))n",
" trainloaders.append(DataLoader(ds_train, batch_size=32, shuffle=True))n",
" valloaders.append(DataLoader(ds_val, batch_size=32))n",
" testloader = DataLoader(testset, batch_size=32)n",
" return trainloaders, valloaders, testloadern",
“n”,
“trainloaders, valloaders, testloader = load_datasets(NUM_CLIENTS)”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“id”: “OBp7kB4G0sPB”
},
“source”: [
“### Model training/evaluationn”,
“n”,
“Let’s continue with the usual model definition (including set_parameters and get_parameters), training and test functions:”
]
},
{
“cell_type”: “code”,
“execution_count”: null,
“metadata”: {
“id”: “2X3cVBXMpP6w”
},
“outputs”: [],
“source”: [
“class Net(nn.Module):n”,
" def init(self) -> None:n",
" super(Net, self).init()n",
" self.conv1 = nn.Conv2d(3, 6, 5)n",
" self.pool = nn.MaxPool2d(2, 2)n",
" self.conv2 = nn.Conv2d(6, 16, 5)n",
" self.fc1 = nn.Linear(16 * 5 * 5, 120)n",
" self.fc2 = nn.Linear(120, 84)n",
" self.fc3 = nn.Linear(84, 10)n",
“n”,
" def forward(self, x: torch.Tensor) -> torch.Tensor:n",
" x = self.pool(F.relu(self.conv1(x)))n",
" x = self.pool(F.relu(self.conv2(x)))n",
" x = x.view(-1, 16 * 5 * 5)n",
" x = F.relu(self.fc1(x))n",
" x = F.relu(self.fc2(x))n",
" x = self.fc3(x)n",
" return xn",
“n”,
“n”,
“def get_parameters(net) -> List[np.ndarray]:n”,
" return [val.cpu().numpy() for _, val in net.state_dict().items()]n",
“n”,
“n”,
“def set_parameters(net, parameters: List[np.ndarray]):n”,
" params_dict = zip(net.state_dict().keys(), parameters)n",
" state_dict = OrderedDict({k: torch.Tensor(v) for k, v in params_dict})n",
" net.load_state_dict(state_dict, strict=True)n",
“n”,
“n”,
“def train(net, trainloader, epochs: int):n”,
" “”“Train the network on the training set.”“”n",
" criterion = torch.nn.CrossEntropyLoss()n",
" optimizer = torch.optim.Adam(net.parameters())n",
" net.train()n",
" for epoch in range(epochs):n",
" correct, total, epoch_loss = 0, 0, 0.0n",
" for images, labels in trainloader:n",
" images, labels = images.to(DEVICE), labels.to(DEVICE)n",
" optimizer.zero_grad()n",
" outputs = net(images)n",
" loss = criterion(net(images), labels)n",
" loss.backward()n",
" optimizer.step()n",
" # Metricsn",
" epoch_loss += lossn",
" total += labels.size(0)n",
" correct += (torch.max(outputs.data, 1)[1] == labels).sum().item()n",
" epoch_loss /= len(testloader.dataset)n",
" epoch_acc = correct / totaln",
" print(f"Epoch {epoch+1}: train loss {epoch_loss}, accuracy {epoch_acc}“)n”,
“n”,
“n”,
“def test(net, testloader):n”,
" “”“Evaluate the network on the entire test set.”“”n",
" criterion = torch.nn.CrossEntropyLoss()n",
" correct, total, loss = 0, 0, 0.0n",
" net.eval()n",
" with torch.no_grad():n",
" for images, labels in testloader:n",
" images, labels = images.to(DEVICE), labels.to(DEVICE)n",
" outputs = net(images)n",
" loss += criterion(outputs, labels).item()n",
" _, predicted = torch.max(outputs.data, 1)n",
" total += labels.size(0)n",
" correct += (predicted == labels).sum().item()n",
" loss /= len(testloader.dataset)n",
" accuracy = correct / totaln",
" return loss, accuracy"
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“id”: “1lCf3oljdClM”
},
“source”: [
“### Flower clientn”,
“n”,
“To implement the Flower client, we (again) create a subclass of flwr.client.NumPyClient and implement the three methods get_parameters, fit, and evaluate. Here, we also pass the cid to the client and use it log additional details:”
]
},
{
“cell_type”: “code”,
“execution_count”: null,
“metadata”: {
“id”: “ye6Jt5p3LWtF”
},
“outputs”: [],
“source”: [
“class FlowerClient(fl.client.NumPyClient):n”,
" def init(self, cid, net, trainloader, valloader):n",
" self.cid = cidn",
" self.net = netn",
" self.trainloader = trainloadern",
" self.valloader = valloadern",
“n”,
" def get_parameters(self):n",
" print(f"[Client {self.cid}] get_parameters")n",
" return get_parameters(self.net)n",
“n”,
" def fit(self, parameters, config):n",
" print(f"[Client {self.cid}] fit, config: {config}“)n”,
" set_parameters(self.net, parameters)n",
" train(self.net, self.trainloader, epochs=1)n",
" return get_parameters(self.net), len(self.trainloader), {}n",
“n”,
" def evaluate(self, parameters, config):n",
" print(f"[Client {self.cid}] evaluate, config: {config}“)n”,
" set_parameters(self.net, parameters)n",
" loss, accuracy = test(self.net, self.valloader)n",
" return float(loss), len(self.valloader), {“accuracy”: float(accuracy)}n",
“n”,
“n”,
“def client_fn(cid) -> FlowerClient:n”,
" net = Net().to(DEVICE)n",
" trainloader = trainloaders[int(cid)]n",
" valloader = valloaders[int(cid)]n",
" return FlowerClient(cid, net, trainloader, valloader)"
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“id”: “a6HP2cYCsqxD”
},
“source”: [
“## Strategy customizationn”,
“n”,
“So far, everything should look familiar if you’ve worked through the introductory notebook. With that, we’re ready to introduce a number of new features. "
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“id”: “p6FHRf8HrbzV”
},
“source”: [
“### Server-side parameter initializationn”,
“n”,
“Flower, by default, initializes the global model by asking one random client for the initial parameters. In many cases, we want more control over parameter initialization though. Flower therefore allows you to directly pass the initial parameters to the Strategy:”
]
},
{
“cell_type”: “code”,
“execution_count”: null,
“metadata”: {
“id”: “GPeFWwyFrY9f”
},
“outputs”: [],
“source”: [
“# Create an instance of the model and get the parametersn”,
“params = get_parameters(Net())n”,
“n”,
“# Pass parameters to the Strategy for server-side parameter initializationn”,
“strategy = fl.server.strategy.FedAvg(n”,
" fraction_fit=0.3,n”,
" fraction_eval=0.3,n",
" min_fit_clients=3,n",
" min_eval_clients=3,n",
" min_available_clients=NUM_CLIENTS,n",
" initial_parameters=fl.common.weights_to_parameters(params),n",
“)n”,
“n”,
“# Start simulationn”,
“fl.simulation.start_simulation(n”,
" client_fn=client_fn,n",
" num_clients=NUM_CLIENTS,n",
" num_rounds=3, # Just three roundsn",
" strategy=strategy,n",
“)”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“id”: “Y4q2B84tMduY”
},
“source”: [
“Passing initial_parameters to the FedAvg strategy prevents Flower from asking one of the clients for the initial parameters. If we look closely, we can see that the logs do not show any calls to the FlowerClient.get_parameters method.”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“id”: “axzXSMtlfhXU”
},
“source”: [
“### Starting with a customized strategyn”,
“n”,
“We’ve seen the function start_simulation before. It accepts a number of arguments, amongst them the client_fn used to create FlowerClient instances, the number of clients to simulate num_clients, the number of rounds num_rounds, and the strategy.n”,
“n”,
“The strategy encapsulates the federated learning approach/algorithm, for example, FedAvg or FedAdagrad. Let’s try to use a different strategy this time:”
]
},
{
“cell_type”: “code”,
“execution_count”: null,
“metadata”: {
“id”: “ELNy0-0nfyI2”
},
“outputs”: [],
“source”: [
“# Create FedAdam strategyn”,
“strategy=fl.server.strategy.FedAdagrad(n”,
" fraction_fit=0.3,n",
" fraction_eval=0.3,n",
" min_fit_clients=3,n",
" min_eval_clients=3,n",
" min_available_clients=NUM_CLIENTS,n",
" initial_parameters=fl.common.weights_to_parameters(get_parameters(Net())),n",
“)n”,
“n”,
“# Start simulationn”,
“fl.simulation.start_simulation(n”,
" client_fn=client_fn,n",
" num_clients=NUM_CLIENTS,n",
" num_rounds=3, # Just three roundsn",
" strategy=strategy,n",
“)”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“id”: “wt3_SmQKnpRO”
},
“source”: [
“## Server-side parameter evaluationn”,
“n”,
“Flower can evaluate the aggregated model on the server-side or on the client-side. Client-side and server-side evaluation are similar in some ways, but different in others.n”,
“n”,
“Centralized Evaluation (or server-side evaluation) is conceptually simple: it works the same way that evaluation in centralized machine learning does. If there is a server-side dataset that can be used for evaluation purposes, then that’s great. We can evaluate the newly aggregated model after each round of training without having to send the model to clients. We’re also fortunate in the sense that our entire evaluation dataset is available at all times.n”,
“n”,
“Federated Evaluation (or client-side evaluation) is more complex, but also more powerful: it doesn’t require a centralized dataset and allows us to evaluate models over a larger set of data, which often yields more realistic evaluation results. In fact, many scenarios require us to use Federated Evaluation if we want to get representative evaluation results at all. But this power comes at a cost: once we start to evaluate on the client side, we should be aware that our evaluation dataset can change over consecutive rounds of learning if those clients are not always available. Moreover, the dataset held by each client can also change over consecutive rounds. This can lead to evaluation results that are not stable, so even if we would not change the model, we’d see our evaluation results fluctuate over consecutive rounds.n”,
“n”,
“We’ve seen how federated evaluation works on the client side (i.e., by implementing the evaluate method in FlowerClient). Now let’s see how we can evaluate aggregated model parameters on the server-side:”
]
},
{
“cell_type”: “code”,
“execution_count”: null,
“metadata”: {
“id”: “MDovnUvsn7if”
},
“outputs”: [],
“source”: [
“# The evaluate function will be by Flower called after every roundn”,
“def evaluate(n”,
" weights: fl.common.Weights,n",
“) -> Optional[Tuple[float, Dict[str, fl.common.Scalar]]]:n”,
" net = Net()n",
" valloader = valloaders[0]n",
" set_parameters(net, weights) # Update model with the latest parametersn",
" loss, accuracy = test(net, valloader)n",
" print(f"Server-side evaluation loss {loss} / accuracy {accuracy}“)n”,
" return loss, {“accuracy”: accuracy}"
]
},
{
“cell_type”: “code”,
“execution_count”: null,
“metadata”: {
“id”: “r5aGRnyQnu0Z”
},
“outputs”: [],
“source”: [
“strategy = fl.server.strategy.FedAvg(n”,
" fraction_fit=0.3,n",
" fraction_eval=0.3,n",
" min_fit_clients=3,n",
" min_eval_clients=3,n",
" min_available_clients=NUM_CLIENTS,n",
" initial_parameters=fl.common.weights_to_parameters(get_parameters(Net())),n",
" eval_fn=evaluate, # Pass the evaluation functionn",
“)n”,
“n”,
“fl.simulation.start_simulation(n”,
" client_fn=client_fn,n",
" num_clients=NUM_CLIENTS,n",
" num_rounds=3, # Just three roundsn",
" strategy=strategy,n",
“)”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“id”: “-yW_cnCapDN9”
},
“source”: [
“## Sending/receiving arbitrary values to/from clientsn”,
“n”,
“In some situations, we want to configure client-side execution (trainig, evaluation) from the server-side. One example for that is the server asking the clients to train for a certain number of local epochs. Flower provides a way to send configuration values from the server to the clients using a dictionary. Let’s look at an example where the clients receive values from the server through the config parameter in fit (config is also available in evaluate). The fit method receives the configuration dictionary through the config parameter and can then read values from this dictionary. In this example, it reads current_round and local_epochs and uses those values to improve the logging and configure the number of local training epochs:”
]
},
{
“cell_type”: “code”,
“execution_count”: null,
“metadata”: {
“id”: “ZH3f2rt7h-Ih”
},
“outputs”: [],
“source”: [
“class FlowerClient(fl.client.NumPyClient):n”,
" def init(self, cid, net, trainloader, valloader):n",
" self.cid = cidn",
" self.net = netn",
" self.trainloader = trainloadern",
" self.valloader = valloadern",
“n”,
" def get_parameters(self):n",
" print(f"[Client {self.cid}] get_parameters")n",
" return get_parameters(self.net)n",
“n”,
" def fit(self, parameters, config):n",
" # Read values from confign",
" current_round = config[“current_round”]n",
" local_epochs = config[“local_epochs”]n",
“n”,
" # Use values provided by the confign",
" print(f"[Client {self.cid}, round {current_round}] fit, config: {config}“)n”,
" set_parameters(self.net, parameters)n",
" train(self.net, self.trainloader, epochs=local_epochs)n",
" return get_parameters(self.net), len(self.trainloader), {}n",
“n”,
" def evaluate(self, parameters, config):n",
" print(f"[Client {self.cid}] evaluate, config: {config}“)n”,
" set_parameters(self.net, parameters)n",
" loss, accuracy = test(self.net, self.valloader)n",
" return float(loss), len(self.valloader), {“accuracy”: float(accuracy)}n",
“n”,
“n”,
“def client_fn(cid) -> FlowerClient:n”,
" net = Net().to(DEVICE)n",
" trainloader = trainloaders[int(cid)]n",
" valloader = valloaders[int(cid)]n",
" return FlowerClient(cid, net, trainloader, valloader)"
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“id”: “F8eKOYYsix3W”
},
“source”: [
“So how can we send this config dictionary from server to clients? The built-in Flower Strategies provide way to do this, and it works similarly to the way server-side evaluation works. We provide a function to the strategy, and the strategy calls this function for every round of federated learning:”
]
},
{
“cell_type”: “code”,
“execution_count”: null,
“metadata”: {
“id”: “SPNBmtcm4aWN”
},
“outputs”: [],
“source”: [
“def fit_config(rnd: int):n”,
" “”“Return training configuration dict for each round.n”,
" n",
" Perform two rounds of training with one local epoch, increase to two localn",
" epochs afterwards.n",
" “”“n”,
" config = {n",
" “current_round”: rnd, # The current round of federated learningn",
" “local_epochs”: 1 if rnd < 2 else 2, # n",
" }n",
" return config"
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“id”: “9o0rirlfl7h-”
},
“source”: [
“Next, we’ll just pass this function to the FedAvg strategy before starting the simulation:”
]
},
{
“cell_type”: “code”,
“execution_count”: null,
“metadata”: {
“id”: “MC-274Yyl8C5”
},
“outputs”: [],
“source”: [
“strategy = fl.server.strategy.FedAvg(n”,
" fraction_fit=0.3,n",
" fraction_eval=0.3,n",
" min_fit_clients=3,n",
" min_eval_clients=3,n",
" min_available_clients=NUM_CLIENTS,n",
" initial_parameters=fl.common.weights_to_parameters(get_parameters(Net())),n",
" eval_fn=evaluate,n",
" on_fit_config_fn=fit_config, # Pass the fit_config functionn",
“)n”,
“n”,
“fl.simulation.start_simulation(n”,
" client_fn=client_fn,n",
" num_clients=NUM_CLIENTS,n",
" num_rounds=3, # Just three roundsn",
" strategy=strategy,n",
“)”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“id”: “8r0p0CVmlvgM”
},
“source”: [
“As we can see, the client logs now include the current round of federated learning (which they read from the config dictionary). We can also configure local training to run for one epoch during the first and second round of federated learning, and then for two epochs during the third round.n”,
“n”,
“Clients can also return arbitrary values to the server. To do so, they return a dictionary from fit and/or evaluate. We have seen and used this concept throughout this notebook without mentioning it explicitly: our FlowerClient returns a dictionary containing a custom key/value pair as the third return value in evaluate.”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“id”: “POBApsmwuCx2”
},
“source”: [
“## Scaling federated learningn”,
“n”,
“As a last step in this notebook, let’s see how we can use Flower to experiment with a large number of clients.”
]
},
{
“cell_type”: “code”,
“execution_count”: null,
“metadata”: {
“id”: “vVUW5oDgxqRR”
},
“outputs”: [],
“source”: [
“NUM_CLIENTS = 1000n”,
“n”,
“trainloaders, valloaders, testloader = load_datasets(NUM_CLIENTS)”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“id”: “b6Z3N_svyCFt”
},
“source”: [
“We now have 1000 partitions, each holding 45 training and 5 validation examples. Given that the number of training examples on each client is quite small, we should probably traing the model a bit longer, so we configure the clients to perform 3 local training epochs. We should also adjust the fraction of clients selected for training during each round (we don’t want all 1000 clients participating in every round), so we adjust fraction_fit to 0.05, which means that only 5% of available clients (so 50 clients) will be selected for training each round:n”
]
},
{
“cell_type”: “code”,
“execution_count”: null,
“metadata”: {
“id”: “zVTrw7OsttE7”
},
“outputs”: [],
“source”: [
“def fit_config(rnd: int):n”,
" config = {n",
" “current_round”: rnd,n",
" “local_epochs”: 3,n",
" }n",
" return confign",
“n”,
“strategy = fl.server.strategy.FedAvg(n”,
" fraction_fit=0.025, # Train on 25 clients (each round)n",
" fraction_eval=0.05, # Evaluate on 50 clients (each round)n",
" min_fit_clients=20,n",
" min_eval_clients=40,n",
" min_available_clients=NUM_CLIENTS,n",
" initial_parameters=fl.common.weights_to_parameters(get_parameters(Net())),n",
" on_fit_config_fn=fit_config,n",
“)n”,
“n”,
“fl.simulation.start_simulation(n”,
" client_fn=client_fn,n",
" num_clients=NUM_CLIENTS,n",
" num_rounds=3,n",
" strategy=strategy,n",
“)”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“id”: “Ez00lLOA3waL”
},
“source”: [
“## Recapn”,
“n”,
“In this notebook, we’ve seen how we can gradually enhance our system by customizing the strategy, initializing parameters on the server side, choosing a different strategy, and evaluating models on the server-side. That’s quite a bit of flexibility with so little code, right?n”,
“n”,
“In the later sections, we’ve seen how we can communicate arbitrary values between server and clients to fully customize client-side execution. With that capability, we built a large-scale Federated Learning simulation using the Flower Virtual Client Engine and ran an experiment involving 1000 clients in the same workload - all in a Jupyter Notebook!”
]
}
],
“metadata”: {
“accelerator”: “GPU”,
“colab”: {
“collapsed_sections”: [],
“name”: “Flower-2-Strategies-in-FL-PyTorch.ipynb”,
“provenance”: [],
“toc_visible”: true
},
“kernelspec”: {
“display_name”: “Python 3”,
“name”: “python3”
},
“language_info”: {
“name”: “python”
}
},
“nbformat”: 4,
“nbformat_minor”: 0
}



