|
| 1 | +<!-- |
| 2 | + Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + or more contributor license agreements. See the NOTICE file |
| 4 | + distributed with < this work for additional information |
| 5 | + regarding copyright ownership. The ASF licenses this file |
| 6 | + to you under the Apache License, Version 2.0 (the |
| 7 | + "License"); you may not use this file except in compliance |
| 8 | + with the License. You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, |
| 13 | + software distributed under the License is distributed on an |
| 14 | + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + KIND, either express or implied. See the License for the |
| 16 | + specific language governing permissions and limitations |
| 17 | + under the License. |
| 18 | +--> |
| 19 | + |
| 20 | + |
| 21 | +# Horizontal Federated Learning Example |
| 22 | + |
| 23 | +This is an example of federated learning (FL) using the Singa framework. In FL, there is a server and a set of clients. Each client has a local dataset. |
| 24 | +In each iteration, each client trains the model using its local dataset and uploads the model gradient to the server, which aggregates to get the global |
| 25 | +gradient using the Federated Average algorithm. The server sends the global gradient to all clients for iterative model training. |
| 26 | +This example uses the Bank dataset and an MLP model in FL. |
| 27 | + |
| 28 | +## Preparation |
| 29 | + |
| 30 | +Go to the Conda environment that contains the Singa library, and run |
| 31 | + |
| 32 | +```bash |
| 33 | +pip install -r requirements.txt |
| 34 | +``` |
| 35 | + |
| 36 | +Download the bank dataset and split it into 3 partitions. |
| 37 | + |
| 38 | +```bash |
| 39 | +# 1. download the data from https://archive.ics.uci.edu/ml/datasets/bank+marketing |
| 40 | +# 2. put it under the /data folder |
| 41 | +# 3. run the following command which: |
| 42 | +# (1) splits the dataset into N subsets |
| 43 | +# (2) splits each subsets into train set and test set (8:2) |
| 44 | +python -m bank N |
| 45 | +``` |
| 46 | + |
| 47 | +## Run the example |
| 48 | + |
| 49 | +Run the server first (set the number of epochs to 3) |
| 50 | + |
| 51 | +```bash |
| 52 | +python -m src.server -m 3 --num_clients 3 |
| 53 | +``` |
| 54 | + |
| 55 | +Then, start 3 clients in different terminal |
| 56 | + |
| 57 | +```bash |
| 58 | +python -m src.client --model mlp --data bank -m 3 -i 0 -d non-iid |
| 59 | +python -m src.client --model mlp --data bank -m 3 -i 1 -d non-iid |
| 60 | +python -m src.client --model mlp --data bank -m 3 -i 2 -d non-iid |
| 61 | +``` |
| 62 | + |
| 63 | +Finally, the server and clients finish the FL training. |
0 commit comments