Repository: PaddlePaddle/models

Issue:
lexical_analysis易用性
1. windows环境下，在models/PaddleNLP/lexical_analysis目录下执行python run_sequence_labeling.py  --train_data ./data/train.tsv --test_data ./data/test.tsv --do_train True  --do_test True  --do_infer False --save_model_per_batches 10000  --batch_size 100 --epoch 10 --use_gpu 0 --traindata_shuffle_buffer 200000 --word_emb_dim 768  --grnn_hidden_dim 768 对lexical_analysis模型进行训练，报错信息如下：
![21](https://user-images.githubusercontent.com/37854899/57517132-bd2a0d00-7348-11e9-8488-08022edff794.PNG)
需要将reader.py 57行sum(1 for line in open(filename, "r"))修改为 sum(1 for line in open(filename, "r"，encoding='UTF-8'))，模型可正常完成训练。linux下训练无该问题。
2. 建议将现有默认数据名称./data/train_data、./data/test_data修改为./data/train.tsv、./data/test.tsv与下载数据名称保持一致；
![22](https://user-images.githubusercontent.com/37854899/57517569-ba7be780-7349-11e9-9892-dff8f0752984.PNG)
3. 建议将是否使用GPU设置：Whether or not to use GPU. -1 means CPU, else GPU id
 修改为  0：使用CPU，1：使用GPU ；和其他模型use_GPU设置保持一致。
![23](https://user-images.githubusercontent.com/37854899/57521226-409c2c00-7352-11e9-9865-8fa01aa3fd8f.PNG)
4. run_sequence_labeling.py中没有用到do_valid字段，需要在代码中删除该冗余字段。
![24](https://user-images.githubusercontent.com/37854899/57527098-b1971000-7361-11e9-9358-89b24613b241.PNG)



Discussion:

Resolving patch:
diff --git a/PaddleNLP/lexical_analysis/reader.py b/PaddleNLP/lexical_analysis/reader.py
--- a/PaddleNLP/lexical_analysis/reader.py
+++ b/PaddleNLP/lexical_analysis/reader.py
@@ -54,7 +54,7 @@ def num_labels(self):
 
     def get_num_examples(self, filename):
         """num of line of file"""
-        return sum(1 for line in open(filename, "r"))
+        return sum(1 for line in io.open(filename, "r", encoding='utf-8'))
 
     def word_to_ids(self, words):
         """convert word to word index"""
diff --git a/PaddleNLP/lexical_analysis/run_sequence_labeling.py b/PaddleNLP/lexical_analysis/run_sequence_labeling.py
--- a/PaddleNLP/lexical_analysis/run_sequence_labeling.py
+++ b/PaddleNLP/lexical_analysis/run_sequence_labeling.py
@@ -40,23 +40,17 @@
 data_g.add_arg("word_dict_path", str, "./conf/word.dic", "The path of the word dictionary.")
 data_g.add_arg("label_dict_path", str, "./conf/tag.dic", "The path of the label dictionary.")
 data_g.add_arg("word_rep_dict_path", str, "./conf/q2b.dic", "The path of the word replacement Dictionary.")
-data_g.add_arg("train_data", str, "./data/train_data", "The folder where the training data is located.")
-data_g.add_arg("test_data", str, "./data/test_data", "The folder where the training data is located.")
+data_g.add_arg("train_data", str, "./data/train.tsv", "The folder where the training data is located.")
+data_g.add_arg("test_data", str, "./data/test.tsv", "The folder where the training data is located.")
 data_g.add_arg("infer_data", str, "./data/test.tsv", "The folder where the training data is located.")
 data_g.add_arg("model_save_dir", str, "./models", "The model will be saved in this path.")
 data_g.add_arg("init_checkpoint", str, "", "Path to init model")
 
-data_g.add_arg("corpus_type_list", str, ["human", "feed", "query", "title", "news"],
-        "The pattern list of different types of corpus used in training.", nargs='+')
-data_g.add_arg("corpus_proportion_list", float, [0.2, 0.2, 0.2, 0.2, 0.2],
-        "The proportion list of different types of corpus used in training.", nargs='+')
-
 # 3. train parameters
 train_g = utils.ArgumentGroup(parser, "training", "training options")
 
 train_g.add_arg("do_train", bool, True, "whether to perform training")
-train_g.add_arg("do_valid", bool, False, "whether to perform validation")
-train_g.add_arg("do_test", bool, True, "whether to perform validation")
+train_g.add_arg("do_test", bool, True, "whether to perform testing")
 train_g.add_arg("do_infer", bool, False, "whether to perform inference")
 train_g.add_arg("random_seed", int, 0, "random seed for training")
 train_g.add_arg("save_model_per_batches", int, 10000, "Save the model once per xxxx batch of training")
@@ -64,7 +58,7 @@
 train_g.add_arg("batch_size", int, 80, "The number of sequences contained in a mini-batch, "
         "or the maximum number of tokens (include paddings) contained in a mini-batch.")
 train_g.add_arg("epoch", int, 10, "corpus iteration num")
-train_g.add_arg("use_gpu", int, -1, "Whether or not to use GPU. -1 means CPU, else GPU id")
+train_g.add_arg("use_cuda", bool, False, "If set, use GPU for training.")
 train_g.add_arg("traindata_shuffle_buffer", int, 200, "The buffer size used in shuffle the training data.")
 train_g.add_arg("base_learning_rate", float, 1e-3, "The basic learning rate that affects the entire network.")
 train_g.add_arg("emb_learning_rate", float, 5,
@@ -76,12 +70,6 @@
 args = parser.parse_args()
 # yapf: enable.
 
-if len(args.corpus_proportion_list) != len(args.corpus_type_list):
-    sys.stderr.write(
-        "The length of corpus_proportion_list should be equal to the length of corpus_type_list.\n"
-    )
-    exit(-1)
-
 print(args)
 
 
@@ -217,8 +205,8 @@ def main(args):
 
 
     # init executor
-    if args.use_gpu >= 0:
-        place = fluid.CUDAPlace(args.use_gpu)
+    if args.use_cuda:
+        place = fluid.CUDAPlace(int(os.getenv('FLAGS_selected_gpus', '0')))
         dev_count = fluid.core.get_cuda_device_count()
     else:
         place = fluid.CPUPlace()


Test patch: