-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutils.py
110 lines (84 loc) · 3.56 KB
/
utils.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
import numpy as np
import torch
import torch.utils.data as utils
from math import ceil
from scipy.sparse import csr_matrix,lil_matrix
from sklearn.preprocessing import OneHotEncoder
def load_data(ds_name, use_node_labels):
graph_indicator = np.loadtxt("datasets/%s/%s_graph_indicator.txt"%(ds_name,ds_name), dtype=np.int64)
_,graph_size = np.unique(graph_indicator, return_counts=True)
edges = np.loadtxt("datasets/%s/%s_A.txt"%(ds_name,ds_name), dtype=np.int64, delimiter=",")
edges -= 1
A = csr_matrix((np.ones(edges.shape[0]), (edges[:,0], edges[:,1])), shape=(graph_indicator.size, graph_indicator.size))
if use_node_labels:
x = np.loadtxt("datasets/%s/%s_node_labels.txt"%(ds_name,ds_name), dtype=np.int64).reshape(-1,1)
enc = OneHotEncoder(sparse=False)
x = enc.fit_transform(x)
else:
x = A.sum(axis=1)
adj = []
features = []
idx = 0
for i in range(graph_size.size):
adj.append(A[idx:idx+graph_size[i],idx:idx+graph_size[i]])
features.append(x[idx:idx+graph_size[i],:])
idx += graph_size[i]
class_labels = np.loadtxt("datasets/%s/%s_graph_labels.txt"%(ds_name,ds_name), dtype=np.int64)
return adj, features, class_labels
def sparse_mx_to_torch_sparse_tensor(sparse_mx):
sparse_mx = sparse_mx.tocoo().astype(np.float32)
indices = torch.from_numpy(np.vstack((sparse_mx.row,
sparse_mx.col))).long()
values = torch.from_numpy(sparse_mx.data)
shape = torch.Size(sparse_mx.shape)
return torch.sparse.FloatTensor(indices, values, shape)
def generate_batches(adj, features, y, batch_size, device, shuffle=False):
N = len(y)
if shuffle:
index = np.random.permutation(N)
else:
index = np.array(range(N), dtype=np.int32)
n_batches = ceil(N/batch_size)
adj_lst = list()
features_lst = list()
graph_indicator_lst = list()
y_lst = list()
for i in range(0, N, batch_size):
n_graphs = min(i+batch_size, N) - i
n_nodes = sum([adj[index[j]].shape[0] for j in range(i, min(i+batch_size, N))])
adj_batch = lil_matrix((n_nodes, n_nodes))
features_batch = np.zeros((n_nodes, features[0].shape[1]))
graph_indicator_batch = np.zeros(n_nodes)
y_batch = np.zeros(n_graphs)
idx = 0
for j in range(i, min(i+batch_size, N)):
n = adj[index[j]].shape[0]
adj_batch[idx:idx+n, idx:idx+n] = adj[index[j]]
features_batch[idx:idx+n,:] = features[index[j]]
graph_indicator_batch[idx:idx+n] = j-i
y_batch[j-i] = y[index[j]]
idx += n
adj_lst.append(sparse_mx_to_torch_sparse_tensor(adj_batch).to(device))
features_lst.append(torch.FloatTensor(features_batch).to(device))
graph_indicator_lst.append(torch.LongTensor(graph_indicator_batch).to(device))
y_lst.append(torch.LongTensor(y_batch).to(device))
return adj_lst, features_lst, graph_indicator_lst, y_lst
def accuracy(output, labels):
preds = output.max(1)[1].type_as(labels)
correct = preds.eq(labels).double()
correct = correct.sum()
return correct / len(labels)
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count