train.py 12.0 KB
Newer Older
X
xuezhong 已提交
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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
"""
This file is used to train the model.
"""
import os
import sys
import math
import time
import random
import argparse

import numpy as np
import paddle
import paddle.fluid as fluid

import reader
from network import lex_net
from bilm import init_pretraining_params


def parse_args():
    """
    Parsing the input parameters.
    """
    parser = argparse.ArgumentParser("Training for lexical analyzer.")
    parser.add_argument(
        "--traindata_dir",
        type=str,
        default="data/train_data",
        help="The folder where the training data is located.")
    parser.add_argument(
        "--testdata_dir",
        type=str,
        default="data/test_data",
        help="The folder where the training data is located.")
    parser.add_argument(
        "--model_save_dir",
        type=str,
        default="./models",
        help="The model will be saved in this path.")
    parser.add_argument(
        "--save_model_per_batchs",
        type=int,
        default=1000,
        help="Save the model once per xxxx batch of training")
    parser.add_argument(
        "--eval_window",
        type=int,
        default=20,
        help="Training will be suspended when the evaluation indicators on the validation set" \
             " no longer increase. The eval_window specifies the scope of the evaluation.")
    parser.add_argument(
        "--batch_size",
        type=int,
        default=32,
        help="The number of sequences contained in a mini-batch, or the maximum" \
             "number of tokens (include paddings) contained in a mini-batch.")
    parser.add_argument(
        "--corpus_type_list",
        type=str,
        default=["human", "feed", "query", "title", "news"],
        nargs='+',
        help="The pattern list of different types of corpus used in training.")
    parser.add_argument(
        "--corpus_proportion_list",
        type=float,
        default=[0.2, 0.2, 0.2, 0.2, 0.2],
        nargs='+',
        help="The proportion list of different types of corpus used in training."
    )
    parser.add_argument(
        "--use_gpu",
        type=int,
        default=False,
        help="Whether or not to use GPU. 0-->CPU 1-->GPU")
    parser.add_argument(
        "--traindata_shuffle_buffer",
        type=int,
        default=200000,
        help="The buffer size used in shuffle the training data.")
    parser.add_argument(
        "--word_emb_dim",
        type=int,
        default=128,
        help="The dimension in which a word is embedded.")
    parser.add_argument(
        "--grnn_hidden_dim",
        type=int,
        default=256,
        help="The number of hidden nodes in the GRNN layer.")
    parser.add_argument(
        "--bigru_num",
        type=int,
        default=2,
        help="The number of bi_gru layers in the network.")
    parser.add_argument(
        "--base_learning_rate",
        type=float,
        default=1e-3,
        help="The basic learning rate that affects the entire network.")
    parser.add_argument(
        "--emb_learning_rate",
        type=float,
        default=5,
        help="The real learning rate of the embedding layer will be" \
        " (emb_learning_rate * base_learning_rate)."
    )
    parser.add_argument(
        "--crf_learning_rate",
        type=float,
        default=0.2,
        help="The real learning rate of the embedding layer will be" \
             " (crf_learning_rate * base_learning_rate)."
    )
    parser.add_argument(
        "--word_dict_path",
        type=str,
        default="../data/vocabulary_min5k.txt",
        help="The path of the word dictionary.")
    parser.add_argument(
        "--label_dict_path",
        type=str,
        default="data/tag.dic",
        help="The path of the label dictionary.")
    parser.add_argument(
        "--word_rep_dict_path",
        type=str,
        default="conf/q2b.dic",
        help="The path of the word replacement Dictionary.")
    parser.add_argument(
        "--num_iterations",
        type=int,
        default=40000,
        help="The maximum number of iterations. If set to 0 (default), do not limit the number."
    )

    #add elmo args
    parser.add_argument(
        "--elmo_l2_coef",
        type=float,
        default=0.001,
        help="Weight decay. (default: %(default)f)")
    parser.add_argument(
        "--elmo_dict_dir",
        default='data/vocabulary_min5k.txt',
        help="If set, load elmo dict.")
    parser.add_argument(
        '--pretrain_elmo_model_path',
        default="data/baike_elmo_checkpoint",
        help="If set, load elmo checkpoint.")
    args = parser.parse_args()
    if len(args.corpus_proportion_list) != len(args.corpus_type_list):
        sys.stderr.write(
            "The length of corpus_proportion_list should be equal to the length of corpus_type_list.\n"
        )
        exit(-1)
    return args


def print_arguments(args):
    print('-----------  Configuration Arguments -----------')
    for arg, value in sorted(vars(args).items()):
        print('%s: %s' % (arg, value))
    print('------------------------------------------------')


def to_lodtensor(data, place):
    """
    Convert data in list into lodtensor.
    """
    seq_lens = [len(seq) for seq in data]
    cur_len = 0
    lod = [cur_len]
    for l in seq_lens:
        cur_len += l
        lod.append(cur_len)
    flattened_data = np.concatenate(data, axis=0).astype("int64")
    flattened_data = flattened_data.reshape([len(flattened_data), 1])
    res = fluid.LoDTensor()
    res.set(flattened_data, place)
    res.set_lod([lod])
    return res


def test(exe, chunk_evaluator, save_dirname, test_data, place):
    """
    Test the network in training.
    """
    inference_scope = fluid.core.Scope()
    with fluid.scope_guard(inference_scope):
        [inference_program, feed_target_names,
         fetch_targets] = fluid.io.load_inference_model(save_dirname, exe)
        chunk_evaluator.reset()
        for data in test_data():
            word = to_lodtensor(list(map(lambda x: x[0], data)), place)
            target = to_lodtensor(list(map(lambda x: x[1], data)), place)
            result_list = exe.run(inference_program,
                                  feed={"word": word,
                                        "target": target},
                                  fetch_list=fetch_targets)
            number_infer = np.array(result_list[0])
            number_label = np.array(result_list[1])
            number_correct = np.array(result_list[2])
            chunk_evaluator.update(
                int(number_infer[0]),
                int(number_label[0]), int(number_correct[0]))
    return chunk_evaluator.eval()


