-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
181 lines (144 loc) · 6.98 KB
/
train.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import torch
import numpy as np
import argparse
from utils import set_seed, load_feat, load_graph
from data_process_utils import check_data_leakage
####################################################################
####################################################################
####################################################################
def print_model_info(model):
print(model)
parameters = filter(lambda p: p.requires_grad, model.parameters())
parameters = sum([np.prod(p.size()) for p in parameters])
print('Trainable Parameters: %d' % parameters)
def get_args():
parser=argparse.ArgumentParser()
parser.add_argument('--data', type=str, default='movie')
parser.add_argument('--device', type=int, default=0)
parser.add_argument('--batch_size', type=int, default=600)
parser.add_argument('--epochs', type=int, default=300)
parser.add_argument('--max_edges', type=int, default=50)
parser.add_argument('--num_edgeType', type=int, default=0, help='num of edgeType')
parser.add_argument('--lr', type=float, default=0.0005)
parser.add_argument('--weight_decay', type=float, default=1e-4)
parser.add_argument('--predict_class', action='store_true')
# model
parser.add_argument('--window_size', type=int, default=5)
parser.add_argument('--dropout', type=float, default=0.1)
parser.add_argument('--model', type=str, default='sthn')
parser.add_argument('--neg_samples', type=int, default=1)
parser.add_argument('--extra_neg_samples', type=int, default=5)
parser.add_argument('--num_neighbors', type=int, default=50)
parser.add_argument('--channel_expansion_factor', type=int, default=2)
parser.add_argument('--sampled_num_hops', type=int, default=1)
parser.add_argument('--time_dims', type=int, default=100)
parser.add_argument('--hidden_dims', type=int, default=100)
parser.add_argument('--num_layers', type=int, default=1)
parser.add_argument('--check_data_leakage', action='store_true')
parser.add_argument('--ignore_node_feats', action='store_true')
parser.add_argument('--node_feats_as_edge_feats', action='store_true')
parser.add_argument('--ignore_edge_feats', action='store_true')
parser.add_argument('--use_onehot_node_feats', action='store_true')
parser.add_argument('--use_type_feats', action='store_true')
parser.add_argument('--use_graph_structure', action='store_true')
parser.add_argument('--structure_time_gap', type=int, default=2000)
parser.add_argument('--structure_hops', type=int, default=1)
parser.add_argument('--use_node_cls', action='store_true')
parser.add_argument('--use_cached_subgraph', action='store_true')
return parser.parse_args()
def load_all_data(args):
# load graph
g, df = load_graph(args.data)
args.train_edge_end = df[df['ext_roll'].gt(0)].index[0]
args.val_edge_end = df[df['ext_roll'].gt(1)].index[0]
args.num_nodes = max(int(df['src'].max()), int(df['dst'].max())) + 1
args.num_edges = len(df)
print('Train %d, Valid %d, Test %d'%(args.train_edge_end,
args.val_edge_end-args.train_edge_end,
len(df)-args.val_edge_end))
print('Num nodes %d, num edges %d'%(args.num_nodes, args.num_edges))
# load feats
node_feats, edge_feats = load_feat(args.data)
node_feat_dims = 0 if node_feats is None else node_feats.shape[1]
edge_feat_dims = 0 if edge_feats is None else edge_feats.shape[1]
# feature pre-processing
if args.use_onehot_node_feats:
print('>>> Use one-hot node features')
node_feats = torch.eye(args.num_nodes)
node_feat_dims = node_feats.size(1)
if args.ignore_node_feats:
print('>>> Ignore node features')
node_feats = None
node_feat_dims = 0
if args.use_type_feats:
edge_type = df.label.values
args.num_edgeType = len(set(edge_type.tolist()))
edge_feats = torch.nn.functional.one_hot(torch.from_numpy(edge_type-1),
num_classes=args.num_edgeType)
edge_feat_dims = edge_feats.size(1)
print('Node feature dim %d, edge feature dim %d'%(node_feat_dims, edge_feat_dims))
# double check (if data leakage then cannot continue the code)
if args.check_data_leakage:
check_data_leakage(args, g, df)
args.node_feat_dims = node_feat_dims
args.edge_feat_dims = edge_feat_dims
if node_feats != None:
node_feats = node_feats.to(args.device)
if edge_feats != None:
edge_feats = edge_feats.to(args.device)
return node_feats, edge_feats, g, df, args
def load_model(args):
# get model
edge_predictor_configs = {
'dim_in_time': args.time_dims,
'dim_in_node': args.node_feat_dims,
'predict_class': 1 if not args.predict_class else args.num_edgeType+1,
}
if args.model == 'sthn':
if args.predict_class:
from model import Multiclass_Interface as STHN_Interface
else:
from model import STHN_Interface
from link_pred_train_utils import link_pred_train
mixer_configs = {
'per_graph_size' : args.max_edges,
'time_channels' : args.time_dims,
'input_channels' : args.edge_feat_dims,
'hidden_channels' : args.hidden_dims,
'out_channels' : args.hidden_dims,
'num_layers' : args.num_layers,
'dropout' : args.dropout,
'channel_expansion_factor': args.channel_expansion_factor,
'window_size' : args.window_size,
'use_single_layer' : False
}
else:
NotImplementedError()
model = STHN_Interface(mixer_configs, edge_predictor_configs)
for k, v in model.named_parameters():
print(k, v.requires_grad)
print_model_info(model)
return model, args, link_pred_train
####################################################################
####################################################################
####################################################################
if __name__ == "__main__":
args = get_args()
args.use_graph_structure = True
args.ignore_node_feats = True # we only use graph structure
args.use_type_feats = True # type encoding
args.use_cached_subgraph = True
print(args)
args.device = f"cuda:{args.device}" if torch.cuda.is_available() else "cpu"
args.device = torch.device(args.device)
set_seed(0)
###################################################
# load feats + graph
node_feats, edge_feats, g, df, args = load_all_data(args)
###################################################
# get model
model, args, link_pred_train = load_model(args)
###################################################
# Link prediction
print('Train link prediction task from scratch ...')
model = link_pred_train(model.to(args.device), args, g, df, node_feats, edge_feats)