Flower Simulation example using TensorFlow/Keras#
This introductory example uses the simulation capabilities of Flower to simulate a large number of clients on either a single machine of a cluster of machines. Take a look at the Documentation for a deep dive on how Flower simulation works.
Running the example (via Jupyter Notebook)#
Alternatively, you can run
sim.ipynb locally or in any other Jupyter environment.
Running the example#
Start by cloning the code example. We prepared a single-line command that you can copy into your shell which will checkout the example for you:
git clone --depth=1 https://github.com/adap/flower.git && mv flower/examples/simulation-tensorflow . && rm -rf flower && cd simulation-tensorflow
This will create a new directory called
simulation-tensorflow containing the following files:
-- README.md <- Your're reading this right now -- sim.ipynb <- Example notebook -- sim.py <- Example code -- pyproject.toml <- Example dependencies -- requirements.txt <- Example dependencies
Project dependencies (such as
flwr) are defined in
requirements.txt. We recommend Poetry to install those dependencies and manage your virtual environment (Poetry installation) or pip, but feel free to use a different way of installing dependencies and managing virtual environments if you have other preferences.
poetry install poetry shell
Poetry will install all your dependencies in a newly created virtual environment. To verify that everything works correctly you can run the following command:
poetry run python3 -c "import flwr"
If you don’t see any errors you’re good to go!
Write the command below in your terminal to install the dependencies according to the configuration file requirements.txt.
pip install -r requirements.txt
Run Federated Learning Example#
# You can run the example without activating your environemnt poetry run python3 sim.py # Or by first activating it poetry shell # and then run the example python sim.py # you can exit your environment by typing "exit"
You can adjust the CPU/GPU resources you assign to each of your virtual clients. By default, your clients will only use 1xCPU core. For example:
# Will assign 2xCPUs to each client python sim.py --num_cpus=2 # Will assign 2xCPUs and 20% of the GPU's VRAM to each client # This means that you can have 5 concurrent clients on each GPU # (assuming you have enough CPUs) python sim.py --num_cpus=2 --num_gpus=0.2
Take a look at the Documentation for more details on how you can customise your simulation.