train.py 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Copyright (c) 2020 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 paddle
import paddlehub as hub
K
KP 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
from paddlehub.datasets import MSRA_NER

import ast
import argparse

parser = argparse.ArgumentParser(__doc__)
parser.add_argument("--num_epoch", type=int, default=3, help="Number of epoches for fine-tuning.")
parser.add_argument("--use_gpu", type=ast.literal_eval, default=True, help="Whether use GPU for fine-tuning, input should be True or False")
parser.add_argument("--learning_rate", type=float, default=5e-5, help="Learning rate used to train with warmup.")
parser.add_argument("--max_seq_len", type=int, default=128, help="Number of words of the longest seqence.")
parser.add_argument("--batch_size", type=int, default=32, help="Total examples' number in batch for training.")
parser.add_argument("--checkpoint_dir", type=str, default='./checkpoint', help="Directory to model checkpoint")
parser.add_argument("--save_interval", type=int, default=1, help="Save checkpoint every n epoch.")

args = parser.parse_args()

33 34

if __name__ == '__main__':
K
KP 已提交
35
    label_list = MSRA_NER.label_list
36 37 38
    label_map = {
        idx: label for idx, label in enumerate(label_list)
    }
K
KP 已提交
39

40 41 42 43
    model = hub.Module(
        name='ernie_tiny',
        version='2.0.1',
        task='token-cls',
K
KP 已提交
44
        label_map=label_map,   # Required for token classification task
45 46
    )

K
KP 已提交
47 48 49 50
    tokenizer = model.get_tokenizer()
    train_dataset = MSRA_NER(
        tokenizer=tokenizer,
        max_seq_len=args.max_seq_len,
51 52
        mode='train'
    )
K
KP 已提交
53 54 55
    dev_dataset = MSRA_NER(
        tokenizer=tokenizer,
        max_seq_len=args.max_seq_len,
56 57
        mode='dev'
    )
K
KP 已提交
58 59 60 61 62
    test_dataset = MSRA_NER(
        tokenizer=tokenizer,
        max_seq_len=args.max_seq_len,
        mode='test'
    )
63

K
KP 已提交
64 65 66 67 68 69 70 71 72 73
    optimizer = paddle.optimizer.AdamW(learning_rate=args.learning_rate, parameters=model.parameters())
    trainer = hub.Trainer(model, optimizer, checkpoint_dir=args.checkpoint_dir, use_gpu=args.use_gpu)
    trainer.train(
        train_dataset,
        epochs=args.num_epoch,
        batch_size=args.batch_size,
        eval_dataset=dev_dataset,
        save_interval=args.save_interval,
    )
    trainer.evaluate(test_dataset, batch_size=args.batch_size)