flwr.client package

Flower Client.

class flwr.client.Client[source]

Bases: abc.ABC

Abstract base class for Flower clients.

abstract evaluate(ins: flwr.common.typing.EvaluateIns)flwr.common.typing.EvaluateRes[source]

Evaluate the provided weights using the locally held dataset.

abstract fit(ins: flwr.common.typing.FitIns)flwr.common.typing.FitRes[source]

Refine the provided weights using the locally held dataset.

abstract get_parameters()flwr.common.typing.ParametersRes[source]

Return the current local model parameters.

class flwr.client.KerasClient[source]

Bases: abc.ABC

Abstract base class for Flower clients which use Keras.

abstract evaluate(weights: List[numpy.ndarray], config: Dict[str, str]) → Tuple[int, float, float][source]

Evaluate the provided weights using the locally held dataset.

Parameters
  • weights – List[numpy.ndarray]. The current (global) model weights. This argument has the structure expected by Keras’ model.set_weights(weights) and returned by Keras’ model.get_weights().

  • config – Dict[str, str]. Configuration parameters which allow the server to influence evaluation on the client. It can be used to communicate arbitrary values from the server to the client, for example, to influence the number of examples used for evaluation.

Returns

An int representing the number of examples used for evaluation, a float representing the loss, and a float representing the accuracy of the (global) model weights on the local dataset.

Return type

A tuple containing three elements

abstract fit(weights: List[numpy.ndarray], config: Dict[str, str]) → Tuple[List[numpy.ndarray], int, int][source]

Refine/train the provided weights using the locally held dataset.

Parameters
  • weights – List[numpy.ndarray]. The current (global) model weights. This argument has the structure expected by Keras’ model.set_weights(weights) and returned by Keras’ model.get_weights().

  • config – Dict[str, str]. Configuration parameters which allow the server to influence training on the client. It can be used to communicate arbitrary values from the server to the client, for example, to set the number of (local) training epochs.

Returns

Updated weights (usually obtained by calling Keras’ model.get_weights()), an int representing the number of examples used for training (num_examples), and a second int representing the maximum number of examples that might have been used during training (num_examples_ceil). If the client does not terminate training early (e.g., due to a timeout or other stopping condition), then num_examples == num_examples_ceil.

Return type

A tuple containing three elements

abstract get_weights() → List[numpy.ndarray][source]

Return the current local model weights.

Returns

The local model weights as a list of NumPy ndarrays. In many cases, it will be sufficient to just return the return value of Keras’ model.get_weights().

flwr.client.start_client(server_address: str, client: flwr.client.client.Client, grpc_max_message_length: int = 536870912) → None[source]

Start a Flower Client which connects to a gRPC server.

Parameters
  • server_address – str. The IPv6 address of the server. If the Flower server runs on the same machine on port 8080, then server_address would be “[::]:8080”.

  • client – flwr.client.Client. An implementation of the abstract base class flwr.client.Client.

  • grpc_max_message_length – int (default: 536_870_912, this equals 512MB). The maximum length of gRPC messages that can be exchanged with the Flower server. The default should be sufficient for most models. Users who train very large models might need to increase this value. Note that the Flower server needs to started with the same value (see flwr.server.start_server), otherwise it will not know about the increased limit and block larger messages.

Returns

None.

flwr.client.start_keras_client(server_address: str, client: flwr.client.keras_client.KerasClient, grpc_max_message_length: int = 536870912) → None[source]

Start a Flower KerasClient which connects to a gRPC server.

Parameters
  • server_address – str. The IPv6 address of the server. If the Flower server runs on the same machine on port 8080, then server_address would be “[::]:8080”.

  • client – flwr.client.KerasClient. An implementation of the abstract base class flwr.client.KerasClient.

  • grpc_max_message_length – int (default: 536_870_912, this equals 512MB). The maximum length of gRPC messages that can be exchanged with the Flower server. The default should be sufficient for most models. Users who train very large models might need to increase this value. Note that the Flower server needs to started with the same value (see flwr.server.start_server), otherwise it will not know about the increased limit and block larger messages.

Returns

None.

Submodules

flwr.client.app module

Flower client app.

flwr.client.app.start_client(server_address: str, client: flwr.client.client.Client, grpc_max_message_length: int = 536870912) → None[source]

Start a Flower Client which connects to a gRPC server.

