functional#
Module Contents#
Record metrics information |
|
|
|
Evaluate classify task model accuracy. |
|
Read config from json_file to get config for user_name |
Return gpu ( |
|
|
Generate data partition report for clients in |
- setup_seed(seed)#
- 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:
- 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 bypandas.read_csv()
orcsv.reader()
.- Parameters:
targets (list or numpy.ndarray) – Targets for all data samples, with each element is in range of
0
toclass_num-1
.data_indices (dict) – Dict of
client_id: [data indices]
.class_num (int, optional) – Total number of classes. If set to
None
, thenclass_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 asNone
.
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