Path: blob/master/cloud/notebooks/python_sdk/experiments/federated_learning/Federated Learning Demo Part II - for Party.ipynb
6405 views
WML Federated Learning with MNIST for Party using ibm-watsonx-ai
.
Learning Goals
When you complete this notebook, you should know how to:
Load the data that you intend to use in the Federated Learning experiment.
Install IBM Federated Learning libraries.
Define a data handler. For more details on data handlers.
Configure the party to train data with the aggregator.
1. Set up the environment
Before you use the sample code in this notebook, you must perform the following setup tasks:
Create a watsonx.ai Runtime Service instance (a free plan is offered and information about how to create the instance can be found here).
Install and import the ibm-watsonx-ai
and dependecies
Note: ibm-watsonx-ai
documentation can be found here.
Connection to watsonx.ai Runtime
Authenticate the watsonx.ai Runtime service on IBM Cloud. You need to provide platform api_key
and instance location
.
You can use IBM Cloud CLI to retrieve platform API Key and instance location.
API Key can be generated in the following way:
In result, get the value of api_key
from the output.
Location of your watsonx.ai Runtime instance can be retrieved in the following way:
In result, get the value of location
from the output.
Tip: Your Cloud API key
can be generated by going to the Users section of the Cloud console. From that page, click your name, scroll down to the API Keys section, and click Create an IBM Cloud API key. Give your key a name and click Create, then copy the created key and paste it below. You can also get a service specific url by going to the Endpoint URLs section of the watsonx.ai Runtime docs. You can check your instance location in your watsonx.ai Runtime Service instance details.
You can also get service specific apikey by going to the Service IDs section of the Cloud Console. From that page, click Create, then copy the created key and paste it below.
Action: Enter your api_key
and location
in the following cell.
Action: Assign project ID below
Paste Variables From Admin Notebook
Paste in the ID's you got from the end of the Part I notebook. If you have not run through Part I, open the notebook and run through it first.
Download MNIST handwritten digits dataset
As the party, you must provide the dataset that you will use to train the Federated Learning model. In this tutorial, a dataset is provided by default, the MNIST handwritten digits dataset.
The party should run a data handler to ensure that their datasets are in compatible format and consistent. In this tutorial, an example data handler for the MNIST dataset is provided.
This data handler is written to the local working directory of this notebook
Verify Data Handler Exists
total 153480
-rw-r--r--@ 1 [email protected] staff 19K Nov 19 09:58 Federated Learning Demo Part 1 - for Admin.ipynb
-rw-r--r--@ 1 [email protected] staff 32K Nov 18 13:07 Federated Learning Demo Part 2 - for Party.ipynb
-rw-r--r-- 1 [email protected] staff 11M Nov 19 10:00 MNIST-pkl.zip
drwxr-xr-x 6 [email protected] staff 192B Nov 18 13:05 __MACOSX
-rw-r--r-- 1 [email protected] staff 12K Nov 19 10:00 mnist-keras-test-payload.json
-rw-r--r-- 1 [email protected] staff 7.5M Nov 19 10:00 mnist-keras-test.pkl
-rw-r--r-- 1 [email protected] staff 37M Nov 19 10:00 mnist-keras-train.pkl
-rw-r--r-- 1 [email protected] staff 7.5M Nov 19 10:00 mnist-keras-valid.pkl
-rw-r--r-- 1 [email protected] staff 2.2K Nov 19 10:00 mnist_keras_data_handler.py
-rw-r--r-- 1 [email protected] staff 9.7M Nov 19 09:58 tf_mnist_model.zip
Here you can finally connect to the aggregator to begin training.
Each party must run their party configuration file to call out to the aggregator. Here is an example of a party configuration.
Because you had already defined the training ID, RTS ID and data handler in the previous sections of this notebook, and the local training and protocol handler are all defined by the SDK, you will only need to define the information for the dataset file under ["data"]["info"]
.
In this tutorial, the data path is already defined as we have loaded the examplar MNIST dataset from previous sections.
Establish Connection To Aggregator and Start Training
If you want to clean up all created assets:
experiments
trainings
pipelines
model definitions
models
functions
deployments
please follow up this sample notebook.
You successfully completed this notebook! You have learned to:
Start a Federated Learning experiment
Load a template model
Create an RTS and launch the experiment job
Load a dataset for training
Define the data handler
Configure the party
Connect to the aggregator
Train your Federated Learning model
Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
Author
Rinay Shah, Software Developer at IBM.
Copyright © 2020-2025 IBM. This notebook and its source code are released under the terms of the MIT License.