From 03c817412c638c905dea6c1a1967691f3ced57b8 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Thu, 27 May 2021 19:59:03 +0200 Subject: Add configurable run script --- experiment/__init__.py | 0 experiment/__main__.py | 17 +++++++++++++++++ experiment/experiment.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 experiment/__init__.py create mode 100644 experiment/__main__.py create mode 100644 experiment/experiment.py (limited to 'experiment') diff --git a/experiment/__init__.py b/experiment/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/experiment/__main__.py b/experiment/__main__.py new file mode 100644 index 0000000..bf4de94 --- /dev/null +++ b/experiment/__main__.py @@ -0,0 +1,17 @@ +import sys + +import torch + +from experiment.experiment import Experiment + +file = None +if len(sys.argv) < 2: + print('Provide YAML configuration file as argument') + exit(1) + +device = torch.device("cuda" if torch.cuda.is_available() else "cpu") +file = sys.argv[1] +prefix = sys.argv[2] if len(sys.argv) > 2 else '' + +experiment = Experiment(file, prefix, device) +experiment.run() diff --git a/experiment/experiment.py b/experiment/experiment.py new file mode 100644 index 0000000..600732f --- /dev/null +++ b/experiment/experiment.py @@ -0,0 +1,32 @@ +import subprocess +import os +import time + +from train.train import train_model +from util.parse_config import parse_file +from model.encoder import EncoderModel + +class Experiment: + def __init__(self, file, prefix, device): + self.file = file + self.device = device + self.make_dir(prefix) + self.copy_config(file) + + def run(self): + model_config, train_config = parse_file(self.file) + model = EncoderModel(device=self.device, **model_config).to(self.device) + train_model(model, device=self.device, **train_config) + + def dir_path(self, file): + return '{}/{}'.format(self.dirname, file) + + def make_dir(self, prefix): + time_string = time.strftime('%Y%m%d%H%M%S') + prefix = '' if prefix == '' else '{}-'.format(prefix) + dirname = 'outputs/{}{}'.format(prefix, time_string) + self.dirname = dirname + os.mkdir(dirname) + + def copy_config(self, file): + subprocess.run(['cp', file, '{}/config.yaml'.format(self.dirname)]) -- cgit v1.2.3