diff --git a/examples/simulation/client/entrypoint b/examples/simulation/client/entrypoint new file mode 100644 index 000000000..3f660fac1 --- /dev/null +++ b/examples/simulation/client/entrypoint @@ -0,0 +1,58 @@ +#!./.simulation/bin/python +import collections +import math +import os +import random + +import fire +import numpy + +from fedn.utils.helpers import get_helper, save_metadata, save_metrics + +HELPER_MODULE = 'numpyarrayhelper' +N_SAMPLES = 1000 +DELAY = 10 + + +def init_seed(out_path='seed.npz'): + """ Initialize seed model. + + :param out_path: The path to save the seed model to. + :type out_path: str + """ + # Init and save + model = numpy.ones(size=(1, N_SAMPLES)) + helper = get_helper(HELPER_MODULE) + helper.save(model, out_path) + + +def train(in_model_path, out_model_path): + """ Train model. + + :param in_model_path: The path to the input model. + :type in_model_path: str + :param out_model_path: The path to save the output model to. + :type out_model_path: str + """ + + model = numpy.ones(size=(1, N_SAMPLES)) + time.sleep(DELAY+DELAY*random.random()) + + # Metadata needed for aggregation server side + metadata = { + 'num_examples': N_SAMPLES, + } + + # Save JSON metadata file + save_metadata(metadata, out_model_path) + + # Save model update + helper = get_helper(HELPER_MODULE) + helper.save(model, out_model_path) + + +if __name__ == '__main__': + fire.Fire({ + 'init_seed': init_seed, + 'train': train, + }) diff --git a/examples/simulation/client/fedn.yaml b/examples/simulation/client/fedn.yaml new file mode 100644 index 000000000..29c475270 --- /dev/null +++ b/examples/simulation/client/fedn.yaml @@ -0,0 +1,5 @@ +entry_points: + train: + command: /venv/bin/python entrypoint train $ENTRYPOINT_OPTS + validate: + command: /venv/bin/python entrypoint validate $ENTRYPOINT_OPTS \ No newline at end of file diff --git a/examples/simulation/run_clients.py b/examples/simulation/run_clients.py new file mode 100644 index 000000000..42fc5dc7c --- /dev/null +++ b/examples/simulation/run_clients.py @@ -0,0 +1,16 @@ +#!./.simulation/bin/python +import os + +from fedn.network.clients import Client + +API_SERVER = 'localhost:8092' + + +config = {'remote_compute_context': False, + 'validator': False, + 'trainer': True, + 'init': 'client.yaml', + } + +client = Client(config) +client.run()