We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
fluid/PaddleNLP/sequence_tagging_for_ner 预测问题
用ner模型在预测的时候发现，给预测数据随意标记的label会影响到最终的预测结果；当把数据减少一些(比如10w)后，这个问题就没有了，数据比较大的时候term的预测结果就会往这个随意标记的类别大量倾斜。。。

定义term的类别只有1和0， 预测是时候任意定义所有label=2，发现用较大预测数据集时候，大部分term的标签被预测为2，数据量较少时候，则是正常的

预测结果正常：
![image](https://user-images.githubusercontent.com/19464821/49934978-c2566d80-ff0a-11e8-8726-091f6b1a7549.png)

预测结果出现问题：
![image](https://user-images.githubusercontent.com/19464821/49934993-cf735c80-ff0a-11e8-8fb0-0a8bb720777c.png)


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `fluid/PaddleNLP/sequence_tagging_for_ner/train.py`
Content:
```
1 from __future__ import print_function
2 
3 import os
4 import math
5 import time
6 import numpy as np
7 import six
8 
9 import paddle
10 import paddle.fluid as fluid
11 
12 import reader
13 from network_conf import ner_net
14 from utils import logger, load_dict
15 from utils_extend import to_lodtensor, get_embedding
16 
17 
18 def test(exe, chunk_evaluator, inference_program, test_data, test_fetch_list,
19          place):
20     chunk_evaluator.reset()
21     for data in test_data():
22         word = to_lodtensor([x[0] for x in data], place)
23         mark = to_lodtensor([x[1] for x in data], place)
24         target = to_lodtensor([x[2] for x in data], place)
25         rets = exe.run(inference_program,
26                        feed={"word": word,
27                              "mark": mark,
28                              "target": target},
29                        fetch_list=test_fetch_list)
30         num_infer = np.array(rets[0])
31         num_label = np.array(rets[1])
32         num_correct = np.array(rets[2])
33         chunk_evaluator.update(num_infer[0], num_label[0], num_correct[0])
34     return chunk_evaluator.eval()
35 
36 
37 def main(train_data_file,
38          test_data_file,
39          vocab_file,
40          target_file,
41          emb_file,
42          model_save_dir,
43          num_passes,
44          use_gpu,
45          parallel,
46          batch_size=200):
47     if not os.path.exists(model_save_dir):
48         os.mkdir(model_save_dir)
49 
50     word_dict = load_dict(vocab_file)
51     label_dict = load_dict(target_file)
52 
53     word_vector_values = get_embedding(emb_file)
54 
55     word_dict_len = len(word_dict)
56     label_dict_len = len(label_dict)
57 
58     if "CE_MODE_X" in os.environ:
59         fluid.default_startup_program().random_seed = 110
60 
61     avg_cost, feature_out, word, mark, target = ner_net(
62         word_dict_len, label_dict_len, parallel)
63 
64     sgd_optimizer = fluid.optimizer.SGD(learning_rate=1e-3)
65     sgd_optimizer.minimize(avg_cost)
66 
67     crf_decode = fluid.layers.crf_decoding(
68         input=feature_out, param_attr=fluid.ParamAttr(name='crfw'))
69 
70     (precision, recall, f1_score, num_infer_chunks, num_label_chunks,
71      num_correct_chunks) = fluid.layers.chunk_eval(
72          input=crf_decode,
73          label=target,
74          chunk_scheme="IOB",
75          num_chunk_types=int(math.ceil((label_dict_len - 1) / 2.0)))
76     chunk_evaluator = fluid.metrics.ChunkEvaluator()
77 
78     inference_program = fluid.default_main_program().clone(for_test=True)
79     test_fetch_list = [num_infer_chunks, num_label_chunks, num_correct_chunks]
80 
81     if "CE_MODE_X" not in os.environ:
82         train_reader = paddle.batch(
83             paddle.reader.shuffle(
84                 reader.data_reader(train_data_file, word_dict, label_dict),
85                 buf_size=20000),
86             batch_size=batch_size)
87         test_reader = paddle.batch(
88             paddle.reader.shuffle(
89                 reader.data_reader(test_data_file, word_dict, label_dict),
90                 buf_size=20000),
91             batch_size=batch_size)
92     else:
93         train_reader = paddle.batch(
94             reader.data_reader(train_data_file, word_dict, label_dict),
95             batch_size=batch_size)
96         test_reader = paddle.batch(
97             reader.data_reader(test_data_file, word_dict, label_dict),
98             batch_size=batch_size)
99 
100     place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()
101     feeder = fluid.DataFeeder(feed_list=[word, mark, target], place=place)
102     exe = fluid.Executor(place)
103 
104     exe.run(fluid.default_startup_program())
105 
106     embedding_name = 'emb'
107     embedding_param = fluid.global_scope().find_var(embedding_name).get_tensor()
108     embedding_param.set(word_vector_values, place)
109 
110     time_begin = time.time()
111     for pass_id in six.moves.xrange(num_passes):
112         chunk_evaluator.reset()
113         for batch_id, data in enumerate(train_reader()):
114             cost_var, nums_infer, nums_label, nums_correct = exe.run(
115                 fluid.default_main_program(),
116                 feed=feeder.feed(data),
117                 fetch_list=[
118                     avg_cost, num_infer_chunks, num_label_chunks,
119                     num_correct_chunks
120                 ])
121             if batch_id % 5 == 0:
122                 print("Pass " + str(pass_id) + ", Batch " + str(batch_id) +
123                       ", Cost " + str(cost_var[0]))
124             chunk_evaluator.update(nums_infer, nums_label, nums_correct)
125         pass_precision, pass_recall, pass_f1_score = chunk_evaluator.eval()
126         print("[TrainSet] pass_id:" + str(pass_id) + " pass_precision:" + str(
127             pass_precision) + " pass_recall:" + str(pass_recall) +
128               " pass_f1_score:" + str(pass_f1_score))
129 
130         test_pass_precision, test_pass_recall, test_pass_f1_score = test(
131             exe, chunk_evaluator, inference_program, test_reader,
132             test_fetch_list, place)
133         print("[TestSet] pass_id:" + str(pass_id) + " pass_precision:" + str(
134             test_pass_precision) + " pass_recall:" + str(test_pass_recall) +
135               " pass_f1_score:" + str(test_pass_f1_score))
136 
137         save_dirname = os.path.join(model_save_dir, "params_pass_%d" % pass_id)
138         fluid.io.save_inference_model(save_dirname, ['word', 'mark', 'target'],
139                                       crf_decode, exe)
140 
141     if "CE_MODE_X" in os.environ:
142         print("kpis	train_precision	%f" % pass_precision)
143         print("kpis	test_precision	%f" % test_pass_precision)
144         print("kpis	train_duration	%f" % (time.time() - time_begin))
145 
146 
147 if __name__ == "__main__":
148     main(
149         train_data_file="data/train",
150         test_data_file="data/test",
151         vocab_file="data/vocab.txt",
152         target_file="data/target.txt",
153         emb_file="data/wordVectors.txt",
154         model_save_dir="models",
155         num_passes=2000,
156         use_gpu=False,
157         parallel=False)
158
```

Path: `fluid/PaddleNLP/sequence_tagging_for_ner/infer.py`
Content:
```
1 from __future__ import print_function
2 
3 import numpy as np
4 import six
5 
6 import paddle
7 import paddle.fluid as fluid
8 
9 from network_conf import ner_net
10 import reader
11 from utils import load_dict, load_reverse_dict
12 from utils_extend import to_lodtensor
13 
14 
15 def infer(model_path, batch_size, test_data_file, vocab_file, target_file,
16           use_gpu):
17     """
18     use the model under model_path to predict the test data, the result will be printed on the screen
19 
20     return nothing
21     """
22     word_dict = load_dict(vocab_file)
23     word_reverse_dict = load_reverse_dict(vocab_file)
24 
25     label_dict = load_dict(target_file)
26     label_reverse_dict = load_reverse_dict(target_file)
27 
28     test_data = paddle.batch(
29         reader.data_reader(test_data_file, word_dict, label_dict),
30         batch_size=batch_size)
31     place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()
32     exe = fluid.Executor(place)
33 
34     inference_scope = fluid.core.Scope()
35     with fluid.scope_guard(inference_scope):
36         [inference_program, feed_target_names,
37          fetch_targets] = fluid.io.load_inference_model(model_path, exe)
38         for data in test_data():
39             word = to_lodtensor([x[0] for x in data], place)
40             mark = to_lodtensor([x[1] for x in data], place)
41             target = to_lodtensor([x[2] for x in data], place)
42             crf_decode = exe.run(
43                 inference_program,
44                 feed={"word": word,
45                       "mark": mark,
46                       "target": target},
47                 fetch_list=fetch_targets,
48                 return_numpy=False)
49             lod_info = (crf_decode[0].lod())[0]
50             np_data = np.array(crf_decode[0])
51             assert len(data) == len(lod_info) - 1
52             for sen_index in six.moves.xrange(len(data)):
53                 assert len(data[sen_index][0]) == lod_info[
54                     sen_index + 1] - lod_info[sen_index]
55                 word_index = 0
56                 for tag_index in six.moves.xrange(lod_info[sen_index],
57                                         lod_info[sen_index + 1]):
58                     word = word_reverse_dict[data[sen_index][0][word_index]]
59                     gold_tag = label_reverse_dict[data[sen_index][2][
60                         word_index]]
61                     tag = label_reverse_dict[np_data[tag_index][0]]
62                     print(word + "\t" + gold_tag + "\t" + tag)
63                     word_index += 1
64                 print("")
65 
66 
67 if __name__ == "__main__":
68     infer(
69         model_path="models/params_pass_0",
70         batch_size=6,
71         test_data_file="data/test",
72         vocab_file="data/vocab.txt",
73         target_file="data/target.txt",
74         use_gpu=False)
75
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/fluid/PaddleNLP/sequence_tagging_for_ner/infer.py b/fluid/PaddleNLP/sequence_tagging_for_ner/infer.py
--- a/fluid/PaddleNLP/sequence_tagging_for_ner/infer.py
+++ b/fluid/PaddleNLP/sequence_tagging_for_ner/infer.py
@@ -38,12 +38,10 @@
         for data in test_data():
             word = to_lodtensor([x[0] for x in data], place)
             mark = to_lodtensor([x[1] for x in data], place)
-            target = to_lodtensor([x[2] for x in data], place)
             crf_decode = exe.run(
                 inference_program,
                 feed={"word": word,
-                      "mark": mark,
-                      "target": target},
+                      "mark": mark},
                 fetch_list=fetch_targets,
                 return_numpy=False)
             lod_info = (crf_decode[0].lod())[0]
diff --git a/fluid/PaddleNLP/sequence_tagging_for_ner/train.py b/fluid/PaddleNLP/sequence_tagging_for_ner/train.py
--- a/fluid/PaddleNLP/sequence_tagging_for_ner/train.py
+++ b/fluid/PaddleNLP/sequence_tagging_for_ner/train.py
@@ -61,22 +61,21 @@
     avg_cost, feature_out, word, mark, target = ner_net(
         word_dict_len, label_dict_len, parallel)
 
-    sgd_optimizer = fluid.optimizer.SGD(learning_rate=1e-3)
-    sgd_optimizer.minimize(avg_cost)
-
     crf_decode = fluid.layers.crf_decoding(
         input=feature_out, param_attr=fluid.ParamAttr(name='crfw'))
 
     (precision, recall, f1_score, num_infer_chunks, num_label_chunks,
-     num_correct_chunks) = fluid.layers.chunk_eval(
-         input=crf_decode,
-         label=target,
-         chunk_scheme="IOB",
-         num_chunk_types=int(math.ceil((label_dict_len - 1) / 2.0)))
+    num_correct_chunks) = fluid.layers.chunk_eval(
+        input=crf_decode,
+        label=target,
+        chunk_scheme="IOB",
+        num_chunk_types=int(math.ceil((label_dict_len - 1) / 2.0)))
     chunk_evaluator = fluid.metrics.ChunkEvaluator()
 
     inference_program = fluid.default_main_program().clone(for_test=True)
     test_fetch_list = [num_infer_chunks, num_label_chunks, num_correct_chunks]
+    sgd_optimizer = fluid.optimizer.SGD(learning_rate=1e-3)
+    sgd_optimizer.minimize(avg_cost)
 
     if "CE_MODE_X" not in os.environ:
         train_reader = paddle.batch(
@@ -135,7 +134,7 @@
               " pass_f1_score:" + str(test_pass_f1_score))
 
         save_dirname = os.path.join(model_save_dir, "params_pass_%d" % pass_id)
-        fluid.io.save_inference_model(save_dirname, ['word', 'mark', 'target'],
+        fluid.io.save_inference_model(save_dirname, ['word', 'mark'],
                                       crf_decode, exe)
 
     if "CE_MODE_X" in os.environ:

{"golden_diff": "diff --git a/fluid/PaddleNLP/sequence_tagging_for_ner/infer.py b/fluid/PaddleNLP/sequence_tagging_for_ner/infer.py\n--- a/fluid/PaddleNLP/sequence_tagging_for_ner/infer.py\n+++ b/fluid/PaddleNLP/sequence_tagging_for_ner/infer.py\n@@ -38,12 +38,10 @@\n         for data in test_data():\n             word = to_lodtensor([x[0] for x in data], place)\n             mark = to_lodtensor([x[1] for x in data], place)\n-            target = to_lodtensor([x[2] for x in data], place)\n             crf_decode = exe.run(\n                 inference_program,\n                 feed={\"word\": word,\n-                      \"mark\": mark,\n-                      \"target\": target},\n+                      \"mark\": mark},\n                 fetch_list=fetch_targets,\n                 return_numpy=False)\n             lod_info = (crf_decode[0].lod())[0]\ndiff --git a/fluid/PaddleNLP/sequence_tagging_for_ner/train.py b/fluid/PaddleNLP/sequence_tagging_for_ner/train.py\n--- a/fluid/PaddleNLP/sequence_tagging_for_ner/train.py\n+++ b/fluid/PaddleNLP/sequence_tagging_for_ner/train.py\n@@ -61,22 +61,21 @@\n     avg_cost, feature_out, word, mark, target = ner_net(\n         word_dict_len, label_dict_len, parallel)\n \n-    sgd_optimizer = fluid.optimizer.SGD(learning_rate=1e-3)\n-    sgd_optimizer.minimize(avg_cost)\n-\n     crf_decode = fluid.layers.crf_decoding(\n         input=feature_out, param_attr=fluid.ParamAttr(name='crfw'))\n \n     (precision, recall, f1_score, num_infer_chunks, num_label_chunks,\n-     num_correct_chunks) = fluid.layers.chunk_eval(\n-         input=crf_decode,\n-         label=target,\n-         chunk_scheme=\"IOB\",\n-         num_chunk_types=int(math.ceil((label_dict_len - 1) / 2.0)))\n+    num_correct_chunks) = fluid.layers.chunk_eval(\n+        input=crf_decode,\n+        label=target,\n+        chunk_scheme=\"IOB\",\n+        num_chunk_types=int(math.ceil((label_dict_len - 1) / 2.0)))\n     chunk_evaluator = fluid.metrics.ChunkEvaluator()\n \n     inference_program = fluid.default_main_program().clone(for_test=True)\n     test_fetch_list = [num_infer_chunks, num_label_chunks, num_correct_chunks]\n+    sgd_optimizer = fluid.optimizer.SGD(learning_rate=1e-3)\n+    sgd_optimizer.minimize(avg_cost)\n \n     if \"CE_MODE_X\" not in os.environ:\n         train_reader = paddle.batch(\n@@ -135,7 +134,7 @@\n               \" pass_f1_score:\" + str(test_pass_f1_score))\n \n         save_dirname = os.path.join(model_save_dir, \"params_pass_%d\" % pass_id)\n-        fluid.io.save_inference_model(save_dirname, ['word', 'mark', 'target'],\n+        fluid.io.save_inference_model(save_dirname, ['word', 'mark'],\n                                       crf_decode, exe)\n \n     if \"CE_MODE_X\" in os.environ:\n", "issue": "fluid/PaddleNLP/sequence_tagging_for_ner \u9884\u6d4b\u95ee\u9898\n\u7528ner\u6a21\u578b\u5728\u9884\u6d4b\u7684\u65f6\u5019\u53d1\u73b0\uff0c\u7ed9\u9884\u6d4b\u6570\u636e\u968f\u610f\u6807\u8bb0\u7684label\u4f1a\u5f71\u54cd\u5230\u6700\u7ec8\u7684\u9884\u6d4b\u7ed3\u679c\uff1b\u5f53\u628a\u6570\u636e\u51cf\u5c11\u4e00\u4e9b(\u6bd4\u598210w)\u540e\uff0c\u8fd9\u4e2a\u95ee\u9898\u5c31\u6ca1\u6709\u4e86\uff0c\u6570\u636e\u6bd4\u8f83\u5927\u7684\u65f6\u5019term\u7684\u9884\u6d4b\u7ed3\u679c\u5c31\u4f1a\u5f80\u8fd9\u4e2a\u968f\u610f\u6807\u8bb0\u7684\u7c7b\u522b\u5927\u91cf\u503e\u659c\u3002\u3002\u3002\r\n\r\n\u5b9a\u4e49term\u7684\u7c7b\u522b\u53ea\u67091\u548c0\uff0c \u9884\u6d4b\u662f\u65f6\u5019\u4efb\u610f\u5b9a\u4e49\u6240\u6709label=2\uff0c\u53d1\u73b0\u7528\u8f83\u5927\u9884\u6d4b\u6570\u636e\u96c6\u65f6\u5019\uff0c\u5927\u90e8\u5206term\u7684\u6807\u7b7e\u88ab\u9884\u6d4b\u4e3a2\uff0c\u6570\u636e\u91cf\u8f83\u5c11\u65f6\u5019\uff0c\u5219\u662f\u6b63\u5e38\u7684\r\n\r\n\u9884\u6d4b\u7ed3\u679c\u6b63\u5e38\uff1a\r\n![image](https://user-images.githubusercontent.com/19464821/49934978-c2566d80-ff0a-11e8-8726-091f6b1a7549.png)\r\n\r\n\u9884\u6d4b\u7ed3\u679c\u51fa\u73b0\u95ee\u9898\uff1a\r\n![image](https://user-images.githubusercontent.com/19464821/49934993-cf735c80-ff0a-11e8-8fb0-0a8bb720777c.png)\r\n\n", "before_files": [{"content": "from __future__ import print_function\n\nimport os\nimport math\nimport time\nimport numpy as np\nimport six\n\nimport paddle\nimport paddle.fluid as fluid\n\nimport reader\nfrom network_conf import ner_net\nfrom utils import logger, load_dict\nfrom utils_extend import to_lodtensor, get_embedding\n\n\ndef test(exe, chunk_evaluator, inference_program, test_data, test_fetch_list,\n         place):\n    chunk_evaluator.reset()\n    for data in test_data():\n        word = to_lodtensor([x[0] for x in data], place)\n        mark = to_lodtensor([x[1] for x in data], place)\n        target = to_lodtensor([x[2] for x in data], place)\n        rets = exe.run(inference_program,\n                       feed={\"word\": word,\n                             \"mark\": mark,\n                             \"target\": target},\n                       fetch_list=test_fetch_list)\n        num_infer = np.array(rets[0])\n        num_label = np.array(rets[1])\n        num_correct = np.array(rets[2])\n        chunk_evaluator.update(num_infer[0], num_label[0], num_correct[0])\n    return chunk_evaluator.eval()\n\n\ndef main(train_data_file,\n         test_data_file,\n         vocab_file,\n         target_file,\n         emb_file,\n         model_save_dir,\n         num_passes,\n         use_gpu,\n         parallel,\n         batch_size=200):\n    if not os.path.exists(model_save_dir):\n        os.mkdir(model_save_dir)\n\n    word_dict = load_dict(vocab_file)\n    label_dict = load_dict(target_file)\n\n    word_vector_values = get_embedding(emb_file)\n\n    word_dict_len = len(word_dict)\n    label_dict_len = len(label_dict)\n\n    if \"CE_MODE_X\" in os.environ:\n        fluid.default_startup_program().random_seed = 110\n\n    avg_cost, feature_out, word, mark, target = ner_net(\n        word_dict_len, label_dict_len, parallel)\n\n    sgd_optimizer = fluid.optimizer.SGD(learning_rate=1e-3)\n    sgd_optimizer.minimize(avg_cost)\n\n    crf_decode = fluid.layers.crf_decoding(\n        input=feature_out, param_attr=fluid.ParamAttr(name='crfw'))\n\n    (precision, recall, f1_score, num_infer_chunks, num_label_chunks,\n     num_correct_chunks) = fluid.layers.chunk_eval(\n         input=crf_decode,\n         label=target,\n         chunk_scheme=\"IOB\",\n         num_chunk_types=int(math.ceil((label_dict_len - 1) / 2.0)))\n    chunk_evaluator = fluid.metrics.ChunkEvaluator()\n\n    inference_program = fluid.default_main_program().clone(for_test=True)\n    test_fetch_list = [num_infer_chunks, num_label_chunks, num_correct_chunks]\n\n    if \"CE_MODE_X\" not in os.environ:\n        train_reader = paddle.batch(\n            paddle.reader.shuffle(\n                reader.data_reader(train_data_file, word_dict, label_dict),\n                buf_size=20000),\n            batch_size=batch_size)\n        test_reader = paddle.batch(\n            paddle.reader.shuffle(\n                reader.data_reader(test_data_file, word_dict, label_dict),\n                buf_size=20000),\n            batch_size=batch_size)\n    else:\n        train_reader = paddle.batch(\n            reader.data_reader(train_data_file, word_dict, label_dict),\n            batch_size=batch_size)\n        test_reader = paddle.batch(\n            reader.data_reader(test_data_file, word_dict, label_dict),\n            batch_size=batch_size)\n\n    place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()\n    feeder = fluid.DataFeeder(feed_list=[word, mark, target], place=place)\n    exe = fluid.Executor(place)\n\n    exe.run(fluid.default_startup_program())\n\n    embedding_name = 'emb'\n    embedding_param = fluid.global_scope().find_var(embedding_name).get_tensor()\n    embedding_param.set(word_vector_values, place)\n\n    time_begin = time.time()\n    for pass_id in six.moves.xrange(num_passes):\n        chunk_evaluator.reset()\n        for batch_id, data in enumerate(train_reader()):\n            cost_var, nums_infer, nums_label, nums_correct = exe.run(\n                fluid.default_main_program(),\n                feed=feeder.feed(data),\n                fetch_list=[\n                    avg_cost, num_infer_chunks, num_label_chunks,\n                    num_correct_chunks\n                ])\n            if batch_id % 5 == 0:\n                print(\"Pass \" + str(pass_id) + \", Batch \" + str(batch_id) +\n                      \", Cost \" + str(cost_var[0]))\n            chunk_evaluator.update(nums_infer, nums_label, nums_correct)\n        pass_precision, pass_recall, pass_f1_score = chunk_evaluator.eval()\n        print(\"[TrainSet] pass_id:\" + str(pass_id) + \" pass_precision:\" + str(\n            pass_precision) + \" pass_recall:\" + str(pass_recall) +\n              \" pass_f1_score:\" + str(pass_f1_score))\n\n        test_pass_precision, test_pass_recall, test_pass_f1_score = test(\n            exe, chunk_evaluator, inference_program, test_reader,\n            test_fetch_list, place)\n        print(\"[TestSet] pass_id:\" + str(pass_id) + \" pass_precision:\" + str(\n            test_pass_precision) + \" pass_recall:\" + str(test_pass_recall) +\n              \" pass_f1_score:\" + str(test_pass_f1_score))\n\n        save_dirname = os.path.join(model_save_dir, \"params_pass_%d\" % pass_id)\n        fluid.io.save_inference_model(save_dirname, ['word', 'mark', 'target'],\n                                      crf_decode, exe)\n\n    if \"CE_MODE_X\" in os.environ:\n        print(\"kpis\ttrain_precision\t%f\" % pass_precision)\n        print(\"kpis\ttest_precision\t%f\" % test_pass_precision)\n        print(\"kpis\ttrain_duration\t%f\" % (time.time() - time_begin))\n\n\nif __name__ == \"__main__\":\n    main(\n        train_data_file=\"data/train\",\n        test_data_file=\"data/test\",\n        vocab_file=\"data/vocab.txt\",\n        target_file=\"data/target.txt\",\n        emb_file=\"data/wordVectors.txt\",\n        model_save_dir=\"models\",\n        num_passes=2000,\n        use_gpu=False,\n        parallel=False)\n", "path": "fluid/PaddleNLP/sequence_tagging_for_ner/train.py"}, {"content": "from __future__ import print_function\n\nimport numpy as np\nimport six\n\nimport paddle\nimport paddle.fluid as fluid\n\nfrom network_conf import ner_net\nimport reader\nfrom utils import load_dict, load_reverse_dict\nfrom utils_extend import to_lodtensor\n\n\ndef infer(model_path, batch_size, test_data_file, vocab_file, target_file,\n          use_gpu):\n    \"\"\"\n    use the model under model_path to predict the test data, the result will be printed on the screen\n\n    return nothing\n    \"\"\"\n    word_dict = load_dict(vocab_file)\n    word_reverse_dict = load_reverse_dict(vocab_file)\n\n    label_dict = load_dict(target_file)\n    label_reverse_dict = load_reverse_dict(target_file)\n\n    test_data = paddle.batch(\n        reader.data_reader(test_data_file, word_dict, label_dict),\n        batch_size=batch_size)\n    place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()\n    exe = fluid.Executor(place)\n\n    inference_scope = fluid.core.Scope()\n    with fluid.scope_guard(inference_scope):\n        [inference_program, feed_target_names,\n         fetch_targets] = fluid.io.load_inference_model(model_path, exe)\n        for data in test_data():\n            word = to_lodtensor([x[0] for x in data], place)\n            mark = to_lodtensor([x[1] for x in data], place)\n            target = to_lodtensor([x[2] for x in data], place)\n            crf_decode = exe.run(\n                inference_program,\n                feed={\"word\": word,\n                      \"mark\": mark,\n                      \"target\": target},\n                fetch_list=fetch_targets,\n                return_numpy=False)\n            lod_info = (crf_decode[0].lod())[0]\n            np_data = np.array(crf_decode[0])\n            assert len(data) == len(lod_info) - 1\n            for sen_index in six.moves.xrange(len(data)):\n                assert len(data[sen_index][0]) == lod_info[\n                    sen_index + 1] - lod_info[sen_index]\n                word_index = 0\n                for tag_index in six.moves.xrange(lod_info[sen_index],\n                                        lod_info[sen_index + 1]):\n                    word = word_reverse_dict[data[sen_index][0][word_index]]\n                    gold_tag = label_reverse_dict[data[sen_index][2][\n                        word_index]]\n                    tag = label_reverse_dict[np_data[tag_index][0]]\n                    print(word + \"\\t\" + gold_tag + \"\\t\" + tag)\n                    word_index += 1\n                print(\"\")\n\n\nif __name__ == \"__main__\":\n    infer(\n        model_path=\"models/params_pass_0\",\n        batch_size=6,\n        test_data_file=\"data/test\",\n        vocab_file=\"data/vocab.txt\",\n        target_file=\"data/target.txt\",\n        use_gpu=False)\n", "path": "fluid/PaddleNLP/sequence_tagging_for_ner/infer.py"}], "after_files": [{"content": "from __future__ import print_function\n\nimport os\nimport math\nimport time\nimport numpy as np\nimport six\n\nimport paddle\nimport paddle.fluid as fluid\n\nimport reader\nfrom network_conf import ner_net\nfrom utils import logger, load_dict\nfrom utils_extend import to_lodtensor, get_embedding\n\n\ndef test(exe, chunk_evaluator, inference_program, test_data, test_fetch_list,\n         place):\n    chunk_evaluator.reset()\n    for data in test_data():\n        word = to_lodtensor([x[0] for x in data], place)\n        mark = to_lodtensor([x[1] for x in data], place)\n        target = to_lodtensor([x[2] for x in data], place)\n        rets = exe.run(inference_program,\n                       feed={\"word\": word,\n                             \"mark\": mark,\n                             \"target\": target},\n                       fetch_list=test_fetch_list)\n        num_infer = np.array(rets[0])\n        num_label = np.array(rets[1])\n        num_correct = np.array(rets[2])\n        chunk_evaluator.update(num_infer[0], num_label[0], num_correct[0])\n    return chunk_evaluator.eval()\n\n\ndef main(train_data_file,\n         test_data_file,\n         vocab_file,\n         target_file,\n         emb_file,\n         model_save_dir,\n         num_passes,\n         use_gpu,\n         parallel,\n         batch_size=200):\n    if not os.path.exists(model_save_dir):\n        os.mkdir(model_save_dir)\n\n    word_dict = load_dict(vocab_file)\n    label_dict = load_dict(target_file)\n\n    word_vector_values = get_embedding(emb_file)\n\n    word_dict_len = len(word_dict)\n    label_dict_len = len(label_dict)\n\n    if \"CE_MODE_X\" in os.environ:\n        fluid.default_startup_program().random_seed = 110\n\n    avg_cost, feature_out, word, mark, target = ner_net(\n        word_dict_len, label_dict_len, parallel)\n\n    crf_decode = fluid.layers.crf_decoding(\n        input=feature_out, param_attr=fluid.ParamAttr(name='crfw'))\n\n    (precision, recall, f1_score, num_infer_chunks, num_label_chunks,\n    num_correct_chunks) = fluid.layers.chunk_eval(\n        input=crf_decode,\n        label=target,\n        chunk_scheme=\"IOB\",\n        num_chunk_types=int(math.ceil((label_dict_len - 1) / 2.0)))\n    chunk_evaluator = fluid.metrics.ChunkEvaluator()\n\n    inference_program = fluid.default_main_program().clone(for_test=True)\n    test_fetch_list = [num_infer_chunks, num_label_chunks, num_correct_chunks]\n    sgd_optimizer = fluid.optimizer.SGD(learning_rate=1e-3)\n    sgd_optimizer.minimize(avg_cost)\n\n    if \"CE_MODE_X\" not in os.environ:\n        train_reader = paddle.batch(\n            paddle.reader.shuffle(\n                reader.data_reader(train_data_file, word_dict, label_dict),\n                buf_size=20000),\n            batch_size=batch_size)\n        test_reader = paddle.batch(\n            paddle.reader.shuffle(\n                reader.data_reader(test_data_file, word_dict, label_dict),\n                buf_size=20000),\n            batch_size=batch_size)\n    else:\n        train_reader = paddle.batch(\n            reader.data_reader(train_data_file, word_dict, label_dict),\n            batch_size=batch_size)\n        test_reader = paddle.batch(\n            reader.data_reader(test_data_file, word_dict, label_dict),\n            batch_size=batch_size)\n\n    place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()\n    feeder = fluid.DataFeeder(feed_list=[word, mark, target], place=place)\n    exe = fluid.Executor(place)\n\n    exe.run(fluid.default_startup_program())\n\n    embedding_name = 'emb'\n    embedding_param = fluid.global_scope().find_var(embedding_name).get_tensor()\n    embedding_param.set(word_vector_values, place)\n\n    time_begin = time.time()\n    for pass_id in six.moves.xrange(num_passes):\n        chunk_evaluator.reset()\n        for batch_id, data in enumerate(train_reader()):\n            cost_var, nums_infer, nums_label, nums_correct = exe.run(\n                fluid.default_main_program(),\n                feed=feeder.feed(data),\n                fetch_list=[\n                    avg_cost, num_infer_chunks, num_label_chunks,\n                    num_correct_chunks\n                ])\n            if batch_id % 5 == 0:\n                print(\"Pass \" + str(pass_id) + \", Batch \" + str(batch_id) +\n                      \", Cost \" + str(cost_var[0]))\n            chunk_evaluator.update(nums_infer, nums_label, nums_correct)\n        pass_precision, pass_recall, pass_f1_score = chunk_evaluator.eval()\n        print(\"[TrainSet] pass_id:\" + str(pass_id) + \" pass_precision:\" + str(\n            pass_precision) + \" pass_recall:\" + str(pass_recall) +\n              \" pass_f1_score:\" + str(pass_f1_score))\n\n        test_pass_precision, test_pass_recall, test_pass_f1_score = test(\n            exe, chunk_evaluator, inference_program, test_reader,\n            test_fetch_list, place)\n        print(\"[TestSet] pass_id:\" + str(pass_id) + \" pass_precision:\" + str(\n            test_pass_precision) + \" pass_recall:\" + str(test_pass_recall) +\n              \" pass_f1_score:\" + str(test_pass_f1_score))\n\n        save_dirname = os.path.join(model_save_dir, \"params_pass_%d\" % pass_id)\n        fluid.io.save_inference_model(save_dirname, ['word', 'mark'],\n                                      crf_decode, exe)\n\n    if \"CE_MODE_X\" in os.environ:\n        print(\"kpis\ttrain_precision\t%f\" % pass_precision)\n        print(\"kpis\ttest_precision\t%f\" % test_pass_precision)\n        print(\"kpis\ttrain_duration\t%f\" % (time.time() - time_begin))\n\n\nif __name__ == \"__main__\":\n    main(\n        train_data_file=\"data/train\",\n        test_data_file=\"data/test\",\n        vocab_file=\"data/vocab.txt\",\n        target_file=\"data/target.txt\",\n        emb_file=\"data/wordVectors.txt\",\n        model_save_dir=\"models\",\n        num_passes=2000,\n        use_gpu=False,\n        parallel=False)\n", "path": "fluid/PaddleNLP/sequence_tagging_for_ner/train.py"}, {"content": "from __future__ import print_function\n\nimport numpy as np\nimport six\n\nimport paddle\nimport paddle.fluid as fluid\n\nfrom network_conf import ner_net\nimport reader\nfrom utils import load_dict, load_reverse_dict\nfrom utils_extend import to_lodtensor\n\n\ndef infer(model_path, batch_size, test_data_file, vocab_file, target_file,\n          use_gpu):\n    \"\"\"\n    use the model under model_path to predict the test data, the result will be printed on the screen\n\n    return nothing\n    \"\"\"\n    word_dict = load_dict(vocab_file)\n    word_reverse_dict = load_reverse_dict(vocab_file)\n\n    label_dict = load_dict(target_file)\n    label_reverse_dict = load_reverse_dict(target_file)\n\n    test_data = paddle.batch(\n        reader.data_reader(test_data_file, word_dict, label_dict),\n        batch_size=batch_size)\n    place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()\n    exe = fluid.Executor(place)\n\n    inference_scope = fluid.core.Scope()\n    with fluid.scope_guard(inference_scope):\n        [inference_program, feed_target_names,\n         fetch_targets] = fluid.io.load_inference_model(model_path, exe)\n        for data in test_data():\n            word = to_lodtensor([x[0] for x in data], place)\n            mark = to_lodtensor([x[1] for x in data], place)\n            crf_decode = exe.run(\n                inference_program,\n                feed={\"word\": word,\n                      \"mark\": mark},\n                fetch_list=fetch_targets,\n                return_numpy=False)\n            lod_info = (crf_decode[0].lod())[0]\n            np_data = np.array(crf_decode[0])\n            assert len(data) == len(lod_info) - 1\n            for sen_index in six.moves.xrange(len(data)):\n                assert len(data[sen_index][0]) == lod_info[\n                    sen_index + 1] - lod_info[sen_index]\n                word_index = 0\n                for tag_index in six.moves.xrange(lod_info[sen_index],\n                                        lod_info[sen_index + 1]):\n                    word = word_reverse_dict[data[sen_index][0][word_index]]\n                    gold_tag = label_reverse_dict[data[sen_index][2][\n                        word_index]]\n                    tag = label_reverse_dict[np_data[tag_index][0]]\n                    print(word + \"\\t\" + gold_tag + \"\\t\" + tag)\n                    word_index += 1\n                print(\"\")\n\n\nif __name__ == \"__main__\":\n    infer(\n        model_path=\"models/params_pass_0\",\n        batch_size=6,\n        test_data_file=\"data/test\",\n        vocab_file=\"data/vocab.txt\",\n        target_file=\"data/target.txt\",\n        use_gpu=False)\n", "path": "fluid/PaddleNLP/sequence_tagging_for_ner/infer.py"}]}