nvflare.app_opt.sklearn.recipes.svm module¶
- class SVMFedAvgRecipe(*, name: str = 'svm_fedavg', min_clients: int, kernel: str = 'rbf', train_script: str, train_args: str | Dict[str, str] = '', backend: str = 'sklearn', launch_external_process: bool = False, command: str = 'python3 -u')[source]¶
Bases:
RecipeA recipe for Federated SVM with Scikit-learn.
This recipe implements federated SVM training using support vector aggregation. Unlike iterative algorithms, SVM training only requires one round: - Round 0: Each client trains a local SVM and sends their support vectors - Server aggregates all support vectors and trains a global SVM - Round 1: Clients validate using the global support vectors
The recipe configures: - A federated job with kernel parameter - Scatter-and-gather controller (2 rounds) - Custom SVMAssembler for support vector aggregation - CollectAndAssembleAggregator for combining client updates - Script runners for client-side training execution
Training Process: - Round 0 (Training): Each client trains a local SVM on their data and extracts
support vectors. The server collects all support vectors, trains a global SVM, and extracts the global support vectors.
Round 1 (Validation): Each client validates using the global support vectors.
- Parameters:
name – Name of the federated learning job. Defaults to “svm_fedavg”.
min_clients – Minimum number of clients required to start a training round.
kernel – Kernel type for SVM. Options: ‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’. Defaults to ‘rbf’.
train_script – Path to the training script that will be executed on each client.
train_args – Command line arguments to pass to the training script. Can be: - str: Same arguments for all clients (uses job.to_clients) - dict[str, str]: Per-client arguments mapping site names to args (uses job.to per site)
backend – Backend library to use (‘sklearn’ or ‘cuml’). Defaults to ‘sklearn’.
launch_external_process – Whether to launch the script in external process. Defaults to False.
command – If launch_external_process=True, command to run script (prepended to script). Defaults to “python3 -u”.
Example
```python recipe = SVMFedAvgRecipe(
name=”svm_cancer”, min_clients=3, kernel=”rbf”, train_script=”client.py”, train_args=”–data_path /tmp/data/cancer.csv –train_start 0 –train_end 100”,
)
from nvflare.recipe import SimEnv env = SimEnv(num_clients=3) run = recipe.execute(env) print(“Result:”, run.get_result()) ```
Note
This recipe uses a custom SVMAssembler that implements support vector aggregation. The training only requires one round since SVM is not an iterative algorithm in the federated setting. A second round is included for validation purposes.
This is base class of a recipe. Recipes are implemented by jobs. A concrete recipe must provide the job for recipe implementation.
- Parameters:
job – the job that implements the recipe.