-
Notifications
You must be signed in to change notification settings - Fork 4
/
prepareMnist_for_tf.py
80 lines (68 loc) · 2.43 KB
/
prepareMnist_for_tf.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
import numpy as np
from torch_geometric.datasets import MNISTSuperpixels
from utils_tf import *
from utils import DegreeMaxEigTransform
#select if node degree and location of superpixel region would be used by model or not.
#after any chnageing please remove MNIST/processed folder in order to preprocess changes again.
transform=DegreeMaxEigTransform(adddegree=True,addposition=False)
train_dataset = MNISTSuperpixels(root='dataset/MNIST/', train=True, pre_transform=transform)
test_dataset = MNISTSuperpixels(root='dataset/MNIST/', train=False, pre_transform=transform)
# how many supports of Chebnet and Cayleynet to be prepared
nkernel=5
#cayleynet's zoom param
h=1
################
n=70000
nmax=75
# number of node per graph
ND=75*np.ones((n,1))
# node feature matrix
FF=np.zeros((n,nmax,2))
# one-hot coding output matrix
YY=np.zeros((n,10))
# Convolution kernels, supports
SP=np.zeros((n,2*nkernel+1,nmax,nmax),dtype=np.float32)
d=train_dataset
for i in range(0,len(d)):
print(i)
nd=75
A=np.zeros((nd,nd),dtype=np.float32)
A[d[i].edge_index[0],d[i].edge_index[1]]=1
FF[i,:,:]=d[i].x.numpy()
gtrt=d[i].y.numpy()[0]
YY[i,gtrt]=1
# set chebnet kernel first nkernel is chebnet's support
chebnet = chebyshev_polynomials(A, nkernel-1,st=True)
for j in range(0,nkernel):
SP[i,j ,0:nd,0:nd]=chebnet[j].toarray()
# set gcn kernel nkernel-th support i sgcn support
SP[i,nkernel ,0:nd,0:nd]= (normalize_adj(A + sp.eye(nd))).toarray()
# last nkernel is cayleynet's support
r=int((nkernel-1)/2)
cayley=cayley_polynomials(A,h,r)
for j in range(0,nkernel):
SP[i,j+1+nkernel ,0:nd,0:nd]=cayley[j]
d=test_dataset
for i in range(0,len(d)):
print(i)
nd=75
A=np.zeros((nd,nd),dtype=np.float32)
A[d[i].edge_index[0],d[i].edge_index[1]]=1
FF[i+60000,:,:]=d[i].x.numpy()
gtrt=d[i].y.numpy()[0]
YY[i+60000,gtrt]=1
# set chebnet kernel
chebnet = chebyshev_polynomials(A, nkernel-1,st=True)
for j in range(0,nkernel):
SP[i+60000,j ,0:nd,0:nd]=chebnet[j].toarray()
# set gcn kernel
SP[i+60000,nkernel ,0:nd,0:nd]= (normalize_adj(A + sp.eye(nd))).toarray()
# last nkernel is cayleynet's support
r=int((nkernel-1)/2)
cayley=cayley_polynomials(A,h,r)
for j in range(0,nkernel):
SP[i+60000,j+1+nkernel ,0:nd,0:nd]=cayley[j]
np.save('supports',SP)
np.save('feats',FF)
np.save('output',YY)
np.save('nnodes',ND)