-
Notifications
You must be signed in to change notification settings - Fork 1
/
partition_data.py
280 lines (242 loc) · 9.96 KB
/
partition_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# -*- coding: utf-8 -*-
import math
import functools
import numpy as np
import torch
import torch.distributed as dist
class Partition(object):
""" Dataset-like object, but only access a subset of it. """
def __init__(self, data, indices):
self.data = data
self.indices = indices
def __len__(self):
return len(self.indices)
def __getitem__(self, index):
data_idx = self.indices[index]
return self.data[data_idx]
class DataPartitioner(object):
""" Partitions a dataset into different chuncks. """
def __init__(
self,
seed,
data,
partition_sizes,
non_iid_alpha = 1.0,
partition_type="non_iid_dirichlet",
):
# prepare info.
self.random_state = np.random.RandomState(seed)
self.data = data
self.non_iid_alpha = non_iid_alpha
self.partition_sizes = partition_sizes
self.partition_type = partition_type
self.partitions = []
# get data, data_size, indices of the data.
self.data_size = len(data)
if type(data) is not Partition:
self.data = data
indices = np.array([x for x in range(0, self.data_size)])
else:
self.data = data.data
indices = data.indices
# apply partition function.
self.partition_indices(indices)
def partition_indices(self, indices):
indices = self._create_indices(indices)
# partition indices.
from_index = 0
for partition_size in self.partition_sizes:
to_index = from_index + int(partition_size * self.data_size)
self.partitions.append(indices[from_index:to_index])
from_index = to_index
# display the class distribution over the partitions.
self.targets_of_partitions = record_class_distribution(
self.partitions,
self.data.targets if hasattr(self.data, "targets") else self.data.golds,
)
def _create_indices(self, indices):
if self.partition_type == "origin":
pass
elif self.partition_type == "random":
# it will randomly shuffle the indices.
self.random_state.shuffle(indices)
elif self.partition_type == "sorted":
# it will sort the indices based on the data label.
indices = [
i[0]
for i in sorted(
[
(idx, target)
for idx, target in enumerate(self.data.targets)
if idx in indices
],
key=lambda x: x[1],
)
]
elif self.partition_type == "non_iid_dirichlet":
num_indices = len(indices)
n_workers = len(self.partition_sizes)
targets = (
self.data.targets if hasattr(self.data, "targets") else self.data.golds
)
num_classes = len(np.unique(targets))
indices2targets = np.array(list(enumerate(targets)))
list_of_indices = build_non_iid_by_dirichlet(
random_state=self.random_state,
indices2targets=indices2targets,
non_iid_alpha=self.non_iid_alpha,
num_classes=num_classes,
num_indices=num_indices,
n_workers=n_workers,
)
indices = functools.reduce(lambda a, b: a + b, list_of_indices)
else:
raise NotImplementedError(
f"The partition scheme={self.partition_type} is not implemented yet"
)
return indices
def use(self, partition_ind):
return Partition(self.data, self.partitions[partition_ind]), self.targets_of_partitions[partition_ind]
def build_non_iid_by_dirichlet(
random_state, indices2targets, non_iid_alpha, num_classes, num_indices, n_workers
):
n_auxi_workers = 10
# random shuffle targets indices.
random_state.shuffle(indices2targets)
# partition indices.
from_index = 0
splitted_targets = []
num_splits = math.ceil(n_workers / n_auxi_workers)
split_n_workers = [
n_auxi_workers
if idx < num_splits - 1
else n_workers - n_auxi_workers * (num_splits - 1)
for idx in range(num_splits)
]
split_ratios = [_n_workers / n_workers for _n_workers in split_n_workers]
for idx, ratio in enumerate(split_ratios):
to_index = from_index + int(n_auxi_workers / n_workers * num_indices)
splitted_targets.append(
indices2targets[
from_index : (num_indices if idx == num_splits - 1 else to_index)
]
)
from_index = to_index
#
idx_batch = []
for _targets in splitted_targets:
# rebuild _targets.
_targets = np.array(_targets)
_targets_size = len(_targets)
# use auxi_workers for this subset targets.
_n_workers = min(n_auxi_workers, n_workers)
n_workers = n_workers - n_auxi_workers
# get the corresponding idx_batch.
min_size = 0
while min_size < int(0.50 * _targets_size / _n_workers):
_idx_batch = [[] for _ in range(_n_workers)]
for _class in range(num_classes):
# get the corresponding indices in the original 'targets' list.
idx_class = np.where(_targets[:, 1] == _class)[0]
idx_class = _targets[idx_class, 0]
# sampling.
try:
proportions = random_state.dirichlet(
np.repeat(non_iid_alpha, _n_workers)
)
# balance
proportions = np.array(
[
p * (len(idx_j) < _targets_size / _n_workers)
for p, idx_j in zip(proportions, _idx_batch)
]
)
proportions = proportions / proportions.sum()
proportions = (np.cumsum(proportions) * len(idx_class)).astype(int)[
:-1
]
_idx_batch = [
idx_j + idx.tolist()
for idx_j, idx in zip(
_idx_batch, np.split(idx_class, proportions)
)
]
sizes = [len(idx_j) for idx_j in _idx_batch]
min_size = min([_size for _size in sizes])
except ZeroDivisionError:
pass
idx_batch += _idx_batch
return idx_batch
def record_class_distribution(partitions, targets):
targets_of_partitions = {}
targets_np = np.array(targets)
for idx, partition in enumerate(partitions):
unique_elements, counts_elements = np.unique(
targets_np[partition], return_counts=True
)
targets_of_partitions[idx] = list(zip(unique_elements, counts_elements))
# print(
# f"the histogram of the targets in the partitions: {targets_of_partitions.items()}"
# )
return targets_of_partitions
# class Partition(object):
# def __init__(self, data, index):
# self.data = data
# self.index = index
# def __len__(self):
# return len(self.index)
# def __getitem__(self, index):
# data_idx = self.index[index]
# return self.data[data_idx]
# def skew_sort(indices, skew, classes, class_size, seed):
# # skew belongs to [0,1]
# rng = random.Random()
# rng.seed(seed)
# class_indices = {}
# for i in range(0, classes):
# class_indices[i]=indices[0:class_size[i]]
# indices = indices[class_size[i]:]
# random_indices = []
# sorted_indices = []
# for i in range(0, classes):
# sorted_size = int(skew*class_size[i])
# sorted_indices = sorted_indices + class_indices[i][0:sorted_size]
# random_indices = random_indices + class_indices[i][sorted_size:]
# rng.shuffle(random_indices)
# return random_indices, sorted_indices
# class DataPartitioner(object):
# """ Partitions a dataset into different chunks"""
# def __init__(self, data, sizes, skew, classes, class_size, seed, device):
# self.data = data
# self.partitions = []
# data_len = len(data)
# dataset = torch.utils.data.DataLoader(data, batch_size=512, shuffle=False, num_workers=2)
# labels = []
# for batch_idx, (inputs, targets) in enumerate(dataset):
# labels = labels+targets.tolist()
# #labels = [data[i][1] for i in range(0, data_len)]
# sort_index = np.argsort(np.array(labels))
# indices = sort_index.tolist()
# indices_rand, indices = skew_sort(indices, skew=skew, classes=classes, class_size=class_size, seed=seed)
# for i, frac in enumerate(sizes):
# if skew==1:
# part_len = int(frac*data_len)
# self.partitions.append(indices[0:part_len])
# if len(sizes)>10 and i<10:
# #print('here', i, len(sizes), len(indices))
# indices = indices[2*part_len:]+indices[part_len:2*part_len]
# else:
# indices = indices[part_len:]
# elif skew==0:
# part_len = int(frac*data_len)
# self.partitions.append(indices_rand[0:part_len])
# indices_rand = indices_rand[part_len:]
# else:
# part_len = int(frac*data_len*skew);
# part_len_rand = int(frac*data_len*(1-skew))
# part_ind = indices[0:part_len]+indices_rand[0:part_len_rand]
# self.partitions.append(part_ind)
# indices = indices[part_len:]
# indices_rand = indices_rand[part_len_rand:]
# def use(self, partition):
# return Partition(self.data, self.partitions[partition])