-
Notifications
You must be signed in to change notification settings - Fork 858
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(framework) Update
sklearn
template (#4293)
- Loading branch information
Showing
5 changed files
with
97 additions
and
49 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
14 changes: 13 additions & 1 deletion
14
src/py/flwr/cli/new/templates/app/code/server.sklearn.py.tpl
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
67 changes: 67 additions & 0 deletions
67
src/py/flwr/cli/new/templates/app/code/task.sklearn.py.tpl
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,67 @@ | ||
"""$project_name: A Flower / $framework_str app.""" | ||
|
||
import numpy as np | ||
from flwr_datasets import FederatedDataset | ||
from flwr_datasets.partitioner import IidPartitioner | ||
from sklearn.linear_model import LogisticRegression | ||
|
||
fds = None # Cache FederatedDataset | ||
|
||
|
||
def load_data(partition_id: int, num_partitions: int): | ||
"""Load partition MNIST data.""" | ||
# Only initialize `FederatedDataset` once | ||
global fds | ||
if fds is None: | ||
partitioner = IidPartitioner(num_partitions=num_partitions) | ||
fds = FederatedDataset( | ||
dataset="mnist", | ||
partitioners={"train": partitioner}, | ||
) | ||
|
||
dataset = fds.load_partition(partition_id, "train").with_format("numpy") | ||
|
||
X, y = dataset["image"].reshape((len(dataset), -1)), dataset["label"] | ||
|
||
# Split the on edge data: 80% train, 20% test | ||
X_train, X_test = X[: int(0.8 * len(X))], X[int(0.8 * len(X)) :] | ||
y_train, y_test = y[: int(0.8 * len(y))], y[int(0.8 * len(y)) :] | ||
|
||
return X_train, X_test, y_train, y_test | ||
|
||
|
||
def get_model(penalty: str, local_epochs: int): | ||
|
||
return LogisticRegression( | ||
penalty=penalty, | ||
max_iter=local_epochs, | ||
warm_start=True, | ||
) | ||
|
||
|
||
def get_model_params(model): | ||
if model.fit_intercept: | ||
params = [ | ||
model.coef_, | ||
model.intercept_, | ||
] | ||
else: | ||
params = [model.coef_] | ||
return params | ||
|
||
|
||
def set_model_params(model, params): | ||
model.coef_ = params[0] | ||
if model.fit_intercept: | ||
model.intercept_ = params[1] | ||
return model | ||
|
||
|
||
def set_initial_params(model): | ||
n_classes = 10 # MNIST has 10 classes | ||
n_features = 784 # Number of features in dataset | ||
model.classes_ = np.array([i for i in range(10)]) | ||
|
||
model.coef_ = np.zeros((n_classes, n_features)) | ||
if model.fit_intercept: | ||
model.intercept_ = np.zeros((n_classes,)) |
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