-
Notifications
You must be signed in to change notification settings - Fork 11
/
parallel_experiment.py
70 lines (56 loc) · 2.12 KB
/
parallel_experiment.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
import argparse
import os
import pickle
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
from logistic_parallel import LogisticParallelSGD
from parameters import Parameters
from utils import pickle_it
parser = argparse.ArgumentParser()
parser.add_argument('--dataset_file', type=str, help="pickle file of the dataset (X, y)")
parser.add_argument('--output_directory', type=str)
parser.add_argument('--num_epochs', type=int)
parser.add_argument('--num_cores', type=int)
parser.add_argument('--model', choices=['top', 'rand', 'sgd'])
parser.add_argument('--k', type=int)
parser.add_argument('--initial_lr', type=float)
parser.add_argument('--lr', choices=['bottou'])
args = parser.parse_args()
if args.k and args.model == 'sgd':
args.k = None
print('k ignored')
# create uniquely named suffix for the results
name = "{}{}-cores{}".format(args.model, args.k or "", args.num_cores)
directory = os.path.join(args.output_directory, name)
i = 0
while os.path.exists("{}-{}".format(directory, i)):
i += 1
directory = "{}-{}".format(directory, i)
os.makedirs(directory)
print('will save results in "{}"'.format(directory))
with_memory = args.model in ['top', 'rand']
print('use memory', with_memory)
use_top = (args.model == 'top')
print('use top coordinates', use_top)
print('load dataset')
with open(args.dataset_file, 'rb') as f:
X, y = pickle.load(f)
n, d = X.shape
model = Parameters(num_epoch=args.num_epochs,
lr_type=args.lr,
initial_lr=args.initial_lr,
n_cores=args.num_cores,
regularizer=1. / n,
take_k=args.k,
take_top=args.model is 'top',
with_memory=with_memory,
estimate='final')
print('start experiment')
m = LogisticParallelSGD(model)
iters, timers, losses = m.fit(X, y,
num_features=X.shape[1],
num_samples=X.shape[0])
pickle_it(iters, 'iters', directory)
pickle_it(timers, 'timers', directory)
pickle_it(losses, 'losses', directory)
print('results saved in "{}"'.format(directory))