forked from XuanheZhou/workload-performance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphembedding.py
More file actions
125 lines (93 loc) · 4.03 KB
/
graphembedding.py
File metadata and controls
125 lines (93 loc) · 4.03 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
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
import scipy.sparse as sp
import torch
import numpy as np
from constants import DATAPATH, NODE_DIM
import torch.nn.functional as F
# ## Load Data
def encode_onehot(labels):
classes = set(labels)
classes_dict = {c: np.identity(len(classes))[i, :] for i, c in
enumerate(classes)}
labels_onehot = np.array(list(map(classes_dict.get, labels)),
dtype=np.int32)
return labels_onehot
def normalize(mx):
"""Row-normalize sparse matrix"""
rowsum = np.array(mx.sum(1))
r_inv = np.power(rowsum, -1).flatten()
r_inv[np.isinf(r_inv)] = 0.
r_mat_inv = sp.diags(r_inv)
mx = r_mat_inv.dot(mx)
return mx
def accuracy(output, labels):
preds = output.max(1)[1].type_as(labels)
correct = preds.eq(labels).double()
correct = correct.sum()
return correct / len(labels)
def sparse_mx_to_torch_sparse_tensor(sparse_mx):
"""Convert a scipy sparse matrix to a torch sparse tensor."""
sparse_mx = sparse_mx.tocoo().astype(np.float32)
indices = torch.from_numpy(
np.vstack((sparse_mx.row, sparse_mx.col)).astype(np.int64))
values = torch.from_numpy(sparse_mx.data)
shape = torch.Size(sparse_mx.shape)
return torch.sparse.FloatTensor(indices, values, shape)
def load_data(dataset, path=DATAPATH):
print('Loading {} dataset...'.format(dataset))
vmatrix = np.genfromtxt("{}{}.content".format(path, dataset),
dtype=np.dtype(str))
ematrix = np.genfromtxt("{}{}.cites".format(path, dataset),
dtype=np.float32)
return load_data_from_matrix(vmatrix, ematrix)
def load_data_from_matrix(vmatrix, ematrix):
idx_features_labels = vmatrix
# encode vertices
features = sp.csr_matrix(idx_features_labels[:, 1:-1], dtype=np.float32)
# encode labels
# labels = encode_onehot(idx_features_labels[:, -2])
labels = idx_features_labels[:, -1].astype(float)
# encode edges
idx = np.array(idx_features_labels[:, 0], dtype=np.int32)
idx_map = {j: i for i, j in enumerate(idx)}
edges_unordered = ematrix[:, :-1]
# print(list(map(idx_map.get, edges_unordered.flatten())))
# print(edges_unordered.flatten())
edges = np.array(list(map(idx_map.get, edges_unordered.flatten())),
dtype=np.int32).reshape(edges_unordered.shape)
# edges (weights are computed in gcn)
# modified begin.
edges_value = ematrix[:, -1:]
# modified end.
# adj = sp.coo_matrix((np.ones(edges.shape[0]), (edges[:, 0], edges[:, 1])),shape=(node_dim, node_dim),dtype=np.float32)
# print("old_adj = ", adj)
adj = sp.coo_matrix((edges_value[:, 0], (edges[:, 0], edges[:, 1])), shape=(NODE_DIM, NODE_DIM), dtype=np.float32)
# print("new_adj = ", adj)
# print(adj.shape)
# build symmetric adjacency matrix
# adj = adj + adj.T.multiply(adj.T > adj) - adj.multiply(adj.T > adj)
features = normalize(features)
adj = normalize(adj + sp.eye(adj.shape[0]))
operator_num = adj.shape[0]
idx_train = range(int(0.8 * operator_num))
# print("idx_train", idx_train)
idx_val = range(int(0.8 * operator_num), int(0.9 * operator_num))
idx_test = range(int(0.9 * operator_num), int(operator_num))
features = torch.FloatTensor(np.array(features.todense()))
# labels = torch.LongTensor(np.where(labels)[1])
adj = sparse_mx_to_torch_sparse_tensor(adj)
idx_train = torch.LongTensor(idx_train)
idx_val = torch.LongTensor(idx_val)
idx_test = torch.LongTensor(idx_test)
# padding to the same size
# print(features.shape)
# print(node_dim - features.shape[0])
dim = (0, 0, 0, NODE_DIM - features.shape[0])
features = F.pad(features, dim, "constant", value=0)
labels = labels.astype(np.float32)
labels = torch.from_numpy(labels)
# print(labels[idx_train].dtype)
labels.unsqueeze(1)
labels = labels * 10
labels = F.pad(labels, [0, NODE_DIM - labels.shape[0]], "constant", value=0)
# print("features", features.shape)
return adj, features, labels, idx_train, idx_val, idx_test