Class FederatedTrainerBase<TModel, TData, TMetadata, T>
- Namespace
- AiDotNet.FederatedLearning.Trainers
- Assembly
- AiDotNet.dll
Base class for federated learning trainers.
public abstract class FederatedTrainerBase<TModel, TData, TMetadata, T> : FederatedLearningComponentBase<T>, IFederatedTrainer<TModel, TData, TMetadata>
Type Parameters
TModelThe type of the global model.
TDataThe type of per-client training data.
TMetadataThe type of metadata returned by training.
TThe numeric type used for calculations.
- Inheritance
-
FederatedTrainerBase<TModel, TData, TMetadata, T>
- Implements
-
IFederatedTrainer<TModel, TData, TMetadata>
- Derived
- Inherited Members
Methods
GetAggregationStrategyOrThrow()
protected IAggregationStrategy<TModel> GetAggregationStrategyOrThrow()
Returns
- IAggregationStrategy<TModel>
GetGlobalModel()
Retrieves the current global model after federated training.
public TModel GetGlobalModel()
Returns
- TModel
The trained global model.
Remarks
The global model represents the collective knowledge learned from all participating clients.
For Beginners: This is the final product of the collaborative learning process - a model that benefits from all participants' data without ever accessing their raw data directly.
GetNumberOfClientsOrThrow()
protected int GetNumberOfClientsOrThrow()
Returns
Initialize(TModel, int)
Initializes the federated learning process with client configurations and the global model.
public void Initialize(TModel globalModel, int numberOfClients)
Parameters
globalModelTModelThe initial global model to be distributed to clients.
numberOfClientsintThe number of clients participating in federated learning.
Remarks
This method sets up the initial state for federated learning by:
- Initializing the global model that will be shared across all clients
- Registering client configurations (number of clients, data distribution, etc.)
- Setting up communication channels for model updates
For Beginners: Initialization is like setting up a study group before the first session. You need to know who's participating, what materials everyone has, and establish how you'll share information.
SetAggregationStrategy(IAggregationStrategy<TModel>)
Sets the aggregation strategy used to combine client updates.
public void SetAggregationStrategy(IAggregationStrategy<TModel> strategy)
Parameters
strategyIAggregationStrategy<TModel>The aggregation strategy to use.
Remarks
Different aggregation strategies handle various challenges in federated learning:
- FedAvg: Simple weighted averaging of model updates
- FedProx: Handles clients with different computational capabilities
- FedBN: Special handling for batch normalization layers
For Beginners: The aggregation strategy is the rule for combining everyone's contributions. Different rules work better for different situations, like how you might weight expert opinions more heavily in certain contexts.
SetGlobalModel(TModel)
protected void SetGlobalModel(TModel globalModel)
Parameters
globalModelTModel
Train(Dictionary<int, TData>, int, double, int)
Executes multiple rounds of federated learning until convergence or maximum rounds reached.
public abstract TMetadata Train(Dictionary<int, TData> clientData, int rounds, double clientSelectionFraction = 1, int localEpochs = 1)
Parameters
clientDataDictionary<int, TData>Dictionary mapping client IDs to their local training data.
roundsintMaximum number of federated learning rounds to execute.
clientSelectionFractiondoubleFraction of clients to select per round (0.0 to 1.0).
localEpochsintNumber of training epochs each client performs per round.
Returns
- TMetadata
Aggregated metadata across all training rounds.
Remarks
This method orchestrates the entire federated learning process by:
- Running multiple training rounds
- Monitoring convergence (when the model stops improving significantly)
- Tracking performance metrics across rounds
- Applying privacy mechanisms if configured
For Beginners: This is the complete federated learning process from start to finish. It's like running an entire semester of study group sessions, where you continue meeting until everyone has mastered the material or you've run out of time.
TrainRound(Dictionary<int, TData>, double, int)
Executes one round of federated learning where clients train locally and updates are aggregated.
public abstract TMetadata TrainRound(Dictionary<int, TData> clientData, double clientSelectionFraction = 1, int localEpochs = 1)
Parameters
clientDataDictionary<int, TData>Dictionary mapping client IDs to their local training data.
clientSelectionFractiondoubleFraction of clients to select for this round (0.0 to 1.0).
localEpochsintNumber of training epochs each client should perform locally.
Returns
- TMetadata
Metadata about the training round including accuracy, loss, and convergence metrics.
Remarks
A federated learning round consists of several steps:
- The global model is sent to selected clients
- Each client trains the model on their local data
- Clients send their model updates back to the server
- The server aggregates these updates using an aggregation strategy
- The global model is updated with the aggregated result
For Beginners: Think of this as one iteration in a collaborative learning cycle. Everyone gets the current version of the shared knowledge, studies independently, then contributes their improvements. These improvements are combined to create an even better version for the next round.
For example:
- Round 1: Clients receive initial model, train for 5 epochs, send updates
- Server aggregates updates and improves global model
- Round 2: Clients receive improved model, train again, send updates
- This continues until the model reaches desired accuracy