train.py 2.6 KB
Newer Older
0
0YuanZhang0 已提交
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.
"""
SequenceTagging network structure
"""

from __future__ import division
from __future__ import print_function

21
import paddle
0
0YuanZhang0 已提交
22 23
import paddle.fluid as fluid
from paddle.fluid.optimizer import AdamOptimizer
24
from paddle.static import InputSpec as Input
25 26 27 28 29

from sequence_tagging import SeqTagging, LacLoss, ChunkEval
from reader import LacDataset, LacDataLoader
from utils.check import check_gpu, check_version
from utils.configure import PDConfig
0
0YuanZhang0 已提交
30 31 32


def main(args):
33
    place = paddle.set_device(args.device)
0
0YuanZhang0 已提交
34 35
    fluid.enable_dygraph(place) if args.dynamic else None

L
LielinJiang 已提交
36 37
    inputs = [
        Input(
38 39 40 41 42
            [None, None], 'int64', name='words'),
        Input(
            [None], 'int64', name='length'),
        Input(
            [None, None], 'int64', name='target'),
L
LielinJiang 已提交
43
    ]
0
0YuanZhang0 已提交
44

0
0YuanZhang0 已提交
45
    labels = [Input([None, None], 'int64', name='labels')]
0
0YuanZhang0 已提交
46

0
seq_tag  
0YuanZhang0 已提交
47 48
    dataset = LacDataset(args)
    train_dataset = LacDataLoader(args, place, phase="train")
0
0YuanZhang0 已提交
49 50 51

    vocab_size = dataset.vocab_size
    num_labels = dataset.num_labels
52 53 54 55 56
    model = paddle.Model(
        SeqTagging(
            args, vocab_size, num_labels, mode="train"),
        inputs=inputs,
        labels=labels)
0
0YuanZhang0 已提交
57 58 59 60 61

    optim = AdamOptimizer(
        learning_rate=args.base_learning_rate,
        parameter_list=model.parameters())

62
    model.prepare(optim, LacLoss(), ChunkEval(num_labels))
0
0YuanZhang0 已提交
63

0
0YuanZhang0 已提交
64 65 66 67 68
    if args.init_from_checkpoint:
        model.load(args.init_from_checkpoint)

    if args.init_from_pretrain_model:
        model.load(args.init_from_pretrain_model, reset_optimizer=True)
0
0YuanZhang0 已提交
69

0
seq_tag  
0YuanZhang0 已提交
70
    model.fit(train_dataset.dataloader,
0
0YuanZhang0 已提交
71 72 73 74 75 76 77 78
              epochs=args.epoch,
              batch_size=args.batch_size,
              eval_freq=args.eval_freq,
              save_freq=args.save_freq,
              save_dir=args.save_dir)


if __name__ == '__main__':
0
0YuanZhang0 已提交
79 80 81
    args = PDConfig(yaml_file="sequence_tagging.yaml")
    args.build()
    args.Print()
L
LielinJiang 已提交
82

0
0YuanZhang0 已提交
83 84
    use_gpu = True if args.device == "gpu" else False
    check_gpu(use_gpu)
85 86
    # TODO: add check for 2.0.0-alpha0 if fluid.require_version support
    # check_version()
0
0YuanZhang0 已提交
87

0
0YuanZhang0 已提交
88
    main(args)