train.py 6.6 KB
Newer Older
1
# -*- coding: UTF-8 -*-
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
#   Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# 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 os
import sys
import math
import time
import random
import argparse
import multiprocessing

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

import reader
import utils
import creator
31
from eval import test_process
32 33
sys.path.append('../models/')
from model_check import check_cuda
34 35
from model_check import check_version

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

# the function to train model
def do_train(args):
    train_program = fluid.default_main_program()
    startup_program = fluid.default_startup_program()

    dataset = reader.Dataset(args)
    with fluid.program_guard(train_program, startup_program):
        train_program.random_seed = args.random_seed
        startup_program.random_seed = args.random_seed

        with fluid.unique_name.guard():
            train_ret = creator.create_model(
                args, dataset.vocab_size, dataset.num_labels, mode='train')
            test_program = train_program.clone(for_test=True)

52 53
            optimizer = fluid.optimizer.Adam(
                learning_rate=args.base_learning_rate)
54 55 56 57 58 59 60 61 62
            optimizer.minimize(train_ret["avg_cost"])

    # init executor
    if args.use_cuda:
        place = fluid.CUDAPlace(int(os.getenv('FLAGS_selected_gpus', '0')))
        dev_count = fluid.core.get_cuda_device_count()
    else:
        dev_count = min(multiprocessing.cpu_count(), args.cpu_num)
        if (dev_count < args.cpu_num):
63 64 65 66
            print(
                "WARNING: The total CPU NUM in this machine is %d, which is less than cpu_num parameter you set. "
                "Change the cpu_num from %d to %d" %
                (dev_count, args.cpu_num, dev_count))
67 68 69
        os.environ['CPU_NUM'] = str(dev_count)
        place = fluid.CPUPlace()

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    train_reader = creator.create_pyreader(
        args,
        file_name=args.train_data,
        feed_list=train_ret['feed_list'],
        place=place,
        model='lac',
        reader=dataset)

    test_reader = creator.create_pyreader(
        args,
        file_name=args.test_data,
        feed_list=train_ret['feed_list'],
        place=place,
        model='lac',
        reader=dataset,
        mode='test')
86 87 88 89 90 91

    exe = fluid.Executor(place)
    exe.run(startup_program)

    if args.init_checkpoint:
        utils.init_checkpoint(exe, args.init_checkpoint, train_program)
92
    if dev_count > 1:
93
        device = "GPU" if args.use_cuda else "CPU"
94
        print("%d %s are used to train model" % (dev_count, device))
95 96
        # multi cpu/gpu config
        exec_strategy = fluid.ExecutionStrategy()
97

98 99
        build_strategy = fluid.compiler.BuildStrategy()

100 101 102 103 104
        compiled_prog = fluid.compiler.CompiledProgram(
            train_program).with_data_parallel(
                loss_name=train_ret['avg_cost'].name,
                build_strategy=build_strategy,
                exec_strategy=exec_strategy)
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    else:
        compiled_prog = fluid.compiler.CompiledProgram(train_program)

    # start training
    num_train_examples = dataset.get_num_examples(args.train_data)
    max_train_steps = args.epoch * num_train_examples // args.batch_size
    print("Num train examples: %d" % num_train_examples)
    print("Max train steps: %d" % max_train_steps)

    ce_info = []
    step = 0
    for epoch_id in range(args.epoch):
        ce_time = 0
        for data in train_reader():
            # this is for minimizing the fetching op, saving the training speed.
            if step % args.print_steps == 0:
                fetch_list = [
122 123
                    train_ret["avg_cost"], train_ret["precision"],
                    train_ret["recall"], train_ret["f1_score"]
124 125 126 127 128 129 130 131
                ]
            else:
                fetch_list = []

            start_time = time.time()
            outputs = exe.run(
                compiled_prog,
                fetch_list=fetch_list,
132
                feed=data[0], )
133 134 135

            end_time = time.time()
            if step % args.print_steps == 0:
136 137 138
                avg_cost, precision, recall, f1_score = [
                    np.mean(x) for x in outputs
                ]
139

140 141 142 143
                print(
                    "[train] step = %d, loss = %.5f, P: %.5f, R: %.5f, F1: %.5f, elapsed time %.5f"
                    % (step, avg_cost, precision, recall, f1_score,
                       end_time - start_time))
144 145 146 147 148 149 150 151 152

            if step % args.validation_steps == 0:
                test_process(exe, test_program, test_reader, train_ret)

                ce_time += end_time - start_time
                ce_info.append([ce_time, avg_cost, precision, recall, f1_score])

            # save checkpoints
            if step % args.save_steps == 0 and step != 0:
153 154
                save_path = os.path.join(args.model_save_dir,
                                         "step_" + str(step))
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
                fluid.io.save_persistables(exe, save_path, train_program)
            step += 1

    if args.enable_ce:
        card_num = get_cards()
        ce_cost = 0
        ce_f1 = 0
        ce_p = 0
        ce_r = 0
        ce_time = 0
        try:
            ce_time = ce_info[-2][0]
            ce_cost = ce_info[-2][1]
            ce_p = ce_info[-2][2]
            ce_r = ce_info[-2][3]
            ce_f1 = ce_info[-2][4]
        except:
            print("ce info error")
173 174 175 176 177
        print("kpis\teach_step_duration_card%s\t%s" % (card_num, ce_time))
        print("kpis\ttrain_cost_card%s\t%f" % (card_num, ce_cost))
        print("kpis\ttrain_precision_card%s\t%f" % (card_num, ce_p))
        print("kpis\ttrain_recall_card%s\t%f" % (card_num, ce_r))
        print("kpis\ttrain_f1_card%s\t%f" % (card_num, ce_f1))
178 179 180 181 182 183 184 185 186


def get_cards():
    num = 0
    cards = os.environ.get('CUDA_VISIBLE_DEVICES', '')
    if cards != '':
        num = len(cards.split(","))
    return num

187

188 189 190 191 192
if __name__ == "__main__":
    # 参数控制可以根据需求使用argparse,yaml或者json
    # 对NLP任务推荐使用PALM下定义的configure,可以统一argparse,yaml或者json格式的配置文件。

    parser = argparse.ArgumentParser(__doc__)
193
    utils.load_yaml(parser, 'conf/args.yaml')
194 195 196

    args = parser.parse_args()
    check_cuda(args.use_cuda)
197
    check_version()
198 199 200 201

    print(args)

    do_train(args)