train.py 8.6 KB
Newer Older
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
# -*- coding: utf-8 -*-
#   Copyright (c) 2018 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.

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

import numpy as np
import time
import os
import logging
import random
import math
import contextlib

import paddle
import paddle.fluid as fluid
30
from paddle.fluid.clip import GradientClipByGlobalNorm
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 59

import reader

import sys
if sys.version[0] == '2':
    reload(sys)
    sys.setdefaultencoding("utf-8")

from args import *
from base_model import BaseModel
from attention_model import AttentionModel
import logging
import pickle


def main():
    args = parse_args()
    print(args)
    num_layers = args.num_layers
    src_vocab_size = args.src_vocab_size
    tar_vocab_size = args.tar_vocab_size
    batch_size = args.batch_size
    dropout = args.dropout
    init_scale = args.init_scale
    max_grad_norm = args.max_grad_norm
    hidden_size = args.hidden_size

    place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
    with fluid.dygraph.guard(place):
Z
zhengya01 已提交
60
        #args.enable_ce = True
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        if args.enable_ce:
            fluid.default_startup_program().random_seed = 102
            fluid.default_main_program().random_seed = 102
            np.random.seed(102)
            random.seed(102)

        # Training process

        if args.attention:
            model = AttentionModel(
                hidden_size,
                src_vocab_size,
                tar_vocab_size,
                batch_size,
                num_layers=num_layers,
                init_scale=init_scale,
                dropout=dropout)
        else:
            model = BaseModel(
                hidden_size,
                src_vocab_size,
                tar_vocab_size,
                batch_size,
                num_layers=num_layers,
                init_scale=init_scale,
                dropout=dropout)
87
        gloabl_norm_clip = GradientClipByGlobalNorm(max_grad_norm)
88 89 90
        lr = args.learning_rate
        opt_type = args.optimizer
        if opt_type == "sgd":
91 92 93
            optimizer = fluid.optimizer.SGD(lr,
                                            parameter_list=model.parameters(),
                                            grad_clip=gloabl_norm_clip)
94
        elif opt_type == "adam":
95 96 97 98
            optimizer = fluid.optimizer.Adam(
                lr,
                parameter_list=model.parameters(),
                grad_clip=gloabl_norm_clip)
99 100 101 102 103 104 105 106 107 108 109 110
        else:
            print("only support [sgd|adam]")
            raise Exception("opt type not support")

        train_data_prefix = args.train_data_prefix
        eval_data_prefix = args.eval_data_prefix
        test_data_prefix = args.test_data_prefix
        vocab_prefix = args.vocab_prefix
        src_lang = args.src_lang
        tar_lang = args.tar_lang
        print("begin to load data")
        raw_data = reader.raw_data(src_lang, tar_lang, vocab_prefix,
111 112
                                   train_data_prefix, eval_data_prefix,
                                   test_data_prefix, args.max_len)
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        print("finished load data")
        train_data, valid_data, test_data, _ = raw_data

        def prepare_input(batch, epoch_id=0):
            src_ids, src_mask, tar_ids, tar_mask = batch
            res = {}
            src_ids = src_ids.reshape((src_ids.shape[0], src_ids.shape[1]))
            in_tar = tar_ids[:, :-1]
            label_tar = tar_ids[:, 1:]

            in_tar = in_tar.reshape((in_tar.shape[0], in_tar.shape[1]))
            label_tar = label_tar.reshape(
                (label_tar.shape[0], label_tar.shape[1], 1))
            inputs = [src_ids, in_tar, label_tar, src_mask, tar_mask]
            return inputs, np.sum(tar_mask)

        # get train epoch size
        def eval(data, epoch_id=0):
            model.eval()
            eval_data_iter = reader.get_data_iter(data, batch_size, mode='eval')
            total_loss = 0.0
            word_count = 0.0
            for batch_id, batch in enumerate(eval_data_iter):
136
                input_data_feed, word_num = prepare_input(batch, epoch_id)
137 138 139 140 141 142 143 144
                loss = model(input_data_feed)

                total_loss += loss * batch_size
                word_count += word_num
            ppl = np.exp(total_loss.numpy() / word_count)
            model.train()
            return ppl

