-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexperiment.py
239 lines (203 loc) · 7.04 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
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
import os
import torch
import logging
import pandas as pd
from data import GlossingDataset
from pytorch_lightning import Trainer
from ctc_model import CTCGlossingModel
from containers import Hyperparameters
from morpheme_model import MorphemeGlossingModel
from pytorch_lightning import loggers as pl_loggers
from pytorch_lightning.callbacks import EarlyStopping
from pytorch_lightning.callbacks import ModelCheckpoint
language_code_mapping = {
"Arapaho": "arp",
"Gitksan": "git",
"Lezgi": "lez",
"Natugu": "ntu",
"Nyangbo": "nyb",
"Tsez": "ddo",
"Uspanteko": "usp",
}
model_type_mapping = {"ctc": CTCGlossingModel, "morph": MorphemeGlossingModel}
def _make_experiment_name(
language: str,
track: int,
model_type: str,
hyperparameters: Hyperparameters,
trial: int,
):
experiment_name = language_code_mapping[language]
experiment_name = experiment_name + "-" + f"track{track}"
experiment_name = experiment_name + "-" + f"model={model_type}"
experiment_name = experiment_name + "-" + f"trial={trial}"
hyperparameter_str = "-".join(
[f"{param}={value}" for param, value in hyperparameters._asdict().items()]
)
experiment_name = experiment_name + "-" + hyperparameter_str
return experiment_name
def _check_arguments(
language: str,
track: int,
model_type: str,
data_path: str,
hyperparameters: Hyperparameters,
):
assert isinstance(language, str) and language in language_code_mapping
assert isinstance(track, int) and track in [1, 2]
assert isinstance(model_type, str) and model_type in model_type_mapping
assert os.path.isdir(data_path) and os.path.exists(data_path)
assert (
isinstance(hyperparameters.batch_size, int) and hyperparameters.batch_size >= 1
)
assert (
isinstance(hyperparameters.num_layers, int) and hyperparameters.num_layers >= 1
)
assert (
isinstance(hyperparameters.hidden_size, int)
and hyperparameters.hidden_size >= 1
)
assert (
isinstance(hyperparameters.dropout, float)
and 0.0 <= hyperparameters.dropout <= 1.0
)
def _make_train_path(language: str, track: int, data_path: str) -> str:
language_code = language_code_mapping[language]
return os.path.join(
data_path, f"{language}/{language_code}-train-track{track}-uncovered"
)
def _make_dev_path_uncovered(language: str, track: int, data_path: str) -> str:
language_code = language_code_mapping[language]
return os.path.join(
data_path, f"{language}/{language_code}-dev-track{track}-uncovered"
)
def _make_dev_path_covered(language: str, track: int, data_path: str) -> str:
language_code = language_code_mapping[language]
return os.path.join(
data_path, f"{language}/{language_code}-dev-track{track}-covered"
)
def _make_test_path(language: str, track: int, data_path: str) -> str:
language_code = language_code_mapping[language]
return os.path.join(
data_path, f"{language}/{language_code}-test-track{track}-covered"
)
def _make_dataset(
language: str, track: int, data_path: str, batch_size: int
) -> GlossingDataset:
train_file = _make_train_path(language, track, data_path)
validation_file = _make_dev_path_uncovered(language, track, data_path)
test_file = _make_test_path(language, track, data_path)
dm = GlossingDataset(
train_file=train_file,
validation_file=validation_file,
test_file=test_file,
batch_size=batch_size,
)
return dm
def _make_model(
model_type: str,
dataset: GlossingDataset,
track: int,
hyperparameters: Hyperparameters,
):
if model_type == "ctc":
return CTCGlossingModel(
source_alphabet_size=dataset.source_alphabet_size,
target_alphabet_size=dataset.target_alphabet_size,
hidden_size=hyperparameters.hidden_size,
num_layers=hyperparameters.num_layers,
dropout=hyperparameters.dropout,
)
elif model_type == "morph":
learn_segmentation = track == 1
classify_num_morphemes = track == 1
return MorphemeGlossingModel(
source_alphabet_size=dataset.source_alphabet_size,
target_alphabet_size=dataset.target_alphabet_size,
hidden_size=hyperparameters.hidden_size,
num_layers=hyperparameters.num_layers,
dropout=hyperparameters.dropout,
learn_segmentation=learn_segmentation,
classify_num_morphemes=classify_num_morphemes,
)
else:
raise ValueError(f"Unknown Model Type: {model_type}")
def experiment(
base_path: str,
language: str,
track: int,
model_type: str,
hyperparameters: Hyperparameters,
data_path: str = "./data",
verbose: bool = False,
trial: int = 0,
):
# Global Settings
torch.set_float32_matmul_precision("medium")
logging.disable(logging.WARNING)
# Check Arguments
_check_arguments(language, track, model_type, data_path, hyperparameters)
# Make Experiment Name and Base Path
experiment_name = _make_experiment_name(
language, track, model_type, hyperparameters, trial
)
base_path = os.path.join(base_path, experiment_name)
if os.path.exists(base_path):
raise FileExistsError(f"Model Path {base_path} exists.")
else:
os.makedirs(base_path, exist_ok=True)
# Prepare Data
dm = _make_dataset(language, track, data_path, hyperparameters.batch_size)
dm.prepare_data()
dm.setup(stage="fit")
# Define Logger and Callbacks
logger = pl_loggers.CSVLogger(
save_dir=os.path.join(base_path, "logs"), name=experiment_name
)
checkpoint_callback = ModelCheckpoint(
dirpath=os.path.join(base_path, "saved_models"),
filename=experiment_name + "-{val_accuracy}",
monitor="val_accuracy",
save_last=True,
save_top_k=1,
mode="max",
verbose=False,
)
early_stopping_callback = EarlyStopping(
monitor="val_accuracy", patience=3, mode="max", verbose=False
)
# Define Model
model = _make_model(model_type, dm, track, hyperparameters)
# Train Model
trainer = Trainer(
accelerator="gpu",
devices=1,
gradient_clip_val=1.0,
max_epochs=100,
enable_progress_bar=verbose,
log_every_n_steps=1,
logger=logger,
check_val_every_n_epoch=1,
enable_model_summary=verbose,
callbacks=[early_stopping_callback, checkpoint_callback],
min_epochs=1,
)
trainer.fit(model, dm)
logs = pd.read_csv(
os.path.join(base_path, "logs", experiment_name, "version_0", "metrics.csv")
)
best_val_accuracy = logs["val_accuracy"].max()
return best_val_accuracy
if __name__ == "__main__":
hparams = Hyperparameters(
batch_size=2, num_layers=1, hidden_size=512, dropout=0.1, scheduler_gamma=1.0
)
res = experiment(
base_path="./results",
language="Natugu",
track=1,
model_type="morph",
hyperparameters=hparams,
verbose=True,
)
print(res)