-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
179 lines (146 loc) · 6.34 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
from core import console
with console.status('importing modules'):
import torch
import numpy as np
from rich import box
from rich.table import Table
from time import time
from typing import Annotated
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from core import globals
from core.datasets import DatasetLoader
from core.args.utils import print_args, create_arguments, strip_kwargs, ArgInfo
from core.loggers import Logger
from core.methods.node import supported_methods, NodeClassification
from core.utils import seed_everything, confidence_interval
from torch_geometric.data import Data
#from core.privacy.mechanisms.composed import *
def run(weight1, seed: Annotated[int, ArgInfo(help='initial random seed')] = 12345,
repeats: Annotated[int, ArgInfo(help='number of times the experiment is repeated')] = 1,
debug: Annotated[bool, ArgInfo(help='enable global debug mode')] = False,
**kwargs
):
seed_everything(seed)
if debug:
console.info('debug mode enabled')
globals['debug'] = True
console.log_level = console.DEBUG
with console.status('loading dataset'):
loader_args = strip_kwargs(DatasetLoader, kwargs)
data_initial = DatasetLoader(**loader_args).load(verbose=True)
num_classes = data_initial.y.max().item() + 1
config = dict(**kwargs, seed=seed, repeats=repeats)
logger_args = strip_kwargs(Logger.setup, kwargs)
logger = Logger.setup(enabled=False, config=config, **logger_args)
### initiallize method ###
Method = supported_methods[kwargs['method']]
method_args = strip_kwargs(Method, kwargs)
method: NodeClassification = Method(num_classes=num_classes, weight1 = weight1 ,**method_args)
run_metrics = {}
### run experiment ###
for iteration in range(repeats):
start_time = time()
data = Data(**data_initial.to_dict())
metrics = method.fit(data)
end_time = time()
metrics['duration'] = end_time - start_time
### process results ###
for metric, value in metrics.items():
run_metrics[metric] = run_metrics.get(metric, []) + [value]
### print results ###
table = Table(title=f'run {iteration + 1}', box=box.HORIZONTALS)
table.add_column('metric')
table.add_column('last', style="cyan")
table.add_column('mean', style="cyan")
table.add_row('test/acc', f'{run_metrics["test/acc"][-1]:.2f}', f'{np.mean(run_metrics["test/acc"]):.2f}')
console.info(table)
console.print()
### reset method's parameters for the next run ###
method.reset_parameters()
logger.enable()
summary = {}
for metric, values in run_metrics.items():
summary[metric + '_mean'] = np.mean(values)
summary[metric + '_std'] = np.std(values)
summary[metric + '_ci'] = confidence_interval(values, size=1000, ci=95, seed=seed)
logger.log_summary(summary)
logger.finish()
print()
print('test/acc', 'last', f'{run_metrics["test/acc"][-1]:.2f}', 'mean', f'{np.mean(run_metrics["test/acc"]):.2f}')
#print('weight_list', ComposedNoisyMechanism(NoisyMechanism).weight_list)
def main(weight1):
init_parser = ArgumentParser(add_help=False, conflict_handler='resolve')
method_subparser = init_parser.add_subparsers(dest='method', required=True, title='algorithm to use')
for method_name, method_class in supported_methods.items():
method_parser = method_subparser.add_parser(
name=method_name,
help=method_class.__doc__,
formatter_class=ArgumentDefaultsHelpFormatter
)
# dataset args
group_dataset = method_parser.add_argument_group('dataset arguments')
create_arguments(DatasetLoader, group_dataset)
# method args
group_method = method_parser.add_argument_group('method arguments')
create_arguments(method_class, group_method)
# experiment args
group_expr = method_parser.add_argument_group('experiment arguments')
create_arguments(run, group_expr)
create_arguments(Logger.setup, group_expr)
parser = ArgumentParser(parents=[init_parser], formatter_class=ArgumentDefaultsHelpFormatter)
kwargs = vars(parser.parse_args())
print_args(kwargs, num_cols=2)
try:
start = time()
run(weight1, **kwargs)
end = time()
console.info(f'Total running time: {(end - start):.2f} seconds.')
except KeyboardInterrupt:
print('\n')
console.warning('Graceful Shutdown')
except RuntimeError:
raise
finally:
if torch.cuda.is_available():
gpu_mem = torch.cuda.max_memory_allocated() / 1024 ** 3
console.info(f'Max GPU memory used = {gpu_mem:.2f} GB\n')
weight = [0.5, 1, 2, 4, 8, 16]
weight_list = [None] * 3 # Initialize weight_list with None values
all_lists = []
for i in range(len(weight)):
weight_list[0] = weight[i]
for j in range(len(weight)):
weight_list[1] = weight[j]
for k in range(len(weight)):
weight_list[2] = weight[k]
all_lists.append(weight_list.copy())
def simplify_ratios(lst):
ratios = []
for i in range(len(lst)):
for j in range(i+1, len(lst)):
ratio = []
for k in range(len(lst[i])):
if lst[j][k] == 0:
continue
elif lst[i][k] == 0:
ratio = None
break
else:
ratio.append(lst[j][k] / lst[i][k])
if ratio is not None and all(x == ratio[0] for x in ratio):
ratios.append((i,j,ratio[0]))
result = []
used_indices = set()
for i in range(len(lst)):
if i not in used_indices:
result.append(lst[i])
for (j,k,ratio) in ratios:
if i == j and k not in used_indices and all(lst[i][x]*ratio == lst[k][x] for x in range(len(lst[i]))):
used_indices.add(k)
elif i == k and j not in used_indices and all(lst[i][x]/ratio == lst[j][x] for x in range(len(lst[i]))):
used_indices.add(j)
return result
final_list = simplify_ratios(all_lists)
if __name__ == '__main__':
for i in range(len(final_list)):
main(final_list[i])