train.py 6.2 KB
Newer Older
H
hetianjian 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#  Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserve.
#
#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.

H
hetianjian 已提交
15 16 17 18
import numpy as np
import os
from functools import partial
import logging
Z
zhengya01 已提交
19
import time
H
hetianjian 已提交
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
import paddle
import paddle.fluid as fluid
import argparse
import network
import reader

logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger("fluid")
logger.setLevel(logging.INFO)


def parse_args():
    parser = argparse.ArgumentParser("gnn")
    parser.add_argument(
        '--train_path', type=str, default='./data/diginetica/train.txt', help='dir of training data')
    parser.add_argument(
        '--config_path', type=str, default='./data/diginetica/config.txt', help='dir of config')
    parser.add_argument(
        '--model_path', type=str, default='./saved_model', help="path of model parameters")
    parser.add_argument(
        '--epoch_num', type=int, default=30, help='number of epochs to train for')
    parser.add_argument(
        '--batch_size', type=int, default=100, help='input batch size')
    parser.add_argument(
        '--hidden_size', type=int, default=100, help='hidden state size')
    parser.add_argument(
        '--l2', type=float, default=1e-5, help='l2 penalty')
    parser.add_argument(
        '--lr', type=float, default=0.001, help='learning rate')
    parser.add_argument(
        '--step', type=int, default=1, help='gnn propogation steps')
    parser.add_argument(
        '--lr_dc', type=float, default=0.1, help='learning rate decay rate')
    parser.add_argument(
        '--lr_dc_step', type=int, default=3, help='the number of steps after which the learning rate decay')
    parser.add_argument(
        '--use_cuda', type=int, default=0, help='whether to use gpu')
    parser.add_argument(
        '--use_parallel', type=int, default=1, help='whether to use parallel executor')
Z
zhengya01 已提交
59 60
    parser.add_argument(
        '--enable_ce', action='store_true', help='If set, run the task with continuous evaluation logs.')
H
hetianjian 已提交
61 62 63 64 65
    return parser.parse_args()


def train():
    args = parse_args()
Z
zhengya01 已提交
66 67 68 69 70 71

    if args.enable_ce:
        SEED = 102
        fluid.default_main_program().random_seed = SEED
        fluid.default_startup_program().random_seed = SEED

H
hetianjian 已提交
72 73
    batch_size = args.batch_size
    items_num = reader.read_config(args.config_path)
H
hutuxian 已提交
74
    loss, acc, py_reader, feed_datas = network.network(items_num, args.hidden_size,
H
hetianjian 已提交
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
                                args.step)

    data_reader = reader.Data(args.train_path, True)
    logger.info("load data complete")

    use_cuda = True if args.use_cuda else False
    use_parallel = True if args.use_parallel else False
    place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()

    exe = fluid.Executor(place)
    step_per_epoch = data_reader.length // batch_size
    optimizer = fluid.optimizer.Adam(
        learning_rate=fluid.layers.exponential_decay(
            learning_rate=args.lr,
            decay_steps=step_per_epoch * args.lr_dc_step,
            decay_rate=args.lr_dc),
        regularization=fluid.regularizer.L2DecayRegularizer(
            regularization_coeff=args.l2))
    optimizer.minimize(loss)

    exe.run(fluid.default_startup_program())

    all_vocab = fluid.global_scope().var("all_vocab").get_tensor()
    all_vocab.set(
        np.arange(1, items_num).astype("int64").reshape((-1, 1)), place)

101
    feed_list = [e.name for e in feed_datas]
H
hetianjian 已提交
102 103

    if use_parallel:
104 105
        exec_strategy = fluid.ExecutionStrategy()
        exec_strategy.num_threads = 1 if os.name == 'nt' else 0
H
hetianjian 已提交
106
        train_exe = fluid.ParallelExecutor(
107
            use_cuda=use_cuda, loss_name=loss.name, exec_strategy=exec_strategy)
H
hetianjian 已提交
108 109 110 111 112
    else:
        train_exe = exe

    logger.info("begin train")

Z
zhengya01 已提交
113 114 115
    total_time = []
    ce_info = []
    start_time = time.time()
H
hetianjian 已提交
116 117 118 119
    loss_sum = 0.0
    acc_sum = 0.0
    global_step = 0
    PRINT_STEP = 500
120
    py_reader.decorate_paddle_reader(data_reader.reader(batch_size, batch_size * 20, True))
H
hetianjian 已提交
121 122
    for i in range(args.epoch_num):
        epoch_sum = []
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        py_reader.start()
        try:
            while True:
                res = train_exe.run(fetch_list=[loss.name, acc.name])
                loss_sum += res[0].mean()
                acc_sum += res[1].mean()
                epoch_sum.append(res[0].mean())
                global_step += 1
                if global_step % PRINT_STEP == 0:
                    ce_info.append([loss_sum / PRINT_STEP, acc_sum / PRINT_STEP])
                    total_time.append(time.time() - start_time)
                    logger.info("global_step: %d, loss: %.4lf, train_acc: %.4lf" % (
                        global_step, loss_sum / PRINT_STEP, acc_sum / PRINT_STEP))
                    loss_sum = 0.0
                    acc_sum = 0.0
                    start_time = time.time()
        except fluid.core.EOFException:
            py_reader.reset()
H
hetianjian 已提交
141 142 143 144 145 146
        logger.info("epoch loss: %.4lf" % (np.mean(epoch_sum)))
        save_dir = args.model_path + "/epoch_" + str(i)
        fetch_vars = [loss, acc]
        fluid.io.save_inference_model(save_dir, feed_list, fetch_vars, exe)
        logger.info("model saved in " + save_dir)

Z
zhengya01 已提交
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
    # only for ce
    if args.enable_ce:
        gpu_num = get_cards(args)
        ce_loss = 0
        ce_acc = 0
        ce_time = 0
        try:
            ce_loss = ce_info[-1][0]
            ce_acc = ce_info[-1][1]
            ce_time = total_time[-1]
        except:
            print("ce info error")
        print("kpis\teach_pass_duration_card%s\t%s" %
                    (gpu_num, ce_time))
        print("kpis\ttrain_loss_card%s\t%f" %
                    (gpu_num, ce_loss))
        print("kpis\ttrain_acc_card%s\t%f" %
                    (gpu_num, ce_acc))


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

H
hetianjian 已提交
173 174 175

if __name__ == "__main__":
    train()