Skip to content

Commit

Permalink
Fix numpyarrayhelper
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Hellander committed Nov 24, 2023
1 parent 423de0d commit cb5c660
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 14 deletions.
4 changes: 2 additions & 2 deletions examples/async-simulation/client/entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def train(in_model_path, out_model_path):
weights = _load_model(in_model_path)

# Train
time.sleep(4)
time.sleep(np.random.randint(4, 15))

# Metadata needed for aggregation server side
metadata = {
Expand Down Expand Up @@ -86,7 +86,7 @@ def validate(in_model_path, out_json_path):

# JSON schema
report = {
"mean": 0.1,
"mean": np.mean(weights),
}

# Save JSON
Expand Down
4 changes: 1 addition & 3 deletions examples/async-simulation/client/fedn.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ entry_points:
train:
command: python entrypoint train
validate:
command: python entrypoint train


command: python entrypoint validate
2 changes: 1 addition & 1 deletion examples/async-simulation/init_fedn.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from fedn import APIClient

DISCOVER_HOST = '3.80.63.103'
DISCOVER_HOST = '54.208.105.152'
DISCOVER_PORT = 8092

client = APIClient(DISCOVER_HOST, DISCOVER_PORT)
Expand Down
6 changes: 3 additions & 3 deletions examples/async-simulation/launch_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

from fedn.network.clients.client import Client

DISCOVER_HOST = '3.80.63.103'
DISCOVER_HOST = '54.208.105.152'
DISCOVER_PORT = 8092
N_CLIENTS = 1
N_CLIENTS = 5

config = {'discover_host': DISCOVER_HOST, 'discover_port': DISCOVER_PORT, 'token': None, 'name': 'testclient',
'client_id': 1, 'remote_compute_context': True, 'force_ssl': False, 'dry_run': False, 'secure': False,
Expand All @@ -19,6 +19,6 @@
config['name'] = 'client-{}'.format(i)
clients.append(Client(config))

time.sleep(60)
time.sleep(120)
for client in clients:
client._detach()
Binary file modified examples/async-simulation/package.tar.gz
Binary file not shown.
1 change: 0 additions & 1 deletion fedn/fedn/network/clients/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,6 @@ def _process_validation_request(self, model_id, is_inference):

except Exception as e:
print("Validation failed with exception {}".format(e), flush=True)
raise
self.state = ClientState.idle
return None

Expand Down
2 changes: 0 additions & 2 deletions fedn/fedn/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ def save_metadata(metadata, filename):
with open(filename+'-metadata', 'w') as outfile:
json.dump(metadata, outfile)

# Save metric data to file


def save_metrics(metrics, filename):
""" Save metrics to file.
Expand Down
5 changes: 3 additions & 2 deletions fedn/fedn/utils/plugins/numpyarrayhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Helper(HelperBase):
""" FEDn helper class for numpy arrays. """

def increment_average(self, model, model_next, n):
def increment_average(self, model, model_next, num_examples, total_examples):
""" Update an incremental average.
:param model: Current model weights.
Expand All @@ -20,7 +20,8 @@ def increment_average(self, model, model_next, n):
:return: Incremental weighted average of model weights.
:rtype: :class:`numpy.array`
"""
return np.add(model, (model_next - model) / n)
w = num_examples / total_examples
return np.add(model, w*(model_next - model))

def save(self, model, path=None):
"""Serialize weights/parameters to file.
Expand Down

0 comments on commit cb5c660

Please sign in to comment.