Classes Reference

FL_Client

FL_Client is a class responsible for representing clients in a federated learning network. An object of this class trains any model it receives on its local dataset. It is also able to store the training history of each round and demonstrate it using Tensorboard. By default, This class is set to run FL locally, however, it can be configured to run FL on multiple machines. To do so, alter server_ip and server_port according to your server.

class FL_Client
Represents clients in a federated learning network.
__init__(name, data, target, server_ip=LOCAL_IP, server_port=PORT)
Creates a FL_Client object
Parameters:
  • name (str) – name of the client.

  • data – training data for any Tensorflow model

  • target – training target for any Tensorflow model

  • server_ip (str) – Optional server IP; Used for running on multiple machines

  • server_port (str) – Optional server port; Used for running on multiple machines

Returns:

FL_Client object.

Return type:

FL_Client

train(epochs, batch_size, lr, loss, optimizer, metrics)
Receives the global model; trains it locally; and sends the trained model back to the server.
Parameters:
  • epochs (int) – number of epochs

  • batch_size (int) – batch size

  • lr (float) – value of learning rate

  • loss – the loss function used in training

  • optimizer – the optimizer function used in training

  • metrics (list[str]) – the metrics given to the fit() function of Tensorflow models

Return type:

None


FL_Server

In PyFed, FL_Server class is used to represent the server in a federated learning network. An object of this class takes the global models as an input, broadcast it among the clients, receives the trained models, and continue this loop for the number of rounds specified for it. After training the model, it uses Tensorboard to demonstrate the training results of each client per round and can test the model on any given test dataset.

By default, this class runs FL locally and on a single system; however, it can be altered to implement FL on multiple machines. For further information, check out Tutorials section.

class FL_Server
Used to represent the server of a federated learning network
__init__(curr_model, num_clients, rounds, port=PORT, multi_system=False)
Creates a FL_Server object
Parameters:
  • curr_model – Any Tensorflow model

  • num_clients (int) – Number of clients participating in the experiment

  • rounds (int) – Number of rounds

  • port (str) – the port on which server runs its socket connection

  • multi_system (bool) – Whether FL is going to run on multiple systems or note

Returns:

FL_Server object.

Return type:

FL_Server

train()
In each round, sends the global model to each client, receives the trained models, updates the global model using FedAvg
Return type:

None

test(data, target, loss, metrics)
Tests the global model on the given dataset
Parameters:
  • data – test data given to evaluate() function of Tensorflow

  • target – test target given to evaluate() function of Tensorflow

  • loss – loss function to evaluate the model

  • metrics – the metrics given to the evaluate() function of Tensorflow models

Return type:

None


FL_Experiment

FL_Experiment can be used to test federated learning for a specific model and dataset as fast as possible. This class takes some configurations as its input, runs federated learning with just a few lines of code, and reports the results of each client along with the accuracy of the model on the test data. This class is for those who simply want to experiment with FL, just as the name suggests.

class FL_Experiment
Implements FL as fast as possible
__init__(num_clients, clients_data, clients_target, server_data, server_target, port=PORT)
Creates a FL_Experiment object
Parameters:
  • num_clients (int) – Number of clients participating in the experiment

  • clients_data (list) – A list of training data for each client

  • clients_target (list) – A list of training target for each client

  • server_data – Test data used by the server to test the global model

  • server_data – Test target used by the server to test the global model

  • port (str) – the port on which server runs its socket connection

Returns:

FL_Experiment object.

Return type:

FL_Experiment

run(model, rounds, epochs, batch_size, lr, optimizer, loss, metrics)
Trains and tests the global model.
Parameters:
  • model – Any Tensorflow model

  • rounds (int) – Number of rounds

  • epochs (int) – number of epochs

  • batch_size (int) – batch size

  • lr (float) – value of learning rate

  • loss – the loss function used in training

  • optimizer – the optimizer function used in training

  • metrics (list[str]) – the metrics given to the fit() function of Tensorflow models

Return type:

None