functional#

Module Contents#

AverageMeter

Record metrics information

setup_seed(seed)

evaluate(model, criterion, test_loader)

Evaluate classify task model accuracy.

read_config_from_json(json_file, user_name)

Read config from json_file to get config for user_name

get_best_gpu()

Return gpu (torch.device) with largest free memory.

partition_report(targets, data_indices[, class_num, ...])

Generate data partition report for clients in data_indices.

setup_seed(seed)#
class AverageMeter#

Bases: object

Record metrics information

reset()#
update(val, n=1)#
evaluate(model, criterion, test_loader)#

Evaluate classify task model accuracy.

Returns:

(loss.sum, acc.avg)

read_config_from_json(json_file: str, user_name: str)#

Read config from json_file to get config for user_name

Parameters:
  • json_file (str) – path for json_file

  • user_name (str) – read config for this user, it can be ‘server’ or ‘client_id’

Returns:

a tuple with ip, port, world_size, rank about user with user_name

Examples

read_config_from_json(‘../../../tests/data/config.json’, ‘server’)

Notes

config.json example as follows {

“server”: {

“ip” : “127.0.0.1”, “port”: “3002”, “world_size”: 3, “rank”: 0

}, “client_0”: {

“ip”: “127.0.0.1”, “port”: “3002”, “world_size”: 3, “rank”: 1

}, “client_1”: {

“ip”: “127.0.0.1”, “port”: “3002”, “world_size”: 3, “rank”: 2

}

}

get_best_gpu()#

Return gpu (torch.device) with largest free memory.

partition_report(targets, data_indices, class_num=None, verbose=True, file=None)#

Generate data partition report for clients in data_indices.

Generate data partition report for each client according to data_indices, including ratio of each class and dataset size in current client. Report can be printed in screen or into file. The output format is comma-separated values which can be read by pandas.read_csv() or csv.reader().

Parameters:
  • targets (list or numpy.ndarray) – Targets for all data samples, with each element is in range of 0 to class_num-1.

  • data_indices (dict) – Dict of client_id: [data indices].

  • class_num (int, optional) – Total number of classes. If set to None, then class_num = max(targets) + 1.

  • verbose (bool, optional) – Whether print data partition report in screen. Default as True.

  • file (str, optional) – Output file name of data partition report. If None, then no output in file. Default as None.

Examples

First generate synthetic data labels and data partition to obtain data_indices ({ client_id: sample indices}):

>>> sample_num = 15
>>> class_num = 4
>>> clients_num = 3
>>> num_per_client = int(sample_num/clients_num)
>>> labels = np.random.randint(class_num, size=sample_num)  # generate 15 labels, each label is 0 to 3
>>> rand_per = np.random.permutation(sample_num)
>>> # partition synthetic data into 3 clients
>>> data_indices = {0: rand_per[0:num_per_client],
...                 1: rand_per[num_per_client:num_per_client*2],
...                 2: rand_per[num_per_client*2:num_per_client*3]}

Check data_indices may look like:

>>> data_indices
{0: array([8, 6, 5, 7, 2]),
 1: array([ 3, 10, 14,  4,  1]),
 2: array([13,  9, 12, 11,  0])}

Now generate partition report for each client and each class:

>>> partition_report(labels, data_indices, class_num=class_num, verbose=True, file=None)
Class frequencies:
client,class0,class1,class2,class3,Amount
Client   0,0.200,0.00,0.200,0.600,5
Client   1,0.400,0.200,0.200,0.200,5
Client   2,0.00,0.400,0.400,0.200,5