Quickstart PyTorch Lightning¶
Dans ce tutoriel d’apprentissage fédéré, nous allons apprendre à entraîner un modèle AutoEncoder sur MNIST en utilisant Flower et PyTorch Lightning. Il est recommandé de créer un environnement virtuel et d’exécuter tout cela dans un virtualenv.
Maintenant que nous avons une idée approximative de ce que cet exemple est sur, allons-y. Tout d’abord, installez Flower dans votre nouvel environnement :
# In a new Python environment
$ pip install flwr
Ensuite, exécutez la commande suivante :
$ flwr new @flwrlabs/quickstart-pytorch-lightning
quickstart-pytorch-lightning
├── pytorchlightning_example
│ ├── client_app.py # Defines your ClientApp
│ ├── server_app.py # Defines your ServerApp
│ └── task.py # Defines your model, training and data loading
├── pyproject.toml # Project metadata like dependencies and configs
└── README.md
Next, activate your environment and navigate to the example directory:
$ cd quickstart-pytorch-lightning
By default, this project uses a local simulation profile that flwr run submits to a
managed local SuperLink, which then executes the run with the Flower Simulation Runtime.
It creates a federation of two SuperNodes using FedAvg as the aggregation strategy.
The dataset will be partitioned using Flower Dataset’s IidPartitioner. To run the
project, do:
# Run with default arguments and stream logs
$ flwr run . --stream
Le processus flwr run . soumet l’exécution, imprime l’ID d’exécution et retourne sans diffuser les journaux. Pour le workflow local complet, voir Exécuter Flower Localement avec un SuperLink Géré.
Avec les arguments par défaut, vous verrez une sortie en flux continu comme celle-ci :
Starting local SuperLink on 127.0.0.1:39093...
Successfully started run 1859953118041441032
INFO : Starting FedAvg strategy:
INFO : ├── Number of rounds: 3
INFO : [ROUND 1/3]
INFO : configure_train: Sampled 2 nodes (out of 2)
INFO : aggregate_train: Received 2 results and 0 failures
INFO : └──> Aggregated MetricRecord: {'train_loss': 0.0487}
INFO : configure_evaluate: Sampled 2 nodes (out of 2)
INFO : aggregate_evaluate: Received 2 results and 0 failures
INFO : └──> Aggregated MetricRecord: {'eval_loss': 0.0495}
INFO : [ROUND 2/3]
INFO : ...
INFO : [ROUND 3/3]
INFO : ...
INFO : Strategy execution finished in 159.24s
INFO : Final results:
INFO : ServerApp-side Evaluate Metrics:
INFO : {}
Chaque client simulé (deux par tour) affichera également un résumé de leur processus d’entraînement local. Attendez-vous à ce que cet sortie soit similaire à :
# The left part indicates the process ID running the `ClientApp`
(ClientAppActor pid=38155) ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
(ClientAppActor pid=38155) ┃ Test metric ┃ DataLoader 0 ┃
(ClientAppActor pid=38155) ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
(ClientAppActor pid=38155) │ test_loss │ 0.045175597071647644 │
(ClientAppActor pid=38155) └───────────────────────────┴───────────────────────────┘
Vous pouvez également surcharger les paramètres définis dans la section [tool.flwr.app.config] de pyproject.toml comme suit:
# Override some arguments
$ flwr run . --run-config num-server-rounds=5
Astuce
Vérifiez la documentation de Run simulations pour en savoir plus sur la façon de configurer et d’exécuter les simulations Flower.
Note
Vérifiez la partie source code de ce tutoriel dans examples/quickstart-pytorch-lightning dans le dépôt GitHub Flower.