utils

utils.compression_utils module

federated.utils.compression_utils.build_encoded_broadcast_fn(weights: tensorflow.python.framework.ops.Tensor)tensorflow_model_optimization.python.core.internal.tensor_encoding.core.core_encoder.Encoder

Function for encoding weights with uniform quantization.

Args:

weights (tf.Tensor): Weights of the model.

Returns:

te.core.Encoder: Encoder.

federated.utils.compression_utils.encoded_broadcast_process(tff_model_fn: tensorflow_federated.python.learning.model.Model)tensorflow_federated.python.core.templates.measured_process.MeasuredProcess

Function for creating a MeasuredProcess used in federated learning. Uses build_encoded_broadcast_fn defined above. Returns MeasuredProcess

Args:

tff_model_fn (tff.learning.Model): Federated learning model.

Returns:

tff.templates.MeasuredProcess: MesuredProcess object.

federated.utils.compression_utils.set_communication_cost_env()tensorflow_federated.python.core.impl.executors.executor_factory.ExecutorFactory

Set environment for loggging communication cost.

Returns:

tff.framework.ExecutorFactory: ExecutorFactory for the environment.

utils.data_utils module

federated.utils.data_utils.get_client_dataset_fn(dataset: tensorflow.python.data.ops.dataset_ops.DatasetV2, number_of_clients_per_round: int, seed: Optional[int] = None)Callable[], tensorflow.python.data.ops.dataset_ops.DatasetV2]

This function generates a function for selecting client-datasets for each round number.

Args:

dataset (tf.data.Dataset): Dataset.

number_of_clients_per_round (int): Number of clients per round.

seed (int, optional): Random seed. Defaults to None.

Returns:

Callable[[], tf.data.Dataset]: A function for choosing clients while training.

federated.utils.data_utils.get_validation_fn(test_dataset: tensorflow.python.data.ops.dataset_ops.DatasetV2, model_fn: Callable[], tensorflow.python.keras.engine.training.Model], loss_fn: Callable[], tensorflow.python.keras.losses.Loss], metrics_fn: Callable[], tensorflow.python.keras.metrics.Metric])Callable[], tensorflow.python.data.ops.dataset_ops.DatasetV2]

This function makes a function for evaluating a model while training.

Args:

test_dataset (tf.data.Dataset): Dataset.

model_fn (Callable[[], tf.keras.models.Model]): Model function.

loss_fn (Callable[[], tf.keras.losses.Loss]): Loss function.

metrics_fn (Callable[[], tf.keras.metrics.Metric]): Which metrics to measure.

Returns:

Callable[[], tf.data.Dataset]: Returns a validation function.

federated.utils.data_utils.randomly_select_clients_for_round(population: int, num_of_clients: int, replace: bool = False, seed: Optional[int] = None)functools.partial

This function creates a partial function for sampling random clients.

Args:

population (int): Client population size.

num_of_clients (int): Number of clients.

replace (bool, optional): With or without replacement. Defaults to False.

seed (int, optional): Random seed. Defaults to None.

Returns:

functools.partial: A partial function for randomly selecting clients.

utils.differential_privacy module

federated.utils.differential_privacy.gaussian_fixed_aggregation_factory(noise_multiplier: float, clients_per_round: int, clipping_value: float)tensorflow_federated.python.aggregators.dp_factory.DifferentiallyPrivateFactory

Function for differential privacy with fixed gaussian aggregation.

Args:

noise_multiplier (float): Noise multiplier.

clients_per_round (int): Clients per round.

clipping_value (float): Clipping value.

Returns:

tff.aggregators.DifferentiallyPrivateFactory: Differential Privacy Factory.

utils.rfa module

federated.utils.rfa.create_rfa_averaging(create_model: Callable[], tensorflow_federated.python.learning.model.Model], iterations: int, v: float, server_optimizer_fn: Callable[], tensorflow.python.keras.optimizer_v2.optimizer_v2.OptimizerV2], client_optimizer_fn: Callable[], tensorflow.python.keras.optimizer_v2.optimizer_v2.OptimizerV2], compression: bool = False)tensorflow_federated.python.core.templates.iterative_process.IterativeProcess

Function for setting up Robust Federated Aggregation.

Args:

