-
Notifications
You must be signed in to change notification settings - Fork 0
/
RCOV_mixed_informatio.py
366 lines (326 loc) · 23.6 KB
/
RCOV_mixed_informatio.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import torch
import torch_geometric as pyg
from models import RCOVmodel
from networkx.generators.random_graphs import erdos_renyi_graph
import wandb
import argparse
import utils
import numpy as np
import random
import train_utils
if __name__ == "__main__":
global args
parser = argparse.ArgumentParser()
parser.add_argument('--num_nodes', dest='num_nodes', type=int, default=20)
parser.add_argument('--lr', dest='lr', type=float, default=0.0001)
parser.add_argument('--d', dest='d', type=int, default=16)
parser.add_argument('--hidden_channels', dest='hidden_channels', type=int, default=64)
parser.add_argument('--n_layers', dest='n_layers', type=int, default=1)
parser.add_argument('--num_test_samples', dest='num_test_samples', type=int, default=100)
parser.add_argument('--train_batch_size', dest='train_batch_size', type=int, default=8)
parser.add_argument('--num_epochs', dest='num_epochs', type=int, default=1000)
parser.add_argument('--lin_trans_for_features_dim', dest='lin_trans_for_features_dim', type=int, default=128)
parser.add_argument('--RCOV_threshold', dest='RCOV_threshold', type=float, default=0.15)
parser.add_argument('--patience', dest='patience', type=int, default=100)
parser.add_argument('--wandb', dest='wandb', action='store_true')
args = parser.parse_args()
num_nodes = args.num_nodes
num_samples = args.num_samples
num_test_samples = args.num_test_samples
margin = 0.01
RCOV_threshold = args.RCOV_threshold
output_dim = 2
d = args.d
n_layers = args.n_layers
train_batch_size = args.train_batch_size
hidden_channels = args.hidden_channels
model_type = RCOVmodel
wd = 0.0001
lin_trans_for_features_dim = args.lin_trans_for_features_dim
n_conv_layers = args.n_layers
loss = torch.nn.CrossEntropyLoss
num_epochs = args.num_epochs
lr = args.lr
train_p = 0.5
optimizer_type = torch.optim.Adam
num_training_samples_for_learning_curve = [10, 20, 50, 80, 100, 200, 500, 1000, 2000, 3000, 5000, 7000, 10000]
max_num_of_sampels = num_training_samples_for_learning_curve[-1]
np.random.seed(0)
teacher = utils.sample_teacher(d, self=True, top=False) # should have unit norm
train_dist_type = erdos_renyi_graph
train_dist = lambda num_nodes: train_dist_type(n=num_nodes, p=train_p)
fixed_lin_transoform = np.random.random(size=(num_nodes, lin_trans_for_features_dim))
seeds = np.random.randint(low=0, high=10000, size=10)
test_informative_accs = []
test_non_informative_accs = []
test_non_informative_rcov_accs = []
test_empty_graphs_accs = []
for i in range(10):
seed = seeds[i]
np.random.seed(seed)
torch.manual_seed(seed)
random.seed(seed)
train_informative_pyg, train_informative_nx = utils.get_dif_graphs_dataset(num_samples=max_num_of_sampels,
num_nodes=num_nodes, num_features=d,
teacher=teacher,
graph_dist=train_dist,
margin=margin,
self_teacher=True,
with_edge_weight=True)
utils.encode_undirected_graph_in_features_then_linear_transform(train_informative_pyg, fixed_lin_transoform)
utils.add_random_uniform_feature(train_informative_pyg)
random_uniform_feature_index = train_informative_pyg[0].x.shape[1] - 1
utils.add_constant_feature_1(train_informative_pyg)
train_informative_mean, train_informative_std, train_informative_skew, train_informative_kurt = utils.get_moments(
train_informative_nx)
utils.set_label_to_num_of_edges(train_informative_pyg, threshold=train_informative_mean * num_nodes)
val_informative_pyg, _ = utils.get_dif_graphs_dataset(num_samples=num_test_samples,
num_nodes=num_nodes, num_features=d,
teacher=teacher, graph_dist=train_dist,
margin=margin,
self_teacher=True,
with_edge_weight=True)
utils.encode_undirected_graph_in_features_then_linear_transform(val_informative_pyg, fixed_lin_transoform)
utils.add_random_uniform_feature(val_informative_pyg)
utils.add_constant_feature_1(val_informative_pyg)
utils.set_label_to_num_of_edges(val_informative_pyg, threshold=train_informative_mean * num_nodes)
test_informative_pyg, _ = utils.get_dif_graphs_dataset(num_samples=max_num_of_sampels,
num_nodes=num_nodes, num_features=d,
teacher=teacher, graph_dist=train_dist,
margin=margin,
self_teacher=True,
with_edge_weight=True)
utils.encode_undirected_graph_in_features_then_linear_transform(test_informative_pyg, fixed_lin_transoform)
utils.add_random_uniform_feature(test_informative_pyg)
utils.add_constant_feature_1(test_informative_pyg)
utils.set_label_to_num_of_edges(test_informative_pyg, threshold=train_informative_mean * num_nodes)
train_non_informative_pyg = utils.build_new_graphs_from_feature(train_informative_pyg,
feature_index=random_uniform_feature_index,
with_edge_weight=True)
val_non_informative_pyg = utils.build_new_graphs_from_feature(val_informative_pyg,
feature_index=random_uniform_feature_index,
with_edge_weight=True)
test_non_informative_pyg = utils.build_new_graphs_from_feature(test_informative_pyg,
feature_index=random_uniform_feature_index,
with_edge_weight=True)
train_non_informative_rcov_pyg, _ = utils.batch_adges_reduce_ratio_add_random_edges_to_pyg_graphs(
pyg_graphs=train_non_informative_pyg, reduce_goal=RCOV_threshold)
val_non_informative_rcov_pyg, _ = utils.batch_adges_reduce_ratio_add_random_edges_to_pyg_graphs(
pyg_graphs=val_non_informative_pyg, reduce_goal=RCOV_threshold)
test_non_informative_rcov_pyg, _ = utils.batch_adges_reduce_ratio_add_random_edges_to_pyg_graphs(
pyg_graphs=test_non_informative_pyg, reduce_goal=RCOV_threshold)
if torch.cuda.is_available():
device = torch.device("cuda")
else:
device = torch.device("cpu")
num_of_features = test_informative_pyg[0].x.shape[1]
config = {
'num_test_samples': num_test_samples,
'd': d,
'num_nodes': num_nodes,
'lr': lr,
'loss': loss.__name__,
'train_batch_size': train_batch_size,
'hidden_channels': hidden_channels,
'n_conv_layers': n_conv_layers,
'output_dim': output_dim,
'num_epochs': num_epochs,
'optimizer': optimizer_type.__name__,
'num_final_feauturs': num_of_features,
'device': device.type,
'num_training_samples_for_learning_curve': num_training_samples_for_learning_curve,
'max_num_of_sampels': max_num_of_sampels,
'wd': wd,
'num_data_samples': len(train_informative_pyg),
'data_dir_number': args.data_dir_number,
'RCOV_threshold': RCOV_threshold,
'iter': i,
}
if args.wandb:
for name, val in config.items():
print(f'{name}: {val}')
run = wandb.init(project='your project', reinit=True, entity='your entity', config=config)
val_informative_loader = pyg.loader.DataLoader(val_informative_pyg[:num_test_samples],
batch_size=num_test_samples)
val_non_informative_loader = pyg.loader.DataLoader(val_non_informative_pyg[:num_test_samples],
batch_size=num_test_samples)
val_non_informative_rcov_loader = pyg.loader.DataLoader(val_non_informative_rcov_pyg[:num_test_samples],
batch_size=num_test_samples)
val_empty_graphs = utils.get_empty_graphs_from_datasets(val_informative_pyg[:num_test_samples])
val_empty_graphs_loader = pyg.loader.DataLoader(val_empty_graphs, batch_size=num_test_samples)
test_informative_loader = pyg.loader.DataLoader(test_informative_pyg[:num_test_samples],
batch_size=num_test_samples)
test_non_informative_loader = pyg.loader.DataLoader(test_non_informative_pyg[:num_test_samples],
batch_size=num_test_samples)
test_non_informative_rcov_loader = pyg.loader.DataLoader(test_non_informative_rcov_pyg[:num_test_samples],
batch_size=num_test_samples)
test_empty_graphs = utils.get_empty_graphs_from_datasets(test_informative_loader)
test_empty_graphs_loader = pyg.loader.DataLoader(test_empty_graphs[:num_test_samples],
batch_size=num_test_samples)
train_empty_graphs = utils.get_empty_graphs_from_datasets(train_informative_pyg)
for num_training_samples in num_training_samples_for_learning_curve:
if args.wandb:
print(f'Training with {num_training_samples} samples')
wandb.log({'num_training_samples': num_training_samples})
train_informative_loader = pyg.loader.DataLoader(train_informative_pyg[:num_training_samples],
batch_size=train_batch_size)
model_informative = model_type(in_channels=num_of_features, out_channels=output_dim, num_layers=n_layers,
hidden_channels=hidden_channels)
optimizer = optimizer_type(params=model_informative.parameters(), lr=lr, weight_decay=wd)
model_informative.to(device)
early_stop = utils.EarlyStopping(patience=args.patience)
loss_fn = loss()
for epoch in range(num_epochs):
print(f'epoch: {epoch}, num_training_samples: {num_training_samples}, iter: {i}, informative graphs')
train_informative_loss, train_informative_acc = train_utils.train_epoch(model_informative,
dloader=train_informative_loader,
loss_fn=loss_fn,
optimizer=optimizer,
classify=True,
device=device)
val_informative_loss, val_informative_acc = train_utils.test_epoch(model_informative,
dloader=val_informative_loader,
loss_fn=loss_fn,
classify=True,
device=device)
early_stop(val_informative_loss)
if early_stop.early_stop:
print(f'early stop at epoch: {epoch}')
break
if args.wandb:
wandb.log({'val_informative_loss': val_informative_loss, 'val_informative_acc': val_informative_acc,
'train_informative_loss': train_informative_loss,
'train_informative_acc': train_informative_acc,
'num_training_samples': num_training_samples})
train_non_informative_loader = pyg.loader.DataLoader(train_non_informative_pyg[:num_training_samples],
batch_size=train_batch_size)
model_non_informative = model_type(in_channels=num_of_features, out_channels=output_dim,
num_layers=n_layers,
hidden_channels=hidden_channels)
optimizer = optimizer_type(params=model_non_informative.parameters(), lr=lr, weight_decay=wd)
model_non_informative.to(device)
early_stop = utils.EarlyStopping(patience=args.patience)
loss_fn = loss()
for epoch in range(num_epochs):
print(f'epoch: {epoch}, num_training_samples: {num_training_samples}, iter: {i}, non-informative graphs')
train_non_informative_loss, train_non_informative_acc = train_utils.train_epoch(model_non_informative,
dloader=train_non_informative_loader,
loss_fn=loss_fn,
optimizer=optimizer,
classify=True,
device=device)
val_non_informative_loss, val_non_informative_acc = train_utils.test_epoch(model_non_informative,
dloader=val_non_informative_rcov_loader,
loss_fn=loss_fn,
classify=True,
device=device)
early_stop(val_non_informative_loss)
if early_stop.early_stop:
print(f'early stop at epoch: {epoch}')
break
if args.wandb:
wandb.log({'val_non_informative_loss': val_non_informative_loss,
'val_non_informative_acc': val_non_informative_acc,
'train_non_informative_loss': train_non_informative_loss,
'train_non_informative_acc': train_non_informative_acc})
train_non_informative_rcov_loader = pyg.loader.DataLoader(train_non_informative_rcov_pyg[:num_training_samples], batch_size=train_batch_size)
model_non_informative_rcov = model_type(in_channels=num_of_features, out_channels=output_dim,
num_layers=n_layers,
hidden_channels=hidden_channels)
optimizer = optimizer_type(params=model_non_informative_rcov.parameters(), lr=lr, weight_decay=wd)
model_non_informative_rcov.to(device)
early_stop = utils.EarlyStopping(patience=args.patience)
loss_fn = loss()
for epoch in range(num_epochs):
train_non_informative_rcov_loss, train_non_informative_rcov_acc = train_utils.train_epoch(model_non_informative_rcov,
dloader=train_non_informative_rcov_loader,
loss_fn=loss_fn,
optimizer=optimizer,
classify=True,
device=device)
val_non_informative_rcov_loss, val_non_informative_rcov_acc = train_utils.test_epoch(model_non_informative_rcov,
dloader=val_non_informative_rcov_loader,
loss_fn=loss_fn,
classify=True,
device=device)
early_stop(val_non_informative_rcov_loss)
if early_stop.early_stop:
print(f'early stop at epoch: {epoch}')
break
if args.wandb:
wandb.log({'val_non_informative_rcov_loss': val_non_informative_rcov_loss,
'val_non_informative_rcov_acc': val_non_informative_rcov_acc,
'train_non_informative_rcov_loss': train_non_informative_rcov_loss,
'train_non_informative_rcov_acc': train_non_informative_rcov_acc})
train_empty_graphs_loader = pyg.loader.DataLoader(train_empty_graphs[:num_training_samples],
batch_size=train_batch_size)
model_empty_graphs = model_type(in_channels=num_of_features, out_channels=output_dim, num_layers=n_layers,
hidden_channels=hidden_channels)
optimizer = optimizer_type(params=model_empty_graphs.parameters(), lr=lr, weight_decay=wd)
model_empty_graphs.to(device)
early_stop = utils.EarlyStopping(patience=args.patience)
loss_fn = loss()
for epoch in range(num_epochs):
train_empty_graphs_loss, train_empty_graphs_acc = train_utils.train_epoch(model_empty_graphs,
dloader=train_empty_graphs_loader,
loss_fn=loss_fn,
optimizer=optimizer,
classify=True,
device=device)
val_empty_graphs_loss, val_empty_graphs_acc = train_utils.test_epoch(model_empty_graphs,
dloader=val_empty_graphs_loader,
loss_fn=loss_fn,
classify=True,
device=device)
early_stop(val_empty_graphs_loss)
if early_stop.early_stop:
print(f'early stop at epoch: {epoch}')
break
if args.wandb:
wandb.log({'val_empty_graphs_loss': val_empty_graphs_loss,
'val_empty_graphs_acc': val_empty_graphs_acc,
'train_empty_graphs_loss': train_empty_graphs_loss,
'train_empty_graphs_acc': train_empty_graphs_acc})
# test
test_informative_loss, test_informative_acc = train_utils.test_epoch(model_informative,
dloader=test_informative_loader,
loss_fn=loss_fn,
classify=True,
device=device)
test_non_informative_loss, test_non_informative_acc = train_utils.test_epoch(model_non_informative,
dloader=test_non_informative_loader,
loss_fn=loss_fn,
classify=True,
device=device)
test_non_informative_rcov_loss, test_non_informative_rcov_acc = train_utils.test_epoch(model_non_informative_rcov,
dloader=test_non_informative_rcov_loader,
loss_fn=loss_fn,
classify=True,
device=device)
test_empty_graphs_loss, test_empty_graphs_acc = train_utils.test_epoch(model_empty_graphs,
dloader=test_empty_graphs_loader,
loss_fn=loss_fn,
classify=True,
device=device)
test_informative_accs.append(test_informative_acc)
test_non_informative_accs.append(test_non_informative_acc)
test_non_informative_rcov_accs.append(test_non_informative_rcov_acc)
test_empty_graphs_accs.append(test_empty_graphs_acc)
if args.wandb:
wandb.log({'test_informative_loss': test_informative_loss,
'test_informative_acc': test_informative_acc,
'test_non_informative_loss': test_non_informative_loss,
'test_non_informative_acc': test_non_informative_acc,
'test_non_informative_rcov_loss': test_non_informative_rcov_loss,
'test_non_informative_rcov_acc': test_non_informative_rcov_acc,
'test_empty_graphs_loss': test_empty_graphs_loss,
'test_empty_graphs_acc': test_empty_graphs_acc})
if args.wandb:
wandb.log({'test_informative_acc': np.mean(test_informative_accs),
'test_non_informative_acc': np.mean(test_non_informative_accs),
'test_non_informative_rcov_acc': np.mean(test_non_informative_rcov_accs),
'test_empty_graphs_acc': np.mean(test_empty_graphs_accs),
'test_informative_std': np.std(test_informative_accs),
'test_non_informative_std': np.std(test_non_informative_accs),
'test_non_informative_rcov_std': np.std(test_non_informative_rcov_accs),
'test_empty_graphs_std': np.std(test_empty_graphs_accs)})