-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrun_bayes.py
executable file
·126 lines (98 loc) · 3.4 KB
/
run_bayes.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
#!/usr/bin/env python
##########################################################################
# Copyright 2018 Kata.ai
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##########################################################################
import pickle
from sacred import Experiment
from ingredients.corpus import ing as corpus_ingredient, read_train_jsonl
from ingredients.evaluation import ing as eval_ingredient, run_evaluation
from ingredients.summarization import ing as summ_ingredient, run_summarization
from models.supervised import NaiveBayesSummarizer
from serialization import dump, load
from utils import SAVE_FILES, setup_mongo_observer
ingredients = [corpus_ingredient, eval_ingredient, summ_ingredient]
ex = Experiment(name='summarization-bayes-testrun', ingredients=ingredients)
setup_mongo_observer(ex)
@ex.config
def default():
# where to load or save the trained model
model_path = 'model'
# proportion of words with highest TF-IDF score to be considered important words
cutoff = 0.1
# path to a pickle file containing the IDF dictionary
idf_path = None
@ex.named_config
def tuned_on_fold1():
cutoff = 0.249972
seed = 880655900
idf_path = 'idf_table.pkl'
@ex.named_config
def tuned_on_fold2():
cutoff = 0.0671735
seed = 467408239
idf_path = 'idf_table.pkl'
@ex.named_config
def tuned_on_fold3():
cutoff = 0.00148999
seed = 262472316
idf_path = None
@ex.named_config
def tuned_on_fold4():
cutoff = 0.000565754
seed = 163917137
idf_path = None
@ex.named_config
def tuned_on_fold5():
cutoff = 0.109262
seed = 67740894
idf_path = None
@ex.capture
def read_idf(idf_path, _log, _run):
_log.info('Reading IDF table from %s', idf_path)
with open(idf_path, 'rb') as f:
idf_table = pickle.load(f)
if SAVE_FILES:
_run.add_resource(idf_path)
return idf_table
@ex.capture
def load_model(model_path, _log, _run):
_log.info('Loading model from %s', model_path)
with open(model_path) as f:
model = load(f.read())
assert isinstance(model, NaiveBayesSummarizer), 'model is not a naive Bayes summarizer'
if SAVE_FILES:
_run.add_resource(model_path)
return model
@ex.command
def train(model_path, _log, _run, cutoff=0.1, idf_path=None):
"""Train a naive Bayes summarizer."""
train_docs = list(read_train_jsonl())
idf_table = None if idf_path is None else read_idf()
model = NaiveBayesSummarizer.train(train_docs, cutoff=cutoff, idf_table=idf_table)
_log.info('Saving model to %s', model_path)
with open(model_path, 'w') as f:
print(dump(model), file=f)
if SAVE_FILES:
_run.add_artifact(model_path)
@ex.command(unobserved=True)
def summarize():
"""Summarize the given file."""
model = load_model()
run_summarization(model)
@ex.automain
def evaluate():
"""Evaluate on a corpus."""
model = load_model()
return run_evaluation(model)