-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsynthetic.py
More file actions
85 lines (75 loc) · 3.06 KB
/
Copy pathsynthetic.py
File metadata and controls
85 lines (75 loc) · 3.06 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
"""
based on [1] in MFMultiClass
"""
import numpy as np
class MFsynthetic(object):
def __init__(self, N, M, L, K):
self.N = N
self.M = M
self.L = L
self.K = K
self.u = None
self.v = None
self.parameter()
self.dyaddict = {}
self.bayesian_error = []
def parameter(self):
self.u = np.random.random(self.N*self.L*self.K).reshape([self.N, self.L, self.K]) * 6.0 - 3.0
self.v = np.random.random(self.M*self.L*self.K).reshape([self.M, self.L, self.K]) * 6.0 - 3.0
def generate(self, fraction, MemorySampleSize = 1e+7):
Nsamp = self.N * self.M * fraction
# deal with large sample size #
if Nsamp >= MemorySampleSize:
Mseg = int(MemorySampleSize / fraction / self.N)
else:
Mseg = self.M
cnt = 0
seg = 0
if fraction < 1.0:
while cnt < Nsamp:
# stage-wise sampling for large sampling size #
if cnt < MemorySampleSize * (seg + 1):
iid = np.random.randint(Mseg * seg, min([Mseg * (seg + 1), self.M]))
else:
seg += 1
self.dyaddict.clear()
print seg, len(self.dyaddict)
continue
uid = np.random.randint(0, self.N)
dyadid = uid + iid * self.N
if dyadid in self.dyaddict:
continue
else:
self.dyaddict[dyadid] = cnt
expprod = np.exp(np.sum(np.multiply(self.u[uid], self.v[iid]), axis=1))
expprodsum = np.sum(expprod)
lid = np.argmax(np.random.multinomial(1, expprod / expprodsum, size=1))
self.bayesian_error.append((np.sum(expprod) - expprod[lid]) / expprodsum)
yield [uid, iid, lid]
cnt += 1
else:
for uid in range(self.N):
for iid in range(self.M):
expprod = np.exp(np.sum(np.multiply(self.u[uid], self.v[iid]), axis=1))
expprodsum = np.sum(expprod)
lid = np.argmax(np.random.multinomial(1, expprod / expprodsum, size=1))
self.bayesian_error.append((np.sum(expprod) - expprod[lid]) / expprodsum)
yield [uid, iid, lid]
def generate2file(self, fraction, filename):
with open(filename, "w") as f:
for samp in self.generate(fraction):
transaction = {"POSTID": samp[0],
"READERID": samp[1],
"EMOTICON": samp[2]}
f.write(str(transaction) + "\n")
return self
if __name__ == "__main__":
np.random.seed(2017)
N = 500
M = 500
L = 3
K = 5
generator = MFsynthetic(N = N, M = M, L = L, K = K) # based on [1]
generator.generate2file(1.0, "data/synthetic_N%d_M%d_L%d_K%d_new" % (N,M,L,K))
bayesian_error = np.array(generator.bayesian_error)
print np.mean(bayesian_error), np.std(bayesian_error)