This notebook is meant to be run in google colab. You can find import your local copy of the file in the the [colab welcome page](https://colab.research.google.com/).
This notebook is meant to be run in google colab. You can find import your local copy of the file in the the [colab welcome page](https://colab.research.google.com/).
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
# Setting up your declearn
# Setting up your declearn
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
We first clone the repo, to have both the package itself and the `examples` folder we will use in this tutorial, then naviguate to the package directory, and finally install the required dependencies
We first clone the repo, to have both the package itself and the `examples` folder we will use in this tutorial, then naviguate to the package directory, and finally install the required dependencies
# Install the package, with TensorFlow and Websockets extra dependencies.
# Install the package, with TensorFlow and Websockets extra dependencies.
# You may want to work in a dedicated virtual environment.
# You may want to work in a dedicated virtual environment.
!pipinstall.[tensorflow,websockets]
!pipinstall.[tensorflow,websockets]
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
# Running your first experiment
# Running your first experiment
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
We are going to train a common model between three simulated clients on the classic [MNIST dataset](http://yann.lecun.com/exdb/mnist/). The input of the model is a set of images of handwritten digits, and the model needs to determine which number between 0 and 9 each image corresponds to.
We are going to train a common model between three simulated clients on the classic [MNIST dataset](http://yann.lecun.com/exdb/mnist/). The input of the model is a set of images of handwritten digits, and the model needs to determine which number between 0 and 9 each image corresponds to.
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## The model
## The model
To do this, we will use a simple CNN, defined in `examples/mnist_quickrun/model.py`
To do this, we will use a simple CNN, defined in `examples/mnist_quickrun/model.py`
We start by splitting the MNIST dataset between 3 clients and storing the output in the `examples/mnist_quickrun` folder. For this we use an experimental utility provided by `declearn`.
We start by splitting the MNIST dataset between 3 clients and storing the output in the `examples/mnist_quickrun` folder. For this we use an experimental utility provided by `declearn`.
Splitting scheme(s) to use. In all cases, shards contain mutually-
Splitting scheme(s) to use. In all cases, shards contain mutually-
exclusive samples and cover the full raw training data.
exclusive samples and cover the full raw training data.
- If "iid", split the dataset through iid random sampling.
- If "iid", split the dataset through iid random sampling.
- If "labels", split into shards that hold all samples associated
- If "labels", split into shards that hold all samples associated
with mutually-exclusive target classes.
with mutually-exclusive target classes.
- If "biased", split the dataset through random sampling according
- If "biased", split the dataset through random sampling according
to a shard-specific random labels distribution.
to a shard-specific random labels distribution.
perc_train: float, default= 0.8
perc_train: float, default= 0.8
Train/validation split in each client dataset, must be in the
Train/validation split in each client dataset, must be in the
]0,1] range.
]0,1] range.
seed: int or None, default=None
seed: int or None, default=None
Optional seed to the RNG used for all sampling operations.
Optional seed to the RNG used for all sampling operations.
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Quickrun
## Quickrun
We can now run our experiment. As explained in the section 2.1 of the [quickstart documentation](https://magnet.gitlabpages.inria.fr/declearn/docs/latest/quickstart), using the `declearn-quickrun` entry-point requires a configuration file, some data, and a model:
We can now run our experiment. As explained in the section 2.1 of the [quickstart documentation](https://magnet.gitlabpages.inria.fr/declearn/docs/latest/quickstart), using the `declearn-quickrun` entry-point requires a configuration file, some data, and a model:
* A TOML file, to store your experiment configurations. Here:
* A TOML file, to store your experiment configurations. Here:
`examples/mnist_quickrun/config.toml`.
`examples/mnist_quickrun/config.toml`.
* A folder with your data, split by client. Here: `examples/mnist_quickrun/data_iid`
* A folder with your data, split by client. Here: `examples/mnist_quickrun/data_iid`
* A model python file, to declare your model wrapped in a `declearn` object. Here: `examples/mnist_quickrun/model.py`.
* A model python file, to declare your model wrapped in a `declearn` object. Here: `examples/mnist_quickrun/model.py`.
We then only have to run the `quickrun` util with the path to the TOML file:
We then only have to run the `quickrun`coroutine with the path to the TOML file:
The python code above is equivalent to running `declearn-quickrun examples/mnist_quickrun/config.toml` in a shell command-line.
The python code above is equivalent to running `declearn-quickrun examples/mnist_quickrun/config.toml` in a shell command-line.
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
The output obtained is the combination of the CLI output of our server and our clients, going through:
The output obtained is the combination of the CLI output of our server and our clients, going through:
*`INFO:Server:Starting clients registration process.` : a first registration step, where clients register with the server
*`INFO:Server:Starting clients registration process.` : a first registration step, where clients register with the server
*`INFO:Server:Sending initialization requests to clients.`: the initilization of the object needed for training on both the server and clients side.
*`INFO:Server:Sending initialization requests to clients.`: the initilization of the object needed for training on both the server and clients side.
*`Server:INFO: Initiating training round 1`: the training starts, where each client makes its local update(s) and send the result to the server which aggregates them
*`Server:INFO: Initiating training round 1`: the training starts, where each client makes its local update(s) and send the result to the server which aggregates them
*`INFO: Initiating evaluation round 1`: the model is evaluated at each round
*`INFO: Initiating evaluation round 1`: the model is evaluated at each round
*`Server:INFO: Stopping training`: the training is finalized
*`Server:INFO: Stopping training`: the training is finalized
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Results
## Results
You can have a look at the results in the `examples/mnist_quickrun/result_*` folder, including the metrics evolution during training.
You can have a look at the results in the `examples/mnist_quickrun/result_*` folder, including the metrics evolution during training.
Now try modifying the `examples/mnist_quickrun/config.toml` file like this, to implement the [scaffold algorithm](https://arxiv.org/abs/1910.06378) and running the experiment again.
Now try modifying the `examples/mnist_quickrun/config.toml` file like this, to implement the [scaffold algorithm](https://arxiv.org/abs/1910.06378) and running the experiment again.