forked from RexYing/diffpool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cross_val.py
47 lines (38 loc) · 1.73 KB
/
cross_val.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
import networkx as nx
import numpy as np
import torch
import pickle
import random
from graph_sampler import GraphSampler
def prepare_val_data(graphs, args, val_idx, max_nodes=0):
random.shuffle(graphs)
val_size = len(graphs) // 10
train_graphs = graphs[:val_idx * val_size]
if val_idx < 9:
train_graphs = train_graphs + graphs[(val_idx+1) * val_size :]
val_graphs = graphs[val_idx*val_size: (val_idx+1)*val_size]
print('Num training graphs: ', len(train_graphs),
'; Num validation graphs: ', len(val_graphs))
print('Number of graphs: ', len(graphs))
print('Number of edges: ', sum([G.number_of_edges() for G in graphs]))
print('Max, avg, std of graph size: ',
max([G.number_of_nodes() for G in graphs]), ', '
"{0:.2f}".format(np.mean([G.number_of_nodes() for G in graphs])), ', '
"{0:.2f}".format(np.std([G.number_of_nodes() for G in graphs])))
# minibatch
dataset_sampler = GraphSampler(train_graphs, normalize=False, max_num_nodes=max_nodes,
features=args.feature_type)
train_dataset_loader = torch.utils.data.DataLoader(
dataset_sampler,
batch_size=args.batch_size,
shuffle=True,
num_workers=args.num_workers)
dataset_sampler = GraphSampler(val_graphs, normalize=False, max_num_nodes=max_nodes,
features=args.feature_type)
val_dataset_loader = torch.utils.data.DataLoader(
dataset_sampler,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.num_workers)
return train_dataset_loader, val_dataset_loader, \
dataset_sampler.max_num_nodes, dataset_sampler.feat_dim, dataset_sampler.assign_feat_dim