forked from smsharma/mining-for-substructure-lens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
executable file
·210 lines (186 loc) · 5.87 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
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
#! /usr/bin/env python
from __future__ import absolute_import, division, print_function, unicode_literals
import sys, os
sys.path.append("./")
import logging
import argparse
import numpy as np
from inference.estimator import ParameterizedRatioEstimator
from inference.utils import load_and_check
def train(
method,
alpha,
data_dir,
sample_name,
model_filename,
aux=False,
architecture="resnet",
log_input=False,
batch_size=128,
n_epochs=50,
optimizer="adam",
initial_lr=1.0e-4,
final_lr=1.0e-6,
limit_samplesize=None,
load=None,
zero_bias=False,
):
aux_data, n_aux = load_aux("{}/samples/z_{}.npy".format(data_dir, sample_name), aux)
if aux_data is None:
logging.info("%s aux variables", n_aux)
else:
logging.info("%s aux variables with shape %s", n_aux, aux_data.shape)
logging.info("")
logging.info("")
logging.info("")
logging.info("Creating estimator")
logging.info("")
estimator = ParameterizedRatioEstimator(
resolution=64,
n_parameters=2,
n_aux=n_aux,
architecture=architecture,
log_input=log_input,
rescale_inputs=True,
zero_bias=zero_bias,
)
if load is not None:
logging.info(
"Loading pre-trained model from %s", "{}/models/{}".format(data_dir, load)
)
estimator.load("{}/models/{}".format(data_dir, load))
estimator.train(
method,
x="{}/samples/x_{}.npy".format(data_dir, sample_name),
theta="{}/samples/theta_{}.npy".format(data_dir, sample_name),
theta_alt="{}/samples/theta_alt_{}.npy".format(data_dir, sample_name),
log_r_xz="{}/samples/log_r_xz_{}.npy".format(data_dir, sample_name),
log_r_xz_alt="{}/samples/log_r_xz_alt_{}.npy".format(data_dir, sample_name),
t_xz="{}/samples/t_xz_{}.npy".format(data_dir, sample_name),
t_xz_alt="{}/samples/t_xz_alt_{}.npy".format(data_dir, sample_name),
aux=aux_data,
alpha=alpha,
optimizer=optimizer,
n_epochs=n_epochs,
batch_size=batch_size,
initial_lr=initial_lr,
final_lr=final_lr,
nesterov_momentum=0.9,
validation_split=0.25,
early_stopping=True,
limit_samplesize=limit_samplesize,
verbose="all",
)
estimator.save("{}/models/{}".format(data_dir, model_filename))
def load_aux(filename, aux=False):
if aux:
return load_and_check(filename)[:, 2].reshape(-1, 1), 1
else:
return None, 0
def parse_args():
parser = argparse.ArgumentParser(
description="High-level script for the training of the neural likelihood ratio estimators"
)
# Main options
parser.add_argument(
"method",
help='Inference method: "carl", "rolr", "alice", "cascal", "rascal", "alices".',
)
parser.add_argument("sample", type=str, help='Sample name, like "train".')
parser.add_argument(
"name", type=str, help="Model name. Defaults to the name of the method."
)
parser.add_argument(
"-z", action="store_true", help="Proivide lens redshift to the network"
)
parser.add_argument(
"--dir",
type=str,
default=".",
help="Directory. Training data will be loaded from the data/samples subfolder, the model saved in the "
"data/models subfolder.",
)
# Training options
parser.add_argument(
"--vgg", action="store_true", help="Usee VGG rather than ResNet."
)
parser.add_argument(
"--deep",
action="store_true",
help="Use a deeper variation, i.e. ResNet-50 instead of ResNet-18.",
)
parser.add_argument(
"--alpha",
type=float,
default=0.0002,
help="alpha parameter weighting the score MSE in the loss function of the SCANDAL, RASCAL, and"
"and ALICES inference methods. Default: 0.0002",
)
parser.add_argument(
"--log", action="store_true", help="Whether the log of the input is taken."
)
parser.add_argument(
"--load",
default=None,
type=str,
help="Path of pretrained model that is loaded before training.",
)
parser.add_argument(
"--zerobias", action="store_true", help="Initialize with zero bias."
)
parser.add_argument(
"--epochs", type=int, default=100, help="Number of epochs. Default: 100."
)
parser.add_argument(
"--optimizer",
default="adam",
help='Optimizer. "amsgrad", "adam", and "sgd" are supported. Default: "adam".',
)
parser.add_argument(
"--batchsize", type=int, default=128, help="Batch size. Default: 128."
)
parser.add_argument(
"--lr",
type=float,
default=1.0e-4,
help="Initial learning rate. Default: 0.0001",
)
parser.add_argument(
"--lrdecay",
type=float,
default=1.0e-2,
help="Learning rate decay (final LR / initial LR). Default: 0.01",
)
return parser.parse_args()
if __name__ == "__main__":
logging.basicConfig(
format="%(asctime)-5.5s %(name)-20.20s %(levelname)-7.7s %(message)s",
datefmt="%H:%M",
level=logging.INFO,
)
logging.info("Hi!")
args = parse_args()
if args.vgg:
architecture = "vgg"
elif args.deep:
architecture = "resnet50"
else:
architecture = "resnet"
train(
method=args.method,
aux=args.z,
alpha=args.alpha,
data_dir="{}/data/".format(args.dir),
sample_name=args.sample,
model_filename=args.name,
log_input=args.log,
batch_size=args.batchsize,
initial_lr=args.lr,
final_lr=args.lrdecay * args.lr,
n_epochs=args.epochs,
optimizer=args.optimizer,
architecture=architecture,
zero_bias=args.zerobias,
load=args.load,
)
logging.info("All done! Have a nice day!")