-
Notifications
You must be signed in to change notification settings - Fork 11
/
setupGC.py
139 lines (118 loc) · 6.84 KB
/
setupGC.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
import random
from random import choices
import numpy as np
import pandas as pd
import torch
from torch_geometric.datasets import TUDataset
from torch_geometric.data import DataLoader
from torch_geometric.transforms import OneHotDegree
from models import GIN, serverGIN
from server import Server
from client import Client_GC
from utils import get_maxDegree, get_stats, split_data, get_numGraphLabels
def _randChunk(graphs, num_client, overlap, seed=None):
random.seed(seed)
np.random.seed(seed)
totalNum = len(graphs)
minSize = min(50, int(totalNum/num_client))
graphs_chunks = []
if not overlap:
for i in range(num_client):
graphs_chunks.append(graphs[i*minSize:(i+1)*minSize])
for g in graphs[num_client*minSize:]:
idx_chunk = np.random.randint(low=0, high=num_client, size=1)[0]
graphs_chunks[idx_chunk].append(g)
else:
sizes = np.random.randint(low=50, high=150, size=num_client)
for s in sizes:
graphs_chunks.append(choices(graphs, k=s))
return graphs_chunks
def prepareData_oneDS(datapath, data, num_client, batchSize, convert_x=False, seed=None, overlap=False):
if data == "COLLAB":
tudataset = TUDataset(f"{datapath}/TUDataset", data, pre_transform=OneHotDegree(491, cat=False))
elif data == "IMDB-BINARY":
tudataset = TUDataset(f"{datapath}/TUDataset", data, pre_transform=OneHotDegree(135, cat=False))
elif data == "IMDB-MULTI":
tudataset = TUDataset(f"{datapath}/TUDataset", data, pre_transform=OneHotDegree(88, cat=False))
else:
tudataset = TUDataset(f"{datapath}/TUDataset", data)
if convert_x:
maxdegree = get_maxDegree(tudataset)
tudataset = TUDataset(f"{datapath}/TUDataset", data, transform=OneHotDegree(maxdegree, cat=False))
graphs = [x for x in tudataset]
print(" **", data, len(graphs))
graphs_chunks = _randChunk(graphs, num_client, overlap, seed=seed)
splitedData = {}
df = pd.DataFrame()
num_node_features = graphs[0].num_node_features
for idx, chunks in enumerate(graphs_chunks):
ds = f'{idx}-{data}'
ds_tvt = chunks
ds_train, ds_vt = split_data(ds_tvt, train=0.8, test=0.2, shuffle=True, seed=seed)
ds_val, ds_test = split_data(ds_vt, train=0.5, test=0.5, shuffle=True, seed=seed)
dataloader_train = DataLoader(ds_train, batch_size=batchSize, shuffle=True)
dataloader_val = DataLoader(ds_val, batch_size=batchSize, shuffle=True)
dataloader_test = DataLoader(ds_test, batch_size=batchSize, shuffle=True)
num_graph_labels = get_numGraphLabels(ds_train)
splitedData[ds] = ({'train': dataloader_train, 'val': dataloader_val, 'test': dataloader_test},
num_node_features, num_graph_labels, len(ds_train))
df = get_stats(df, ds, ds_train, graphs_val=ds_val, graphs_test=ds_test)
return splitedData, df
def prepareData_multiDS(datapath, group='small', batchSize=32, convert_x=False, seed=None):
assert group in ['molecules', 'molecules_tiny', 'small', 'mix', "mix_tiny", "biochem", "biochem_tiny"]
if group == 'molecules' or group == 'molecules_tiny':
datasets = ["MUTAG", "BZR", "COX2", "DHFR", "PTC_MR", "AIDS", "NCI1"]
if group == 'small':
datasets = ["MUTAG", "BZR", "COX2", "DHFR", "PTC_MR", # small molecules
"ENZYMES", "DD", "PROTEINS"] # bioinformatics
if group == 'mix' or group == 'mix_tiny':
datasets = ["MUTAG", "BZR", "COX2", "DHFR", "PTC_MR", "AIDS", "NCI1", # small molecules
"ENZYMES", "DD", "PROTEINS", # bioinformatics
"COLLAB", "IMDB-BINARY", "IMDB-MULTI"] # social networks
if group == 'biochem' or group == 'biochem_tiny':
datasets = ["MUTAG", "BZR", "COX2", "DHFR", "PTC_MR", "AIDS", "NCI1", # small molecules
"ENZYMES", "DD", "PROTEINS"] # bioinformatics
splitedData = {}
df = pd.DataFrame()
for data in datasets:
if data == "COLLAB":
tudataset = TUDataset(f"{datapath}/TUDataset", data, pre_transform=OneHotDegree(491, cat=False))
elif data == "IMDB-BINARY":
tudataset = TUDataset(f"{datapath}/TUDataset", data, pre_transform=OneHotDegree(135, cat=False))
elif data == "IMDB-MULTI":
tudataset = TUDataset(f"{datapath}/TUDataset", data, pre_transform=OneHotDegree(88, cat=False))
else:
tudataset = TUDataset(f"{datapath}/TUDataset", data)
if convert_x:
maxdegree = get_maxDegree(tudataset)
tudataset = TUDataset(f"{datapath}/TUDataset", data, transform=OneHotDegree(maxdegree, cat=False))
graphs = [x for x in tudataset]
print(" **", data, len(graphs))
graphs_train, graphs_valtest = split_data(graphs, test=0.2, shuffle=True, seed=seed)
graphs_val, graphs_test = split_data(graphs_valtest, train=0.5, test=0.5, shuffle=True, seed=seed)
if group.endswith('tiny'):
graphs, _ = split_data(graphs, train=150, shuffle=True, seed=seed)
graphs_train, graphs_valtest = split_data(graphs, test=0.2, shuffle=True, seed=seed)
graphs_val, graphs_test = split_data(graphs_valtest, train=0.5, test=0.5, shuffle=True, seed=seed)
num_node_features = graphs[0].num_node_features
num_graph_labels = get_numGraphLabels(graphs_train)
dataloader_train = DataLoader(graphs_train, batch_size=batchSize, shuffle=True)
dataloader_val = DataLoader(graphs_val, batch_size=batchSize, shuffle=True)
dataloader_test = DataLoader(graphs_test, batch_size=batchSize, shuffle=True)
splitedData[data] = ({'train': dataloader_train, 'val': dataloader_val, 'test': dataloader_test},
num_node_features, num_graph_labels, len(graphs_train))
df = get_stats(df, data, graphs_train, graphs_val=graphs_val, graphs_test=graphs_test)
return splitedData, df
def setup_devices(splitedData, args):
idx_clients = {}
clients = []
for idx, ds in enumerate(splitedData.keys()):
idx_clients[idx] = ds
dataloaders, num_node_features, num_graph_labels, train_size = splitedData[ds]
cmodel_gc = GIN(num_node_features, args.hidden, num_graph_labels, args.nlayer, args.dropout)
# optimizer = torch.optim.Adam(cmodel_gc.parameters(), lr=args.lr, weight_decay=args.weight_decay)
optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, cmodel_gc.parameters()), lr=args.lr, weight_decay=args.weight_decay)
clients.append(Client_GC(cmodel_gc, idx, ds, train_size, dataloaders, optimizer, args))
smodel = serverGIN(nlayer=args.nlayer, nhid=args.hidden)
server = Server(smodel, args.device)
return clients, server, idx_clients