Z
zhengya01 已提交
145 146
        ce_time = []
        ce_ppl = []
147 148
        max_epoch = args.max_epoch
        for epoch_id in range(max_epoch):
149 150
            epoch_start = time.time()

151 152 153 154 155 156 157 158 159 160
            model.train()
            if args.enable_ce:
                train_data_iter = reader.get_data_iter(
                    train_data, batch_size, enable_ce=True)
            else:
                train_data_iter = reader.get_data_iter(train_data, batch_size)

            total_loss = 0
            word_count = 0.0
            batch_times = []
H
hong 已提交
161
            total_reader_cost = 0.0
H
hong 已提交
162
            interval_time_start = time.time()
163 164

            batch_start = time.time()
165
            for batch_id, batch in enumerate(train_data_iter):
166
                batch_reader_end = time.time()
H
hong 已提交
167
                total_reader_cost += batch_reader_end - batch_start
168

169 170 171 172 173 174
                input_data_feed, word_num = prepare_input(
                    batch, epoch_id=epoch_id)
                word_count += word_num
                loss = model(input_data_feed)
                # print(loss.numpy()[0])
                loss.backward()
175
                optimizer.minimize(loss)
176 177 178
                model.clear_gradients()
                total_loss += loss * batch_size

179 180
                train_batch_cost = time.time() - batch_start
                batch_times.append(train_batch_cost)
181
                if batch_id > 0 and batch_id % 100 == 0:
182
                    print(
H
hong 已提交
183
                        "-- Epoch:[%d]; Batch:[%d]; ppl: %.5f, batch_cost: %.5f s, reader_cost: %.5f s, ips: %.5f words/s"
184 185
                        % (epoch_id, batch_id, np.exp(total_loss.numpy() /
                                                      word_count),
H
hong 已提交
186
                           train_batch_cost, total_reader_cost / 100,
H
hong 已提交
187
                           word_count / (time.time() - interval_time_start)))
Z
zhengya01 已提交
188
                    ce_ppl.append(np.exp(total_loss.numpy() / word_count))
189 190
                    total_loss = 0.0
                    word_count = 0.0
H
hong 已提交
191
                    total_reader_cost = 0.0
H
hong 已提交
192
                    interval_time_start = time.time()
193
                batch_start = time.time()
194

195
            train_epoch_cost = time.time() - epoch_start
196
            print(
197 198 199 200
                "\nTrain epoch:[%d]; epoch_cost: %.5f s; avg_batch_cost: %.5f s/step\n"
                % (epoch_id, train_epoch_cost,
                   sum(batch_times) / len(batch_times)))
            ce_time.append(train_epoch_cost)
201

202
            dir_name = os.path.join(args.model_path, "epoch_" + str(epoch_id))
203 204 205 206 207 208 209 210
            print("begin to save", dir_name)
            paddle.fluid.save_dygraph(model.state_dict(), dir_name)
            print("save finished")
            dev_ppl = eval(valid_data)
            print("dev ppl", dev_ppl)
            test_ppl = eval(test_data)
            print("test ppl", test_ppl)

Z
zhengya01 已提交
211 212 213 214 215 216 217 218 219 220 221 222
        if args.enable_ce:
            card_num = get_cards()
            _ppl = 0
            _time = 0
            try:
                _time = ce_time[-1]
                _ppl = ce_ppl[-1]
            except:
                print("ce info error")
            print("kpis\ttrain_duration_card%s\t%s" % (card_num, _time))
            print("kpis\ttrain_ppl_card%s\t%f" % (card_num, _ppl))

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

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


def check_version():
    """
    Log error and exit when the installed version of paddlepaddle is
    not satisfied.
    """
    err = "PaddlePaddle version 1.6 or higher is required, " \
          "or a suitable develop version is satisfied as well. \n" \
          "Please make sure the version is good with your code." \

    try:
        fluid.require_version('1.6.0')
    except Exception as e:
        logger.error(err)
        sys.exit(1)


if __name__ == '__main__':
    check_version()
    main()