-
Notifications
You must be signed in to change notification settings - Fork 9
/
dataset_processing.py
338 lines (292 loc) · 15.6 KB
/
dataset_processing.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# -*- coding: utf-8 -*-
# Author: Jacky
# Creation Date: 2021/3/16
import numpy as np
import pandas as pd
import copy
import torch
from data_set import DataSet
from user_info import UserInfo
from options import args_parser
from data_set import convert
import utils
from options import args_parser
def get_dataset(args):
"""
:param: args:
:return: ratings: dataFrame ['user_id' 'movie_id' 'rating']
:return: user_info: dataFrame ['user_id' 'gender' 'age' 'occupation']
"""
model_manager = utils.ModelManager('data_set')
user_manager = utils.UserInfoManager(args.dataset)
'''Do you want to clean workspace and retrain model/data_set user again?'''
'''if you want to retrain model/data_set user, please set clean_workspace True'''
model_manager.clean_workspace(args.clean_dataset)
user_manager.clean_workspace(args.clean_user)
# 导入模型信息
try:
ratings = model_manager.load_model(args.dataset + '-ratings')
print("Load " + args.dataset + " data_set success.\n")
except OSError:
ratings = DataSet.LoadDataSet(name=args.dataset)
model_manager.save_model(ratings, args.dataset + '-ratings')
# 导入用户信息
try:
user_info = user_manager.load_user_info('user_info')
print("Load " + args.dataset + " user_info success.\n")
except OSError:
user_info = UserInfo.load_user_info(name=args.dataset)
user_manager.save_user_info(user_info, 'user_info')
return ratings, user_info
def sampling(args):
"""
:param args
:return: sample: matrix user_id|movie_id|rating|gender|age|occupation|label
:return: user_group_train, the idx of sample for each client for training
:return: user_group_test, the idx of sample for each client for testing
"""
# 存储每个client信息
model_manager = utils.ModelManager('clients')
'''Do you want to clean workspace and retrain model/clients again?'''
'''if you want to change test_size or retrain model/clients, please set clean_workspace True'''
model_manager.clean_workspace(args.clean_clients)
# 导入模型信息
try:
users_group_train = model_manager.load_model(args.dataset + '-user_group_train')
users_group_test = model_manager.load_model(args.dataset + '-user_group_test')
sample = model_manager.load_model(args.dataset + '-sample')
print("Load " + args.dataset + " clients info success.\n")
except OSError:
# 调用get_dataset函数,得到ratings,user_info
ratings, user_info = get_dataset(args)
# 每个client包含的用户数
users_num_client = int((user_info.index[-1] + 1) / args.clients_num)
# sample user_id|movie_id|rating|gender|age|occupation
sample = pd.merge(ratings, user_info, on=['user_id'], how='inner')
sample = sample.astype({'user_id': 'int64', 'movie_id': 'int64', 'rating': 'float64',
'gender': 'float64', 'age': 'float64', 'occupation': 'float64'})
# 生成每个客户用来train和test的idx
users_group_all, users_group_train, users_group_test ,request_content= {}, {}, {}, {}
# 生成用户数据集
# 对用户数据集进行划分train/test
all_test_num = 0
for i in range(args.clients_num):
print('loading client ' + str(i))
index_begin = ratings[ratings['user_id'] == int(users_num_client) * i + 1].index[0]
index_end = ratings[ratings['user_id'] == users_num_client * (i + 1)].index[-1] \
if i != args.clients_num-1 else ratings.index[-1]
users_group_all[i] = set(np.arange(index_begin, index_end + 1))
NUM_train = int(0.8 * len(users_group_all[i]))
users_group_train[i] = set(np.random.choice(list(users_group_all[i]), NUM_train, replace=False))
users_group_test[i] = users_group_all[i] - users_group_train[i]
# 将set转换回list,并排序
users_group_train[i] = list(users_group_train[i])
users_group_test[i] = list(users_group_test[i])
users_group_train[i].sort()
users_group_test[i].sort()
all_test_num += NUM_train/0.8*0.2
print('generate client ' + str(i) + ' info success\n')
for i in range(args.epochs):
for j in range(args.clients_num):
if j==0:
request_content[i]=np.random.choice(list(users_group_test[j]), int(all_test_num / args.epochs/args.clients_num), replace=False)
else:
request_content[i]=np.append(request_content[i],np.random.choice(list(users_group_test[j]), int(all_test_num / args.epochs/args.clients_num), replace=False))
request_content[i]=list(set(request_content[i]))
request_content[i].sort()
# 存储user_group_train user_group_test sample
model_manager.save_model(users_group_train, args.dataset + '-user_group_train')
model_manager.save_model(users_group_test, args.dataset + '-user_group_test')
return sample, users_group_train, users_group_test, request_content
def sampling_mobility(args,vehicle_num):
"""
:param args
:return: sample: matrix user_id|movie_id|rating|gender|age|occupation|label
:return: user_group_train, the idx of sample for each client for training
:return: user_group_test, the idx of sample for each client for testing
"""
# 存储每个client信息
model_manager = utils.ModelManager('clients')
'''Do you want to clean workspace and retrain model/clients again?'''
'''if you want to change test_size or retrain model/clients, please set clean_workspace True'''
model_manager.clean_workspace(args.clean_clients)
# 导入模型信息
try:
users_group_train = model_manager.load_model(args.dataset + '-user_group_train')
users_group_test = model_manager.load_model(args.dataset + '-user_group_test')
sample = model_manager.load_model(args.dataset + '-sample')
print("Load " + args.dataset + " clients info success.\n")
except OSError:
# 调用get_dataset函数,得到ratings,user_info
ratings, user_info = get_dataset(args)
# 每个client包含的用户数
users_num_client = np.random.randint(10,15,vehicle_num)
users_num_client = sorted( users_num_client )
a=0
for i in range(vehicle_num):
a+=users_num_client[i]
for i in range(vehicle_num):
users_num_client[i] = int((user_info.index[-1] + 1) * users_num_client[i] / a )
print('each vehicle allocated data:',users_num_client)
user_seq_client=[]
for i in range(vehicle_num):
num=0
for j in range(i):
num+=users_num_client[j]
user_seq_client.append(num)
# sample user_id|movie_id|rating|gender|age|occupation
sample = pd.merge(ratings, user_info, on=['user_id'], how='inner')
sample = sample.astype({'user_id': 'int64', 'movie_id': 'int64', 'rating': 'float64',
'gender': 'float64', 'age': 'float64', 'occupation': 'float64'})
# 生成每个客户用来train和test的idx
users_group_all, users_group_train, users_group_test ,request_content= {}, {}, {}, {}
# 生成用户数据集
# 对用户数据集进行划分train/test
all_test_num = 0
for i in range(vehicle_num):
print('loading client ' + str(i))
index_begin = ratings[ratings['user_id'] == user_seq_client[i] + 1].index[0]
index_end = ratings[ratings['user_id'] == user_seq_client[i] + users_num_client[i] ].index[-1]
users_group_all[i] = set(np.arange(index_begin, index_end + 1))
#cache size vs method 0.95
# hit radio vs round 0.8 1m
NUM_train = int(0.8* len(users_group_all[i]))
users_group_train[i] = set(np.random.choice(list(users_group_all[i]), NUM_train, replace=False))
users_group_test[i] = users_group_all[i] - users_group_train[i]
# 将set转换回list,并排序
users_group_train[i] = list(users_group_train[i])
users_group_test[i] = list(users_group_test[i])
users_group_train[i].sort()
users_group_test[i].sort()
all_test_num += NUM_train/0.8*0.2
print('generate client ' + str(i) + ' info success\n')
print('all_test_num',all_test_num)
vehicle_request_num = dict([(k, []) for k in range(args.epochs)])
for i in range(args.epochs):
for j in range(vehicle_num):
if j==0:
vehicle_request_num[i]=[]
request_content[i]=np.random.choice(list(users_group_test[j]), int(all_test_num / args.epochs * users_num_client[j] / a), replace=True)
vehicle_request_num[i].append(int(all_test_num / args.epochs*users_num_client[j]/a))
else:
request_content[i]=np.append(request_content[i],np.random.choice(list(users_group_test[j]), int(all_test_num / args.epochs *users_num_client[j]/a), replace=True))
vehicle_request_num[i].append(int(all_test_num / args.epochs * users_num_client[j] / a))
request_content[i]=list(set(request_content[i]))
request_content[i].sort()
# 存储user_group_train user_group_test sample
model_manager.save_model(users_group_train, args.dataset + '-user_group_train')
model_manager.save_model(users_group_test, args.dataset + '-user_group_test')
return sample, users_group_train, users_group_test, request_content, vehicle_request_num
def sampling_mobility_density(args,vehicle_num):
"""
:param args
:return: sample: matrix user_id|movie_id|rating|gender|age|occupation|label
:return: user_group_train, the idx of sample for each client for training
:return: user_group_test, the idx of sample for each client for testing
"""
# 存储每个client信息
model_manager = utils.ModelManager('clients')
'''Do you want to clean workspace and retrain model/clients again?'''
'''if you want to change test_size or retrain model/clients, please set clean_workspace True'''
model_manager.clean_workspace(args.clean_clients)
# 导入模型信息
try:
users_group_train = model_manager.load_model(args.dataset + '-user_group_train')
users_group_test = model_manager.load_model(args.dataset + '-user_group_test')
sample = model_manager.load_model(args.dataset + '-sample')
print("Load " + args.dataset + " clients info success.\n")
except OSError:
# 调用get_dataset函数,得到ratings,user_info
ratings, user_info = get_dataset(args)
# 每个client包含的用户数
users_num_client = np.random.randint(5,15,vehicle_num)
users_num_client = sorted(users_num_client, reverse=True)
a=0
for i in range(vehicle_num):
a+=users_num_client[i]
for i in range(vehicle_num):
users_num_client[i] = int((user_info.index[-1] + 1) * users_num_client[i] / a )
print('each vehicle allocated data:',users_num_client)
user_seq_client=[]
for i in range(vehicle_num):
num=0
for j in range(i):
num+=users_num_client[j]
user_seq_client.append(num)
# sample user_id|movie_id|rating|gender|age|occupation
sample = pd.merge(ratings, user_info, on=['user_id'], how='inner')
sample = sample.astype({'user_id': 'int64', 'movie_id': 'int64', 'rating': 'float64',
'gender': 'float64', 'age': 'float64', 'occupation': 'float64'})
# 生成每个客户用来train和test的idx
users_group_all, users_group_train, users_group_test,request_content= {}, {}, {}, {}
# 生成用户数据集
# 对用户数据集进行划分train/test
all_test_num = 0
for i in range(vehicle_num):
print('loading client ' + str(i))
index_begin = ratings[ratings['user_id'] == user_seq_client[i] + 1].index[0]
index_end = ratings[ratings['user_id'] == user_seq_client[i] + users_num_client[i] ].index[-1]
users_group_all[i] = set(np.arange(index_begin, index_end + 1))
#1m
NUM_train = int(0.9985 * len(users_group_all[i]))
users_group_train[i] = set(np.random.choice(list(users_group_all[i]), NUM_train, replace=False))
users_group_test[i] = users_group_all[i] - users_group_train[i]
# 将set转换回list,并排序
users_group_train[i] = list(users_group_train[i])
users_group_test[i] = list(users_group_test[i])
users_group_train[i].sort()
users_group_test[i].sort()
# 1m
all_test_num += NUM_train/0.9985*0.0015
print('generate client ' + str(i) + ' info success\n')
print('all_test_num',all_test_num)
vehicle_request_num = []
for j in range(vehicle_num):
if j == 0:
#request_content = np.random.choice(list(users_group_test[j]), int(all_test_num/15 * users_num_client[j] / a), replace=True)
# 100k
request_content = np.random.choice(list(users_group_test[j]),
int(all_test_num * users_num_client[j] / a), replace=True)
else:
#request_content = np.append(request_content, np.random.choice(list(users_group_test[j]), int(all_test_num/15 * users_num_client[j] / a),replace=True))
# 100k
request_content = np.append(request_content, np.random.choice(list(users_group_test[j]),
int(all_test_num * users_num_client[
j] / a),
replace=True))
#vehicle_request_num.append(int(all_test_num / 15 * users_num_client[j] / a))
#100k
vehicle_request_num.append(int(all_test_num * users_num_client[j] / a))
request_content=list(set(request_content))
request_content.sort()
# 存储user_group_train user_group_test sample
model_manager.save_model(users_group_train, args.dataset + '-user_group_train')
model_manager.save_model(users_group_test, args.dataset + '-user_group_test')
return sample, users_group_train, users_group_test, request_content, vehicle_request_num
def average_weights(w):
"""
Returns the average of the weights.
"""
w_avg = copy.deepcopy(w[0])
for key in w_avg.keys():
for i in range(1, len(w)):
w_avg[key] += w[i][key]
w_avg[key] = torch.div(w_avg[key], len(w))
return w_avg
def asy_average_weights(l,g,l_old,vehicle_all_num):
"""
Returns the weight.
"""
l_w=l.state_dict()
g_w=g.state_dict()
l_old_w=l_old.state_dict()
for key in g_w.keys():
g_w[key]+=1/vehicle_all_num*(l_w[key]-l_old_w[key])
return g_w
if __name__ == '__main__':
args = args_parser()
ratings, user_info = get_dataset(args)
sample, users_group_train, users_group_test = sampling(args)
# 验证convert
client_6 = np.array(sample.iloc[users_group_test[6], :])
user_movie_6 = convert(client_6, max(sample['movie_id']))