def train(args):
    """
    Train the network.
    """
    if not os.path.exists(args.model_save_dir):
        os.mkdir(args.model_save_dir)

    word2id_dict = reader.load_reverse_dict(args.word_dict_path)
    label2id_dict = reader.load_reverse_dict(args.label_dict_path)
    word_rep_dict = reader.load_dict(args.word_rep_dict_path)
    word_dict_len = max(map(int, word2id_dict.values())) + 1
    label_dict_len = max(map(int, label2id_dict.values())) + 1

    avg_cost, crf_decode, word, target = lex_net(args, word_dict_len,
                                                 label_dict_len)
    adam_optimizer = fluid.optimizer.Adam(learning_rate=args.base_learning_rate)
    adam_optimizer.minimize(avg_cost)

    (precision, recall, f1_score, num_infer_chunks, num_label_chunks,
     num_correct_chunks) = fluid.layers.chunk_eval(
         input=crf_decode,
         label=target,
         chunk_scheme="IOB",
         num_chunk_types=int(math.ceil((label_dict_len - 1) / 2.0)))
    chunk_evaluator = fluid.metrics.ChunkEvaluator()
    chunk_evaluator.reset()

    train_reader_list = []
    corpus_num = len(args.corpus_type_list)
    for i in range(corpus_num):
        train_reader = paddle.batch(
            paddle.reader.shuffle(
                reader.file_reader(args.traindata_dir, word2id_dict,
                                   label2id_dict, word_rep_dict,
                                   args.corpus_type_list[i]),
                buf_size=args.traindata_shuffle_buffer),
            batch_size=int(args.batch_size * args.corpus_proportion_list[i]))
        train_reader_list.append(train_reader)
    test_reader = paddle.batch(
        reader.file_reader(args.testdata_dir, word2id_dict, label2id_dict,
                           word_rep_dict),
        batch_size=args.batch_size)
    train_reader_itr_list = []
    for train_reader in train_reader_list:
        cur_reader_itr = train_reader()
        train_reader_itr_list.append(cur_reader_itr)

    place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
    feeder = fluid.DataFeeder(feed_list=[word, target], place=place)
    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())

    # load pretrained ELMo model
    init_pretraining_params(exe, args.pretrain_elmo_model_path,
                            fluid.default_main_program())

    batch_id = 0
    start_time = time.time()
    eval_list = []
    iter = 0
    while True:
        full_batch = []
        cur_batch = []
        for i in range(corpus_num):
            reader_itr = train_reader_itr_list[i]
            try:
                cur_batch = next(reader_itr)
            except StopIteration:
                print(args.corpus_type_list[i] +
                      " corpus finish a pass of training")
                new_reader = train_reader_list[i]
                train_reader_itr_list[i] = new_reader()
                cur_batch = next(train_reader_itr_list[i])
            full_batch += cur_batch
        random.shuffle(full_batch)

        cost_var, nums_infer, nums_label, nums_correct = exe.run(
            fluid.default_main_program(),
            fetch_list=[
                avg_cost, num_infer_chunks, num_label_chunks, num_correct_chunks
            ],
            feed=feeder.feed(full_batch))
        print("batch_id:" + str(batch_id) + ", avg_cost:" + str(cost_var[0]))
        chunk_evaluator.update(nums_infer, nums_label, nums_correct)
        batch_id += 1

        if (batch_id % args.save_model_per_batchs == 1):
            save_exe = fluid.Executor(place)
            save_dirname = os.path.join(args.model_save_dir,
                                        "params_batch_%d" % batch_id)

            temp_save_model = os.path.join(args.model_save_dir,
                                           "temp_model_for_test")
            fluid.io.save_inference_model(
                temp_save_model, ['word', 'target'],
                [num_infer_chunks, num_label_chunks, num_correct_chunks],
                save_exe)

            precision, recall, f1_score = chunk_evaluator.eval()
            print("[train] batch_id:" + str(batch_id) + ", precision:" + str(
                precision) + ", recall:" + str(recall) + ", f1:" + str(
                    f1_score))
            chunk_evaluator.reset()
            p, r, f1 = test(exe, chunk_evaluator, temp_save_model, test_reader,
                            place)
            chunk_evaluator.reset()
            print("[test] batch_id:" + str(batch_id) + ", precision:" + str(p) +
                  ", recall:" + str(r) + ", f1:" + str(f1))
            end_time = time.time()
            print("cur_batch_id:" + str(batch_id) + ", last " + str(
                args.save_model_per_batchs) + " batchs, time_cost:" + str(
                    end_time - start_time))
            start_time = time.time()

            if len(eval_list) < 2 * args.eval_window:
                eval_list.append(f1)
            else:
                eval_list.pop(0)
                eval_list.append(f1)
                last_avg_f1 = sum(eval_list[
                    0:args.eval_window]) / args.eval_window
                cur_avg_f1 = sum(eval_list[args.eval_window:2 *
                                           args.eval_window]) / args.eval_window
                if cur_avg_f1 <= last_avg_f1:
                    return
                else:
                    print("keep training!")
        iter += 1
        if (iter == args.num_iterations):
            return


if __name__ == "__main__":
    args = parse_args()
    print_arguments(args)
    train(args)