Federated Learning with PyTorch and Flower (Quickstart Example)

View on GitHub

[!CAUTION] This example uses a deprecated API for Flower. Use instead the quickstart-pytorch which makes use of the Message API.

This introductory example to Flower uses PyTorch, but deep knowledge of PyTorch is not necessarily required to run the example. However, it will help you understand how to adapt Flower to your use case. Running this example in itself is quite easy. This example uses Flower Datasets to download, partition and preprocess the CIFAR-10 dataset.

Install dependencies and project

The dependencies are listed in the pyproject.toml and you can install them as follows:

pip install -e .

Tip: Your pyproject.toml file can define more than just the dependencies of your Flower app. You can also use it to specify hyperparameters for your runs and control which Flower Runtime is used. By default, it uses the Simulation Runtime, but you can switch to the Deployment Runtime when needed. Learn more in the TOML configuration guide.

Run with the Simulation Engine

In the quickstart-pytorch-deprecated directory, use flwr run to run a local simulation:

flwr run .

Refer to the How to Run Simulations guide in the documentation for advice on how to optimize your simulations.

Run with the Deployment Engine

Follow this how-to guide to run the same app in this example but with Flower’s Deployment Engine. After that, you might be interested in setting up secure TLS-enabled communications and SuperNode authentication in your federation.

You can run Flower on Docker too! Check out the Flower with Docker documentation.

Resources