-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
136 lines (116 loc) · 4.58 KB
/
data.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
import os
import numpy as np
import torch
from torch.utils.data import TensorDataset
from torch.utils.data import DataLoader
import random
from scipy.sparse import dok_matrix
from config import gmf_config ,mlp_config , neumf_config
import collections
SEED = 42 #132
random.seed(SEED)
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
torch.cuda.manual_seed_all(SEED)
os.environ['PYTHONHASHSEED'] = str(SEED)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
class PrepareDataset() :
def __init__(self , config):
self.config = config
self.batch_size = config['batch_size']
self.problem = self.config['problem']
def read_file_as_matrix(self,file):
#find number of users and items. Reading file twice to save space on ram
with open(file, 'r') as f:
num_users , num_items = 0 ,0
for line in f:
interaction = line.split()
u , i = int(interaction[0]) , int(interaction[1])
num_users , num_items = max(num_users , u) , max(num_items , i)
num_users +=1
num_items +=1
print('number of users= {} and number of items = {}'.format(num_users , num_items))
rating_mat = dok_matrix((num_users+1 , num_items+1) , dtype= np.float32)
with open(file, 'r') as f:
for line in f:
interaction = line.split()
u , i , r = int(interaction[0]) , int(interaction[1]) , float(interaction[2])
if r > 0:
rating_mat[u ,i] = r
return rating_mat , num_users , num_items
def read_negatives_file(self, path):
with open(path , 'r') as f:
test_users = []
test_items = []
for line in f :
user_items = []
arr = line.split('\t')
user = int(eval(arr[0])[0])
item = int(eval(arr[0])[1])
test_users.append(user)
user_items.append(item)
for i in arr[1:]:
user_items.append(int(i))
test_items.append(user_items)
return test_users , test_items
def get_validation_set(self , all_users , all_items , all_labels):
idx_dict = collections.defaultdict(list)
for i , key in enumerate(all_users):
idx_dict[key].append(i)
users , items , labels , val_users , val_items , val_labels = [] , [] , [] ,[] ,[] , []
for user in idx_dict.keys():
val_idx = random.choice(idx_dict[user])
val_users.append(all_users[val_idx])
val_items.append(all_items[val_idx])
val_labels.append(all_labels[val_idx])
idx_dict[user].remove(val_idx)
for item_idx in idx_dict[user]:
users.append(user)
items.append(all_items[item_idx])
labels.append(all_labels[item_idx])
print('no. of validation interactions', len(val_users))
print('no. of train interactions', len(users))
return users , items , labels , val_users , val_items , val_labels
def get_instances(self, path , problem , num_neg = 4 ):
#num_neg is number of negative instances corresponding to each positive instance i.e negative sampling
print(path)
rating_matrix , num_users , num_items = self.read_file_as_matrix(path)
users , items , labels = [] , [] , []
print(num_users , num_items)
count = 0
if not problem == 'prediction' :
for u , i in rating_matrix.keys():
count+=1
users.append(u)
items.append(i)
labels.append(1)
#negative sampling
for t in range(num_neg):
count+=1
j = np.random.randint(low = 0, high = num_items-1)
while (u, j) in rating_matrix.keys():
j = np.random.randint(low = 0 , high = num_items-1)
users.append(u)
items.append(j)
labels.append(0)
#create validation set
users, items, labels , val_users , val_items , val_labels = self.get_validation_set(users , items , labels)
print('number of interactions' , count)
return users, items, labels , val_users , val_items , val_labels , num_users , num_items
else:
for u , i in rating_matrix.keys():
count+=1
users.append(u)
items.append(i)
labels.append(rating_matrix[u,i])
print('number of interactions' , count)
return users, items, labels , num_users , num_items
def generator(self, users , items , labels , batch_size):
user_tensor = torch.tensor(users , dtype = torch.long)
item_tensor = torch.tensor(items , dtype = torch.long)
label_tensor = torch.tensor(labels , dtype = torch.float)
dataset = TensorDataset(user_tensor , item_tensor , label_tensor)
iterator = DataLoader(dataset , batch_size = batch_size)
return iterator