-
Notifications
You must be signed in to change notification settings - Fork 0
/
cold_start_experiment.py
164 lines (142 loc) · 7.9 KB
/
cold_start_experiment.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
import os
import torch
import random
import pandas as pd
import numpy as np
from utils import get_dataset, copy_dataset, add_self_relation
def run_once(args_dict:dict, dst_dataset:str, seed=0):
# Environment settings
dataset_str : str = args_dict['dataset']
test_type : str = args_dict['test_type']
rate : float = args_dict['rate']
model_type_str : str = args_dict['model_type_str']
topk : int = args_dict['topk']
all_test_users : list = args_dict['all_test_users']
cs_threshold : int = args_dict['cs_threshold']
random.seed(seed)
torch.random.manual_seed(seed)
config_dict = {'seed':seed, 'gpu_id':tuple(range(torch.cuda.device_count())),
'topk':topk, 'checkpoint_dir':'temp/'}
dataset = get_dataset(config_dict, dataset_str, model_type_str)
type_dict = {'user_id':'token', 'item_id':'token', 'rating':'float', 'timestamp':'float'}
# Split the inter file into two files
src_path = os.path.join('./dataset/', dataset_str)
temp_path = os.path.join('./dataset/', dst_dataset)
copy_dataset(dataset_str, dst_dataset)
test_df = pd.DataFrame([], columns=dataset.inter_feat.columns)
train_df = dataset.inter_feat.copy()
test_user_list = all_test_users[seed]
for user in test_user_list:
user_inter = dataset.inter_feat.loc[dataset.inter_feat.loc[:, 'user_id']==user,['user_id', 'item_id']]
index = list(user_inter.index)
train_index = random.choices(index, k=cs_threshold)
test_index = list(set(index)-set(train_index))
test_df = pd.concat([test_df, dataset.inter_feat.loc[test_index,:]])
train_df = train_df.drop(index=test_index, axis=0)
test_df = test_df.reset_index(drop=True)
train_df = train_df.reset_index(drop=True)
train_user_token = [dataset.id2token('user_id', i) for i in train_df.loc[:,'user_id'].tolist()]
train_item_token = [dataset.id2token('item_id', i) for i in train_df.loc[:,'item_id'].tolist()]
train_df.loc[:,'user_id'] = train_user_token
train_df.loc[:,'item_id'] = train_item_token
train_df.columns = [f'{column}:{type_dict.setdefault(column, "token")}' for column in train_df.columns]
test_user_token = [dataset.id2token('user_id', i) for i in test_df.loc[:,'user_id'].tolist()]
test_item_token = [dataset.id2token('item_id', i) for i in test_df.loc[:,'item_id'].tolist()]
test_df.loc[:,'user_id'] = test_user_token
test_df.loc[:,'item_id'] = test_item_token
test_df.to_csv(os.path.join(temp_path, dst_dataset + '.test'), sep='\t', index=False)
train_df.to_csv(os.path.join(temp_path, dst_dataset + '.inter'), sep='\t', index=False)
if test_type == 'random':
src_file = open(os.path.join(src_path, dataset_str + '.kg'), 'r')
dst_file = open(os.path.join(temp_path, dst_dataset + '.kg'), 'w')
is_first = True
for line in src_file:
if is_first:
dst_file.write(line)
is_first = False
continue
if random.random() < rate:
head = dataset.id2token('entity_id', random.randint(1,dataset.entity_num-1))
relation = dataset.id2token('relation_id', random.randint(1,dataset.relation_num-1))
tail = dataset.id2token('entity_id', random.randint(1,dataset.entity_num-1))
dst_file.write('{}\t{}\t{}\n'.format(head, relation, tail))
else:
dst_file.write(line)
while not src_file.closed:
src_file.close()
while not dst_file.closed:
dst_file.close()
elif test_type[0] == 'd':
src_pd = pd.read_table(os.path.join(src_path, dataset_str+'.kg'))
if test_type == 'd_fact':
n_fact = src_pd.shape[0]
dst_pd = src_pd.sample(round(n_fact*rate))
dst_pd.to_csv(os.path.join(temp_path, dst_dataset + '.kg'), '\t', index=False)
elif test_type == 'd_relation':
relation_set = set(src_pd.loc[:, 'relation_id:token'].tolist())
n_relation = len(relation_set)
drop_relation_set = set(random.sample(list(relation_set), k=round(n_relation*(1-rate))))
dst_pd = src_pd.loc[~src_pd.loc[:, 'relation_id:token'].isin(drop_relation_set), :]
dst_pd.to_csv(os.path.join(temp_path, dst_dataset + '.kg'), '\t', index=False)
elif test_type == 'd_entity':
entity_set = set(src_pd.loc[:, 'head_id:token'].tolist())
entity_set.update(set(src_pd.loc[:, 'tail_id:token'].tolist()))
n_entity = len(entity_set)
drop_entity_set = set(random.sample(list(entity_set), k=round(n_entity*(1-rate))))
dst_pd = src_pd.loc[~(src_pd.loc[:,'head_id:token'].isin(drop_entity_set) | src_pd.loc[:, 'tail_id:token'].isin(drop_entity_set)), :]
dst_pd.to_csv(os.path.join(temp_path, dst_dataset + '.kg'), '\t', index=False)
elif test_type[0] == 's':
src_file = open(os.path.join(src_path, '{}.kg'.format(dataset_str)), 'r')
dst_file = open(os.path.join(temp_path, dst_dataset + '.kg'), 'w')
fact_num = len(src_file.readlines()) - 1
entity_token_array = dataset.field2id_token['entity_id']
relation_token_array = dataset.field2id_token['relation_id']
if test_type == 's_entity':
# RecBole library adds 1 additional entity that don't actually exist.
new_entity_num = round(rate * dataset.entity_num)
if rate <= 1:
entity_token_array = entity_token_array[:new_entity_num+1]
else:
new_entity_num = new_entity_num-dataset.entity_num
new_entity_array = np.array(['new_entity_{}'.format(i) for i in range(new_entity_num)])
entity_token_array = np.append(entity_token_array, new_entity_array)
elif test_type == 's_relation':
# RecBole library adds 2 additional relations that don't actually exist in kg file.
relation_num = max(round(rate * (dataset.relation_num-2)),1)
if rate <= 1:
relation_token_array = relation_token_array[:relation_num+1]
else:
new_relation_num = relation_num - dataset.relation_num
new_relation_array = np.array(['new_relation_{}'.format(i) for i in range(new_relation_num)])
relation_token_array = np.append(relation_token_array, new_relation_array)
elif test_type == 's_fact':
fact_num = round(fact_num * rate)
else:
raise NameError('Invalid test type.')
# labels
dst_file.write('head_id:token\trelation_id:token\ttail_id:token\n')
for _ in range(fact_num):
head = entity_token_array[random.randint(1,len(entity_token_array)-1)]
relation = relation_token_array[random.randint(1,len(relation_token_array)-1)]
tail = entity_token_array[random.randint(1,len(entity_token_array)-1)]
dst_file.write('{}\t{}\t{}\n'.format(head, relation, tail))
while not src_file.closed:
src_file.close()
while not dst_file.closed:
dst_file.close()
add_self_relation(dst_dataset)
def generate_test_user_list(con, dataset_str:str, test_user_ratio:float, device=torch.device('cuda:0'), seed=0):
config_dict = {'seed':seed,'checkpoint_dir':'saved{}/'.format(str(device).split(':')[-1])}
dataset = get_dataset(config_dict, dataset_str)
test_user_num = round(test_user_ratio * dataset.user_num)
user_list = list(range(1, dataset.user_num))
test_user_list = []
while len(test_user_list) < test_user_num:
if len(user_list) == 0:
break
user = user_list.pop(random.randint(0, len(user_list)-1))
user_inter = dataset.inter_feat.loc[dataset.inter_feat.loc[:, 'user_id']==user,['user_id', 'item_id']]
if user_inter.shape[0] < 25:
continue
test_user_list.append(user)
con.send(test_user_list)