-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_stanford.py
209 lines (175 loc) · 7.14 KB
/
run_stanford.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
import os
import logging
from typing import List
import scipy.stats
import sklearn_crfsuite
from sklearn.externals import joblib
from pymongo import MongoClient
from sacred.observers import MongoObserver
from sacred import Experiment
from sacred.run import Run
from ingredients.crf_utils import get_tagged_sents_and_words
from ingredients.crf_utils import sent2stanfordfeats
from ingredients.crf_utils import sent2stanfordlabels
from ingredients.crf_utils import evaluate
SECRET = os.environ.get('SACRED_KEY', None)
MONGOL = f'mongodb://<user>:<SECRET>@<uri>:<port>/<dbname>'
ex = Experiment('run_crf')
client = MongoClient(MONGOL)
ex.observers.append(MongoObserver.create(
url=MONGOL, db_name='dbname'))
db = client['<dbname>']
runs = db['runs']
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(levelname)s - %(name)s - %(message)s'))
logger.addHandler(handler)
ex.logger = logger
@ex.config
def default_config():
train_files = ['train.conll']
dev_files = ['dev.conll']
test_files = ['test.conll']
dirpath = '/home/fariz/workspace/dbpedia/dee'
num_experiments = 30
num_window_sizes = 4
retry_limit = 10
@ex.named_config
def cross_val_config():
train_files = [f'train-{i}.conll' for i in range(5)]
dev_files = [f'test-{i}.conll' for i in range(5)]
dirpath = '/home/fariz/workspace/dbpedia/dee'
num_experiments = 30
num_window_sizes = 5
retry_limit = 10
@ex.command
def train(train_corpus: str, dev_corpus: str,
c1: float = 0.0, c2: float = 0.0, algorithm: str = 'lbfgs',
max_iterations: int = 100, all_possible_transitions: bool = False,
model_filename: str=None, _run: Run = None, _log: logger = None):
"""
running crf experiment using stanford ner-crf features main
"""
_run.add_resource(train_corpus)
_run.add_resource(dev_corpus)
train_sents, _ = get_tagged_sents_and_words(train_corpus)
dev_sents, _ = get_tagged_sents_and_words(dev_corpus)
X_train = [sent2stanfordfeats(s) for s in train_sents]
y_train = [sent2stanfordlabels(s) for s in train_sents]
X_dev = [sent2stanfordfeats(s) for s in dev_sents]
y_dev = [sent2stanfordlabels(s) for s in dev_sents]
crf = sklearn_crfsuite.CRF(
algorithm = algorithm,
c1 = c1,
c2 = c2,
max_iterations = max_iterations,
all_possible_transitions= all_possible_transitions,
model_filename=model_filename,
)
logger.info('start training %s', train_corpus)
crf.fit(X_train, y_train)
logger.info('finish training %s', train_corpus)
logger.info('start predict %s', dev_corpus)
y_pred = crf.predict(X_dev)
logger.info('finish predict %s', dev_corpus)
logger.info('start eval %s', dev_corpus)
overall, by_type = evaluate(y_dev, y_pred)
logger.info('finish eval %s', dev_corpus)
_run.info[f'overall_f1'] = overall.f1_score
_run.log_scalar('overall_f1', overall.f1_score)
_log.info('Overall F1 score: %s', overall.f1_score)
_run.info[f'overall_precision'] = overall.precision
_run.log_scalar('overall_precision', overall.precision)
_run.info[f'overall_recall'] = overall.recall
_run.log_scalar('overall_recall', overall.recall)
for _, key in enumerate(sorted(by_type.keys())):
for metric_key in by_type[key]._fields:
metric_val = getattr(by_type[key], metric_key)
_run.info[f'{key}-{metric_key}'] = metric_val
_run.log_scalar(f'{key}-{metric_key}', metric_val)
_log.info(f'{key}-{metric_key}: {metric_val}')
if model_filename is not None:
_log.info(f'saving to: {model_filename}.pkl')
joblib.dump(crf, f'{model_filename}.pkl')
_run.add_artifact(f'{model_filename}.pkl')
@ex.command
def test(model_filename: str, test_corpus: str,
_run: Run = None, _log: logger = None):
"""
run test crf model using stanford ner-crf features main
"""
_run.add_resource(test_corpus)
_run.add_resource(f'{model_filename}.pkl')
test_sents, _ = get_tagged_sents_and_words(test_corpus)
X_test = [sent2stanfordfeats(s) for s in test_sents]
y_test = [sent2stanfordlabels(s) for s in test_sents]
_log.info(f'load from: {model_filename}.pkl')
crf = sklearn_crfsuite.CRF(
model_filename=model_filename
)
y_pred = crf.predict(X_test)
overall, by_type = evaluate(y_test, y_pred)
_run.info[f'overall_f1'] = overall.f1_score
_run.log_scalar('overall_f1', overall.f1_score)
_run.info[f'overall_precision'] = overall.precision
_run.log_scalar('overall_precision', overall.precision)
_run.info[f'overall_recall'] = overall.recall
_run.log_scalar('overall_recall', overall.recall)
_log.info(f'Overall F1 score: {overall.f1_score}')
for _, key in enumerate(sorted(by_type.keys())):
for metric_key in by_type[key]._fields:
metric_val = getattr(by_type[key], metric_key)
_run.info[f'{key}-{metric_key}'] = metric_val
_run.log_scalar(f'{key}-{metric_key}', metric_val)
_log.info(f'{key}-{metric_key}: {metric_val}')
@ex.command
def hyperparams(train_files: List[str],
dev_files: List[str],
dirpath: str, num_experiments: int = 30,
retry_limit: int = 100,
_run: Run = None, _log: logger = None):
"""
run hyperparameter optimization crf experiment using stanford ner-crf features main
"""
# absolute paths for all training sets
train_corpora = [os.path.join(dirpath, t) for t in train_files]
# absolute paths for all dev sets
dev_corpora = [os.path.join(dirpath, t) for t in dev_files]
# absolute paths for all test sets
for i, _ in enumerate(train_corpora):
configs = []
c1_space = scipy.stats.expon(scale=0.5)
c2_space = scipy.stats.expon(scale=0.05)
for _ in range(num_experiments):
c1 = c1_space.rvs()
c2 = c2_space.rvs()
configs.append({
'train_corpus': train_corpora[i],
'dev_corpus': dev_corpora[i],
'c1': c1,
'c2': c2
})
current_idx = 0
current_ret = 0
while current_idx < len(configs) and current_ret < retry_limit:
try:
logger.info(f'Run {current_idx + 1} of {len(configs)}')
logger.info(f'Config: {configs[current_idx]}')
r = ex.run_command(command_name='train', config_updates=configs[current_idx])
current_idx += 1
except KeyboardInterrupt:
logger.info('Experiment aborted')
exit()
except RuntimeError:
if current_ret == retry_limit - 1:
logging.error('RETRY LIMIT EXCEEDED!, Experiment Failed')
break
else:
logger.warning(f'Run failed, will keep retrying {current_ret} of {retry_limit}')
current_ret += 1
except Exception:
break
@ex.automain
def main():
print('crf experiment using stanford ner-crf features main command.')