From af0f48909bcaef555714bed3b3f9932ca13ea5bd Mon Sep 17 00:00:00 2001 From: Steffy-zxf Date: Mon, 11 May 2020 18:14:51 +0800 Subject: [PATCH] update demo for preset net --- demo/text_classification/predict.py | 8 +++++++- demo/text_classification/run_classifier.sh | 5 +++-- demo/text_classification/run_predict.sh | 1 + demo/text_classification/text_classifier.py | 6 +++++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/demo/text_classification/predict.py b/demo/text_classification/predict.py index 81dcd41e..2afea928 100644 --- a/demo/text_classification/predict.py +++ b/demo/text_classification/predict.py @@ -34,6 +34,7 @@ parser.add_argument("--batch_size", type=int, default=1, help="Total examp parser.add_argument("--max_seq_len", type=int, default=512, help="Number of words of the longest seqence.") parser.add_argument("--use_gpu", type=ast.literal_eval, default=False, help="Whether use GPU for finetuning, input should be True or False") parser.add_argument("--use_data_parallel", type=ast.literal_eval, default=False, help="Whether use data parallel.") +parser.add_argument("--network", type=str, default='bilstm', help="Preset network which was connected after Transformer model, such as ERNIE, BERT ,RoBERTa and ELECTRA.") args = parser.parse_args() # yapf: enable. @@ -59,7 +60,7 @@ if __name__ == '__main__': # Construct transfer learning network # Use "pooled_output" for classification tasks on an entire sentence. # Use "sequence_output" for token-level output. - pooled_output = outputs["pooled_output"] + pooled_output = outputs["sequence_output"] # Setup feed list for data feeder # Must feed all the tensor of module need @@ -79,10 +80,15 @@ if __name__ == '__main__': strategy=hub.AdamWeightDecayStrategy()) # Define a classfication finetune task by PaddleHub's API + # network choice: bilstm, bow, cnn, dpcnn, gru, lstm + # If you wanna add network after ERNIE/BERT/RoBERTa/ELECTRA module, + # you must use the outputs["sequence_output"] as the feature of TextClassifierTask + # rather than outputs["pooled_output"] cls_task = hub.TextClassifierTask( data_reader=reader, feature=pooled_output, feed_list=feed_list, + network=args.network, num_classes=dataset.num_labels, config=config) diff --git a/demo/text_classification/run_classifier.sh b/demo/text_classification/run_classifier.sh index c7bfc95e..08dd3bc0 100644 --- a/demo/text_classification/run_classifier.sh +++ b/demo/text_classification/run_classifier.sh @@ -1,5 +1,5 @@ export FLAGS_eager_delete_tensor_gb=0.0 -export CUDA_VISIBLE_DEVICES=0 +export CUDA_VISIBLE_DEVICES=0,1,2,3 CKPT_DIR="./ckpt_chnsenticorp" @@ -12,7 +12,8 @@ python -u text_classifier.py \ --max_seq_len=128 \ --warmup_proportion=0.1 \ --num_epoch=3 \ - --use_data_parallel=True + --use_data_parallel=True \ + --network=bilstm # The sugguested hyper parameters for difference task # for ChineseGLUE: diff --git a/demo/text_classification/run_predict.sh b/demo/text_classification/run_predict.sh index 5daba182..a1a9b76a 100644 --- a/demo/text_classification/run_predict.sh +++ b/demo/text_classification/run_predict.sh @@ -7,3 +7,4 @@ python -u predict.py --checkpoint_dir=$CKPT_DIR \ --max_seq_len=128 \ --use_gpu=True \ --batch_size=24 \ + --network=bilstm diff --git a/demo/text_classification/text_classifier.py b/demo/text_classification/text_classifier.py index 782347a2..8f224302 100644 --- a/demo/text_classification/text_classifier.py +++ b/demo/text_classification/text_classifier.py @@ -85,11 +85,15 @@ if __name__ == '__main__': strategy=strategy) # Define a classfication finetune task by PaddleHub's API + # network choice: bilstm, bow, cnn, dpcnn, gru, lstm + # If you wanna add network after ERNIE/BERT/RoBERTa/ELECTRA module, + # you must use the outputs["sequence_output"] as the feature of TextClassifierTask + # rather than outputs["pooled_output"] cls_task = hub.TextClassifierTask( data_reader=reader, feature=pooled_output, feed_list=feed_list, - network='dpcnn', + network=args.network, num_classes=dataset.num_labels, config=config, metrics_choices=metrics_choices) -- GitLab