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


if __name__ == '__main__':

    # configs
    max_seqlen = 256
    batch_size = 8
W
wangxiao1021 已提交
11
    vocab_path = './pretrain/ERNIE-v1-zh-base/vocab.txt'
W
wangxiao1021 已提交
12 13
    predict_file = './data/test.tsv'
    random_seed = 1
W
wangxiao1021 已提交
14
    config = json.load(open('./pretrain/ERNIE-v1-zh-base/ernie_config.json'))
W
wangxiao1021 已提交
15 16 17 18 19
    input_dim = config['hidden_size']
    num_classes = 2
    task_name = 'chnsenticorp'
    pred_output = './outputs/predict/'
    print_steps = 20
W
wangxiao1021 已提交
20
    pre_params = './pretrain/ERNIE-v1-zh-base/params'
W
wangxiao1021 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

    # -----------------------  for prediction ----------------------- 

    # step 1-1: create readers for prediction
    print('prepare to predict...')
    predict_cls_reader = palm.reader.ClassifyReader(vocab_path, max_seqlen, seed=random_seed, phase='predict')
    # step 1-2: load the training data
    predict_cls_reader.load_data(predict_file, batch_size)
    
    # step 2: create a backbone of the model to extract text features
    pred_ernie = palm.backbone.ERNIE.from_config(config, phase='predict')

    # step 3: register the backbone in reader
    predict_cls_reader.register_with(pred_ernie)
    
    # step 4: create the task output head
    cls_pred_head = palm.head.Classify(num_classes, input_dim, phase='predict')
    
    # step 5-1: create a task trainer
    trainer = palm.Trainer(task_name)
    # step 5-2: build forward graph with backbone and task head
    trainer.build_predict_forward(pred_ernie, cls_pred_head)
 
W
wangxiao1021 已提交
44 45
    # step 6: load checkpoint
    trainer.load_predict_model(pre_params)
W
wangxiao1021 已提交
46 47 48 49 50 51 52

    # step 7: fit prepared reader and data
    trainer.fit_reader(predict_cls_reader, phase='predict')

    # step 8: predict
    print('predicting..')
    trainer.predict(print_steps=print_steps, output_dir=pred_output)