forked from aHuiWang/CIKM2020-S3Rec
-
Notifications
You must be signed in to change notification settings - Fork 1
/
datasets.py
201 lines (163 loc) · 7.31 KB
/
datasets.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
import random
import torch
from torch.utils.data import Dataset
from utils import neg_sample
class PretrainDataset(Dataset):
def __init__(self, args, user_seq, long_sequence):
self.args = args
self.user_seq = user_seq
self.long_sequence = long_sequence
self.max_len = args.max_seq_length
self.part_sequence = []
self.split_sequence()
def split_sequence(self):
for seq in self.user_seq:
input_ids = seq[-(self.max_len+2):-2] # keeping same as train set
for i in range(len(input_ids)):
self.part_sequence.append(input_ids[:i+1])
def __len__(self):
return len(self.part_sequence)
def __getitem__(self, index):
sequence = self.part_sequence[index] # pos_items
# sample neg item for every masked item
masked_item_sequence = []
neg_items = []
# Masked Item Prediction
item_set = set(sequence)
for item in sequence[:-1]:
prob = random.random()
if prob < self.args.mask_p:
masked_item_sequence.append(self.args.mask_id)
neg_items.append(neg_sample(item_set, self.args.item_size))
else:
masked_item_sequence.append(item)
neg_items.append(item)
# add mask at the last position
masked_item_sequence.append(self.args.mask_id)
neg_items.append(neg_sample(item_set, self.args.item_size))
# Segment Prediction
if len(sequence) < 2:
masked_segment_sequence = sequence
pos_segment = sequence
neg_segment = sequence
else:
sample_length = random.randint(1, len(sequence) // 2)
start_id = random.randint(0, len(sequence) - sample_length)
neg_start_id = random.randint(0, len(self.long_sequence) - sample_length)
pos_segment = sequence[start_id: start_id + sample_length]
neg_segment = self.long_sequence[neg_start_id:neg_start_id + sample_length]
masked_segment_sequence = sequence[:start_id] + [self.args.mask_id] * sample_length + sequence[
start_id + sample_length:]
pos_segment = [self.args.mask_id] * start_id + pos_segment + [self.args.mask_id] * (
len(sequence) - (start_id + sample_length))
neg_segment = [self.args.mask_id] * start_id + neg_segment + [self.args.mask_id] * (
len(sequence) - (start_id + sample_length))
assert len(masked_segment_sequence) == len(sequence)
assert len(pos_segment) == len(sequence)
assert len(neg_segment) == len(sequence)
# padding sequence
pad_len = self.max_len - len(sequence)
masked_item_sequence = [0] * pad_len + masked_item_sequence
pos_items = [0] * pad_len + sequence
neg_items = [0] * pad_len + neg_items
masked_segment_sequence = [0]*pad_len + masked_segment_sequence
pos_segment = [0]*pad_len + pos_segment
neg_segment = [0]*pad_len + neg_segment
masked_item_sequence = masked_item_sequence[-self.max_len:]
pos_items = pos_items[-self.max_len:]
neg_items = neg_items[-self.max_len:]
masked_segment_sequence = masked_segment_sequence[-self.max_len:]
pos_segment = pos_segment[-self.max_len:]
neg_segment = neg_segment[-self.max_len:]
# Associated Attribute Prediction
# Masked Attribute Prediction
attributes = []
for item in pos_items:
attribute = [0] * self.args.attribute_size
try:
now_attribute = self.args.item2attribute[str(item)]
for a in now_attribute:
attribute[a] = 1
except:
pass
attributes.append(attribute)
assert len(attributes) == self.max_len
assert len(masked_item_sequence) == self.max_len
assert len(pos_items) == self.max_len
assert len(neg_items) == self.max_len
assert len(masked_segment_sequence) == self.max_len
assert len(pos_segment) == self.max_len
assert len(neg_segment) == self.max_len
cur_tensors = (torch.tensor(attributes, dtype=torch.long),
torch.tensor(masked_item_sequence, dtype=torch.long),
torch.tensor(pos_items, dtype=torch.long),
torch.tensor(neg_items, dtype=torch.long),
torch.tensor(masked_segment_sequence, dtype=torch.long),
torch.tensor(pos_segment, dtype=torch.long),
torch.tensor(neg_segment, dtype=torch.long),)
return cur_tensors
class SASRecDataset(Dataset):
def __init__(self, args, user_seq, test_neg_items=None, data_type='train'):
self.args = args
self.user_seq = user_seq
self.test_neg_items = test_neg_items
self.data_type = data_type
self.max_len = args.max_seq_length
def __getitem__(self, index):
user_id = index
items = self.user_seq[index]
assert self.data_type in {"train", "valid", "test"}
# [0, 1, 2, 3, 4, 5, 6]
# train [0, 1, 2, 3]
# target [1, 2, 3, 4]
# valid [0, 1, 2, 3, 4]
# answer [5]
# test [0, 1, 2, 3, 4, 5]
# answer [6]
if self.data_type == "train":
input_ids = items[:-3]
target_pos = items[1:-2]
answer = [0] # no use
elif self.data_type == 'valid':
input_ids = items[:-2]
target_pos = items[1:-1]
answer = [items[-2]]
else:
input_ids = items[:-1]
target_pos = items[1:]
answer = [items[-1]]
target_neg = []
seq_set = set(items)
for _ in input_ids:
target_neg.append(neg_sample(seq_set, self.args.item_size))
pad_len = self.max_len - len(input_ids)
input_ids = [0] * pad_len + input_ids
target_pos = [0] * pad_len + target_pos
target_neg = [0] * pad_len + target_neg
input_ids = input_ids[-self.max_len:]
target_pos = target_pos[-self.max_len:]
target_neg = target_neg[-self.max_len:]
assert len(input_ids) == self.max_len
assert len(target_pos) == self.max_len
assert len(target_neg) == self.max_len
if self.test_neg_items is not None:
test_samples = self.test_neg_items[index]
cur_tensors = (
torch.tensor(user_id, dtype=torch.long), # user_id for testing
torch.tensor(input_ids, dtype=torch.long),
torch.tensor(target_pos, dtype=torch.long),
torch.tensor(target_neg, dtype=torch.long),
torch.tensor(answer, dtype=torch.long),
torch.tensor(test_samples, dtype=torch.long),
)
else:
cur_tensors = (
torch.tensor(user_id, dtype=torch.long), # user_id for testing
torch.tensor(input_ids, dtype=torch.long),
torch.tensor(target_pos, dtype=torch.long),
torch.tensor(target_neg, dtype=torch.long),
torch.tensor(answer, dtype=torch.long),
)
return cur_tensors
def __len__(self):
return len(self.user_seq)