run.py 3.2 KB
Newer Older
W
wangxiao1021 已提交
1 2 3 4 5 6 7 8 9
# coding=utf-8
import paddlepalm as palm
import json
from paddlepalm.distribute import gpu_dev_count


if __name__ == '__main__':

    # configs
W
wangxiao1021 已提交
10
    max_seqlen = 128
W
wangxiao1021 已提交
11 12 13 14 15
    batch_size = 16
    num_epochs = 20
    print_steps = 5
    lr = 2e-5
    num_classes = 130
W
wangxiao1021 已提交
16
    weight_decay = 0.01
W
wangxiao1021 已提交
17
    num_classes_intent = 26
W
wangxiao1021 已提交
18
    dropout_prob = 0.1
W
wangxiao1021 已提交
19 20 21
    random_seed = 0
    label_map = './data/atis/atis_slot/label_map.json'
    vocab_path = './pretrain/ernie-en-base/vocab.txt'
W
wangxiao1021 已提交
22

W
wangxiao1021 已提交
23 24 25
    train_slot = './data/atis/atis_slot/train.tsv'
    train_intent = './data/atis/atis_intent/train.tsv'
    predict_file = './data/atis/atis_slot/test.tsv'
W
wangxiao1021 已提交
26 27 28
    save_path = './outputs/'
    pred_output = './outputs/predict/'
    save_type = 'ckpt'
W
wangxiao1021 已提交
29 30 31

    pre_params = './pretrain/ernie-en-base/params'
    config = json.load(open('./pretrain/ernie-en-base/ernie_config.json'))
W
wangxiao1021 已提交
32 33 34 35
    input_dim = config['hidden_size']

    # -----------------------  for training ----------------------- 

W
wangxiao1021 已提交
36 37
    # step 1-1: create readers for training 
    seq_label_reader = palm.reader.SequenceLabelReader(vocab_path, max_seqlen, label_map, seed=random_seed)
W
wangxiao1021 已提交
38
    match_reader = palm.reader.MatchReader(vocab_path, max_seqlen, seed=random_seed)
W
wangxiao1021 已提交
39

W
wangxiao1021 已提交
40
    # step 1-2: load the training data
W
wangxiao1021 已提交
41 42
    seq_label_reader.load_data(train_slot, file_format='tsv', num_epochs=None, batch_size=batch_size)
    match_reader.load_data(train_intent, file_format='tsv', num_epochs=None, batch_size=batch_size)
W
wangxiao1021 已提交
43
 
W
wangxiao1021 已提交
44 45 46 47
    # step 2: create a backbone of the model to extract text features
    ernie = palm.backbone.ERNIE.from_config(config)

    # step 3: register the backbone in readers
W
wangxiao1021 已提交
48
    seq_label_reader.register_with(ernie)
W
wangxiao1021 已提交
49 50 51
    match_reader.register_with(ernie)

    # step 4: create task output heads
W
wangxiao1021 已提交
52 53
    seq_label_head = palm.head.SequenceLabel(num_classes, input_dim, dropout_prob)
    match_head = palm.head.Match(num_classes_intent, input_dim, dropout_prob)
W
wangxiao1021 已提交
54
   
W
wangxiao1021 已提交
55
    # step 5-1: create a task trainer
W
wangxiao1021 已提交
56 57 58 59 60 61 62 63
    trainer_seq_label = palm.Trainer("slot", mix_ratio=1.0)
    trainer_match = palm.Trainer("intent", mix_ratio=0.5)
    trainer = palm.MultiHeadTrainer([trainer_seq_label, trainer_match])
    # # step 5-2: build forward graph with backbone and task head
    loss_var1 = trainer_match.build_forward(ernie, match_head)
    loss_var2 = trainer_seq_label.build_forward(ernie, seq_label_head)
    loss_var = trainer.build_forward()

W
wangxiao1021 已提交
64
    # step 6-1*: use warmup
W
wangxiao1021 已提交
65
    n_steps = seq_label_reader.num_examples * 1.5 * num_epochs // batch_size
W
wangxiao1021 已提交
66 67 68 69 70 71 72 73
    warmup_steps = int(0.1 * n_steps)
    sched = palm.lr_sched.TriangularSchedualer(warmup_steps, n_steps)
    # step 6-2: create a optimizer
    adam = palm.optimizer.Adam(loss_var, lr, sched)
    # step 6-3: build backward
    trainer.build_backward(optimizer=adam, weight_decay=weight_decay)

    # step 7: fit prepared reader and data
W
wangxiao1021 已提交
74
    trainer.fit_readers_with_mixratio([seq_label_reader, match_reader], "slot", num_epochs)
W
wangxiao1021 已提交
75

W
wangxiao1021 已提交
76 77 78
    # step 8-1*: load pretrained parameters
    trainer.load_pretrain(pre_params)
    # step 8-2*: set saver to save model
W
wangxiao1021 已提交
79 80 81
    # save_steps = int(n_steps-batch_size)
    save_steps = 10
    trainer_seq_label.set_saver(save_path=save_path, save_steps=save_steps, save_type=save_type, is_multi=True)
W
wangxiao1021 已提交
82
    # step 8-3: start training
W
wangxiao1021 已提交
83
    trainer.train(print_steps=print_steps)