-
Notifications
You must be signed in to change notification settings - Fork 0
/
FED (5).py
1571 lines (1201 loc) · 65.3 KB
/
FED (5).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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import argparse
from utils import *
from Graph_generate.lastfm_data_process import LastFmDataset
from Graph_generate.lastfm_star_data_process import LastFmStarDataset
from Graph_generate.lastfm_graph import LastFmGraph
from Graph_generate.yelp_data_process import YelpDataset
from Graph_generate.yelp_graph import YelpGraph
import torch
import random
import numpy as np
from time import time
import torch.nn as nn
#from data import load_dataset
#from FedRec.server import FedRecServer
#from FedRec.client import FedRecClient
import random
import torch
import torch.nn as nn
import json
import pickle
from utils import *
import time
from torch.nn.utils.rnn import pad_sequence
import argparse
from FM.FM_model import FactorizationMachine
from FM.FM_feature_evaluate import evaluate_feature
from FM.FM_item_evaluate import evaluate_item
# In[2]:
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# In[3]:
#dataset=None
#epochs=200
#batch_size=256
grad_limit=1.0
#clients_limit=0.05
#items_limit=60
#part_percent=1
#attack_lr=0.01
#attack_batch_size=256
# In[4]:
# SCPR agrs
lr=0.02
flr=0.0001
reg=0.001
decay=0.0
qonly=1
bs=64
hs=64
ip=0.01
dr=0.5
optim="Ada"
observe=25
uf=1
rd=0
useremb=1
freeze=0
command=8
seed=0
max_epoch=250
pretrain=1
load_fm_epoch=0
#data_name=LAST_FM
# In[5]:
data_name="LAST_FM_STAR"
dataset = load_dataset(data_name)
ITEM = 'item'
ITEM_FEATURE = 'belong_to'
dataset = load_dataset(data_name)
kg = load_kg(data_name)
hs=64
qonly=1
ip=0.01
dr=0.5
user_length, item_length, feature_length=int(getattr(dataset, 'user').value_len),int(getattr(dataset, 'item').value_len),int(getattr(dataset, 'feature').value_len)
bs = 64
max_epoch=250
lr = 0.02
decay=0.0
flr=0.0001
reg=0.001
observe=25
command=8
uf=1
seed=0
useremb=1
load_fm_epoch=0
PAD_IDX1 = user_length + item_length
PAD_IDX2 = feature_length
filename = 'v1-data-{}-lr-{}-flr-{}-reg-{}-bs-{}-command-{}-uf-{}-seed-{}'.format(
data_name, lr, flr, reg, bs, command, uf, seed)
# In[6]:
TMP_DIR = {
LAST_FM: './tmp/last_fm',
YELP: './tmp/yelp',
LAST_FM_STAR: './tmp/last_fm_star',
YELP_STAR: './tmp/yelp_star',
}
# In[7]:
#getattr(dataset, 'item')#從0~7431,總共7432個
# In[8]:
#getattr(dataset, 'feature')#從0~8437,總共8438個
# In[9]:
#getattr(dataset, 'user')#0~1800 總共1801個uid
# # initialize KG
# In[10]:
DatasetDict = {
LAST_FM: LastFmDataset,
LAST_FM_STAR: LastFmStarDataset,#here
YELP: YelpDataset,
YELP_STAR: YelpDataset
}
GraphDict = {
LAST_FM: LastFmGraph,
LAST_FM_STAR: LastFmGraph,
YELP: YelpGraph,
YELP_STAR: YelpGraph
}
# In[11]:
import os
import json
from easydict import EasyDict as edict
data_dir ="./data/lastfm_star"
data_dir
# In[12]:
class LastFmStarDataset(object):
def __init__(self, data_dir):
self.data_dir = data_dir + '/Graph_generate_data'
self.load_entities()
self.load_relations()
def get_relation(self):
# Entities
USER = 'user'
ITEM = 'item'
FEATURE = 'feature'
# Relations
INTERACT = 'interact'
FRIEND = 'friends'
LIKE = 'like'
BELONG_TO = 'belong_to'
relation_name = [INTERACT, FRIEND, LIKE, BELONG_TO]
fm_relation = {
USER: {#user 这个entity有三个relation,分别relation什么类型的entity
INTERACT: ITEM,
FRIEND: USER,
LIKE: FEATURE,
},
ITEM: {
BELONG_TO: FEATURE,
INTERACT: USER
},
FEATURE: {
LIKE: USER,
BELONG_TO: ITEM
}
}
fm_relation_link_entity_type = {#the keys are the relationship types and the values are lists representing the source entity and target entity respectively
INTERACT: [USER, ITEM],
FRIEND: [USER, USER],
LIKE: [USER, FEATURE],
BELONG_TO: [ITEM, FEATURE]
}
return fm_relation, relation_name, fm_relation_link_entity_type
def load_entities(self):
#load entities with LAST_FM_STAR DATA
entity_files = edict(
user='user_dict.json',
item='item_dict.json',
feature='original_tag_map.json',
)
for entity_name in entity_files:
with open(os.path.join(self.data_dir, entity_files[entity_name]), encoding='utf-8') as f:
mydict = json.load(f)
#user_dict
#friends refers to who are friends with the user .
#"like refers to the item that user like"
#the format:{"0":{"friends":[246, 382, 462, 676, 735],"like":[11,12,13,14,15]},"1":...}
#if entity_name == 'item':#{'0': {'feature_index': [69, 75]}, '1': {'feature_index': [74, 77, 80, 139, 706]}
#print(mydict)
if entity_name == 'feature':#Each feature is assigned a unique index
#print(mydict){'1': 0, '2': 1, '3': 2, '4': 3, '5': 4,...Each feature is assigned a unique index
entity_id = list(mydict.values())#[0, 1, 2, 3, 4, 5,... list of index
else:
#uid's attribute {'id': [0, 1, 2, 3, 4, 5, 6, 7,...]'value_len': 1801}
#iid"s attribute: {'id': [0, 1, 2, 3, 4,...],'value_len': 7432}
#feature's attribute :{'id': [0, 1, 2, 3, 4,...],'value_len': 8438}
entity_id = list(map(int, list(mydict.keys())))#[0, 1, 2, 3, 4, 5, ...]
setattr(self,entity_name, edict(id=entity_id, value_len=max(entity_id) + 1))
#print(getattr(self, entity_name))
print('Load', entity_name, 'of size', len(entity_id))
print(entity_name, 'of max id is', max(entity_id))
#用entity来建立relation
def load_relations(self):
"""
relation: head entity---> tail entity
--
"""
LastFm_relations = edict(#根据user_item_train.json 这个file进行配对
interact=('user_item_train.json', self.user, self.item), # (filename, head_entity, tail_entity) self.user and self.item is uid and iid attribute
friends=('user_dict.json', self.user, self.user),
like=('user_dict.json', self.user, self.feature),
belong_to=('item_dict.json', self.item, self.feature),
)
for name in LastFm_relations:
# Save tail_entity
relation = edict(
data=[],
)
#print(relation)
#{'data': []}
knowledge = [list([]) for i in range(LastFm_relations[name][1].value_len)] #create many list as the size of head_entity
with open(os.path.join(self.data_dir, LastFm_relations[name][0]), encoding='utf-8') as f:
mydict = json.load(f)
if name in ['interact']:#interact=('user_item_train.json', self.user, self.item)
#print(mydict) {'0': [5780, 5781, 5782, 5783, ...],"1":[154, 155, 156, 157,...],...}#user_item interaction for train data
for key, value in mydict.items():
head_id = int(key)
tail_ids = value
knowledge[head_id] = tail_ids#knowledge[0] 用index来代表 head user id,value是[item id]来代表interaction
elif name in ['friends']:#user 的friends是什么
#print(mydict) the data from friends=('user_dict.json'
#from friends like: {'0': {'friends': [246, 382, 462, ...],"like":[11, 12, 13, 14,...]},'1':...}
for key in mydict.keys():
head_str = key
head_id = int(key)
tail_ids = mydict[head_str][name]
knowledge[head_id] = tail_ids #index 是user head id ,value是user tail id(user head 的friends)
elif name in ['like']:#user 喜欢什么feature/attribute
#print(mydict) #the data from like=('user_dict.json'
#from like: {'0': {'friends': [246, 382, 462, ...],"like":[11, 12, 13, 14,...]},'1':...}
for key in mydict.keys():
head_str = key
head_id = int(key)
tail_ids = mydict[head_str][name]
knowledge[head_id] = tail_ids #index 是user head id ,value是user tail id(user head like的attribute)
elif name in ['belong_to']:#该item belongs to 什么feature
#print(mydict)#from {'0': {'feature_index': [69, 75]}, '1': {'feature_index': [74, 77, 80, 139, 706]},
for key in mydict.keys():
head_str = key
head_id = int(key)
tail_ids = mydict[head_str]['feature_index']
knowledge[head_id] = tail_ids#index 是item head id ,value是feature tail id
relation.data = knowledge
setattr(self, name, relation) #for example self.interact includes the {'data':[knowledge of interact]}
tuple_num = 0
for i in knowledge:
tuple_num += len(i)
print('Load', name, 'of size', tuple_num)
#now we have 4 attributes contains the relations of each entity
#print(self.interact) {'data': [[5780, 5781, 5782, 5783, ....
# In[13]:
data_name="LAST_FM_STAR"
dataset = load_dataset(data_name)
# In[14]:
kg = GraphDict[data_name](dataset)
# In[15]:
class LastFmGraph(object):
def __init__(self, dataset):
self.G = dict()
self._load_entities(dataset)
self._load_knowledge(dataset)
self._clean()
def _load_entities(self, dataset):
print('load entities...')
num_nodes = 0
#dataset is a instance of LastFmStarDataset in the file lastfm_star_data_process.py
data_relations, _, _ = dataset.get_relation() # entity_relations, relation_name, link_entity_type
#print(data_relations)# how head entity relate to tail entity {'user': {'interact': 'item', 'friends': 'user', 'like': 'feature'}, 'item': {'belong_to': 'feature', 'interact': 'user'}, 'feature': {'like': 'user', 'belong_to': 'item'}}
entity_list = list(data_relations.keys())#user item feature
for entity in entity_list:
self.G[entity] = {}# loop 完三次后:{'user': {}, 'item': {}, 'feature': {}}
entity_size = getattr(dataset, entity).value_len #等于self.user.value_len
for eid in range(entity_size):
entity_rela_list = data_relations[entity].keys()
self.G[entity][eid] = {r: [] for r in entity_rela_list}
#print(self.G) if entity is user:{'user': {0: {'interact': [], 'friends': [], 'like': []},1:...第一个dic的key是 entity type,value 是entity id,entity id的value则是relation type,relation type 的value是relation entities
num_nodes += entity_size
print('load entity:{:s} : Total {:d} nodes.'.format(entity, entity_size))
print('ALL total {:d} nodes.'.format(num_nodes))
print('===============END==============')
#这里是每个entity的{'user': {0: {'interact': [], 'friends': [], 'like': []},1:...},'item': {0: {'belong_to': [], 'interact': []}, 1:...},'feature': {0: {'like': [], 'belong_to': []}, 1: ...}}
#print(self.G)
def _load_knowledge(self, dataset):
#data_relations_name = relation_name = [INTERACT, FRIEND, LIKE, BELONG_TO]
#link_entity_type=fm_relation_link_entity_type = { #the keys are the relationship types and the values are lists representing the source entity and target entity respectively
#INTERACT: [USER, ITEM],
#FRIEND: [USER, USER],
#LIKE: [USER, FEATURE],
# BELONG_TO: [ITEM, FEATURE]}
_, data_relations_name, link_entity_type = dataset.get_relation()
for relation in data_relations_name:
print('Load knowledge {}...'.format(relation))
data = getattr(dataset, relation).data
num_edges = 0
for he_id, te_ids in enumerate(data):# head_entity_id 取出他的index,也就是head id, tail_entity_ids
if len(te_ids) <= 0:
continue
e_head_type = link_entity_type[relation][0] #找出link_entity_type中的head type and tail type :{user item feature}
e_tail_type = link_entity_type[relation][1]
for te_id in set(te_ids):
self._add_edge(e_head_type, he_id, relation, e_tail_type, te_id)#填入这些slot{'user': {0: {'interact': [], 'friends': [], 'like': []},1:...},'item': {0: {'belong_to': [], 'interact': []}, 1:...},'feature': {0: {'like': [], 'belong_to': []}, 1: ...}}
num_edges += 2
print('Total {:d} {:s} edges.'.format(num_edges, relation))
print('===============END==============')
#KG最终形态!!!!!!!!print(self.G)#{'user': {0: {'interact': [6663, 6664, 6666, 6667,
def _add_edge(self, etype1, eid1, relation, etype2, eid2):
self.G[etype1][eid1][relation].append(eid2)
self.G[etype2][eid2][relation].append(eid1)
def _clean(self):
print('Remove duplicates...')
for etype in self.G:
for eid in self.G[etype]:
for r in self.G[etype][eid]:
data = self.G[etype][eid][r]
data = tuple(sorted(set(data)))
self.G[etype][eid][r] = data
# In[16]:
abc=LastFmGraph(dataset)
# In[ ]:
def predict_feature(model,user_output, given_preference, to_test):#user_output = user id
user_emb = model.user_emb(torch.LongTensor([0]))[..., :-1].detach().numpy() #user_output = user id
#print("torch.LongTensor([0]) shape:",torch.LongTensor([0]).shape)
#print("user_emb shape",user_emb.shape)
#print("given_preference",given_preference)
gp = model.feature_emb(torch.LongTensor(given_preference))[..., :-1].detach().numpy()
#print("torch.LongTensor(given_preference) shape",torch.LongTensor(given_preference).shape)
#print("gp shape",gp.shape)
emb_weight = model.feature_emb.weight[..., :-1].detach().numpy()
#print("emb_weight.shape",emb_weight.shape)
result = list()
for test_feature in to_test:#剩余的那些没有interact过并且不讨厌的feature进行计算
temp = 0
temp += np.inner(user_emb, emb_weight[test_feature])#user 跟residual 的相似度
for i in range(gp.shape[0]):
temp += np.inner(gp[i], emb_weight[test_feature])#有过interaction的feature跟residual的相似度
result.append(temp)
return result
def topk(y_true, pred, k):
y_true_ = y_true[:k]
pred_ = pred[:k]
if sum(y_true_) == 0:
return 0
else:
return roc_auc_score(y_true, pred) if len(np.unique(y_true)) > 1 else None
def rank_by_batch(uid,kg, pickle_file, iter_, bs, pickle_file_length, model, PAD_IDX1, PAD_IDX2, user_length, feature_length, data_name, ITEM, ITEM_FEATURE):#要用full feature
'''
user_output, item_p_output, i_neg2_output, preference_list = list(), list(), list(), list()
'''
left, right = iter_ * bs, min(pickle_file_length, (iter_ + 1) * bs)
I = pickle_file[0][left:right]
II = pickle_file[1][left:right]
III = pickle_file[2][left:right]
IV = pickle_file[3][left:right]
i = 0
index_none = list()
for user_output, item_p_output, i_neg2_output, preference_list in zip(I, II, III, IV):
if i_neg2_output is None or len(i_neg2_output) == 0:
index_none.append(i)
i += 1
i = 0
result_list = list()
for user_output, item_p_output, i_neg2_output, preference_list in zip(I, II, III, IV):#user_output = user id 每一个user都做一次
if i in index_none:
i += 1
continue
#addition
if user_output == uid:
#print("user_output",user_output)
full_feature = kg.G[ITEM][item_p_output][ITEM_FEATURE]
preference_feature = preference_list
residual_preference = list(set(full_feature) - set(preference_feature))
residual_feature_all = list(set(list(range(feature_length - 1))) - set(full_feature))
if len(residual_preference) == 0:
continue
to_test = residual_feature_all + residual_preference
predictions = predict_feature(model, user_output, preference_feature, to_test)#user_output = user id
predictions = np.array(predictions)
#print("predictions:",predictions)
#print("=====================")
predictions = predictions.reshape((len(to_test), 1)[0])#剩余的那些没有interact过并且不讨厌的feature的分数(to test)
#print("predictions=predictions.reshape((len(to_test), 1)[0])",predictions)
y_true = [0] * len(predictions)
for i in range(len(residual_preference)):
y_true[-(i + 1)] = 1
tmp = list(zip(y_true, predictions))
#print("tmp1=list(zip(y_true, predictions))",tmp)
tmp = sorted(tmp, key=lambda x: x[1], reverse=True)
#print("tmp2=sorted(tmp, key=lambda x: x[1], reverse=True)",tmp)
y_true, predictions = zip(*tmp)
#print("y_true",y_true)
#print(" predictions", predictions)
icon = []
for index, item in enumerate(y_true):
if item > 0:
icon.append(index)
auc = roc_auc_score(y_true, predictions) if len(np.unique(y_true)) > 1 else None
#print("uid",uid)
#print("epoch",epoch)
#print("auc",auc)
result_list.append((uid,auc, topk(y_true, predictions, 10), topk(y_true, predictions, 50)
, topk(y_true, predictions, 100), topk(y_true, predictions, 200),
topk(y_true, predictions, 500), len(predictions)))
i += 1
else:
continue
#print("result_list",result_list)
return result_list
def evaluate_feature(kg, model,uid,filename, PAD_IDX1, PAD_IDX2, user_length, feature_length, data_name, ITEM, ITEM_FEATURE):#kg, model, epoch(I set 2), filename, PAD_IDX1(9233), PAD_IDX2(8438), user_length(1801), feature_length(8438), data_name(LAST_FM_STAR), ITEM("item"), ITEM_FEATURE("'belong_to'")
# TODO add const PAD_IDX1, PAD_IDX2, user_length, data_name, ITEM, ITEM_FEATURE
model.eval()
model.cpu()
tt = time.time()
pickle_file = load_fm_sample(dataset=data_name, mode='valid')#this function is in utils.py,set mode to valid means validation set
print('Open evaluation pickle file: takes {} seconds, evaluation length: {}'.format(time.time() - tt, len(pickle_file[0])))
pickle_file_length = len(pickle_file[0])
start = time.time()
print('Starting uid :{} '.format(uid))
bs = 64
max_iter = int(pickle_file_length / float(bs))
max_iter = 100
result = list()
print('max_iter-----------', max_iter)#100个batch
for iter_ in range(max_iter):
if iter_ > 1 and iter_ % 20 == 0:
print('--')
print('Takes {} seconds to finish {}% of this task'.format(str(time.time() - start),
float(iter_) * 100 / max_iter))
result += rank_by_batch(uid,kg, pickle_file, iter_, bs, pickle_file_length, model, PAD_IDX1, PAD_IDX2, user_length, feature_length, data_name, ITEM, ITEM_FEATURE)
auc_mean = np.mean(np.array([item[1] for item in result if item[1] is not None]))
auc_median = np.median(np.array([item[1] for item in result if item[1] is not None]))
print("epoch",epoch)
print("uid",uid)
print('feature auc mean: {}'.format(auc_mean), 'feature auc median: {}'.format(auc_median))
PATH = TMP_DIR[data_name] + '/FM-log-merge/' + filename + '.txt'
if not os.path.isdir(TMP_DIR[data_name] + '/FM-log-merge/'):
os.makedirs(TMP_DIR[data_name] + '/FM-log-merge/')
with open(PATH, 'a') as f:
with open(PATH, 'a') as f:
f.write('validating uid {} on feature prediction\n'.format(uid))
auc_mean = np.mean(np.array([item[1] for item in result if item[1] is not None]))
auc_median = np.median(np.array([item[1] for item in result if item[1] is not None]))
f.write('feature auc mean: {}\n'.format(auc_mean))
f.write('feature auc median: {}\n'.format(auc_median))
f.flush()
model.train()
cuda_(model)
#item evaluation
from sklearn.metrics import roc_auc_score
from utils import *
import time
from torch.nn.utils.rnn import pad_sequence
def topk1(y_true, pred, k):
y_true_ = y_true[:k]
pred_ = pred[:k]
#print("Type of y_true:", type(y_true))
#print("Content of y_true:", y_true)
#print("y_true length:", len(y_true))
#print("pred_ len:", len(pred_))
#print("Content of pred:", pred)
if sum(y_true_) == 0:
return 0
else:
return roc_auc_score(y_true_, pred_) if len(np.unique(y_true)) > 1 else None
def rank_by_batch1(kg,uid, items_emb,feature_emb,pickle_file, iter_, bs, pickle_file_length, model, rd, PAD_IDX1, PAD_IDX2, user_length, feature_length, data_name, ITEM, ITEM_FEATURE):
'''
user_output, item_p_output, i_neg2_output, preference_list = list(), list(), list(), list()
'''
left, right = iter_ * bs, min(pickle_file_length, (iter_ + 1) * bs)
I = pickle_file[0][left:right]
II = pickle_file[1][left:right]
III = pickle_file[2][left:right]
IV = pickle_file[3][left:right]
i = 0
index_none = list()
for user_output, item_p_output, i_neg2_output, preference_list in zip(I, II, III, IV):
if i_neg2_output is None or len(i_neg2_output) == 0:
index_none.append(i)
i += 1
i = 0
result_list = list()
for user_output, item_p_output, i_neg2_output, preference_list in zip(I, II, III, IV):
if i in index_none:
i += 1
continue
if user_output == uid:
total_list = list(i_neg2_output)[: 1000] + [item_p_output]
user_input = [user_output] * len(total_list)
pos_list, pos_list2 = list(), list()
cumu_length = 0
for instance in zip(user_input, total_list):
new_list = list()
new_list.append(instance[0])
new_list.append(instance[1] + user_length)
pos_list.append(torch.LongTensor(new_list))
f = kg.G[ITEM][instance[1]][ITEM_FEATURE]
if rd == 1:
f = list(set(f) - set(preference_list))
cumu_length += len(f)
pos_list2.append(torch.LongTensor(f))
if cumu_length == 0:
pass
pos_list = pad_sequence(pos_list, batch_first=True, padding_value=PAD_IDX1)
prefer_list = torch.LongTensor(preference_list).expand(len(total_list), len(preference_list))
if cumu_length != 0:
pos_list2.sort(key=lambda x: -1 * x.shape[0])
pos_list2 = pad_sequence(pos_list2, batch_first=True, padding_value=PAD_IDX2)
else:
pos_list2 = torch.LongTensor([PAD_IDX2]).expand(pos_list.shape[0], 1)
#for pos,pos2,pre in zip(pos_list, pos_list2, prefer_list):
#if pos[0] == uid:
#print("cuda_(pos_list).shape",cuda_(pos_list).unsqueeze(0).shape)
#print("cuda_(pos_list)",cuda_(pos_list))
#print("cuda_(pos_list2).unsqueeze(0).shape",cuda_(pos_list2).unsqueeze(0).shape)
#print("cuda_(prefer_list).unsqueeze(0)",cuda_(prefer_list).unsqueeze(0).shape)
#print("cuda_(prefer_list)",cuda_(prefer_list))
#print("len(prefer_list)",len(prefer_list))
#predictions, _, _ = model(items_emb,feature_emb,cuda_(pos).unsqueeze(0), cuda_(pos2).unsqueeze(0), cuda_(pre).unsqueeze(0))
#print("cuda_(prefer_list).shape",cuda_(prefer_list).unsqueeze(0).shape)
#print("cuda_(pos_list).shape",cuda_(pos_list).unsqueeze(0).shape)
#print("cuda_(pos_list)",cuda_(pos_list).unsqueeze(0))
predictions_list=[]
for pos,pos2,pre in zip(pos_list,pos_list2,prefer_list):
if pos[0] == uid:
predictions, _, _ = model(items_emb,feature_emb,cuda_(pos).unsqueeze(0), cuda_(pos2).unsqueeze(0), cuda_(pre).unsqueeze(0))
predictions_list.append(predictions)
else:
#print("uid not exist in this batch")
continue
predictions = torch.cat(predictions_list, dim=0)
#print("predictions.shape",predictions.shape)
#print("predictions",predictions)
predictions = predictions.detach().cpu().numpy()
mini_gtitems = [item_p_output]
num_gt = len(mini_gtitems)
num_neg = len(total_list) - num_gt
#print("Shape of predictions:", predictions.shape)
#print("num_neg:", num_neg)
#print(predictions)
predictions = predictions.reshape((num_neg + 1, 1)[0])
#print("reshape prediction:",predictions.shape)
y_true = [0] * len(predictions)
#print("y_true = [0] * len(predictions)",y_true)
y_true[-1] = 1
#print("y_true[-1] = 1",y_true)
tmp = list(zip(y_true, predictions))
#print("tmp = list(zip(y_true, predictions))",tmp)
tmp = sorted(tmp, key=lambda x: x[1], reverse=True)
#print("tmp = sorted(tmp, key=lambda x: x[1], reverse=True)",tmp)
y_true, predictions = zip(*tmp)
#print("y_true, predictions = zip(*tmp)",y_true,predictions)
auc = roc_auc_score(y_true, predictions) if len(np.unique(y_true)) > 1 else None
#if auc == None
#print("auc",auc)
result_list.append((uid,auc, topk1(y_true, predictions, 10), topk1(y_true, predictions, 50)
, topk1(y_true, predictions, 100), topk1(y_true, predictions, 200),
topk1(y_true, predictions, 500), len(predictions)))
#print("result_list",result_list)
#print("finish++++++++++++++++++++++++++++++++++++++++++++++++++++")
i += 1
else:
#print("uid not exist in this iteration")
continue
return result_list
def evaluate_item(kg,items_emb,feature_emb ,client_dic, epoch, filename, rd, PAD_IDX1, PAD_IDX2, user_length, feature_length, data_name, ITEM, ITEM_FEATURE):
#TODO add const PAD_IDX1, PAD_IDX2, user_length, data_name, ITEM, ITEM_FEATURE
model.eval()
tt = time.time()
pickle_file = load_fm_sample(dataset=data_name, mode='valid')
print('evaluate data:{}'.format(data_name))
print('Open evaluation pickle file: takes {} seconds, evaluation length: {}'.format(time.time() - tt, len(pickle_file[0])))
pickle_file_length = len(pickle_file[0])
print('ui length:{}'.format(pickle_file_length))
start = time.time()
#print('Starting {} epoch'.format(epoch))
bs = 64
max_iter = int(pickle_file_length / float(bs))
# Only do 20 iteration for the sake of time
max_iter = 20
result = list()
for iter_ in range(max_iter):
if iter_ > 1 and iter_ % 50 == 0:
print('--')
print('Takes {} seconds to finish {}% of this task'.format(str(time.time() - start),
float(iter_) * 100 / max_iter))
result += rank_by_batch1(kg,user_id,server.items_emb,server.feature_emb, pickle_file, iter_, bs, pickle_file_length, client_dic, rd, PAD_IDX1, PAD_IDX2, user_length, feature_length, data_name, ITEM, ITEM_FEATURE)
#print(result)
#for i in result:
#print("item",i)
auc_mean = np.mean(np.array([item[1] for item in result if item[1] is not None]))
auc_median = np.median(np.array([item[1] for item in result if item[1] is not None]))
print("epoch",epoch)
print("uid",user_id)
print('item auc mean: {}'.format(auc_mean), 'item auc median: {}'.format(auc_median),
'over num {}'.format(len(result)))
PATH = TMP_DIR[data_name] + '/FM-log-merge/' + filename + '.txt'
if not os.path.isdir(TMP_DIR[data_name] + '/FM-log-merge/'):
os.makedirs(TMP_DIR[data_name] + '/FM-log-merge/')
with open(PATH, 'a') as f:
f.write('validating uid {} on item prediction\n'.format(user_id))
auc_mean = np.mean(np.array([item[1] for item in result if item[1] is not None]))
auc_median = np.median(np.array([item[1] for item in result if item[1] is not None]))
f.write('item auc mean: {}\n'.format(auc_mean))
f.write('item auc median: {}\n'.format(auc_median))
model.train()
cuda_(model)
#rec evaluation
import torch
import numpy as np
def evaluate_recall(rating, ground_truth, top_k):
top_k_items = sorted(rating, key=rating.get, reverse=True)[:top_k]
#print("top_k_items",top_k_items)
#print("ground_truth",ground_truth)
hit = 0
for i in top_k_items:
if i in ground_truth:
hit += 1
recall = hit / len(ground_truth)
return recall
def evaluate_ndcg(rating, ground_truth, top_k):
top_k_items = sorted(rating, key=rating.get, reverse=True)[:top_k]
#top_k = min(top_k, torch.tensor(rating).shape[0])
#_, rating_k = torch.topk(torch.tensor(rating), top_k,dim=0)
#rating_k = rating_k.cpu().tolist()
dcg, idcg = 0., 0.
for i, v in enumerate(top_k_items):
if i < len(ground_truth):
idcg += (1 / np.log2(2 + i))
if v in ground_truth:
dcg += (1 / np.log2(2 + i))
ndcg = dcg / idcg
return ndcg
# RecServer
# In[17]:
class FedRecServer(nn.Module):#initialized item embedding
def __init__(self, emb_size, user_length, item_length, feature_length, qonly, hs, ip, dr):#3706,32
super().__init__()
self.user_length = user_length#get from global
self.item_length = item_length
self.feature_length = feature_length
self.hs = hs
self.ip = ip
self.dr = dr
#不用
self.qonly = qonly # only use quadratic form
# dimensions
self.emb_size = emb_size
self.items_emb = nn.Embedding(self.item_length, self.hs+1).to(device)
nn.init.normal_(self.items_emb.weight, std=0.01)
self.feature_emb = nn.Embedding(self.feature_length+1, self.hs + 1, padding_idx=self.feature_length, sparse=False).to(device)
self.feature_emb.weight.data.normal_(0,self.ip)
# _______ set the padding to zero _______
self.feature_emb.weight.data[feature_length,:] = 0
def train_(self,epoch, client,pos_list, pos_list2, neg_list, neg_list2, new_neg_list, new_neg_list2, preference_list_1, preference_list_new, index_none, residual_feature, neg_feature):#clients以及他们的index放进去
#self.pos_list = pos_list
#self.pos_list2 = pos_list2
#self.neg_list = neg_list
#self.neg_list2 = neg_list2
#self.new_neg_list=new_neg_list
#self.new_neg_list2=new_neg_list2
#self.preference_list_1=preference_list_1
#self.preference_list_new = preference_list_new
#self.index_none=index_none
#self.residual_feature=residual_feature
#self.neg_feature=neg_feature
batch_items_emb_grad = torch.zeros_like(self.items_emb.weight)#initialize the gradient value of item embedding
batch_feature_emb_grad = torch.zeros_like(self.feature_emb.weight)
batch_loss=0.
batch_loss2=0.
for i in range(len(pos_list)):
user_id = pos_list[i][0].item()
items_emb_grad,feature_emb_grad, loss, loss_2,result_pos,result_neg=client[user_id].train_(self.items_emb,self.feature_emb,pos_list[i], pos_list2[i], neg_list[i], neg_list2[i], new_neg_list[i], new_neg_list2[i],preference_list_1[i], preference_list_new[i], index_none, residual_feature[i],neg_feature[i])
batch_loss+=loss
batch_loss2+=loss_2
with torch.no_grad():
norm = items_emb_grad.norm(2, dim=-1, keepdim=True)
too_large = norm[:, 0] > grad_limit#1
items_emb_grad[too_large] /= (norm[too_large] / grad_limit)#gradient clipping step
batch_items_emb_grad += items_emb_grad
norm1 = feature_emb_grad.norm(2, dim=-1, keepdim=True)
too_large1 = norm1[:, 0] > grad_limit#1
feature_emb_grad[too_large1] /= (norm1[too_large1] / grad_limit)#gradient clipping step
batch_feature_emb_grad += feature_emb_grad
with torch.no_grad():
self.items_emb.weight.data.add_(batch_items_emb_grad, alpha=-lr)#update 最后的item embedding weight
self.feature_emb.weight.data.add_(batch_feature_emb_grad, alpha=-lr)
#if target_results is not None and target_cnt != 0:
#return batch_loss, batch_loss2, target_results / target_cnt
#else:
return batch_loss, batch_loss2
# # FedClients
# In[18]:
class FedRecClient(nn.Module):#为了学好user embedding(但只储存在此处),item embedding的weight可以上传server
def __init__(self,emb_size, user_length, item_length, feature_length, qonly, hs, ip, dr):
super(FedRecClient, self).__init__()#super().__init__()
self.items_emb_grad = None
self.model=FactorizationMachine(emb_size=hs, user_length=user_length, item_length=item_length,feature_length=feature_length, qonly=qonly, hs=hs, ip=ip, dr=dr).to(device)
def train_(self,items_emb,feature_emb,pos_list, pos_list2, neg_list, neg_list2, new_neg_list, new_neg_list2, preference_list_1, preference_list_new, index_none, residual_feature, neg_feature):#要修改compute,在最后要把item embedding 独立出来,然后再return给server
self.pos_list = pos_list
self.pos_list2 = pos_list2
self.neg_list = neg_list
self.neg_list2 = neg_list2
self.new_neg_list=new_neg_list
self.new_neg_list2=new_neg_list2
self.preference_list_1=preference_list_1
self.preference_list_new = preference_list_new
self.index_none=index_none
self.residual_feature=residual_feature
self.neg_feature=neg_feature
self.items_emb=items_emb
self.feature_emb=feature_emb
#reset the gradient
self.model.zero_grad()
result_pos, feature_bias_matrix_pos, nonzero_matrix_pos = self.model(self.items_emb,self.feature_emb,self.pos_list.unsqueeze(0),self.pos_list2.unsqueeze(0),self.preference_list_1.unsqueeze(0)) # (bs, 1), (bs, 2, 1), (bs, 2, emb_size)
result_neg, feature_bias_matrix_neg, nonzero_matrix_neg = self.model(self.items_emb,self.feature_emb,self.neg_list.unsqueeze(0), self.neg_list2.unsqueeze(0), self.preference_list_1.unsqueeze(0))
if self.model.items_emb.weight.grad is not None:
self.model.items_emb.weight.grad.zero_()
diff = (result_pos - result_neg)
loss = - lsigmoid(diff).sum(dim=0)
if command in [8]:
# The second type of negative sample
new_result_neg, new_feature_bias_matrix_neg, new_nonzero_matrix_neg = self.model(self.items_emb,self.feature_emb,self.new_neg_list.unsqueeze(0), self.new_neg_list2.unsqueeze(0),
self.preference_list_new.unsqueeze(0))
# Reason for this is that, sometimes the sample is missing, so we have to also omit that in result_pos
T = cuda_(torch.tensor([]))
for i in range(1):
if i in index_none:
continue
T = torch.cat([T, result_pos[i]], dim=0)
T = T.view(T.shape[0], -1)
assert T.shape[0] == new_result_neg.shape[0]
diff = T - new_result_neg
if loss is not None:
loss += - lsigmoid(diff).sum(dim=0)
else:
loss = - lsigmoid(diff).sum(dim=0)
# regularization
if reg_float != 0:
if qonly != 1:
feature_bias_matrix_pos_ = (feature_bias_matrix_pos ** 2).sum(dim=1) # (bs, 1)
feature_bias_matrix_neg_ = (feature_bias_matrix_neg ** 2).sum(dim=1) # (bs, 1)
nonzero_matrix_pos_ = (nonzero_matrix_pos ** 2).sum(dim=2).sum(dim=1, keepdim=True) # (bs, 1)
nonzero_matrix_neg_ = (nonzero_matrix_neg ** 2).sum(dim=2).sum(dim=1, keepdim=True) # (bs, 1)
new_nonzero_matrix_neg_ = (new_nonzero_matrix_neg_ ** 2).sum(dim=2).sum(dim=1, keepdim=True)
regular_norm = (
feature_bias_matrix_pos_ + feature_bias_matrix_neg_ + nonzero_matrix_pos_ + nonzero_matrix_neg_ + new_nonzero_matrix_neg_)
loss += (reg * regular_norm).sum(dim=0)
else:
nonzero_matrix_pos_ = (nonzero_matrix_pos ** 2).sum(dim=2).sum(dim=1, keepdim=True)
nonzero_matrix_neg_ = (nonzero_matrix_neg ** 2).sum(dim=2).sum(dim=1, keepdim=True)
loss += (reg * nonzero_matrix_pos_).sum(dim=0)
loss += (reg * nonzero_matrix_neg_).sum(dim=0)
loss1=loss.data
loss.backward()
user_emb_grad = self.model.user_emb.weight.grad
self.model.user_emb.weight.data.add_(user_emb_grad, alpha=-lr)
bias_grad = self.model.Bias.grad
self.model.Bias.data.add_(bias_grad, alpha=-lr)
self.items_emb_grad = self.model.items_emb.weight.grad# stores the gradient computed for the item embeddings into the self.items_emb_grad attribute of the client
if uf == 1:
# updating feature embedding
# we try to optimize
A = self.model.feature_emb(preference_list_1[0].unsqueeze(0)).unsqueeze(0)[..., :-1]
#print("A Shape",A.shape)
#print("=================================================================")
user_emb = self.model.ui_emb[0][0].unsqueeze(0)[..., :-1].unsqueeze(dim=1).detach()
#print("=================================================================")
if useremb == 1:
A = torch.cat([A, user_emb], dim=1)
B = self.model.feature_emb(residual_feature.unsqueeze(0))[..., :-1]
C = self.model.feature_emb(neg_feature.unsqueeze(0))[..., :-1]
D = torch.matmul(A, B.transpose(2, 1))
E = torch.matmul(A, C.transpose(2, 1))
p_vs_residual = D.view(D.shape[0], -1, 1)
p_vs_neg = E.view(E.shape[0], -1, 1)