run_ernie_classifier.py 13.5 KB
Newer Older
Y
Yibing Liu 已提交
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
"""
Sentiment Classification Task
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import time
import argparse
import numpy as np
import multiprocessing
import sys

import paddle
import paddle.fluid as fluid

sys.path.append("../models/classification/")
sys.path.append("..")
print(sys.path)

from nets import bow_net
from nets import lstm_net
from nets import cnn_net
from nets import bilstm_net
from nets import gru_net
from nets import ernie_base_net
from nets import ernie_bilstm_net
from preprocess.ernie import task_reader
from models.representation.ernie import ErnieConfig
32
from models.representation.ernie import ernie_encoder, ernie_encoder_with_paddle_hub
33
#from models.representation.ernie import ernie_pyreader
L
LiuHao 已提交
34
from models.model_check import check_cuda
35 36
from config import PDConfig

Y
Yibing Liu 已提交
37 38
from utils import init_checkpoint

39 40 41 42 43 44 45 46 47 48 49 50
def ernie_pyreader(args, pyreader_name):
    src_ids = fluid.layers.data(
        name="src_ids", shape=[-1, args.max_seq_len, 1], dtype="int64")
    sent_ids = fluid.layers.data(
        name="sent_ids", shape=[-1, args.max_seq_len, 1], dtype="int64")
    pos_ids = fluid.layers.data(
        name="pos_ids", shape=[-1, args.max_seq_len, 1], dtype="int64")
    input_mask = fluid.layers.data(
        name="input_mask", shape=[-1, args.max_seq_len, 1], dtype="float32")
    labels = fluid.layers.data(
        name="labels", shape=[-1, 1], dtype="int64")
    seq_lens = fluid.layers.data(
L
LiuHao 已提交
51 52 53 54 55 56 57 58
        name="seq_lens", shape=[-1], dtype="int64")
    
    pyreader = fluid.io.DataLoader.from_generator(
        feed_list=[src_ids, sent_ids, pos_ids, input_mask, labels, seq_lens],
        capacity=50,
        iterable=False,
        use_double_buffer=True)
    
59 60 61 62 63 64
    ernie_inputs = {
        "src_ids": src_ids,
        "sent_ids": sent_ids,
        "pos_ids": pos_ids,
        "input_mask": input_mask,
        "seq_lens": seq_lens}
L
LiuHao 已提交
65
    
66
    return pyreader, ernie_inputs, labels
Y
Yibing Liu 已提交
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

def create_model(args,
                 embeddings,
                 labels,
                 is_prediction=False):

    """
    Create Model for sentiment classification based on ERNIE encoder
    """
    sentence_embeddings = embeddings["sentence_embeddings"]
    token_embeddings = embeddings["token_embeddings"]

    if args.model_type == "ernie_base":
        ce_loss, probs = ernie_base_net(sentence_embeddings, labels,
            args.num_labels)

    elif args.model_type == "ernie_bilstm":
        ce_loss, probs = ernie_bilstm_net(token_embeddings, labels,
            args.num_labels)

    else:
        raise ValueError("Unknown network type!")

    if is_prediction:
        return probs
    loss = fluid.layers.mean(x=ce_loss)

    num_seqs = fluid.layers.create_tensor(dtype='int64')
    accuracy = fluid.layers.accuracy(input=probs, label=labels, total=num_seqs)

    return loss, accuracy, num_seqs


def evaluate(exe, test_program, test_pyreader, fetch_list, eval_phase):
    """
    Evaluation Function
    """
    test_pyreader.start()
    total_cost, total_acc, total_num_seqs = [], [], []
    time_begin = time.time()
    while True:
        try:
            np_loss, np_acc, np_num_seqs = exe.run(program=test_program,
                                                   fetch_list=fetch_list,
                                                   return_numpy=False)
            np_loss = np.array(np_loss)
            np_acc = np.array(np_acc)
            np_num_seqs = np.array(np_num_seqs)
            total_cost.extend(np_loss * np_num_seqs)
            total_acc.extend(np_acc * np_num_seqs)
            total_num_seqs.extend(np_num_seqs)
        except fluid.core.EOFException:
            test_pyreader.reset()
            break
    time_end = time.time()
    print("[%s evaluation] ave loss: %f, ave acc: %f, elapsed time: %f s" %
        (eval_phase, np.sum(total_cost) / np.sum(total_num_seqs),
        np.sum(total_acc) / np.sum(total_num_seqs), time_end - time_begin))


def infer(exe, infer_program, infer_pyreader, fetch_list, infer_phase):
    """
    Inference Function
    """
    infer_pyreader.start()
    time_begin = time.time()
    while True:
        try:
            batch_probs = exe.run(program=infer_program, fetch_list=fetch_list,
                                return_numpy=True)
            for probs in batch_probs[0]:
                print("%d\t%f\t%f" % (np.argmax(probs), probs[0], probs[1]))
        except fluid.core.EOFException:
            infer_pyreader.reset()
            break
    time_end = time.time()
    print("[%s] elapsed time: %f s" % (infer_phase, time_end - time_begin))


def main(args):
    """
    Main Function
    """
    ernie_config = ErnieConfig(args.ernie_config_path)
    ernie_config.print_config()

    if args.use_cuda:
        place = fluid.CUDAPlace(int(os.getenv('FLAGS_selected_gpus', '0')))
        dev_count = fluid.core.get_cuda_device_count()
    else:
        place = fluid.CPUPlace()
        dev_count = int(os.environ.get('CPU_NUM', multiprocessing.cpu_count()))
    exe = fluid.Executor(place)

    reader = task_reader.ClassifyReader(
        vocab_path=args.vocab_path,
        label_map_config=args.label_map_config,
        max_seq_len=args.max_seq_len,
        do_lower_case=args.do_lower_case,
        random_seed=args.random_seed)

    if not (args.do_train or args.do_val or args.do_infer):
        raise ValueError("For args `do_train`, `do_val` and `do_infer`, at "
                         "least one of them must be True.")

    startup_prog = fluid.Program()
    if args.random_seed is not None:
        startup_prog.random_seed = args.random_seed

    if args.do_train:
        train_data_generator = reader.data_generator(
            input_file=args.train_set,
            batch_size=args.batch_size,
            epoch=args.epoch,
            shuffle=True,
            phase="train")

        num_train_examples = reader.get_num_examples(args.train_set)

        max_train_steps = args.epoch * num_train_examples // args.batch_size // dev_count

        print("Device count: %d" % dev_count)
        print("Num train examples: %d" % num_train_examples)
        print("Max train steps: %d" % max_train_steps)

        train_program = fluid.Program()

        with fluid.program_guard(train_program, startup_prog):
            with fluid.unique_name.guard():
                # create ernie_pyreader
                train_pyreader, ernie_inputs, labels = ernie_pyreader(
                    args,
199
                    pyreader_name='train_pyreader')
Y
Yibing Liu 已提交
200 201

                # get ernie_embeddings
202 203 204 205
                if args.use_paddle_hub:
                    embeddings = ernie_encoder_with_paddle_hub(ernie_inputs, args.max_seq_len)
                else:
                    embeddings = ernie_encoder(ernie_inputs, ernie_config=ernie_config)
Y
Yibing Liu 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

                # user defined model based on ernie embeddings
                loss, accuracy, num_seqs = create_model(
                args,
                embeddings,
                labels=labels,
                is_prediction=False)

                optimizer = fluid.optimizer.Adam(learning_rate=args.lr)
                optimizer.minimize(loss)

        if args.verbose:
            lower_mem, upper_mem, unit = fluid.contrib.memory_usage(
                program=train_program, batch_size=args.batch_size)
            print("Theoretical memory usage in training: %.3f - %.3f %s" %
                (lower_mem, upper_mem, unit))

    if args.do_val:
224 225 226 227 228 229
        test_data_generator = reader.data_generator(
                input_file=args.dev_set,
                batch_size=args.batch_size,
                phase='dev',
                epoch=1,
                shuffle=False)
Y
Yibing Liu 已提交
230 231 232 233 234 235 236 237 238
        test_prog = fluid.Program()
        with fluid.program_guard(test_prog, startup_prog):
            with fluid.unique_name.guard():
                # create ernie_pyreader
                test_pyreader, ernie_inputs, labels = ernie_pyreader(
                    args,
                    pyreader_name='eval_reader')

                # get ernie_embeddings
239 240 241 242
                if args.use_paddle_hub:
                    embeddings = ernie_encoder_with_paddle_hub(ernie_inputs, args.max_seq_len)
                else:
                    embeddings = ernie_encoder(ernie_inputs, ernie_config=ernie_config)
Y
Yibing Liu 已提交
243 244 245 246 247 248 249 250 251 252 253

                # user defined model based on ernie embeddings
                loss, accuracy, num_seqs = create_model(
                args,
                embeddings,
                labels=labels,
                is_prediction=False)

        test_prog = test_prog.clone(for_test=True)

    if args.do_infer:
254 255 256 257 258 259
        infer_data_generator = reader.data_generator(
                input_file=args.test_set,
                batch_size=args.batch_size,
                phase='infer',
                epoch=1,
                shuffle=False)
Y
Yibing Liu 已提交
260 261 262 263 264
        infer_prog = fluid.Program()
        with fluid.program_guard(infer_prog, startup_prog):
            with fluid.unique_name.guard():
                infer_pyreader, ernie_inputs, labels = ernie_pyreader(
                    args,
265
                    pyreader_name="infer_pyreader")
Y
Yibing Liu 已提交
266 267

                # get ernie_embeddings
268 269 270 271
                if args.use_paddle_hub:
                    embeddings = ernie_encoder_with_paddle_hub(ernie_inputs, args.max_seq_len)
                else:
                    embeddings = ernie_encoder(ernie_inputs, ernie_config=ernie_config)
Y
Yibing Liu 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286

                probs = create_model(args,
                                    embeddings,
                                    labels=labels,
                                    is_prediction=True)

        infer_prog = infer_prog.clone(for_test=True)

    exe.run(startup_prog)

    if args.do_train:
        if args.init_checkpoint:
            init_checkpoint(
                exe,
                args.init_checkpoint,
287 288
                main_program=train_program)
    elif args.do_val:
Y
Yibing Liu 已提交
289 290 291 292 293 294
        if not args.init_checkpoint:
            raise ValueError("args 'init_checkpoint' should be set if"
                             "only doing validation or testing!")
        init_checkpoint(
            exe,
            args.init_checkpoint,
295 296 297 298 299 300 301 302 303
            main_program=test_prog)
    elif args.do_infer:
        if not args.init_checkpoint:
            raise ValueError("args 'init_checkpoint' should be set if"
                             "only doing validation or testing!")
        init_checkpoint(
            exe,
            args.init_checkpoint,
            main_program=infer_prog)
Y
Yibing Liu 已提交
304 305

    if args.do_train:
306
        train_exe = exe
L
LiuHao 已提交
307
        train_pyreader.set_batch_generator(train_data_generator)
Y
Yibing Liu 已提交
308 309
    else:
        train_exe = None
310
    if args.do_val:
Y
Yibing Liu 已提交
311
        test_exe = exe
L
LiuHao 已提交
312
        test_pyreader.set_batch_generator(test_data_generator)
313 314
    if args.do_infer:
        test_exe = exe
L
LiuHao 已提交
315
        infer_pyreader.set_batch_generator(infer_data_generator)
Y
Yibing Liu 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329

    if args.do_train:
        train_pyreader.start()
        steps = 0
        total_cost, total_acc, total_num_seqs = [], [], []
        time_begin = time.time()
        while True:
            try:
                steps += 1
                if steps % args.skip_steps == 0:
                    fetch_list = [loss.name, accuracy.name, num_seqs.name]
                else:
                    fetch_list = []

330
                outputs = train_exe.run(program=train_program, fetch_list=fetch_list, return_numpy=False)
Y
Yibing Liu 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
                if steps % args.skip_steps == 0:
                    np_loss, np_acc, np_num_seqs = outputs
                    np_loss = np.array(np_loss)
                    np_acc = np.array(np_acc)
                    np_num_seqs = np.array(np_num_seqs)
                    total_cost.extend(np_loss * np_num_seqs)
                    total_acc.extend(np_acc * np_num_seqs)
                    total_num_seqs.extend(np_num_seqs)

                    if args.verbose:
                        verbose = "train pyreader queue size: %d, " % train_pyreader.queue.size()
                        print(verbose)

                    time_end = time.time()
                    used_time = time_end - time_begin
                    print("step: %d, ave loss: %f, "
                        "ave acc: %f, speed: %f steps/s" %
                        (steps, np.sum(total_cost) / np.sum(total_num_seqs),
                        np.sum(total_acc) / np.sum(total_num_seqs),
                        args.skip_steps / used_time))
                    total_cost, total_acc, total_num_seqs = [], [], []
                    time_begin = time.time()

                if steps % args.save_steps == 0:
                    save_path = os.path.join(args.checkpoints,
                                         "step_" + str(steps))
                    fluid.io.save_persistables(exe, save_path, train_program)

                if steps % args.validation_steps == 0:
                    # evaluate dev set
                    if args.do_val:
                        evaluate(exe, test_prog, test_pyreader,
                                [loss.name, accuracy.name, num_seqs.name],
                                "dev")
365

Y
Yibing Liu 已提交
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
            except fluid.core.EOFException:
                save_path = os.path.join(args.checkpoints, "step_" + str(steps))
                fluid.io.save_persistables(exe, save_path, train_program)
                train_pyreader.reset()
                break

    # final eval on dev set
    if args.do_val:
        print("Final validation result:")
        evaluate(exe, test_prog, test_pyreader,
            [loss.name, accuracy.name, num_seqs.name], "dev")

    # final eval on test set
    if args.do_infer:
        print("Final test result:")
        infer(exe, infer_prog, infer_pyreader,
            [probs.name], "infer")

if __name__ == "__main__":
385 386 387
    args = PDConfig()
    args.build()
    args.print_arguments()
L
LiuHao 已提交
388
    check_cuda(args.use_cuda)
Y
Yibing Liu 已提交
389
    main(args)