### Install PFLlib using Git Source: https://github.com/tsingz0/pfllib/blob/master/docs/quickstart.html This command clones the PFLlib repository from GitHub. Ensure you have Git installed on your system. This is the first step in the installation process. ```bash git clone https://github.com/TsingZ0/PFLlib.git ``` -------------------------------- ### Untitled No description -------------------------------- ### Python Client Implementation Example Source: https://github.com/tsingz0/pfllib/blob/master/docs/extend.html This Python snippet demonstrates the basic structure for implementing a custom client in PFLlib. It shows how to inherit from the base Client class and defines the __init__ and train methods. Specific initialization and training logic should be added within these methods. ```python from flcore.clients.clientbase import Client class clientNAME(Client): def __init__(self, args, id, train_samples, test_samples, **kwargs): super().__init__(args, id, train_samples, test_samples, **kwargs) # add specific initialization def train(self): # client training code of your algorithm ``` -------------------------------- ### Untitled No description -------------------------------- ### Implementing a New Client Algorithm in PFLlib Source: https://github.com/tsingz0/pfllib/blob/master/README.md This Python snippet illustrates how to create a new client-side algorithm for PFLlib. It requires extending the `Client` base class from `./system/flcore/clients/clientbase.py`. The example shows custom initialization and the `train` method for client-specific training logic. ```python # clientNAME.py import necessary pkgs from flcore.clients.clientbase import Client class clientNAME(Client): def __init__(self, args, id, train_samples, test_samples, **kwargs): super().__init__(args, id, train_samples, test_samples, **kwargs) # add specific initialization def train(self): # client training code of your algorithm ``` -------------------------------- ### Untitled No description -------------------------------- ### Generate New Dataset Script Example - Python Source: https://github.com/tsingz0/pfllib/blob/master/docs/extend.html This Python code snippet illustrates the structure for a new dataset generation script. It includes importing necessary packages and utility functions, defining a generation function, and calling it. The process involves downloading, pre-processing, separating data, splitting into train/test sets, and saving the results. ```python # `generate_DATA.py` import necessary pkgs from utils import necessary processing funcs def generate_dataset(...): # download dataset as usual # pre-process dataset as usual X, y, statistic = separate_data((dataset_content, dataset_label), ...) train_data, test_data = split_data(X, y) save_file(config_path, train_path, test_data, train_data, test_data, statistic, ...) # call the generate_dataset func ``` -------------------------------- ### Create Conda Environment for PFLlib Source: https://github.com/tsingz0/pfllib/blob/master/docs/quickstart.html This command creates a conda environment using the provided YAML file, which lists the Python dependencies required for PFLlib. You may need to adjust the PyTorch version based on your CUDA installation. ```bash conda env create -f env_cuda_latest.yaml ``` -------------------------------- ### Implement Custom Client Algorithm in Python Source: https://context7.com/tsingz0/pfllib/llms.txt Provides a Python implementation for a custom client algorithm by extending the base `Client` class. This example demonstrates how to integrate algorithm-specific training logic, including custom regularization terms like proximal loss, within the federated learning client. ```python # system/flcore/clients/clientcustom.py import torch import numpy as np import time from flcore.clients.clientbase import Client class clientCustom(Client): def __init__(self, args, id, train_samples, test_samples, **kwargs): super().__init__(args, id, train_samples, test_samples, **kwargs) # Add algorithm-specific initialization self.mu = args.mu # Regularization parameter self.global_model = None # Reference to global model def train(self): trainloader = self.load_train_data() self.model.train() start_time = time.time() max_local_epochs = self.local_epochs if self.train_slow: max_local_epochs = np.random.randint(1, max_local_epochs // 2) for epoch in range(max_local_epochs): for i, (x, y) in enumerate(trainloader): if type(x) == type([]): x[0] = x[0].to(self.device) else: x = x.to(self.device) y = y.to(self.device) # Standard training step output = self.model(x) loss = self.loss(output, y) # Add custom regularization (e.g., proximal term) if self.global_model is not None: proximal_term = 0 for w, w_t in zip(self.model.parameters(), self.global_model.parameters()): proximal_term += (w - w_t).norm(2) loss += (self.mu / 2) * proximal_term self.optimizer.zero_grad() loss.backward() self.optimizer.step() if self.learning_rate_decay: self.learning_rate_scheduler.step() self.train_time_cost['num_rounds'] += 1 self.train_time_cost['total_cost'] += time.time() - start_time def set_global_model(self, model): """Store reference to global model for regularization""" self.global_model = model ``` -------------------------------- ### Execute FedAvg Algorithm on MNIST (Bash) Source: https://context7.com/tsingz0/pfllib/llms.txt Demonstrates how to run the Federated Averaging (FedAvg) algorithm on the MNIST dataset using PFLlib. It includes examples for basic execution, custom hyperparameter tuning, and multi-GPU utilization. ```bash cd ./system # Basic FedAvg on single GPU python main.py -data MNIST -m CNN -algo FedAvg -gr 2000 -did 0 # FedAvg with custom hyperparameters python main.py -data MNIST -m CNN -algo FedAvg \ -gr 2000 \ -did 0 \ -nc 20 \ -jr 1.0 \ -ls 1 \ -lbs 10 \ -lr 0.005 # Multi-GPU execution python main.py -data MNIST -m CNN -algo FedAvg -gr 2000 -did 0,1,2,3 ``` -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Running an FL Algorithm with PFLlib Source: https://github.com/tsingz0/pfllib/blob/master/README.md This snippet demonstrates the basic workflow for running a federated learning algorithm within the PFLlib framework. It involves generating data scenarios and then executing the main training script with client and server configurations. For new algorithms, modifications are primarily needed in the client and server scripts. ```bash python generate_DATA.py # Generate data scenarios python main.py python clientNAME.py python serverNAME.py ``` -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Run Personalized FL Algorithms (FedPer, FedProx, GPFL) Source: https://context7.com/tsingz0/pfllib/llms.txt Demonstrates how to run different personalized federated learning algorithms. This includes FedPer with personalized head layers, FedProx with regularization, and GPFL for generic and personalized feature learning. Configurations for datasets, models, and algorithm-specific parameters are provided. ```bash cd ./system # FedPer with personalized head layers python main.py -data Cifar10 -m ResNet18 -algo FedPer \ -gr 500 \ -did 0 \ -lr 0.01 \ -lbs 50 \ -nc 100 # FedProx with regularization python main.py -data MNIST -m CNN -algo FedProx \ -gr 1000 \ -did 0 \ -mu 0.01 # GPFL (Generic and Personalized Feature Learning) python main.py -data Cifar100 -m CNN -algo GPFL \ -gr 1000 \ -did 0 \ -bt 1.0 \ -lam 0.5 ``` -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Basic CSS for PFLlib Website Layout Source: https://github.com/tsingz0/pfllib/blob/master/docs/about.html This CSS code defines the fundamental styling for the PFLlib website, including layout, typography, navigation bar behavior (fixed and scrollable), container styling, and responsive adjustments for smaller screens using media queries. It ensures a consistent and accessible user experience across devices. ```css body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; margin: 0; padding: 0; line-height: 1.6; background-color: #f4f4f4; color: #333333; display: flex; flex-direction: column; min-height: 100vh; } .navbar { display: flex; justify-content: center; align-items: center; background-color: rgba(0, 0, 0, 0.7); color: white; padding: 1rem 0rem; position: fixed; width: 100%; z-index: 1000; transition: background-color 0.3s ease; height: 2rem; } .navbar.scrolled { background-color: rgba(0, 0, 0, 0.7); } .navbar-container { display: flex; justify-content: space-between; align-items: center; width: 100%; max-width: 1200px; } .navbar h1 { margin: 0; color: white; } .navbar nav { display: flex; gap: 1rem; } .navbar a { color: white; text-decoration: none; transition: color 0.3s ease; padding: 0rem 1rem; } .navbar a:hover { color: #6DA945; } .container { max-width: 1200px; margin: 8rem auto 2rem; /* Adjusted margin for container */ padding: 0 2rem; flex-grow: 1; /* Ensures container takes up remaining space */ } h1, h2, h3 { color: #333333; } section { margin-bottom: 2rem; } footer { background-color: #333; color: white; text-align: center; padding: 1rem 0; position: relative; width: 100%; } html { scroll-padding-top: 4.5rem; /* Adjust to the height of your navbar */ } a { text-decoration: none; color: #6DA945; } .hamburger { display: none; background: none; border: none; cursor: pointer; padding: 0.5rem; position: absolute; right: 1rem; } .hamburger span { display: block; width: 20px; height: 3px; background: white; margin: 5px 0; transition: 0.3s; } @media (max-width: 768px) { .container { max-width: 100%; flex-direction: column; margin-top: 6rem; } .sidebar, .content { width: 100%; } .navbar-container { flex-direction: row; flex-wrap: wrap; } .hamburger { display: block; } .navbar nav { display: none; flex-direction: column; width: 100%; background: #333333; padding: 1rem; margin-top: 2rem; } .navbar nav.active { display: flex; } } ``` -------------------------------- ### Untitled No description -------------------------------- ### Python Integration of Custom Server in Main Script Source: https://context7.com/tsingz0/pfllib/llms.txt Demonstrates how to integrate the CustomServer into the main federated learning script. This involves importing the server, adding a new algorithm option to the argument parser for momentum, and updating the algorithm selection logic. ```python # system/main.py (add to existing imports and algorithm selection) from flcore.servers.servercustom import CustomServer # Add in argument parser section parser.add_argument('-mom', "--momentum", type=float, default=0.9, help="Momentum for server aggregation") # Add in algorithm selection section (around line 365) elif args.algorithm == "Custom": args.head = copy.deepcopy(args.model.fc) args.model.fc = nn.Identity() args.model = BaseHeadSplit(args.model, args.head) server = CustomServer(args, i) ``` -------------------------------- ### Generating a New Dataset in PFLlib Source: https://github.com/tsingz0/pfllib/blob/master/README.md This snippet shows how to add a new dataset to PFLlib. It involves creating a `generate_DATA.py` file in the `./dataset` directory and implementing download and preprocessing logic using provided utility functions. The function `generate_dataset` handles data separation, splitting, and saving. ```python # `generate_DATA.py` import necessary pkgs from utils import necessary processing funcs def generate_dataset(...): # download dataset as usual # pre-process dataset as usual X, y, statistic = separate_data((dataset_content, dataset_label), ...) train_data, test_data = split_data(X, y) save_file(config_path, train_path, test_path, train_data, test_data, statistic, ...) # call the generate_dataset func ``` -------------------------------- ### Untitled No description -------------------------------- ### Run PFLlib Simulation (FedAvg) Source: https://github.com/tsingz0/pfllib/blob/master/README.md Executes a PFLlib simulation using the FedAvg algorithm. This command specifies the dataset (MNIST), model (CNN), algorithm (FedAvg), number of communication rounds (2000), and GPU device ID (0). Multiple GPUs can be utilized by providing a comma-separated list of device IDs. ```bash cd ./system python main.py -data MNIST -m CNN -algo FedAvg -gr 2000 -did 0 # using the MNIST dataset, the FedAvg algorithm, and the 4-layer CNN model python main.py -data MNIST -m CNN -algo FedAvg -gr 2000 -did 0,1,2,3 # running on multiple GPUs ``` -------------------------------- ### Untitled No description -------------------------------- ### Extend Server Base Class - Python Source: https://github.com/tsingz0/pfllib/blob/master/docs/extend.html This Python code demonstrates how to add a new federated learning algorithm by extending the base `Server` class. It shows the basic structure for initialization, including setting slow clients and selecting clients, and a placeholder for the training logic. ```python # serverNAME.py import necessary pkgs from flcore.clients.clientNAME import clientNAME from flcore.servers.serverbase import Server class NAME(Server): def __init__(self, args, times): super().__init__(args, times) # select slow clients self.set_slow_clients() self.set_clients(clientAVG) def train(self): # server scheduling code of your algorithm ``` -------------------------------- ### Bash Command for Training with New Unseen Clients Source: https://context7.com/tsingz0/pfllib/llms.txt This bash command trains a federated learning model using FedAvg on MNIST with a CNN, involving 20 clients for training and evaluating on 5 new, unseen clients. It specifies the number of clients and new clients, and the frequency of training on new clients. ```bash # Train on 20 clients, then evaluate on 5 new clients python main.py -data MNIST -m CNN -algo FedAvg \ -gr 1000 \ -did 0 \ -nc 20 \ -nnc 5 \ -ften 10 ``` -------------------------------- ### Untitled No description -------------------------------- ### Run FedAvg with Client Dropout and Slow Trainers Source: https://context7.com/tsingz0/pfllib/llms.txt Executes the FedAvg algorithm with specified parameters for client dropout rate and slow trainer simulation. This command initiates a federated learning process on the MNIST dataset using a CNN model. ```bash python main.py -data MNIST -m CNN -algo FedAvg -gr 2000 -did 0 \ -cdr 0.1 \ -tsr 0.2 \ -ssr 0.1 ``` -------------------------------- ### Untitled No description -------------------------------- ### Create Conda Environment Source: https://github.com/tsingz0/pfllib/blob/master/README.md Creates a conda environment from a YAML file. It's recommended to downgrade PyTorch via pip if necessary to match the CUDA version. This is crucial for ensuring compatibility between libraries and the GPU. ```bash conda env create -f env_cuda_latest.yaml # Downgrade torch via pip if needed to match the CUDA version ``` -------------------------------- ### Run PFLlib FL Algorithm with MNIST Dataset Source: https://github.com/tsingz0/pfllib/blob/master/docs/quickstart.html This command executes a Federated Learning algorithm (FedAvg) using the MNIST dataset and a CNN model. It specifies the number of communication rounds and the device ID. Multiple GPUs can be utilized by providing a comma-separated list of device IDs. ```bash # In ./system python main.py -data MNIST -m CNN -algo FedAvg -gr 2000 -did 0 python main.py -data MNIST -m CNN -algo FedAvg -gr 2000 -did 0,1,2,3 ``` -------------------------------- ### Untitled No description -------------------------------- ### Bash Command for Personalized FL with New Client Evaluation Source: https://context7.com/tsingz0/pfllib/llms.txt This command executes the FedBABU algorithm on Cifar100 with a CNN model, focusing on personalized federated learning. It trains on 50 clients and evaluates on 10 new clients, with specific settings for training frequency on new clients. ```bash # Personalized FL with new client evaluation python main.py -data Cifar100 -m CNN -algo FedBABU \ -gr 500 \ -did 0 \ -nc 50 \ -nnc 10 \ -fte 20 ```