-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
144 lines (112 loc) · 5.43 KB
/
Copy pathmain.py
File metadata and controls
144 lines (112 loc) · 5.43 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#asd
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
import os
import numpy as np
import torch
from Method.utils import myconf
from Method.utils.logger import get_logger
from torchvision.transforms import functional as F
from Method.learning_algo import LearningAlgorithm
from Method.gcn.config import args
from Method.utils.label_utils import reassign_labels, special_train_test_split
import warnings
from scipy.sparse import coo_matrix,csr_matrix
from Method.utils.data import preprocess_adj,preprocess_features
warnings.filterwarnings("ignore")
def meanstd_normalization_tensor(tensor):
# tensor: n_node, n_steps, n_dim
n_node, n_steps, n_dim = tensor.shape
# tensor_norm = np.ones([n_node, n_steps, n_dim])
tensor_reshape = preprocessing.scale(np.reshape(tensor, [n_node, n_steps * n_dim]), axis=1)
tensor_norm = np.reshape(tensor_reshape, [n_node, n_steps, n_dim])
return tensor_norm
def graph_loader(filepath,filename,unseen_list):
file = np.load(os.path.join(filepath,filename))
Features = file['attmats'] #(n_node, n_time, att_dim)
Labels = file['labels'] #(n_node, num_classes)
Graphs = file['adjs'] #(n_time, n_node, n_node)
a=1
supports_list=[]
features_list=[]
num_features_nonzero_list=[]
features_tensor = torch.empty(1, Features.shape[0], Features.shape[2])
Features_numpy = Features.transpose(1, 0, 2)
for j in range(Graphs.shape[0]):
f=Features_numpy[j]
g=Graphs[j]
g2=csr_matrix(g)
f2=csr_matrix(f).tolil()
features = preprocess_features(f2)
i = torch.from_numpy(features[0]).long().to("cuda")
v = torch.from_numpy(features[1]).to("cuda")
feature = torch.sparse.FloatTensor(i.t(), v, features[2]).to("cuda")
num_features_nonzero = feature._nnz()
num_features_nonzero_list.append(num_features_nonzero)
supports=preprocess_adj(g2)
i = torch.from_numpy(supports[0]).long().to("cuda")
v = torch.from_numpy(supports[1]).to("cuda")
support = torch.sparse.FloatTensor(i.t(), v, supports[2]).float().to("cuda")
supports_list.append(support)
feature = torch.unsqueeze(feature, 0)
if j ==0:
features_tensor=feature
else:
features_tensor = torch.cat([features_tensor, feature], dim=0)
num_features_nonzero=int(np.mean(num_features_nonzero_list))
if Graphs.dtype==np.int32:
Graphs=Graphs.astype(np.float32)
if Graphs.dtype==np.int64:
Graphs=Graphs.astype(np.float64)
#Features = meanstd_normalization_tensor(Features)
n_node, n_steps, n_dim = np.shape(Features)
# add self-loop
for i in range(n_steps):
Graphs[i, :, :] += np.eye(n_node, dtype=np.float)
features = F.to_tensor(Features)
features = features.permute(2,1,0)
graphs = F.to_tensor(Graphs)
graphs = graphs.permute(1,0,2)
#new split
training_rate=0.7
original_num_classes = Labels.shape[1]
all_labels = list(range(original_num_classes))
seen_labels=[]
for m in all_labels:
if m not in unseen_list:
seen_labels.append(m)
y_true=[]
for _,i in enumerate(Labels):
for j in range(len(i)):
if i[j]==1:
y_true.append(j)
y_true=np.array(y_true)
if unseen :
y_true = reassign_labels(y_true, seen_labels, -1)
train_indices, test_valid_indices = special_train_test_split(y_true, unseen_label_index=-1,
test_size=1 - training_rate,discard=None)
test_indices, valid_indices = train_test_split(test_valid_indices, test_size=1.0/ 3.0)
#return train_indices, valid_indices, test_indices, features, graphs, Labels,y_true
return train_indices, valid_indices, test_indices, features, supports_list, Labels, y_true,num_features_nonzero_list
#features_tensor
if __name__ == '__main__':
cfg_file ="./data/dblp5_config.ini"
config= myconf()
config.read(cfg_file)
l=[]
unseen_list = [] if config.get('Training', 'unseen_list') == '' else [int(i) for i in
config.get('Training',
'unseen_list').split(',')]
unseen=config.getboolean('Training', 'unseen')
x_train_idx, x_val_idx, x_test_idx, features, graphs, labels,y_true,num_features_nonzero_list = graph_loader(config.get('User', 'filepath'),
config.get('User', 'filename'),unseen_list)
lambda_1,lambda_2,lambda_3,beta,beta2=[1,100,0.01,0.01,1]
learning_algo = LearningAlgorithm(1,lambda_1,lambda_2,lambda_3,beta,beta2,cfg_file,num_features_nonzero_list)
learning_algo.logger.info("dataset:"+learning_algo.filename)
learning_algo.logger.info("gcn_dropout:" + str(args.dropout))
log=learning_algo.train_graph(x_train_idx, x_val_idx, x_test_idx, features, graphs, labels,y_true,lambda_1,lambda_2,lambda_3,beta,beta2)
l.append("\nlambda_1,lambda_2,lambda_3,beta,beta2:[{},{},{},{},{}]\n ".format(lambda_1,lambda_2,lambda_3,beta,beta2)+log+"\n")
log_file = os.path.join(learning_algo.save_dir, 'best_result.txt')
logger = get_logger(log_file, 1)
logger.info("result================================:")
logger.info("".join(l))