-
Notifications
You must be signed in to change notification settings - Fork 943
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Daniel J. Beutel <[email protected]>
- Loading branch information
1 parent
256548e
commit d663cd4
Showing
13 changed files
with
680 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from typing import List, Tuple | ||
|
||
import flwr as fl | ||
from flwr.common import Context, Metrics | ||
from flwr.server import Driver, LegacyContext | ||
|
||
|
||
# Define metric aggregation function | ||
def weighted_average(metrics: List[Tuple[int, Metrics]]) -> Metrics: | ||
examples = [num_examples for num_examples, _ in metrics] | ||
|
||
# Multiply accuracy of each client by number of examples used | ||
train_losses = [num_examples * m["train_loss"] for num_examples, m in metrics] | ||
train_accuracies = [ | ||
num_examples * m["train_accuracy"] for num_examples, m in metrics | ||
] | ||
val_losses = [num_examples * m["val_loss"] for num_examples, m in metrics] | ||
val_accuracies = [num_examples * m["val_accuracy"] for num_examples, m in metrics] | ||
|
||
# Aggregate and return custom metric (weighted average) | ||
return { | ||
"train_loss": sum(train_losses) / sum(examples), | ||
"train_accuracy": sum(train_accuracies) / sum(examples), | ||
"val_loss": sum(val_losses) / sum(examples), | ||
"val_accuracy": sum(val_accuracies) / sum(examples), | ||
} | ||
|
||
|
||
# Define strategy | ||
strategy = fl.server.strategy.FedAvg( | ||
fraction_fit=1.0, # Select all available clients | ||
fraction_evaluate=0.0, # Disable evaluation | ||
min_available_clients=2, | ||
fit_metrics_aggregation_fn=weighted_average, | ||
) | ||
|
||
|
||
# Run via `flower-server-app server_workflow:app` | ||
app = fl.server.ServerApp() | ||
|
||
|
||
@app.main() | ||
def main(driver: Driver, context: Context) -> None: | ||
# Construct the LegacyContext | ||
context = LegacyContext( | ||
state=context.state, | ||
config=fl.server.ServerConfig(num_rounds=3), | ||
strategy=strategy, | ||
) | ||
|
||
# Create the workflow | ||
workflow = fl.server.workflow.DefaultWorkflow() | ||
|
||
# Execute | ||
workflow(driver, context) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.