train.py 6.4 KB
Newer Older
Q
Qiao Longfei 已提交
1 2 3 4 5
from __future__ import print_function

import argparse
import logging
import os
6
import time
Q
Qiao Longfei 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

# disable gpu training for this example 
os.environ["CUDA_VISIBLE_DEVICES"] = ""

import paddle
import paddle.fluid as fluid

import reader
from network_conf import skip_gram_word2vec

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


def parse_args():
    parser = argparse.ArgumentParser(description="PaddlePaddle CTR example")
    parser.add_argument(
        '--train_data_path',
        type=str,
Q
Qiao Longfei 已提交
27
        default='./data/enwik8',
Q
Qiao Longfei 已提交
28
        help="The path of training dataset")
Q
Qiao Longfei 已提交
29 30 31
    parser.add_argument(
        '--dict_path',
        type=str,
Q
Qiao Longfei 已提交
32
        default='./data/enwik8_dict',
Q
Qiao Longfei 已提交
33
        help="The path of data dict")
Q
Qiao Longfei 已提交
34 35 36
    parser.add_argument(
        '--test_data_path',
        type=str,
Q
Qiao Longfei 已提交
37
        default='./data/text8',
Q
Qiao Longfei 已提交
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
        help="The path of testing dataset")
    parser.add_argument(
        '--batch_size',
        type=int,
        default=100,
        help="The size of mini-batch (default:1000)")
    parser.add_argument(
        '--num_passes',
        type=int,
        default=10,
        help="The number of passes to train (default: 10)")
    parser.add_argument(
        '--model_output_dir',
        type=str,
        default='models',
        help='The path for model to store (default: models)')
    parser.add_argument(
        '--embedding_size',
        type=int,
        default=64,
        help='sparse feature hashing space for index processing')

    parser.add_argument(
        '--is_local',
        type=int,
        default=1,
        help='Local train or distributed train (default: 1)')
    # the following arguments is used for distributed train, if is_local == false, then you should set them
    parser.add_argument(
        '--role',
        type=str,
        default='pserver',  # trainer or pserver
70
        help='The training role (trainer|pserver) (default: pserver)')
Q
Qiao Longfei 已提交
71 72 73 74 75 76 77 78 79
    parser.add_argument(
        '--endpoints',
        type=str,
        default='127.0.0.1:6000',
        help='The pserver endpoints, like: 127.0.0.1:6000,127.0.0.1:6001')
    parser.add_argument(
        '--current_endpoint',
        type=str,
        default='127.0.0.1:6000',
80
        help='The current pserver endpoint (default: 127.0.0.1:6000)')
Q
Qiao Longfei 已提交
81 82 83 84
    parser.add_argument(
        '--trainer_id',
        type=int,
        default=0,
85
        help='The current trainer id (default: 0)')
Q
Qiao Longfei 已提交
86 87 88 89 90
    parser.add_argument(
        '--trainers',
        type=int,
        default=1,
        help='The num of trianers, (default: 1)')
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    parser.add_argument(
        '--with_hs',
        type=int,
        default=0,
        help='using hierarchical sigmoid, (default: 0)')
    parser.add_argument(
        '--with_nce',
        type=int,
        default=1,
        help='using negtive sampling, (default: 1)')
    parser.add_argument(
        '--max_code_length',
        type=int,
        default=40,
        help='max code length used by hierarchical sigmoid, (default: 40)')
Q
Qiao Longfei 已提交
106 107 108 109

    return parser.parse_args()


Q
Qiao Longfei 已提交
110 111
def train_loop(args, train_program, reader, data_list, loss, trainer_num,
               trainer_id):
Q
Qiao Longfei 已提交
112 113
    train_reader = paddle.batch(
        paddle.reader.shuffle(
114 115
            reader.train((args.with_hs or (not args.with_nce))),
            buf_size=args.batch_size * 100),
Q
Qiao Longfei 已提交
116 117 118 119
        batch_size=args.batch_size)
    place = fluid.CPUPlace()

    feeder = fluid.DataFeeder(feed_list=data_list, place=place)
120

Q
Qiao Longfei 已提交
121 122 123 124
    data_name_list = [var.name for var in data_list]

    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())
125
    start = time.clock()
Q
Qiao Longfei 已提交
126 127 128 129 130 131 132 133
    for pass_id in range(args.num_passes):
        for batch_id, data in enumerate(train_reader()):
            loss_val = exe.run(train_program,
                               feed=feeder.feed(data),
                               fetch_list=[loss])
            if batch_id % 10 == 0:
                logger.info("TRAIN --> pass: {} batch: {} loss: {}".format(
                    pass_id, batch_id, loss_val[0] / args.batch_size))
134 135 136 137
            if batch_id % 1000 == 0 and batch_id != 0:
                elapsed = (time.clock() - start)
                logger.info("Time used: {}".format(elapsed))

Q
Qiao Longfei 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
            if batch_id % 1000 == 0 and batch_id != 0:
                model_dir = args.model_output_dir + '/batch-' + str(batch_id)
                if args.trainer_id == 0:
                    fluid.io.save_inference_model(model_dir, data_name_list,
                                                  [loss], exe)
        model_dir = args.model_output_dir + '/pass-' + str(pass_id)
        if args.trainer_id == 0:
            fluid.io.save_inference_model(model_dir, data_name_list, [loss],
                                          exe)


def train():
    args = parse_args()

    if not os.path.isdir(args.model_output_dir):
        os.mkdir(args.model_output_dir)

Q
Qiao Longfei 已提交
155 156
    word2vec_reader = reader.Word2VecReader(args.dict_path,
                                            args.train_data_path)
157 158

    logger.info("dict_size: {}".format(word2vec_reader.dict_size))
159 160
    logger.info("word_frequencys length: {}".format(
        len(word2vec_reader.word_frequencys)))
161

162 163 164
    loss, data_list = skip_gram_word2vec(
        word2vec_reader.dict_size, word2vec_reader.word_frequencys,
        args.embedding_size, args.max_code_length, args.with_hs, args.with_nce)
Q
Qiao Longfei 已提交
165 166 167 168 169 170
    optimizer = fluid.optimizer.Adam(learning_rate=1e-3)
    optimizer.minimize(loss)

    if args.is_local:
        logger.info("run local training")
        main_program = fluid.default_main_program()
Q
Qiao Longfei 已提交
171
        train_loop(args, main_program, word2vec_reader, data_list, loss, 1, -1)
Q
Qiao Longfei 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    else:
        logger.info("run dist training")
        t = fluid.DistributeTranspiler()
        t.transpile(
            args.trainer_id, pservers=args.endpoints, trainers=args.trainers)
        if args.role == "pserver":
            logger.info("run pserver")
            prog = t.get_pserver_program(args.current_endpoint)
            startup = t.get_startup_program(
                args.current_endpoint, pserver_program=prog)
            exe = fluid.Executor(fluid.CPUPlace())
            exe.run(startup)
            exe.run(prog)
        elif args.role == "trainer":
            logger.info("run trainer")
            train_prog = t.get_trainer_program()
Q
Qiao Longfei 已提交
188 189
            train_loop(args, train_prog, word2vec_reader, data_list, loss,
                       args.trainers, args.trainer_id + 1)
Q
Qiao Longfei 已提交
190 191 192 193


if __name__ == '__main__':
    train()