-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfinetune_data.py
More file actions
282 lines (218 loc) · 10.2 KB
/
finetune_data.py
File metadata and controls
282 lines (218 loc) · 10.2 KB
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
import copy
import pickle
import random
import argparse
import os
import torch
import torch.nn as nn
from torch.utils.data import Dataset
from tqdm import tqdm
from collections import defaultdict
import logging
import re
import pdb
import json
from prompt import *
import numpy as np
class FtBaseDataset(Dataset):
def __init__(self, args):
super().__init__()
self.args = args
self.data_path = args.data_path
self.max_phis_len = args.max_phis_len
self.max_chis_len = args.max_chis_len
self.max_candidate_num = args.max_candidate_num
self.photo_emb_file = args.photo_emb_file
self.comment_emb_file = args.comment_emb_file
def load_json(self, file):
with open(file, "r", encoding="utf-8") as f:
data = json.load(f)
return data
def load_pkl(self, file):
with open(file, 'rb') as file:
data = pickle.load(file)
return data
def load_row_data(self, file):
data = []
with open(file, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
data.append(json.loads(line))
return data
class FtRecDataset(FtBaseDataset):
def __init__(self, args, mode="train"):
super().__init__(args)
self.mode = mode
self.neg_photo_num = args.neg_photo_num
self.all_photos = self.load_pkl(os.path.join(self.data_path, "all_photos.pkl"))
self.photo2id = {str(photo): i for i, photo in enumerate(self.all_photos)}
self.all_comments = self.load_pkl(os.path.join(self.data_path, "all_comments.pkl"))
self.comment2id = {str(comment): i for i, comment in enumerate(self.all_comments)}
self._load_data()
def _load_data(self):
self.inter_data = self.load_row_data(os.path.join(self.data_path, f"rec.{self.mode}.json"))
if self.mode == "train":
self.photo_embs = np.load(os.path.join(self.data_path, self.photo_emb_file))
self.comment_embs = np.load(os.path.join(self.data_path, self.comment_emb_file))
photos = self.load_json(os.path.join(self.data_path, "photo.json"))
self.photo_comment_list = {photo: feat["comment_list"] for photo, feat in photos.items()}
def __len__(self):
return len(self.inter_data)
def _sample_candidates(self, target_photo_id, neg_num):
neg_photos = np.random.choice(np.arange(len(self.all_photos))[1:], neg_num, replace=False)
while target_photo_id in neg_photos:
neg_photos = np.random.choice(np.arange(len(self.all_photos))[1:], neg_num, replace=False)
cands = np.concatenate([[target_photo_id], neg_photos])
labels = np.array([1] + [0] * neg_num)
indices = np.random.permutation(np.arange(len(cands)))
cands = cands[indices]
labels = labels[indices]
return cands, labels
def __getitem__(self, index):
d = self.inter_data[index]
user_id = d["user_id"]
target_photo = d["target_photo"]
photo_inter_his = d["photo_inter_his"][-self.max_phis_len:]
comment_inter_his = d["comment_inter_his"]
target_photo_id = self.photo2id[str(target_photo)]
if self.mode == "train":
if self.neg_photo_num > 0:
candidates, labels = self._sample_candidates(target_photo_id, self.neg_photo_num)
labels = int(labels.argmax())
else:
candidates = None
labels = target_photo_id
else:
if self.max_candidate_num > 0:
candidates, labels = self._sample_candidates(target_photo_id, self.max_candidate_num - 1)
else:
candidates = None
labels = np.zeros(len(self.all_photos), dtype=int)
labels[target_photo_id] = 1
photo_his_pid = []
photo_his_cid = []
for p in photo_inter_his:
photo_his_pid.append(self.photo2id[str(p)])
comment_list = self.photo_comment_list[str(p)]
if len(comment_list) == 0:
comment_id = 0
else:
if self.mode == "train":
pop_comment_list = comment_list[:3]
comment = np.random.choice(pop_comment_list, 1)[0]
else:
comment = comment_list[0]
comment_id = self.comment2id[str(comment)]
photo_his_cid.append(comment_id)
comment_his_pid = []
comment_his_cid = []
for p, c_list in zip(comment_inter_his[0][-self.max_chis_len:], comment_inter_his[1][-self.max_chis_len:]):
comment_his_pid.append(self.photo2id[str(p)])
comment_his_cid.append([self.comment2id[str(c)] for c in c_list])
return dict(candidates=candidates,
labels = labels,
photo_his_pid = photo_his_pid,
photo_his_cid = photo_his_cid,
comment_his_pid = comment_his_pid,
comment_his_cid = comment_his_cid
)
class FtCommRankDataset(FtBaseDataset):
def __init__(self, args, mode="train"):
super().__init__(args)
self.mode = mode
self.neg_comment_num = args.neg_comment_num
self.all_photos = self.load_pkl(os.path.join(self.data_path, "all_photos.pkl"))
self.photo2id = {str(photo): i for i, photo in enumerate(self.all_photos)}
self.all_comments = self.load_pkl(os.path.join(self.data_path, "all_comments.pkl"))
self.comment2id = {str(comment): i for i, comment in enumerate(self.all_comments)}
self._load_data()
def _load_data(self):
self.inter_data = self.load_row_data(os.path.join(self.data_path, f"comm_rank.{self.mode}.json"))
if self.mode == "train":
self.photo_embs = np.load(os.path.join(self.data_path, self.photo_emb_file))
self.comment_embs = np.load(os.path.join(self.data_path, self.comment_emb_file))
photos = self.load_json(os.path.join(self.data_path, "photo.json"))
self.photo_comment_list = {photo: feat["comment_list"] for photo, feat in photos.items()}
def __len__(self):
return len(self.inter_data)
def _sample_candidates(self, pos_comment, pos_comments, all_candidates):
if len(all_candidates)==0 or len(all_candidates) == len(pos_comments):
neg_comments = np.random.choice(self.all_comments[1:], self.neg_comment_num, replace=False)
else:
all_labels = np.array([1 if c in pos_comments else 0 for c in all_candidates])
neg_ids = np.array([]).astype(int)
while len(neg_ids) < self.neg_comment_num:
neg_ids = np.concatenate((
neg_ids,
np.random.permutation(np.where(all_labels==0)[0]),
))
neg_ids = neg_ids[:self.neg_comment_num].astype(int)
neg_comments = all_candidates[neg_ids]
cands = np.concatenate([[pos_comment], neg_comments])
labels = np.array([1] + [0] * self.neg_comment_num)
indices = np.random.permutation(np.arange(len(cands)))
cands = cands[indices]
labels = labels[indices]
return cands, labels
def __getitem__(self, index):
d = self.inter_data[index]
user_id = d["user_id"]
target_photo = d["target_photo"]
pos_comments = d["pos_comments"]
photo_inter_his = d["photo_inter_his"][-self.max_phis_len:]
comment_inter_his = d["comment_inter_his"]
pos_comment = np.random.choice(pos_comments, 1)[0]
com_photo_id = self.photo2id[str(target_photo)]
all_candidates = np.array(self.photo_comment_list[str(target_photo)])
if self.mode == "train":
candidates, labels = self._sample_candidates(pos_comment, pos_comments, all_candidates)
labels = int(labels.argmax())
else:
all_labels = np.array([1 if c in pos_comments else 0 for c in all_candidates])
if len(all_candidates) > self.max_candidate_num:
neg_ids = np.array([]).astype(int)
neg_num = self.max_candidate_num - len(pos_comments)
while len(neg_ids) < neg_num:
neg_ids = np.concatenate((
neg_ids,
np.random.permutation(np.where(all_labels == 0)[0]),
))
neg_ids = neg_ids[:neg_num]
neg_comments = all_candidates[neg_ids]
candidates = np.concatenate([pos_comments, neg_comments])
labels = np.array([1] * len(pos_comments) + [0] * neg_num)
indices = np.random.permutation(np.arange(self.max_candidate_num))
candidates = candidates[indices]
labels = labels[indices]
else:
candidates = all_candidates
labels = all_labels
candidates = np.array([self.comment2id[str(c)] for c in candidates])
photo_his_pid = []
photo_his_cid = []
for p in photo_inter_his:
photo_his_pid.append(self.photo2id[str(p)])
comment_list = self.photo_comment_list[str(p)]
if len(comment_list) == 0:
comment_id = 0
else:
if self.mode == "train":
pop_comment_list = comment_list[:3]
comment = np.random.choice(pop_comment_list, 1)[0]
else:
comment = comment_list[0]
comment_id = self.comment2id[str(comment)]
photo_his_cid.append(comment_id)
comment_his_pid = []
comment_his_cid = []
for p, c_list in zip(comment_inter_his[0][-self.max_chis_len:], comment_inter_his[1][-self.max_chis_len:]):
comment_his_pid.append(self.photo2id[str(p)])
comment_his_cid.append([self.comment2id[str(c)] for c in c_list])
return dict(com_photo_id=com_photo_id,
candidates=candidates,
labels=labels,
photo_his_pid=photo_his_pid,
photo_his_cid=photo_his_cid,
comment_his_pid=comment_his_pid,
comment_his_cid=comment_his_cid
)