train.py 6.0 KB
Newer Older
G
guosheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# 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 logging
import os

import numpy as np
import paddle
import paddle.fluid as fluid
D
dengkaipeng 已提交
21
from paddle.io import DataLoader
G
guosheng 已提交
22 23 24 25

from utils.configure import PDConfig
from utils.check import check_gpu, check_version

26 27
from hapi.model import Input, set_device
from hapi.callbacks import ProgBarLogger
G
guosheng 已提交
28
from reader import create_data_loader
G
guosheng 已提交
29
from transformer import Transformer, CrossEntropyCriterion
G
guosheng 已提交
30 31


G
guosheng 已提交
32
class TrainCallback(ProgBarLogger):
33 34 35 36 37
    def __init__(self,
                 args,
                 verbose=2,
                 train_steps_fn=None,
                 eval_steps_fn=None):
38
        # TODO(guosheng): save according to step
G
guosheng 已提交
39 40 41 42 43 44
        super(TrainCallback, self).__init__(args.print_step, verbose)
        # the best cross-entropy value with label smoothing
        loss_normalizer = -(
            (1. - args.label_smooth_eps) * np.log(
                (1. - args.label_smooth_eps)) + args.label_smooth_eps *
            np.log(args.label_smooth_eps / (args.trg_vocab_size - 1) + 1e-20))
G
guosheng 已提交
45
        self.loss_normalizer = loss_normalizer
46 47
        self.train_steps_fn = train_steps_fn
        self.eval_steps_fn = eval_steps_fn
G
guosheng 已提交
48 49

    def on_train_begin(self, logs=None):
G
guosheng 已提交
50
        super(TrainCallback, self).on_train_begin(logs)
G
guosheng 已提交
51 52
        self.train_metrics += ["normalized loss", "ppl"]

53 54 55 56
    def on_train_batch_begin(self, step, logs=None):
        if step == 0 and self.train_steps_fn:
            self.train_progbar._num = self.train_steps_fn()

G
guosheng 已提交
57 58 59
    def on_train_batch_end(self, step, logs=None):
        logs["normalized loss"] = logs["loss"][0] - self.loss_normalizer
        logs["ppl"] = np.exp(min(logs["loss"][0], 100))
G
guosheng 已提交
60
        super(TrainCallback, self).on_train_batch_end(step, logs)
G
guosheng 已提交
61 62

    def on_eval_begin(self, logs=None):
G
guosheng 已提交
63 64 65
        super(TrainCallback, self).on_eval_begin(logs)
        self.eval_metrics = list(
            self.eval_metrics) + ["normalized loss", "ppl"]
G
guosheng 已提交
66

67 68 69 70
    def on_eval_batch_begin(self, step, logs=None):
        if step == 0 and self.eval_steps_fn:
            self.eval_progbar._num = self.eval_steps_fn()

G
guosheng 已提交
71 72 73
    def on_eval_batch_end(self, step, logs=None):
        logs["normalized loss"] = logs["loss"][0] - self.loss_normalizer
        logs["ppl"] = np.exp(min(logs["loss"][0], 100))
G
guosheng 已提交
74
        super(TrainCallback, self).on_eval_batch_end(step, logs)
G
guosheng 已提交
75 76 77


def do_train(args):
78 79
    device = set_device("gpu" if args.use_cuda else "cpu")
    fluid.enable_dygraph(device) if args.eager_run else None
G
guosheng 已提交
80 81 82 83 84 85 86

    # set seed for CE
    random_seed = eval(str(args.random_seed))
    if random_seed is not None:
        fluid.default_main_program().random_seed = random_seed
        fluid.default_startup_program().random_seed = random_seed

G
guosheng 已提交
87
    # define inputs
G
guosheng 已提交
88
    inputs = [
G
guosheng 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        Input(
            [None, None], "int64", name="src_word"),
        Input(
            [None, None], "int64", name="src_pos"),
        Input(
            [None, args.n_head, None, None],
            "float32",
            name="src_slf_attn_bias"),
        Input(
            [None, None], "int64", name="trg_word"),
        Input(
            [None, None], "int64", name="trg_pos"),
        Input(
            [None, args.n_head, None, None],
            "float32",
            name="trg_slf_attn_bias"),
        Input(
            [None, args.n_head, None, None],
            "float32",
            name="trg_src_attn_bias"),
G
guosheng 已提交
109 110 111 112 113 114 115 116
    ]
    labels = [
        Input(
            [None, 1], "int64", name="label"),
        Input(
            [None, 1], "float32", name="weight"),
    ]

G
guosheng 已提交
117
    # def dataloader
118 119
    (train_loader, train_steps_fn), (
        eval_loader, eval_steps_fn) = create_data_loader(args, device)
120

G
guosheng 已提交
121
    # define model
G
guosheng 已提交
122 123 124 125 126 127 128
    transformer = Transformer(
        args.src_vocab_size, args.trg_vocab_size, args.max_length + 1,
        args.n_layer, args.n_head, args.d_key, args.d_value, args.d_model,
        args.d_inner_hid, args.prepostprocess_dropout, args.attention_dropout,
        args.relu_dropout, args.preprocess_cmd, args.postprocess_cmd,
        args.weight_sharing, args.bos_idx, args.eos_idx)

G
guosheng 已提交
129 130
    transformer.prepare(
        fluid.optimizer.Adam(
G
guosheng 已提交
131 132 133 134
            learning_rate=fluid.layers.noam_decay(
                args.d_model,
                args.warmup_steps,
                learning_rate=args.learning_rate),
G
guosheng 已提交
135 136 137 138 139 140 141
            beta1=args.beta1,
            beta2=args.beta2,
            epsilon=float(args.eps),
            parameter_list=transformer.parameters()),
        CrossEntropyCriterion(args.label_smooth_eps),
        inputs=inputs,
        labels=labels)
G
guosheng 已提交
142 143 144

    ## init from some checkpoint, to resume the previous training
    if args.init_from_checkpoint:
G
guosheng 已提交
145
        transformer.load(args.init_from_checkpoint)
G
guosheng 已提交
146 147
    ## init from some pretrain models, to better solve the current task
    if args.init_from_pretrain_model:
G
guosheng 已提交
148
        transformer.load(args.init_from_pretrain_model, reset_optimizer=True)
G
guosheng 已提交
149

G
guosheng 已提交
150
    # model train
151
    transformer.fit(train_data=train_loader,
G
guosheng 已提交
152
                    eval_data=eval_loader,
G
guosheng 已提交
153
                    epochs=args.epoch,
G
guosheng 已提交
154 155
                    eval_freq=1,
                    save_freq=1,
G
guosheng 已提交
156
                    save_dir=args.save_model,
157 158 159 160 161 162
                    callbacks=[
                        TrainCallback(
                            args,
                            train_steps_fn=train_steps_fn,
                            eval_steps_fn=eval_steps_fn)
                    ])
G
guosheng 已提交
163 164 165 166 167 168 169 170 171 172


if __name__ == "__main__":
    args = PDConfig(yaml_file="./transformer.yaml")
    args.build()
    args.Print()
    check_gpu(args.use_cuda)
    check_version()

    do_train(args)