-
Notifications
You must be signed in to change notification settings - Fork 15
/
synthetic.py
152 lines (127 loc) · 4.49 KB
/
synthetic.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
import numpy as np
import random
import scipy.sparse as sp
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score as acc
def high_dim_gaussian(mu, sigma):
if mu.ndim > 1:
d = len(mu)
res = np.zeros(d)
for i in range(d):
res[i] = np.random.normal(mu[i], sigma[i])
else:
d = 1
res = np.zeros(d)
res = np.random.normal(mu, sigma)
return res
def generate_uniform_theta(Y, c):
theta = np.zeros(len(Y), dtype='float')
for i in range(c):
idx = np.where(Y == i)
sample = np.random.uniform(low=0, high=1, size=len(idx[0]))
sample_sum = np.sum(sample)
for j in range(len(idx[0])):
theta[idx[0][j]] = sample[j] * len(idx[0]) / sample_sum
return theta
def generate_theta_dirichlet(Y, c):
theta = np.zeros(len(Y), dtype='float')
for i in range(c):
idx = np.where(Y == i)
temp = np.random.uniform(low=0, high=1, size=len(idx[0]))
sample = np.random.dirichlet(temp, 1)
sample_sum = np.sum(sample)
for j in range(len(idx[0])):
theta[idx[0][j]] = sample[0][j] * len(idx[0]) / sample_sum
return theta
def SBM(sizes, probs, mus, sigmas, noise,
radius, feats_type='gaussian', selfloops=True):
# -----------------------------------------------
# step1: get c,d,n
# -----------------------------------------------
c = len(sizes)
if mus.ndim > 1:
d = mus.shape[1]
else:
d = 1
n = sizes.sum()
all_node_ids = [ids for ids in range(0, n)]
# -----------------------------------------------
# step2: generate Y with sizes
# -----------------------------------------------
Y = np.zeros(n, dtype='int')
for i in range(c):
class_i_ids = random.sample(all_node_ids, sizes[i])
Y[class_i_ids] = i
for item in class_i_ids:
all_node_ids.remove(item)
# -----------------------------------------------
# step3: generate A with Y and probs
# -----------------------------------------------
if selfloops:
A = np.diag(np.ones(n, dtype='int'))
else:
A = np.zeros((n, n), dtype='int')
for i in range(n):
for j in range(i + 1, n):
prob_ = probs[Y[i]][Y[j]]
rand_ = random.random()
if rand_ <= prob_:
A[i][j] = 1
A[j][i] = 1
# -----------------------------------------------
# step4: generate X with Y and mus, sigmas
# -----------------------------------------------
X = np.zeros((n, d), dtype='float')
for i in range(n):
mu = mus[Y[i]]
sigma = sigmas[Y[i]]
X[i] = high_dim_gaussian(mu, sigma)
return A, X, Y
def generate(p, q, idx):
A, X, Y = \
SBM(sizes=np.array([100, 100]),
probs=np.array([[p, q], [q, p]]),
mus=np.array([[-0.5]*20, [0.5]*20]),
sigmas=np.array([[2]*20, [2]*20]),
noise=[],
radius=[],
selfloops=False)
return A, X, Y
def calculate(A, X, Y):
A = sp.coo_matrix(A)
A = A + A.T.multiply(A.T > A) - A.multiply(A.T > A)
rowsum = np.array(A.sum(1)).clip(min=1)
r_inv_sqrt = np.power(rowsum, -0.5).flatten()
r_mat_inv_sqrt = sp.diags(r_inv_sqrt)
A = A.dot(r_mat_inv_sqrt).transpose().dot(r_mat_inv_sqrt)
low = 0.5 * sp.eye(A.shape[0]) + A
high = 0.5 * sp.eye(A.shape[0]) - A
low = low.todense()
high = high.todense()
low_signal = np.dot(np.dot(low, low), X)
high_signal = np.dot(np.dot(high, high), X)
low_MLP = MLPClassifier(hidden_layer_sizes=(16), activation='relu', max_iter=2000)
low_MLP.fit(low_signal[:100, :], Y[:100])
low_pred = low_MLP.predict(low_signal[100:, :])
high_MLP = MLPClassifier(hidden_layer_sizes=(16), activation='relu', max_iter=2000)
high_MLP.fit(high_signal[:100, :], Y[:100])
high_pred = high_MLP.predict(high_signal[100:, :])
return acc(Y[100:], low_pred), acc(Y[100:], high_pred)
low_record = []
high_record = []
for i in range(1, 11):
q = i * 0.01
p = 0.05
low_rec = []
high_rec = []
mlp_rec = []
print(i, p, q)
for j in range(10):
A, X, Y = generate(p, q, 0)
low, high, = calculate(A, X, Y)
low_rec.append(low)
high_rec.append(high)
low_record.append([np.max(low_rec), np.min(low_rec), np.mean(low_rec)])
high_record.append([np.max(high_rec), np.min(high_rec), np.mean(high_rec)])
print(low_record)
print(high_record)