This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
300 lines (251 loc) · 6.9 KB
/
main.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
import logging
import argparse
from LSTM.main_fn import main_lstm
from GAN.main_fn import main_gan
from Transformer.main_fn import main_transformer
from FNO.main_fn import main_FNO
import torch
logging.basicConfig(
filename="application_log.log", level=logging.INFO, format="%(message)s"
)
def get_cmd_args():
parser = argparse.ArgumentParser(description="Train and test the model.")
parser.add_argument(
"--model", type=str, required=True, help="Model to use for training."
)
parser.add_argument(
"--model_save_name",
type=str,
default="LSTM_model_latest",
help="Model to use for training.",
)
parser.add_argument(
"--data_dir", type=str, required=True, help="Path to the data directory."
)
parser.add_argument(
"--num_epochs", type=int, default=1, help="Number of epochs for training."
)
parser.add_argument(
"--custom_loss", type=str, default="MSE", help="Custom loss function name."
)
# Add arguments for the shg and sfg weight losses
parser.add_argument(
"--shg_weight", type=float, default=None, help="Weight for the SHG loss."
)
parser.add_argument(
"--sfg_weight", type=float, default=None, help="Weight for the SFG loss."
)
parser.add_argument(
"--epoch_save_interval", type=int, default=1, help="Epoch save interval."
)
parser.add_argument(
"--tune_train",
type=int,
default=0,
help="Whether to do hyperparameter tuning or not.",
)
parser.add_argument("--output_dir", type=str, default=".", help="Output directory.")
parser.add_argument(
"--do_prediction",
type=int,
default=0,
help="Whether to do prediction or not.",
)
parser.add_argument(
"--do_analysis",
type=int,
default=0,
help="Whether to do prediction or not.",
)
parser.add_argument(
"--do_funky",
type=int,
default=0,
help="Whether to do funky prediction or not.",
)
parser.add_argument(
"--model_param_path",
type=str,
default=None,
help="Path to the model parameters.",
)
parser.add_argument(
"--verbose",
type=int,
default=1,
help="Whether to print the progress or not.",
)
parser.add_argument(
"--batch_size",
type=int,
default=7_500,
help="How large should the batch be to maximize GPU memory util",
)
# This would use [testset_last_file - 91] file, beca
parser.add_argument(
"--analysis_file",
type=int,
default=91,
help="The file to use for final analysis.",
)
parser.add_argument(
"--analysis_example",
type=int,
default=15,
help="The example of the file to use for analysis",
)
parser.add_argument(
"--crystal_length",
type=int,
default=100,
help="The assumed length for our crystal",
)
parser.add_argument(
"--is_slice",
type=int,
default=1,
help="Are we modeling a slice of the crystal or the whole crystal?",
)
parser.add_argument(
"--lstm_hidden_size",
type=int,
default=1024,
help="Hidden size of the LSTM model",
)
parser.add_argument(
"--lstm_num_layers",
type=int,
default=1,
help="Number of layers of the LSTM model",
)
parser.add_argument(
"--lstm_linear_layer_size",
type=int,
default=4096,
help="Number of layers of the LSTM model",
)
parser.add_argument(
"--loss_reduction",
type=str,
default="mean",
help="Reduction type for the loss function.",
)
parser.add_argument(
"--lr",
type=float,
default=1e-4,
help="Learning rate for the model.",
)
parser.add_argument(
"--lstm_dropout",
type=float,
default=0.3,
help="Dropout rate for the LSTM model.",
)
parser.add_argument(
"--fc_dropout",
type=float,
default=0.3,
help="Dropout rate for the fully connected layers.",
)
# 0 for training mode, 1 for test mode
# and 2 for "step-wise analysis" mode
parser.add_argument(
"--train_load_mode",
type=int,
default=0,
help="Load the train dataset in test mode.",
)
parser.add_argument(
"--val_load_mode",
type=int,
default=0,
help="Load the val dataset in test mode.",
)
parser.add_argument(
"--test_load_mode",
type=int,
default=1,
help="Load the test dataset in test mode.",
)
parser.add_argument(
"--shuffle",
type=int,
default=1,
help="Whether to shuffle the training dataset or not.",
)
parser.add_argument(
"--has_fc_dropout",
type=int,
default=1,
help="Whether to have the dropout layers in the post-LSTM linear layers.",
)
parser.add_argument(
"--bidirectional",
type=int,
default=0,
help="Whether to have the LSTM model bidirectional or not.",
)
parser.add_argument(
"--layernorm",
type=int,
default=0,
help="Whether to have LayerNorm in the LSTM model or not.",
)
parser.add_argument(
"--FNO_modes",
type=int,
default=16,
help="Number of modes for the FNO model.",
)
parser.add_argument(
"--FNO_lifted_dim",
type=int,
default=64,
help="Lifted dimension for the FNO model",
)
parser.add_argument(
"--FNO_fourier_layers",
type=int,
default=4,
help="Number of fourier layers for the FNO model.",
)
parser.add_argument(
"--custom_code",
type=int,
default=0,
help="If we want to run some temporary code but go through the main function.",
)
parser.add_argument(
"--load_in_gpu",
type=int,
default=1,
help="If we want to load the data in the GPU or not.",
)
parser.add_argument(
"--max_norm",
type=float,
default=1.0,
help="The maxium gradient norm for our clipper.",
)
return parser.parse_args()
# Currently using v2 (reIm) data
if __name__ == "__main__":
# Get the args from command line
args = get_cmd_args()
# what is the meaning of life?
# seed = 42
# torch.manual_seed(seed)
# if torch.cuda.is_available():
# torch.cuda.manual_seed(seed)
# torch.cuda.manual_seed_all(seed)
if "LSTM" in args.model:
main_lstm(args)
elif args.model == "Transformer":
main_transformer(args)
elif args.model == "GAN":
main_gan(args)
elif args.model == "FNO":
main_FNO(args)
else:
raise ValueError("Model not supported.")