-
Notifications
You must be signed in to change notification settings - Fork 2
/
DataHandler.py
138 lines (116 loc) · 4.1 KB
/
DataHandler.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
import pickle
import numpy as np
from scipy.sparse import coo_matrix
from Params import args
import scipy.sparse as sp
import torch
import torch.utils.data as data
import torch.utils.data as dataloader
from collections import defaultdict
from tqdm import tqdm
import random
class DataHandler:
def __init__(self):
if args.data == 'baby':
predir = './Datasets/baby/'
elif args.data == 'sports':
predir = './Datasets/sports/'
elif args.data == 'tiktok':
predir = './Datasets/tiktok/'
self.predir = predir
self.trnfile = predir + 'trnMat.pkl'
self.tstfile = predir + 'tstMat.pkl'
self.imagefile = predir + 'image_feat.npy'
self.textfile = predir + 'text_feat.npy'
if args.data == 'tiktok':
self.audiofile = predir + 'audio_feat.npy'
def loadOneFile(self, filename):
with open(filename, 'rb') as fs:
ret = (pickle.load(fs) != 0).astype(np.float32)
# ret = pickle.load(fs)
if type(ret) != coo_matrix:
ret = sp.coo_matrix(ret)
return ret
def normalizeAdj(self, mat):
degree = np.array(mat.sum(axis=-1))
dInvSqrt = np.reshape(np.power(degree, -0.5), [-1])
dInvSqrt[np.isinf(dInvSqrt)] = 0.0
dInvSqrtMat = sp.diags(dInvSqrt)
return mat.dot(dInvSqrtMat).transpose().dot(dInvSqrtMat).tocoo()
def makeTorchAdj(self, mat):
# make ui adj
a = sp.csr_matrix((args.user, args.user))
b = sp.csr_matrix((args.item, args.item))
mat = sp.vstack([sp.hstack([a, mat]), sp.hstack([mat.transpose(), b])])
mat = (mat != 0) * 1.0
mat = (mat + sp.eye(mat.shape[0])) * 1.0
mat = self.normalizeAdj(mat)
# make cuda tensor
idxs = torch.from_numpy(np.vstack([mat.row, mat.col]).astype(np.int64))
vals = torch.from_numpy(mat.data.astype(np.float32))
shape = torch.Size(mat.shape)
return torch.sparse.FloatTensor(idxs, vals, shape).cuda()
def loadFeatures(self, filename):
feats = np.load(filename)
return torch.tensor(feats).float().cuda(), np.shape(feats)[1]
def LoadData(self):
trnMat = self.loadOneFile(self.trnfile)
tstMat = self.loadOneFile(self.tstfile)
self.trnMat = trnMat
args.user, args.item = trnMat.shape
self.torchBiAdj = self.makeTorchAdj(trnMat)
trnData = TrnData(trnMat)
self.trnLoader = dataloader.DataLoader(trnData, batch_size=args.batch, shuffle=True, num_workers=0)
tstData = TstData(tstMat, trnMat)
self.tstLoader = dataloader.DataLoader(tstData, batch_size=args.tstBat, shuffle=False, num_workers=0)
self.image_feats, args.image_feat_dim = self.loadFeatures(self.imagefile)
self.text_feats, args.text_feat_dim = self.loadFeatures(self.textfile)
if args.data == 'tiktok':
self.audio_feats, args.audio_feat_dim = self.loadFeatures(self.audiofile)
self.diffusionData = DiffusionData(torch.FloatTensor(self.trnMat.A))
self.diffusionLoader = dataloader.DataLoader(self.diffusionData, batch_size=args.batch, shuffle=True, num_workers=0)
class TrnData(data.Dataset):
def __init__(self, coomat):
self.rows = coomat.row
self.cols = coomat.col
self.dokmat = coomat.todok()
self.negs = np.zeros(len(self.rows)).astype(np.int32)
def negSampling(self):
for i in range(len(self.rows)):
u = self.rows[i]
while True:
iNeg = np.random.randint(args.item)
if (u, iNeg) not in self.dokmat:
break
self.negs[i] = iNeg
def __len__(self):
return len(self.rows)
def __getitem__(self, idx):
return self.rows[idx], self.cols[idx], self.negs[idx]
class TstData(data.Dataset):
def __init__(self, coomat, trnMat):
self.csrmat = (trnMat.tocsr() != 0) * 1.0
tstLocs = [None] * coomat.shape[0]
tstUsrs = set()
for i in range(len(coomat.data)):
row = coomat.row[i]
col = coomat.col[i]
if tstLocs[row] is None:
tstLocs[row] = list()
tstLocs[row].append(col)
tstUsrs.add(row)
tstUsrs = np.array(list(tstUsrs))
self.tstUsrs = tstUsrs
self.tstLocs = tstLocs
def __len__(self):
return len(self.tstUsrs)
def __getitem__(self, idx):
return self.tstUsrs[idx], np.reshape(self.csrmat[self.tstUsrs[idx]].toarray(), [-1])
class DiffusionData(data.Dataset):
def __init__(self, data):
self.data = data
def __getitem__(self, index):
item = self.data[index]
return item, index
def __len__(self):
return len(self.data)