This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_hat.py
157 lines (128 loc) · 5.09 KB
/
train_hat.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import json
import pickle
import collections
import tensorflow as tf
import numpy as np
from absl import flags, logging
from data_utils import data_pipeline
from modeling import hat_model
FLAGS = flags.FLAGS
## Required parameters
flags.DEFINE_string(
"hat_config", None,
"JSON file for HAT model configuration"
)
flags.DEFINE_string(
"trainset_file", None,
"The pre-processed intermediate TFRecords file for training"
)
flags.DEFINE_string(
"count_file", None,
"The pre-processed intermediate JSON file for training"
)
flags.DEFINE_string(
"output_dir", None,
"The output directory where the model checkpoints will be written.")
flags.DEFINE_string(
"export_model_dir", None,
"The path to export model for serving"
)
## Other parameters
flags.DEFINE_string(
"init_checkpoint", None,
"Initial checkpoint. Both pre-trained model and fine-tuned model are possible.")
flags.DEFINE_integer("train_batch_size", 32, "Total batch size for training.")
flags.DEFINE_integer("export_batch_size", 1, "Total batch size for exported model.")
flags.DEFINE_float("learning_rate", 5e-4, "The initial learning rate for Adam.")
flags.DEFINE_float("num_train_epochs", 3.0,
"Total number of training epochs to perform.")
flags.DEFINE_float(
"warmup_proportion", 0.1,
"Proportion of training to perform linear learning rate warmup for. "
"E.g., 0.1 = 10% of training.")
flags.DEFINE_integer("save_checkpoints_steps", 1000,
"How often to save the model checkpoint.")
flags.DEFINE_integer("keep_checkpoint_max", 5,
"How many checkpoints to keep.")
flags.DEFINE_integer("iterations_per_loop", 1000,
"How many steps to make in each estimator call.")
def main(_):
logging.set_verbosity(logging.INFO)
# multi-GPU training configuration
dist_strategy = tf.distribute.MirroredStrategy()
run_config = tf.estimator.RunConfig(
model_dir=FLAGS.output_dir,
save_checkpoints_steps=FLAGS.save_checkpoints_steps,
keep_checkpoint_max=FLAGS.keep_checkpoint_max,
train_distribute=dist_strategy
)
# count steps
with open(FLAGS.count_file, 'r', encoding='utf-8') as fi:
num_of_examples = json.load(fi)["num_of_examples"]
num_train_steps = int(
num_of_examples / FLAGS.train_batch_size * FLAGS.num_train_epochs)
num_warmup_steps = int(num_train_steps * FLAGS.warmup_proportion)
# declare model
with open(FLAGS.hat_config, 'r', encoding='utf-8') as fi:
hat_config_dict = json.load(fi)
model_fn = hat_model.model_fn_builder(
hat_config=hat_config_dict,
init_checkpoint=FLAGS.init_checkpoint,
learning_rate=FLAGS.learning_rate,
num_train_steps=num_train_steps,
num_warmup_steps=num_warmup_steps,
)
estimator = tf.estimator.Estimator(
model_fn=model_fn,
config=run_config,
params={'batch_size': FLAGS.train_batch_size}
)
# train model
logging.info("***** Running training *****")
logging.info(" Num examples = %d", num_of_examples)
logging.info(" Batch size = %d", FLAGS.train_batch_size)
logging.info(" Num steps = %d", num_train_steps)
train_input_fn = data_pipeline.file_based_input_fn_builder(
input_file=FLAGS.trainset_file,
input_length=hat_config_dict["input_length"],
output_length=hat_config_dict["output_length"],
is_training=True,
drop_remainder=True,
batch_size=FLAGS.train_batch_size)
train_spec = tf.estimator.TrainSpec(input_fn=train_input_fn, max_steps=num_train_steps)
estimator.train(input_fn=train_input_fn, max_steps=num_train_steps)
# export model
def serving_input_receiver_fn():
"""An input receiver that expects a serialized tf.Example."""
reciever_tensors = {
"input_ids": tf.placeholder(
dtype=tf.int64,
shape=[FLAGS.export_batch_size, hat_config_dict["input_length"]]
),
"bos_mask": tf.placeholder(
dtype=tf.int64,
shape=[FLAGS.export_batch_size, hat_config_dict["input_length"]]
),
"label_ids": tf.placeholder(
dtype=tf.int64,
shape=[FLAGS.export_batch_size, hat_config_dict["output_length"]]
)
}
features = {
"input_ids": reciever_tensors["input_ids"],
"label_ids": reciever_tensors["label_ids"],
"bos_mask": reciever_tensors["bos_mask"]
}
return tf.estimator.export.ServingInputReceiver(features, reciever_tensors)
estimator.export_savedmodel(FLAGS.export_model_dir, serving_input_receiver_fn, strip_default_attrs=True)
if __name__ == "__main__":
flags.mark_flag_as_required("hat_config")
flags.mark_flag_as_required("trainset_file")
flags.mark_flag_as_required("count_file")
flags.mark_flag_as_required("output_dir")
flags.mark_flag_as_required("export_model_dir")
tf.app.run()