Table of Contents

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

TModel

The type of the global model.

TData

The type of per-client training data.

TMetadata

The type of metadata returned by training.

T

The 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

int

Initialize(TModel, int)

Initializes the federated learning process with client configurations and the global model.

public void Initialize(TModel globalModel, int numberOfClients)

Parameters

globalModel TModel

The initial global model to be distributed to clients.

numberOfClients int

The 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

strategy IAggregationStrategy<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

globalModel TModel

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

clientData Dictionary<int, TData>

Dictionary mapping client IDs to their local training data.

rounds int

Maximum number of federated learning rounds to execute.

clientSelectionFraction double

Fraction of clients to select per round (0.0 to 1.0).

localEpochs int

Number 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

clientData Dictionary<int, TData>

Dictionary mapping client IDs to their local training data.

clientSelectionFraction double

Fraction of clients to select for this round (0.0 to 1.0).

localEpochs int

Number 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:

  1. The global model is sent to selected clients
  2. Each client trains the model on their local data
  3. Clients send their model updates back to the server
  4. The server aggregates these updates using an aggregation strategy
  5. 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