-
Notifications
You must be signed in to change notification settings - Fork 3
/
experiment.py
89 lines (75 loc) · 2.7 KB
/
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
Module for using the comet-ml experiment framework
"""
import os
import time
from typing import Sequence
from types import SimpleNamespace
from comet_ml import Experiment, ExistingExperiment
from utils import get_version_string
def initialize_experiment(
args: SimpleNamespace, params: Sequence[str] = tuple(), experiment_name: str = ""
) -> Experiment:
"""
Initialize experiment tracking if specified
"""
track = args.track
version = get_version_string()
if track and "-dirty" in version:
raise RuntimeError(
"Trying to track an experiment, but the workspace is dirty! "
"Commit your changes first, then try again."
)
api_key = None if track else ""
experiment_type = Experiment
experiment_args = [api_key]
experiment_path = os.path.join(args.output_dir, "experiment.guid")
if not isinstance(track, str) and os.path.exists(experiment_path):
track = experiment_path
if isinstance(track, str):
experiment_type = ExistingExperiment
if track.endswith(".guid"):
wait_count = 0
while not os.path.exists(track):
wait_string = "..."[: wait_count % 4]
wait_count += 1
print(
f"\r\033[KWaiting for experiment: {track} {wait_string}", end="",
)
time.sleep(1)
print(f"\r\033[KLoading experiment: {track}")
with open(track, "rt") as guid_file:
experiment_args.append(guid_file.readline().strip())
else:
experiment_args.append(track)
experiment = experiment_type(
*experiment_args,
project_name="storium-gpt2-baselines",
workspace="umass-nlp",
disabled=not track,
auto_metric_logging=False,
auto_output_logging=False,
auto_param_logging=False,
log_git_metadata=False,
log_git_patch=False,
log_env_details=False,
log_graph=False,
log_code=False,
parse_args=False,
)
if track and experiment_type == Experiment:
with open(experiment_path, "wt") as guid_file:
guid_file.write(experiment.id)
# This needs to be called separately to disable monkey patching of the
# ML frameworks which is on by default :(
experiment.disable_mp()
if experiment_type is Experiment:
experiment.log_parameter("version", version)
for name in params:
experiment.log_parameters(
getattr(getattr(args, name, None), "__dict__", {}), prefix=name
)
if experiment_name:
# name the experiment if specified
experiment.set_name(experiment_name)
return experiment