Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify Fed.py #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion main_fed.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
best_loss = None
val_acc_list, net_list = [], []

dict_len = []
for i in range(args.num_users):
dict_len.append(len(dict_users[i]))

if args.all_clients:
print("Aggregation over all clients")
w_locals = [w_glob for i in range(args.num_users)]
Expand All @@ -89,7 +93,7 @@
w_locals.append(copy.deepcopy(w))
loss_locals.append(copy.deepcopy(loss))
# update global weights
w_glob = FedAvg(w_locals)
w_glob = FedAvg(w_locals, dict_len)

# copy weight to net_glob
net_glob.load_state_dict(w_glob)
Expand Down
5 changes: 3 additions & 2 deletions models/Fed.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
from torch import nn


def FedAvg(w):
def FedAvg(w, dict_len):
w_avg = copy.deepcopy(w[0])
for k in w_avg.keys():
w_avg[k] = w_avg[k] * dict_len[0]
for i in range(1, len(w)):
w_avg[k] += w[i][k]
w_avg[k] += w[i][k] * dict_len[i]
w_avg[k] = torch.div(w_avg[k], len(w))
return w_avg