forked from orcax/PGPR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
297 lines (241 loc) · 8.38 KB
/
utils.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
from __future__ import absolute_import, division, print_function
import logging
import logging.handlers
import math
import pickle
import random
import sys
import numpy as np
import scipy.sparse as sp
import torch
from collections import defaultdict
from sklearn.feature_extraction.text import TfidfTransformer
# Dataset names.
BEAUTY = 'beauty'
CELL = 'cell'
CLOTH = 'cloth'
CD = 'cd'
# Dataset directories.
DATASET_DIR = {
BEAUTY: './data/Amazon_Beauty',
CELL: './data/Amazon_Cellphones',
CLOTH: './data/Amazon_Clothing',
CD: './data/Amazon_CDs',
}
# Model result directories.
TMP_DIR = {
BEAUTY: './tmp/Amazon_Beauty',
CELL: './tmp/Amazon_Cellphones',
CLOTH: './tmp/Amazon_Clothing',
CD: './tmp/Amazon_CDs',
}
# Label files.
LABELS = {
BEAUTY: (TMP_DIR[BEAUTY] + '/train_label.pkl', TMP_DIR[BEAUTY] + '/test_label.pkl'),
CLOTH: (TMP_DIR[CLOTH] + '/train_label.pkl', TMP_DIR[CLOTH] + '/test_label.pkl'),
CELL: (TMP_DIR[CELL] + '/train_label.pkl', TMP_DIR[CELL] + '/test_label.pkl'),
CD: (TMP_DIR[CD] + '/train_label.pkl', TMP_DIR[CD] + '/test_label.pkl')
}
# Brand Popularity files.
BRAND_FILE = {
BEAUTY: './brand_data/pid_fairness_beauty.pickle',
CELL: './brand_data/pid_fairness_cell.pickle',
CLOTH: './brand_data/brand_cloth_2014.pickle',
CD: './brand_data/pid_fairness_cd.pickle',
}
# Entities
USER = 'user'
PRODUCT = 'product'
WORD = 'word'
RPRODUCT = 'related_product'
BRAND = 'brand'
CATEGORY = 'category'
# Relations
PURCHASE = 'purchase'
MENTION = 'mentions'
DESCRIBED_AS = 'described_as'
PRODUCED_BY = 'produced_by'
BELONG_TO = 'belongs_to'
ALSO_BOUGHT = 'also_bought'
ALSO_VIEWED = 'also_viewed'
BOUGHT_TOGETHER = 'bought_together'
SELF_LOOP = 'self_loop' # only for kg env
KG_RELATION = {
USER: {
PURCHASE: PRODUCT,
MENTION: WORD,
},
WORD: {
MENTION: USER,
DESCRIBED_AS: PRODUCT,
},
PRODUCT: {
PURCHASE: USER,
DESCRIBED_AS: WORD,
PRODUCED_BY: BRAND,
BELONG_TO: CATEGORY,
ALSO_BOUGHT: RPRODUCT,
ALSO_VIEWED: RPRODUCT,
BOUGHT_TOGETHER: RPRODUCT,
},
BRAND: {
PRODUCED_BY: PRODUCT,
},
CATEGORY: {
BELONG_TO: PRODUCT,
},
RPRODUCT: {
ALSO_BOUGHT: PRODUCT,
ALSO_VIEWED: PRODUCT,
BOUGHT_TOGETHER: PRODUCT,
}
}
PATH_PATTERN = {
# length = 3
#############
# Paths starting with the User
1: ((None, USER), (MENTION, WORD), (DESCRIBED_AS, PRODUCT)),
# Paths starting with the Product
2: ((None, PRODUCT), (DESCRIBED_AS, WORD), (MENTION, USER)),
# length = 4
#############
# Paths starting with the User
11: ((None, USER), (PURCHASE, PRODUCT), (PURCHASE, USER), (PURCHASE, PRODUCT)),
12: ((None, USER), (PURCHASE, PRODUCT), (DESCRIBED_AS, WORD), (DESCRIBED_AS, PRODUCT)),
13: ((None, USER), (PURCHASE, PRODUCT), (PRODUCED_BY, BRAND), (PRODUCED_BY, PRODUCT)),
14: ((None, USER), (PURCHASE, PRODUCT), (BELONG_TO, CATEGORY), (BELONG_TO, PRODUCT)),
15: ((None, USER), (PURCHASE, PRODUCT), (ALSO_BOUGHT, RPRODUCT), (ALSO_BOUGHT, PRODUCT)),
16: ((None, USER), (PURCHASE, PRODUCT), (ALSO_VIEWED, RPRODUCT), (ALSO_VIEWED, PRODUCT)),
17: ((None, USER), (PURCHASE, PRODUCT), (BOUGHT_TOGETHER, RPRODUCT), (BOUGHT_TOGETHER, PRODUCT)),
18: ((None, USER), (MENTION, WORD), (MENTION, USER), (PURCHASE, PRODUCT)),
# Paths starting with the Product
21: ((None, PRODUCT), (PURCHASE, USER), (PURCHASE, PRODUCT), (PURCHASE, USER)),
22: ((None, PRODUCT), (DESCRIBED_AS, WORD), (DESCRIBED_AS, PRODUCT), (PURCHASE, USER)),
23: ((None, PRODUCT), (PRODUCED_BY, BRAND), (PRODUCED_BY, PRODUCT), (PURCHASE, USER)),
24: ((None, PRODUCT), (BELONG_TO, CATEGORY), (BELONG_TO, PRODUCT), (PURCHASE, USER)),
25: ((None, PRODUCT), (ALSO_BOUGHT, RPRODUCT), (ALSO_BOUGHT, PRODUCT), (PURCHASE, USER)),
26: ((None, PRODUCT), (ALSO_VIEWED, RPRODUCT), (ALSO_VIEWED, PRODUCT), (PURCHASE, USER)),
27: ((None, PRODUCT), (BOUGHT_TOGETHER, RPRODUCT), (BOUGHT_TOGETHER, PRODUCT), (PURCHASE, USER)),
28: ((None, PRODUCT), (PURCHASE, USER), (MENTION, WORD), (MENTION, USER)),
}
def get_entities():
return list(KG_RELATION.keys())
def get_relations(entity_head):
return list(KG_RELATION[entity_head].keys())
def get_entity_tail(entity_head, relation):
return KG_RELATION[entity_head][relation]
def compute_tfidf_fast(vocab, docs):
"""Compute TFIDF scores for all vocabs.
Args:
docs: list of list of integers, e.g. [[0,0,1], [1,2,0,1]]
Returns:
sp.csr_matrix, [num_docs, num_vocab]
"""
# (1) Compute term frequency in each doc.
data, indices, indptr = [], [], [0]
for d in docs:
term_count = {}
for term_idx in d:
if term_idx not in term_count:
term_count[term_idx] = 1
else:
term_count[term_idx] += 1
indices.extend(term_count.keys())
data.extend(term_count.values())
indptr.append(len(indices))
tf = sp.csr_matrix((data, indices, indptr), dtype=int, shape=(len(docs), len(vocab)))
# (2) Compute normalized tfidf for each term/doc.
transformer = TfidfTransformer(smooth_idf=True)
tfidf = transformer.fit_transform(tf)
return tfidf
def get_logger(logname):
logger = logging.getLogger(logname)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('[%(levelname)s] %(message)s')
ch = logging.StreamHandler(sys.stdout)
ch.setFormatter(formatter)
logger.addHandler(ch)
fh = logging.handlers.RotatingFileHandler(logname, mode='w')
fh.setFormatter(formatter)
logger.addHandler(fh)
return logger
def set_random_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
def save_dataset(dataset, dataset_obj):
dataset_file = TMP_DIR[dataset] + '/dataset.pkl'
with open(dataset_file, 'wb') as f:
pickle.dump(dataset_obj, f)
def load_dataset(dataset):
dataset_file = TMP_DIR[dataset] + '/dataset.pkl'
dataset_obj = pickle.load(open(dataset_file, 'rb'))
return dataset_obj
def save_labels(dataset, labels, mode='train'):
if mode == 'train':
label_file = LABELS[dataset][0]
elif mode == 'test':
label_file = LABELS[dataset][1]
else:
raise Exception('mode should be one of {train, test}.')
with open(label_file, 'wb') as f:
pickle.dump(labels, f)
def load_labels(dataset, mode='train'):
if mode == 'train':
label_file = LABELS[dataset][0]
elif mode == 'test':
label_file = LABELS[dataset][1]
else:
raise Exception('mode should be one of {train, test}.')
user_products = pickle.load(open(label_file, 'rb'))
return user_products
def save_embed(dataset, embed):
embed_file = '{}/transe_embed.pkl'.format(TMP_DIR[dataset])
pickle.dump(embed, open(embed_file, 'wb'))
def load_embed(dataset):
embed_file = '{}/transe_embed.pkl'.format(TMP_DIR[dataset])
print('Load embedding:', embed_file)
embed = pickle.load(open(embed_file, 'rb'))
return embed
def save_kg(dataset, kg):
kg_file = TMP_DIR[dataset] + '/kg.pkl'
pickle.dump(kg, open(kg_file, 'wb'))
def load_kg(dataset):
kg_file = TMP_DIR[dataset] + '/kg.pkl'
kg = pickle.load(open(kg_file, 'rb'))
return kg
def invert_labels(labels):
"""
Inverts a mapping of user:[products] to product:[users] and vice versa
:param labels: dictionary of user:[products] (or vice versa)
:return: inverted dictionary of product:[users] (or vice versa)
"""
inv_labels = defaultdict(set)
for k, vs in labels.items():
for v in vs:
inv_labels[v].add(k)
return inv_labels
def represents_int(s):
try:
int(s)
return True
except ValueError:
return False
def calculate_fairness(item_predictions, fairness_dict):
fairness_score = 0
num_items = 0
for pid in item_predictions:
if pid in fairness_dict:
if fairness_dict[pid] > 10: # there are some corrupt values
continue
fairness_score += math.sqrt(fairness_dict[pid])
num_items += 1
if num_items == 0:
return 0
denominator = float(num_items - math.sqrt(num_items))
if denominator <= 0:
denominator = 1
fairness_score = float(fairness_score - math.sqrt(num_items)) / denominator
return fairness_score