5. Running FedLearn and Code Example

This section provides step-by-step instructions and code examples for running FedLearn in both local and collaborative modes.

5.1 Running FedLearn in Local Mode

Local mode is useful for testing and development purposes. It simulates a federated learning environment on a single machine.

Step 1: Prepare Your Data

Ensure your dataset is prepared and located in the directory specified in your config.json file.

Step 2: Configure FedLearn

Update your config.json file for local mode:

json
{
  "server_address": "localhost:8080",
  "num_rounds": 10,
  "strategy": "FedAvg",
  "grpc_max_message_length": 104857600,
  "mode": "local",
  "num_clients": 3,
  "local_epochs": 5,
  "batch_size": 32,
  "learning_rate": 0.01,
  "model_name": "simple_cnn",
  "dataset": "mnist",
  "data_dir": "./data",
  "output_dir": "./output"
}

Step 3: Run FedLearn

Create a Python script run_fedlearn_local.py:

Run the script:

5.2 Running FedLearn in Collaborative Mode

Collaborative mode is used for actual distributed federated learning across multiple devices or organizations.

Step 1: Server Setup

On the server machine, create a script run_server.py:

Run the server:

Step 2: Client Setup

On each client machine, create a script run_client.py:

Run the client on each participating machine:

5.3 Example: Image Classification with MNIST

Here's a complete example using the MNIST dataset for image classification, adapted for lung cancer detection, and to be used when running FedLearn in Collaborative Mode (5.2):

This example demonstrates how to use Cifer's FedLearn for a typical image classification task using the MNIST dataset. It includes data loading, model definition, federated learning execution, and final model evaluation and saving.

Last updated