-
Notifications
You must be signed in to change notification settings - Fork 2
/
DataHander.py
135 lines (113 loc) · 4.39 KB
/
DataHander.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
import numpy as np
from torch.utils.data import DataLoader
import torch.utils.data as data
import torch as t
from utils import load_data, load_model, save_model, fix_random_seed_as
import scipy.sparse as sp
from scipy.sparse import csr_matrix, coo_matrix, dok_matrix
from param import args
import pickle
import torch, dgl
class DataHandler:
def __init__(self):
predir = ''
if args.dataset == 'yelp':
predir = './datasets/yelp/'
self.datapath = predir + 'dataset.pkl'
elif args.dataset == 'ciao':
predir = './datasets/ciao/'
self.datapath = predir + 'dataset.pkl'
elif args.dataset == 'epinions':
predir = './datasets/epinions/'
self.datapath = predir + 'dataset.pkl'
self.predir = predir
def loadOneFile(self,data_path):
with open(data_path, 'rb') as f:
data = pickle.load(f)
return data
def LoadData(self):
self.dataset = self.loadOneFile(self.datapath)
trnMat = self.dataset['train']
tstMat = self.dataset['test']
valMat = self.dataset['val']
trainset = TrnData(trnMat)
testset = TstData(tstMat, trnMat)
valset = TstData(valMat,trnMat)
self.n_user, self.n_item = self.dataset['userCount'], self.dataset['itemCount']
args.user, args.item = self.n_user, self.n_item
self.trainloader = DataLoader(
dataset=trainset,
batch_size=args.batch_size,
shuffle=True,
num_workers=args.num_workers
)
self.valloader = DataLoader(
dataset=valset,
batch_size=args.test_batch_size,
shuffle=False,
num_workers=args.num_workers
)
self.testloader = DataLoader(
dataset=testset,
batch_size=args.test_batch_size,
shuffle=False,
num_workers=args.num_workers
)
self.uu_graph = dgl.from_scipy(self.dataset['trust'])
uimat = self.dataset['train'].tocsr()
self.ui_graph = self.makeBiAdj(uimat,self.n_user,self.n_item)
def makeBiAdj(self, mat,n_user,n_item):
a = sp.csr_matrix((n_user, n_user))
b = sp.csr_matrix((n_item, n_item))
mat = sp.vstack([sp.hstack([a, mat]), sp.hstack([mat.transpose(), b])])
mat = (mat != 0) * 1.0
mat = mat.tocoo()
edge_src,edge_dst = mat.nonzero()
ui_graph = dgl.graph(data=(edge_src, edge_dst),
idtype=torch.int32,
num_nodes=mat.shape[0]
)
return ui_graph
# 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()
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])