create_model (Callable[[], tff.learning.Model]): Function for creating a model.

iterations (int): Calls to Secure Average Oracle.

v (float): L2 Threshold.

server_optimizer_fn (Callable[[], tf.keras.optimizers.Optimizer]): Server Optimizer Function.

client_optimizer_fn (Callable[[], tf.keras.optimizers.Optimizer]): Client Optimizer Function.

compression (bool, optional): Whether the model should be compressed. Defaults to False.

Returns:

tff.templates.IterativeProcess: Returns an Iterative Process with the RFA Averaging scheme

federated.utils.rfa.create_robust_measured_process(model: tensorflow_federated.python.learning.model.Model, iterations: int, v: float, compression: bool = False)tensorflow_federated.python.core.templates.measured_process.MeasuredProcess

Function that creates robust measured process used in federated aggregation.

Args:

model (tf.keras.Model): Model to train.

iterations (int): Number of iterations.

v (float): L2 threshold.

compression (bool, optional): If the model should be compressed. Defaults to False.

Returns:

tff.templates.MeasuredProcess: Returns a tff.templates.MeasuredProcess which defines how to aggregate client updates.

utils.training_loops module

federated.utils.training_loops.centralized_training_loop(model: tensorflow.python.keras.engine.training.Model, dataset: tensorflow.python.data.ops.dataset_ops.DatasetV2, name: str, epochs: int, output: str, save_model: bool = True, validation_dataset: Optional[tensorflow.python.data.ops.dataset_ops.DatasetV2] = None, test_dataset: Optional[tensorflow.python.data.ops.dataset_ops.DatasetV2] = None)Tuple[tensorflow.python.keras.callbacks.History, float]

Function trains a model on a dataset using centralized machine learning, and tests its performance.

Args:

model (tf.keras.Model): Model.

dataset (tf.data.Dataset): Dataset.

name (str): Experiment name.

epochs (int): Number of epochs.

output (str): Logs output destination.

save_model (bool, optional): If the model should be saved. Defaults to True.

validation_dataset (tf.data.Dataset, optional): Validation dataset. Defaults to None.

test_dataset (tf.data.Dataset, optional): Test dataset. Defaults to None.

Returns:

[tf.keras.callbacks.History, float]: Returns the history object after fitting the model, and training time.

federated.utils.training_loops.federated_training_loop(iterative_process: tensorflow_federated.python.core.templates.iterative_process.IterativeProcess, get_client_dataset: Callable[[int], List[tensorflow.python.data.ops.dataset_ops.DatasetV2]], number_of_rounds: int, name: str, output: str, batch_size: int, number_of_training_points: Optional[int] = None, keras_model_fn: Optional[Callable[], tensorflow.python.keras.engine.training.Model]] = None, loss_fn: Optional[Callable[], tensorflow.python.keras.losses.Loss]] = None, metrics_fn: Optional[Callable[], tensorflow.python.keras.metrics.Metric]] = None, save_model: bool = False, validate_model: Optional[Callable[[Any, int], Dict[str, float]]] = None, noise_multiplier: Optional[int] = None)Tuple[tensorflow_federated.python.core.api.computation_base.Computation, float, float]

Function trains a model on a dataset using federated learning.

Args:

iterative_process (tff.templates.IterativeProcess): Iterative process.

get_client_dataset (Callable[[int], List[tf.data.Dataset]]): Function for getting dataset for clients.

number_of_rounds (int): Number of rounds.

name (str): Experiment name.

output (str): Logs destination.

batch_size (int): Batch size.

number_of_training_points (int, optional): Number of datapoints.. Defaults to None.

keras_model_fn (Callable[[], tf.keras.Model], optional): Keras model function. Defaults to None.

loss_fn (Callable[[], tf.keras.losses.Loss], optional): Loss function. Defaults to None.

metrics_fn (Callable[[], tf.keras.metrics.Metric], optional): Metrics function. Defaults to None.

save_model (bool, optional): If the model should be saved. Defaults to False.

validate_model (Callable[[Any, int], Dict[str, float]], optional): Validation function. Defaults to None.

noise_multiplier (int, optional): Noise multiplier. Defaults to None.

Returns:

[tff.Computation, float]: Returns the last state of the server and training time