-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtrainval.py
196 lines (158 loc) · 6.22 KB
/
trainval.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
from haven import haven_chk as hc
from haven import haven_results as hr
from haven import haven_utils as hu
import torch
import torchvision
import tqdm
import pandas as pd
import pprint
import itertools
import os
import pylab as plt
import exp_configs
import time
import numpy as np
import os
import pandas as pd
from tqdm import tqdm
from src import utils as ut
from src import datasets
from src import losses
from scipy.io import savemat
from src.partition_rules import partition_rules
from src.selection_rules import VB_selection_rules
from src.selection_rules import FB_selection_rules
from src.update_rules import update_rules
from src import datasets
from src.partition_rules import partition_rules
from src.selection_rules import VB_selection_rules
from src.selection_rules import FB_selection_rules
from src.update_rules import update_rules
import argparse
from src import utils
from torch.utils.data import sampler
from torch.utils.data.sampler import RandomSampler
from torch.backends import cudnn
from torch.nn import functional as F
from torch.utils.data import DataLoader
def trainval(exp_dict, savedir_base, datadir, reset=False, num_workers=0):
# bookkeepting stuff
# ==================
pprint.pprint(exp_dict)
exp_id = hu.hash_dict(exp_dict)
savedir = os.path.join(savedir_base, exp_id)
if reset:
hc.delete_and_backup_experiment(savedir)
os.makedirs(savedir, exist_ok=True)
if not os.path.join(savedir, "exp_dict.json"):
hu.save_json(os.path.join(savedir, "exp_dict.json"), exp_dict)
print("Experiment saved in %s" % savedir)
# BCD train
# ==================
# Ignore the following combinations
if not ut.is_valid_exp(exp_dict):
return
score_list_fname = os.path.join(savedir, 'score_list.pkl')
if os.path.exists(score_list_fname):
score_list = hu.load_pkl(score_list_fname)
else:
score_list = train(dataset_name=exp_dict['dataset']['name'],
loss_name=exp_dict['dataset']['loss'],
block_size=exp_dict['block_size'],
partition_rule=exp_dict['partition'],
selection_rule=exp_dict['selection'],
update_rule=exp_dict['update'],
n_iters=exp_dict['max_iters'],
L1=exp_dict.get('l1', 0),
L2=0,
datasets_path=datadir)
hu.save_pkl(score_list_fname, score_list)
print('Experiment completed.')
return score_list
def train(dataset_name, loss_name, block_size, partition_rule,
selection_rule,
update_rule, n_iters, L1, L2, optimal=None,
datasets_path=""):
np.random.seed(1)
# load dataset
dataset = datasets.load(dataset_name, path=datasets_path)
A, b, args = dataset["A"], dataset["b"], dataset["args"]
args.update({"L2": L2, "L1": L1, "block_size": block_size,
"update_rule": update_rule})
# loss function
lossObject = losses.create_lossObject(loss_name, A, b, args)
# Get partitions
partition = partition_rules.get_partition(
A, b, lossObject, block_size, p_rule=partition_rule)
# Initialize x
x = np.zeros(lossObject.n_params)
score_list = []
pbar = tqdm(desc="starting", total=n_iters, leave=True)
###### TRAINING STARTS HERE ############
block = np.array([])
for i in range(n_iters + 1):
# Compute loss
loss = lossObject.f_func(x, A, b)
dis2opt = loss - \
exp_configs.OPTIMAL_LOSS[dataset_name + "_" + loss_name]
score_list += [{"loss": loss, "iteration": i, "selected": block}]
stdout = ("%d - %s_%s_%s - dis2opt:%.16f - nz: %d/%d" %
(i, partition_rule, selection_rule, update_rule, dis2opt, (x != 0).sum(), x.size))
print(stdout)
# Check convergence
if (i > 5 and (np.array_equal(work, np.where(x > 1e-16)[0]))):
score_list[-1]["converged"] = dis2opt
if (i > 5 and (dis2opt == 0 or dis2opt < 1e-8)):
break
# Check increase
if (i > 0) and (loss > score_list[-1]["loss"] + 1e-6):
raise ValueError("loss value has increased...")
# Select block
if partition is None:
block, args = VB_selection_rules.select(
selection_rule, x, A, b, lossObject, args, iteration=i)
else:
block, args = FB_selection_rules.select(
selection_rule, x, A, b, lossObject, args, partition, iteration=i)
# Update block
x, args = update_rules.update(
update_rule, x, A, b, lossObject, args=args, block=block, iteration=i)
pbar.close()
for score_dict in score_list:
score_dict["loss"] -= exp_configs.OPTIMAL_LOSS[dataset_name + "_" + loss_name]
return score_list
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-e', '--exp_group_list', nargs="+")
parser.add_argument('-sb', '--savedir_base', required=True)
parser.add_argument('-d', '--datadir', required=True)
parser.add_argument("-r", "--reset", default=0, type=int)
parser.add_argument("-ei", "--exp_id", default=None)
parser.add_argument("-j", "--run_jobs", default=0, type=int)
parser.add_argument("-nw", "--num_workers", type=int, default=0)
args = parser.parse_args()
# Collect experiments
# ===================
if args.exp_id is not None:
# select one experiment
savedir = os.path.join(args.savedir_base, args.exp_id)
exp_dict = hu.load_json(os.path.join(savedir, "exp_dict.json"))
exp_list = [exp_dict]
else:
# select exp group
exp_list = []
for exp_group_name in args.exp_group_list:
exp_list += exp_configs.EXP_GROUPS[exp_group_name]
# Run experiments
# ===============
if args.run_jobs:
import usr_configs as uc
uc.run_jobs(exp_list, args.savedir_base, args.datadir)
else:
for exp_dict in exp_list:
# do trainval
trainval(exp_dict=exp_dict,
savedir_base=args.savedir_base,
datadir=args.datadir,
reset=args.reset,
num_workers=args.num_workers)