Strategy

class Strategy[source]

Bases: ABC

服务器策略实现的抽象基类。

Methods

aggregate_evaluate(server_round, replies)

Aggregate evaluation metrics from client nodes.

aggregate_train(server_round, replies)

Aggregate training results from client nodes.

configure_evaluate(server_round, arrays, ...)

配置下一轮评估。

configure_train(server_round, arrays, ...)

配置下一轮训练。

start(grid, initial_arrays[, num_rounds, ...])

Execute the federated learning strategy.

summary()

Log summary configuration of the strategy.

abstract aggregate_evaluate(server_round: int, replies: Iterable[Message]) MetricRecord | None[source]

Aggregate evaluation metrics from client nodes.

参数:
  • server_round (int) -- 本轮联邦学习。

  • replies (Iterable[Message]) -- Iterable of reply messages received from client nodes after evaluation. MetricRecords in the messages are aggregated.

返回:

Aggregated evaluation metrics from all participating clients, or None if aggregation failed.

返回类型:

Optional[MetricRecord]

abstract aggregate_train(server_round: int, replies: Iterable[Message]) tuple[ArrayRecord | None, MetricRecord | None][source]

Aggregate training results from client nodes.

参数:
  • server_round (int) -- The current round of federated learning, starting from 1.

  • replies (Iterable[Message]) -- Iterable of reply messages received from client nodes after training. Each message contains ArrayRecords and MetricRecords that get aggregated.

返回:

A tuple containing: - ArrayRecord: Aggregated ArrayRecord, or None if aggregation failed - MetricRecord: Aggregated MetricRecord, or None if aggregation failed

返回类型:

tuple[Optional[ArrayRecord], Optional[MetricRecord]]

abstract configure_evaluate(server_round: int, arrays: ArrayRecord, config: ConfigRecord, grid: Grid) Iterable[Message][source]

配置下一轮评估。

参数:
  • server_round (int) -- 本轮联邦学习。

  • arrays (ArrayRecord) -- Current global ArrayRecord (e.g. global model) to be sent to client nodes for evaluation.

  • config (ConfigRecord) -- Configuration to be sent to clients nodes for evaluation.

  • grid (Grid) -- The Grid instance used for node sampling and communication.

返回:

An iterable of messages to be sent to selected client nodes for evaluation.

返回类型:

Iterable[Message]

abstract configure_train(server_round: int, arrays: ArrayRecord, config: ConfigRecord, grid: Grid) Iterable[Message][source]

配置下一轮训练。

参数:
  • server_round (int) -- 本轮联邦学习。

  • arrays (ArrayRecord) -- Current global ArrayRecord (e.g. global model) to be sent to client nodes for training.

  • config (ConfigRecord) -- Configuration to be sent to clients nodes for training.

  • grid (Grid) -- The Grid instance used for node sampling and communication.

返回:

An iterable of messages to be sent to selected client nodes for training.

返回类型:

Iterable[Message]

start(grid: Grid, initial_arrays: ArrayRecord, num_rounds: int = 3, timeout: float = 3600, train_config: ConfigRecord | None = None, evaluate_config: ConfigRecord | None = None, evaluate_fn: Callable[[int, ArrayRecord], MetricRecord] | None = None) Result[source]

Execute the federated learning strategy.

Runs the complete federated learning workflow for the specified number of rounds, including training, evaluation, and optional centralized evaluation.

参数:
  • grid (Grid) -- The Grid instance used to send/receive Messages from nodes executing a ClientApp.

  • initial_arrays (ArrayRecord) -- Initial model parameters (arrays) to be used for federated learning.

  • num_rounds (int (default: 3)) -- Number of federated learning rounds to execute.

  • timeout (float (default: 3600)) -- Timeout in seconds for waiting for node responses.

  • train_config (ConfigRecord, optional) -- Configuration to be sent to nodes during training rounds. If unset, an empty ConfigRecord will be used.

  • evaluate_config (ConfigRecord, optional) -- Configuration to be sent to nodes during evaluation rounds. If unset, an empty ConfigRecord will be used.

  • evaluate_fn (Callable[[int, ArrayRecord], MetricRecord], optional) -- Optional function for centralized evaluation of the global model. Takes server round number and array record, returns a MetricRecord. If provided, will be called before the first round and after each round. Defaults to None.

返回:

Results containing final model arrays and also training metrics, evaluation metrics and global evaluation metrics (if provided) from all rounds.

返回类型:

Results

abstract summary() None[source]

Log summary configuration of the strategy.