Note
Go to the end to download the full example code.
Federated Node Classification Example
Federated Node Classification with FedGCN on the Cora dataset.
(Time estimate: 3 minutes)
Load libraries
import attridict
from fedgraph.federated_methods import run_fedgraph
Specify the Node Classification configuration
config = {
# Task, Method, and Dataset Settings
"fedgraph_task": "NC",
"dataset": "cora",
"method": "FedGCN", # Federated learning method, e.g., "FedGCN"
"iid_beta": 10000, # Dirichlet distribution parameter for label distribution among clients
"distribution_type": "average", # Distribution type among clients
# Training Configuration
"global_rounds": 100,
"local_step": 3,
"learning_rate": 0.5,
"n_trainer": 2,
"batch_size": -1, # -1 indicates full batch training
# Model Structure
"num_layers": 2,
"num_hops": 1, # Number of n-hop neighbors for client communication
# Resource and Hardware Settings
"gpu": False,
"num_cpus_per_trainer": 1,
"num_gpus_per_trainer": 0,
# Logging and Output Configuration
"logdir": "./runs",
# Security and Privacy
"use_encryption": False, # Whether to use Homomorphic Encryption for secure aggregation
# Dataset Handling Options
"use_huggingface": False, # Load dataset directly from Hugging Face Hub
"saveto_huggingface": False, # Save partitioned dataset to Hugging Face Hub
# Scalability and Cluster Configuration
"use_cluster": False, # Use Kubernetes for scalability if True
}
Run fedgraph method
config = attridict(config)
run_fedgraph(config)
config: {'fedgraph_task': 'NC', 'dataset': 'cora', 'method': 'FedGCN', 'iid_beta': 10000, 'distribution_type': 'average', 'global_rounds': 100, 'local_step': 3, 'learning_rate': 0.5, 'n_trainer': 2, 'batch_size': -1, 'num_layers': 2, 'num_hops': 1, 'gpu': False, 'num_cpus_per_trainer': 1, 'num_gpus_per_trainer': 0, 'logdir': './runs', 'use_encryption': False, 'use_huggingface': False, 'saveto_huggingface': False, 'use_cluster': False}
Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.x to ./data/cora/raw/ind.cora.x...
Downloaded ./data/cora/raw/ind.cora.x
Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.tx to ./data/cora/raw/ind.cora.tx...
Downloaded ./data/cora/raw/ind.cora.tx
Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.allx to ./data/cora/raw/ind.cora.allx...
Downloaded ./data/cora/raw/ind.cora.allx
Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.y to ./data/cora/raw/ind.cora.y...
Downloaded ./data/cora/raw/ind.cora.y
Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.ty to ./data/cora/raw/ind.cora.ty...
Downloaded ./data/cora/raw/ind.cora.ty
Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.ally to ./data/cora/raw/ind.cora.ally...
Downloaded ./data/cora/raw/ind.cora.ally
Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.graph to ./data/cora/raw/ind.cora.graph...
Downloaded ./data/cora/raw/ind.cora.graph
Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.test.index to ./data/cora/raw/ind.cora.test.index...
Downloaded ./data/cora/raw/ind.cora.test.index
server aggregates all local neighbor feature sums
clients received feature aggregation from server
global_rounds 100
average_final_test_loss, 0.9797245250940323
Average test accuracy, 0.787
Total running time of the script: (0 minutes 26.925 seconds)