Parameters
  • server_address – str. The IPv6 address of the server. If the Flower server runs on the same machine on port 8080, then server_address would be “[::]:8080”.

  • client – flwr.client.Client. An implementation of the abstract base class flwr.client.Client.

  • grpc_max_message_length – int (default: 536_870_912, this equals 512MB). The maximum length of gRPC messages that can be exchanged with the Flower server. The default should be sufficient for most models. Users who train very large models might need to increase this value. Note that the Flower server needs to started with the same value (see flwr.server.start_server), otherwise it will not know about the increased limit and block larger messages.

Returns

None.

flwr.client.app.start_keras_client(server_address: str, client: flwr.client.keras_client.KerasClient, grpc_max_message_length: int = 536870912) → None[source]

Start a Flower KerasClient which connects to a gRPC server.

Parameters
  • server_address – str. The IPv6 address of the server. If the Flower server runs on the same machine on port 8080, then server_address would be “[::]:8080”.

  • client – flwr.client.KerasClient. An implementation of the abstract base class flwr.client.KerasClient.

  • grpc_max_message_length – int (default: 536_870_912, this equals 512MB). The maximum length of gRPC messages that can be exchanged with the Flower server. The default should be sufficient for most models. Users who train very large models might need to increase this value. Note that the Flower server needs to started with the same value (see flwr.server.start_server), otherwise it will not know about the increased limit and block larger messages.

Returns

None.

flwr.client.client module

Flower client (abstract base class).

class flwr.client.client.Client[source]

Bases: abc.ABC

Abstract base class for Flower clients.

abstract evaluate(ins: flwr.common.typing.EvaluateIns)flwr.common.typing.EvaluateRes[source]

Evaluate the provided weights using the locally held dataset.

abstract fit(ins: flwr.common.typing.FitIns)flwr.common.typing.FitRes[source]

Refine the provided weights using the locally held dataset.

abstract get_parameters()flwr.common.typing.ParametersRes[source]

Return the current local model parameters.

flwr.client.keras_client module

Flower client app.

class flwr.client.keras_client.KerasClient[source]

Bases: abc.ABC

Abstract base class for Flower clients which use Keras.

abstract evaluate(weights: List[numpy.ndarray], config: Dict[str, str]) → Tuple[int, float, float][source]

Evaluate the provided weights using the locally held dataset.

Parameters
  • weights – List[numpy.ndarray]. The current (global) model weights. This argument has the structure expected by Keras’ model.set_weights(weights) and returned by Keras’ model.get_weights().

  • config – Dict[str, str]. Configuration parameters which allow the server to influence evaluation on the client. It can be used to communicate arbitrary values from the server to the client, for example, to influence the number of examples used for evaluation.

Returns

An int representing the number of examples used for evaluation, a float representing the loss, and a float representing the accuracy of the (global) model weights on the local dataset.

Return type

A tuple containing three elements

abstract fit(weights: List[numpy.ndarray], config: Dict[str, str]) → Tuple[List[numpy.ndarray], int, int][source]

Refine/train the provided weights using the locally held dataset.

Parameters
  • weights – List[numpy.ndarray]. The current (global) model weights. This argument has the structure expected by Keras’ model.set_weights(weights) and returned by Keras’ model.get_weights().

  • config – Dict[str, str]. Configuration parameters which allow the server to influence training on the client. It can be used to communicate arbitrary values from the server to the client, for example, to set the number of (local) training epochs.

Returns

Updated weights (usually obtained by calling Keras’ model.get_weights()), an int representing the number of examples used for training (num_examples), and a second int representing the maximum number of examples that might have been used during training (num_examples_ceil). If the client does not terminate training early (e.g., due to a timeout or other stopping condition), then num_examples == num_examples_ceil.

Return type

A tuple containing three elements

abstract get_weights() → List[numpy.ndarray][source]

Return the current local model weights.

Returns

The local model weights as a list of NumPy ndarrays. In many cases, it will be sufficient to just return the return value of Keras’ model.get_weights().

class flwr.client.keras_client.KerasClientWrapper(keras_client: flwr.client.keras_client.KerasClient)[source]

Bases: flwr.client.client.Client

Wrapper which translates between Client and KerasClient.

evaluate(ins: flwr.common.typing.EvaluateIns)flwr.common.typing.EvaluateRes[source]

Evaluate the provided weights using the locally held dataset.

fit(ins: flwr.common.typing.FitIns)flwr.common.typing.FitRes[source]

Refine the provided weights using the locally held dataset.

get_parameters()flwr.common.typing.ParametersRes[source]

Return the current local model parameters.