SquarePartitioner

class SquarePartitioner(num_partitions: int)[source]

Bases: IdToSizeFncPartitioner

Partitioner creates partitions of size that are correlated with squared id.

The amount of data each client gets is correlated with the squared partition ID. For instance, if the IDs range from 1 to M, client with ID 1 gets 1 unit of data, client 2 gets 4 units, and so on, up to client M which gets M^2 units.

Parameters:

num_partitions (int) – The total number of partitions that the data will be divided into.

Examples

>>> from flwr_datasets import FederatedDataset
>>> from flwr_datasets.partitioner import SquarePartitioner
>>>
>>> partitioner = SquarePartitioner(num_partitions=10)
>>> fds = FederatedDataset(dataset="mnist", partitioners={"train": partitioner})
>>> partition = fds.load_partition(0)

Methods

is_dataset_assigned()

Check if a dataset has been assigned to the partitioner.

load_partition(partition_id)

Load a single partition based on the partition index.

Attributes

dataset

Dataset property.

num_partitions

Total number of partitions.

partition_id_to_indices

Node id to the list of indices.

partition_id_to_size

Node id to the number of samples.

property dataset: Dataset

Dataset property.

is_dataset_assigned() bool

Check if a dataset has been assigned to the partitioner.

This method returns True if a dataset is already set for the partitioner, otherwise, it returns False.

Returns:

dataset_assigned – True if a dataset is assigned, otherwise False.

Return type:

bool

load_partition(partition_id: int) Dataset

Load a single partition based on the partition index.

The number of samples is dependent on the partition partition_id.

Parameters:

partition_id (int) – the index that corresponds to the requested partition

Returns:

dataset_partition – single dataset partition

Return type:

Dataset

property num_partitions: int

Total number of partitions.

property partition_id_to_indices: dict[int, list[int]]

Node id to the list of indices.

property partition_id_to_size: dict[int, int]

Node id to the number of samples.