-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_eval_test.py
125 lines (97 loc) · 4.15 KB
/
train_eval_test.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
import tensorflow as tf
from seq2seq_tf2.models.sequence_to_sequence import SequenceToSequence
from seq2seq_tf2.batcher import batcher, Vocab
from seq2seq_tf2.train_helper import train_model
from seq2seq_tf2.test_helper import beam_decode, greedy_decode
from tqdm import tqdm
from utils.data_utils import get_result_filename
import pandas as pd
# from rouge import Rouge
import pprint
def train(params):
assert params["mode"].lower() == "train", "change training mode to 'train'"
vocab = Vocab(params["vocab_path"], params["vocab_size"])
print('true vocab is ', vocab)
print("Creating the batcher ...")
b = batcher(vocab, params)
print("Building the model ...")
model = SequenceToSequence(params)
print("Creating the checkpoint manager")
checkpoint_dir = "{}/checkpoint".format(params["seq2seq_model_dir"])
ckpt = tf.train.Checkpoint(SequenceToSequence=model)
ckpt_manager = tf.train.CheckpointManager(ckpt, checkpoint_dir, max_to_keep=5)
ckpt.restore(ckpt_manager.latest_checkpoint)
if ckpt_manager.latest_checkpoint:
print("Restored from {}".format(ckpt_manager.latest_checkpoint))
else:
print("Initializing from scratch.")
print("Starting the training ...")
train_model(model, b, params, ckpt, ckpt_manager)
def test(params):
assert params["mode"].lower() == "test", "change training mode to 'test' or 'eval'"
# assert params["beam_size"] == params["batch_size"], "Beam size must be equal to batch_size, change the params"
print("Building the model ...")
model = SequenceToSequence(params)
print("Creating the vocab ...")
vocab = Vocab(params["vocab_path"], params["vocab_size"])
print("Creating the batcher ...")
b = batcher(vocab, params)
print("Creating the checkpoint manager")
checkpoint_dir = "{}/checkpoint".format(params["seq2seq_model_dir"])
ckpt = tf.train.Checkpoint(SequenceToSequence=model)
ckpt_manager = tf.train.CheckpointManager(ckpt, checkpoint_dir, max_to_keep=5)
# path = params["model_path"] if params["model_path"] else ckpt_manager.latest_checkpoint
# path = ckpt_manager.latest_checkpoint
ckpt.restore(ckpt_manager.latest_checkpoint)
print("Model restored")
# for batch in b:
# yield batch_greedy_decode(model, batch, vocab, params)
if params['greedy_decode']:
# params['batch_size'] = 512
predict_result(model, params, vocab, params['test_save_dir'])
def predict_result(model, params, vocab, result_save_path):
dataset = batcher(vocab, params)
# 预测结果
results = greedy_decode(model, dataset, vocab, params)
results = list(map(lambda x: x.replace(" ",""), results))
# 保存结果
save_predict_result(results, params)
return results
def save_predict_result(results, params):
# 读取结果
test_df = pd.read_csv(params['test_x_dir'])
# 填充结果
test_df['Prediction'] = results[:20000]
# 提取ID和预测结果两列
test_df = test_df[['QID', 'Prediction']]
# 保存结果.
result_save_path = get_result_filename(params)
test_df.to_csv(result_save_path, index=None, sep=',')
def test_and_save(params):
assert params["test_save_dir"], "provide a dir where to save the results"
gen = test(params)
with tqdm(total=params["num_to_test"], position=0, leave=True) as pbar:
for i in range(params["num_to_test"]):
trial = next(gen)
with open(params["test_save_dir"] + "/article_" + str(i) + ".txt", "w", encoding='utf-8') as f:
f.write("article:\n")
f.write(trial.text)
f.write("\n\nabstract:\n")
f.write(trial.abstract)
pbar.update(1)
def evaluate(params):
gen = test(params)
reals = []
preds = []
with tqdm(total=params["max_num_to_eval"], position=0, leave=True) as pbar:
for i in range(params["max_num_to_eval"]):
trial = next(gen)
reals.append(trial.real_abstract)
preds.append(trial.abstract)
pbar.update(1)
r = Rouge()
scores = r.get_scores(preds, reals, avg=True)
print("\n\n")
pprint.pprint(scores)
if __name__ == '__main__':
pass