Browse Source

seed everything

new-sep-loss
Michael Pilosov 10 months ago
parent
commit
762366b858
  1. 20
      main.py
  2. 4
      makefile
  3. 12
      search.py

20
main.py

@ -1,6 +1,9 @@
import argparse import argparse
import random
import numpy as np
import pytorch_lightning as pl import pytorch_lightning as pl
import torch
from pytorch_lightning.callbacks import EarlyStopping from pytorch_lightning.callbacks import EarlyStopping
from callbacks import SaveImageCallback from callbacks import SaveImageCallback
@ -29,6 +32,7 @@ def parse_args():
parser.add_argument( parser.add_argument(
"-L", "--log_every_n_steps", type=int, default=5, help="Logging frequency" "-L", "--log_every_n_steps", type=int, default=5, help="Logging frequency"
) )
parser.add_argument("--seed", default=21, type=int, help="Seed")
parser.add_argument( parser.add_argument(
"-w", "-w",
"--num_workers", "--num_workers",
@ -42,8 +46,21 @@ def parse_args():
return args return args
def seed_everything(seed=42):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # if you are using multi-GPU.
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
if __name__ == "__main__": if __name__ == "__main__":
args = parse_args() args = parse_args()
seed_everything(args.seed)
early_stop_callback = EarlyStopping( early_stop_callback = EarlyStopping(
monitor="hp_metric", # Metric to monitor monitor="hp_metric", # Metric to monitor
min_delta=1e-5, # Minimum change in the monitored quantity to qualify as an improvement min_delta=1e-5, # Minimum change in the monitored quantity to qualify as an improvement
@ -53,7 +70,7 @@ if __name__ == "__main__":
) )
save_img_callback = SaveImageCallback( save_img_callback = SaveImageCallback(
save_interval=1, save_interval=0,
final_dir="out", final_dir="out",
) )
@ -77,6 +94,7 @@ if __name__ == "__main__":
# Initialize trainer with parsed arguments # Initialize trainer with parsed arguments
trainer = pl.Trainer( trainer = pl.Trainer(
deterministic=True,
callbacks=[early_stop_callback, save_img_callback], callbacks=[early_stop_callback, save_img_callback],
max_epochs=args.max_epochs, max_epochs=args.max_epochs,
log_every_n_steps=args.log_every_n_steps, log_every_n_steps=args.log_every_n_steps,

4
makefile

@ -14,3 +14,7 @@ animate:
-c:v libx264 \ -c:v libx264 \
-vf "fps=12,format=yuv420p,pad=ceil(iw/2)*2:ceil(ih/2)*2" \ -vf "fps=12,format=yuv420p,pad=ceil(iw/2)*2:ceil(ih/2)*2" \
~/animated.mp4 ~/animated.mp4
clean:
rm -rf lightning_logs/*
rm out/*.png

12
search.py

@ -18,19 +18,21 @@ job_plugin = studio.installed_plugins["jobs"]
# do a sweep over learning rates # do a sweep over learning rates
# Define the ranges or sets of values for each hyperparameter # Define the ranges or sets of values for each hyperparameter
alpha_values = list(np.round(np.linspace(2, 6, 41), 4)) alpha_values = list(np.round(np.linspace(2, 4, 21), 4))
# learning_rate_values = list(np.round(np.logspace(-5, -3, 41), 5)) # learning_rate_values = list(np.round(np.logspace(-5, -3, 41), 5))
learning_rate_values = [5e-4] learning_rate_values = [5e-4]
batch_size_values = [64, 128] batch_size_values = [128]
max_epochs_values = [500] max_epochs_values = [500]
seeds = list(range(21, 1992))
# Generate all possible combinations of hyperparameters # Generate all possible combinations of hyperparameters
all_params = [ all_params = [
(alpha, lr, bs, me) (alpha, lr, bs, me, s)
for alpha in alpha_values for alpha in alpha_values
for lr in learning_rate_values for lr in learning_rate_values
for bs in batch_size_values for bs in batch_size_values
for me in max_epochs_values for me in max_epochs_values
for s in seeds
] ]
@ -38,8 +40,8 @@ all_params = [
search_params = sample(all_params, min(NUM_JOBS, len(all_params))) search_params = sample(all_params, min(NUM_JOBS, len(all_params)))
for idx, params in enumerate(search_params): for idx, params in enumerate(search_params):
a, lr, bs, me = params a, lr, bs, me, s = params
cmd = f"cd ~/colors && python main.py --alpha {a} --lr {lr} --bs {bs} --max_epochs {me}" cmd = f"cd ~/colors && python main.py --alpha {a} --lr {lr} --bs {bs} --max_epochs {me} --seed {s}"
# job_name = f"color2_{bs}_{a}_{lr:2.2e}" # job_name = f"color2_{bs}_{a}_{lr:2.2e}"
# job_plugin.run(cmd, machine=Machine.T4, name=job_name) # job_plugin.run(cmd, machine=Machine.T4, name=job_name)
print(f"Running {params}: {cmd}") print(f"Running {params}: {cmd}")

Loading…
Cancel
Save