We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
文本分类text_classification训练选项不匹配
1. train.py中batch_size参数冗余

2. 命令行选项gru配置错误

3. GRU Layer embedding学习率默认配置过大，导致模型训练补课收敛。

--- 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/text_classification/train.py`
Content:
```
1 import os
2 import six
3 import sys
4 import time
5 import unittest
6 import contextlib
7 
8 import paddle
9 import paddle.fluid as fluid
10 
11 import utils
12 from nets import bow_net
13 from nets import cnn_net
14 from nets import lstm_net
15 from nets import gru_net
16 
17 
18 def train(train_reader,
19           word_dict,
20           network,
21           use_cuda,
22           parallel,
23           save_dirname,
24           lr=0.2,
25           batch_size=128,
26           pass_num=30):
27     """
28     train network
29     """
30     data = fluid.layers.data(
31         name="words", shape=[1], dtype="int64", lod_level=1)
32 
33     label = fluid.layers.data(name="label", shape=[1], dtype="int64")
34 
35     if not parallel:
36         cost, acc, prediction = network(data, label, len(word_dict))
37     else:
38         places = fluid.layers.device.get_places(device_count=2)
39         pd = fluid.layers.ParallelDo(places)
40         with pd.do():
41             cost, acc, prediction = network(
42                 pd.read_input(data), pd.read_input(label), len(word_dict))
43 
44             pd.write_output(cost)
45             pd.write_output(acc)
46 
47         cost, acc = pd()
48         cost = fluid.layers.mean(cost)
49         acc = fluid.layers.mean(acc)
50 
51     sgd_optimizer = fluid.optimizer.Adagrad(learning_rate=lr)
52     sgd_optimizer.minimize(cost)
53 
54     place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
55     exe = fluid.Executor(place)
56     feeder = fluid.DataFeeder(feed_list=[data, label], place=place)
57 
58     # For internal continuous evaluation
59     if "CE_MODE_X" in os.environ:
60         fluid.default_startup_program().random_seed = 110
61     exe.run(fluid.default_startup_program())
62     for pass_id in six.moves.xrange(pass_num):
63         pass_start = time.time()
64         data_size, data_count, total_acc, total_cost = 0, 0, 0.0, 0.0
65         for data in train_reader():
66             avg_cost_np, avg_acc_np = exe.run(fluid.default_main_program(),
67                                               feed=feeder.feed(data),
68                                               fetch_list=[cost, acc])
69             data_size = len(data)
70             total_acc += data_size * avg_acc_np
71             total_cost += data_size * avg_cost_np
72             data_count += data_size
73         avg_cost = total_cost / data_count
74 
75         avg_acc = total_acc / data_count
76         print("pass_id: %d, avg_acc: %f, avg_cost: %f, pass_time_cost: %f" %
77               (pass_id, avg_acc, avg_cost, time.time() - pass_start))
78 
79         epoch_model = save_dirname + "/" + "epoch" + str(pass_id)
80         fluid.io.save_inference_model(epoch_model, ["words", "label"], acc, exe)
81 
82         pass_end = time.time()
83         # For internal continuous evaluation
84         if "CE_MODE_X" in os.environ:
85             print("kpis	train_acc	%f" % avg_acc)
86             print("kpis	train_cost	%f" % avg_cost)
87             print("kpis	train_duration	%f" % (pass_end - pass_start))
88 
89 
90 def train_net():
91     word_dict, train_reader, test_reader = utils.prepare_data(
92         "imdb", self_dict=False, batch_size=128, buf_size=50000)
93 
94     if sys.argv[1] == "bow":
95         train(
96             train_reader,
97             word_dict,
98             bow_net,
99             use_cuda=False,
100             parallel=False,
101             save_dirname="bow_model",
102             lr=0.002,
103             pass_num=30,
104             batch_size=4)
105     elif sys.argv[1] == "cnn":
106         train(
107             train_reader,
108             word_dict,
109             cnn_net,
110             use_cuda=True,
111             parallel=False,
112             save_dirname="cnn_model",
113             lr=0.01,
114             pass_num=30,
115             batch_size=4)
116     elif sys.argv[1] == "lstm":
117         train(
118             train_reader,
119             word_dict,
120             lstm_net,
121             use_cuda=True,
122             parallel=False,
123             save_dirname="lstm_model",
124             lr=0.05,
125             pass_num=30,
126             batch_size=4)
127     elif sys.argv[1] == "gru":
128         train(
129             train_reader,
130             word_dict,
131             lstm_net,
132             use_cuda=True,
133             parallel=False,
134             save_dirname="gru_model",
135             lr=0.05,
136             pass_num=30,
137             batch_size=4)
138     else:
139         print("network name cannot be found!")
140         sys.exit(1)
141 
142 
143 if __name__ == "__main__":
144     train_net()
145
```

Path: `fluid/PaddleNLP/text_classification/nets.py`
Content:
```
1 import sys
2 import time
3 import numpy as np
4 
5 import paddle
6 import paddle.fluid as fluid
7 
8 
9 def bow_net(data,
10             label,
11             dict_dim,
12             emb_dim=128,
13             hid_dim=128,
14             hid_dim2=96,
15             class_dim=2):
16     """
17     bow net
18     """
19     emb = fluid.layers.embedding(input=data, size=[dict_dim, emb_dim])
20     bow = fluid.layers.sequence_pool(input=emb, pool_type='sum')
21     bow_tanh = fluid.layers.tanh(bow)
22     fc_1 = fluid.layers.fc(input=bow_tanh, size=hid_dim, act="tanh")
23     fc_2 = fluid.layers.fc(input=fc_1, size=hid_dim2, act="tanh")
24     prediction = fluid.layers.fc(input=[fc_2], size=class_dim, act="softmax")
25     cost = fluid.layers.cross_entropy(input=prediction, label=label)
26     avg_cost = fluid.layers.mean(x=cost)
27     acc = fluid.layers.accuracy(input=prediction, label=label)
28 
29     return avg_cost, acc, prediction
30 
31 
32 def cnn_net(data,
33             label,
34             dict_dim,
35             emb_dim=128,
36             hid_dim=128,
37             hid_dim2=96,
38             class_dim=2,
39             win_size=3):
40     """
41     conv net
42     """
43     emb = fluid.layers.embedding(input=data, size=[dict_dim, emb_dim])
44 
45     conv_3 = fluid.nets.sequence_conv_pool(
46         input=emb,
47         num_filters=hid_dim,
48         filter_size=win_size,
49         act="tanh",
50         pool_type="max")
51 
52     fc_1 = fluid.layers.fc(input=[conv_3], size=hid_dim2)
53 
54     prediction = fluid.layers.fc(input=[fc_1], size=class_dim, act="softmax")
55     cost = fluid.layers.cross_entropy(input=prediction, label=label)
56     avg_cost = fluid.layers.mean(x=cost)
57     acc = fluid.layers.accuracy(input=prediction, label=label)
58 
59     return avg_cost, acc, prediction
60 
61 
62 def lstm_net(data,
63              label,
64              dict_dim,
65              emb_dim=128,
66              hid_dim=128,
67              hid_dim2=96,
68              class_dim=2,
69              emb_lr=30.0):
70     """
71     lstm net
72     """
73     emb = fluid.layers.embedding(
74         input=data,
75         size=[dict_dim, emb_dim],
76         param_attr=fluid.ParamAttr(learning_rate=emb_lr))
77 
78     fc0 = fluid.layers.fc(input=emb, size=hid_dim * 4)
79 
80     lstm_h, c = fluid.layers.dynamic_lstm(
81         input=fc0, size=hid_dim * 4, is_reverse=False)
82 
83     lstm_max = fluid.layers.sequence_pool(input=lstm_h, pool_type='max')
84     lstm_max_tanh = fluid.layers.tanh(lstm_max)
85 
86     fc1 = fluid.layers.fc(input=lstm_max_tanh, size=hid_dim2, act='tanh')
87 
88     prediction = fluid.layers.fc(input=fc1, size=class_dim, act='softmax')
89 
90     cost = fluid.layers.cross_entropy(input=prediction, label=label)
91     avg_cost = fluid.layers.mean(x=cost)
92     acc = fluid.layers.accuracy(input=prediction, label=label)
93 
94     return avg_cost, acc, prediction
95 
96 
97 def gru_net(data,
98             label,
99             dict_dim,
100             emb_dim=128,
101             hid_dim=128,
102             hid_dim2=96,
103             class_dim=2,
104             emb_lr=400.0):
105     """
106     gru net
107     """
108     emb = fluid.layers.embedding(
109         input=data,
110         size=[dict_dim, emb_dim],
111         param_attr=fluid.ParamAttr(learning_rate=emb_lr))
112 
113     fc0 = fluid.layers.fc(input=emb, size=hid_dim * 3)
114     gru_h = fluid.layers.dynamic_gru(input=fc0, size=hid_dim, is_reverse=False)
115     gru_max = fluid.layers.sequence_pool(input=gru_h, pool_type='max')
116     gru_max_tanh = fluid.layers.tanh(gru_max)
117     fc1 = fluid.layers.fc(input=gru_max_tanh, size=hid_dim2, act='tanh')
118     prediction = fluid.layers.fc(input=fc1, size=class_dim, act='softmax')
119 
120     cost = fluid.layers.cross_entropy(input=prediction, label=label)
121     avg_cost = fluid.layers.mean(x=cost)
122     acc = fluid.layers.accuracy(input=prediction, label=label)
123 
124     return avg_cost, acc, prediction
125
```
--- 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/text_classification/nets.py b/fluid/PaddleNLP/text_classification/nets.py
--- a/fluid/PaddleNLP/text_classification/nets.py
+++ b/fluid/PaddleNLP/text_classification/nets.py
@@ -101,7 +101,7 @@
             hid_dim=128,
             hid_dim2=96,
             class_dim=2,
-            emb_lr=400.0):
+            emb_lr=30.0):
     """
     gru net
     """
diff --git a/fluid/PaddleNLP/text_classification/train.py b/fluid/PaddleNLP/text_classification/train.py
--- a/fluid/PaddleNLP/text_classification/train.py
+++ b/fluid/PaddleNLP/text_classification/train.py
@@ -22,7 +22,6 @@
           parallel,
           save_dirname,
           lr=0.2,
-          batch_size=128,
           pass_num=30):
     """
     train network
@@ -100,8 +99,7 @@
             parallel=False,
             save_dirname="bow_model",
             lr=0.002,
-            pass_num=30,
-            batch_size=4)
+            pass_num=30)
     elif sys.argv[1] == "cnn":
         train(
             train_reader,
@@ -111,8 +109,7 @@
             parallel=False,
             save_dirname="cnn_model",
             lr=0.01,
-            pass_num=30,
-            batch_size=4)
+            pass_num=30)
     elif sys.argv[1] == "lstm":
         train(
             train_reader,
@@ -122,19 +119,17 @@
             parallel=False,
             save_dirname="lstm_model",
             lr=0.05,
-            pass_num=30,
-            batch_size=4)
+            pass_num=30)
     elif sys.argv[1] == "gru":
         train(
             train_reader,
             word_dict,
-            lstm_net,
+            gru_net,
             use_cuda=True,
             parallel=False,
             save_dirname="gru_model",
             lr=0.05,
-            pass_num=30,
-            batch_size=4)
+            pass_num=30)
     else:
         print("network name cannot be found!")
         sys.exit(1)

{"golden_diff": "diff --git a/fluid/PaddleNLP/text_classification/nets.py b/fluid/PaddleNLP/text_classification/nets.py\n--- a/fluid/PaddleNLP/text_classification/nets.py\n+++ b/fluid/PaddleNLP/text_classification/nets.py\n@@ -101,7 +101,7 @@\n             hid_dim=128,\n             hid_dim2=96,\n             class_dim=2,\n-            emb_lr=400.0):\n+            emb_lr=30.0):\n     \"\"\"\n     gru net\n     \"\"\"\ndiff --git a/fluid/PaddleNLP/text_classification/train.py b/fluid/PaddleNLP/text_classification/train.py\n--- a/fluid/PaddleNLP/text_classification/train.py\n+++ b/fluid/PaddleNLP/text_classification/train.py\n@@ -22,7 +22,6 @@\n           parallel,\n           save_dirname,\n           lr=0.2,\n-          batch_size=128,\n           pass_num=30):\n     \"\"\"\n     train network\n@@ -100,8 +99,7 @@\n             parallel=False,\n             save_dirname=\"bow_model\",\n             lr=0.002,\n-            pass_num=30,\n-            batch_size=4)\n+            pass_num=30)\n     elif sys.argv[1] == \"cnn\":\n         train(\n             train_reader,\n@@ -111,8 +109,7 @@\n             parallel=False,\n             save_dirname=\"cnn_model\",\n             lr=0.01,\n-            pass_num=30,\n-            batch_size=4)\n+            pass_num=30)\n     elif sys.argv[1] == \"lstm\":\n         train(\n             train_reader,\n@@ -122,19 +119,17 @@\n             parallel=False,\n             save_dirname=\"lstm_model\",\n             lr=0.05,\n-            pass_num=30,\n-            batch_size=4)\n+            pass_num=30)\n     elif sys.argv[1] == \"gru\":\n         train(\n             train_reader,\n             word_dict,\n-            lstm_net,\n+            gru_net,\n             use_cuda=True,\n             parallel=False,\n             save_dirname=\"gru_model\",\n             lr=0.05,\n-            pass_num=30,\n-            batch_size=4)\n+            pass_num=30)\n     else:\n         print(\"network name cannot be found!\")\n         sys.exit(1)\n", "issue": "\u6587\u672c\u5206\u7c7btext_classification\u8bad\u7ec3\u9009\u9879\u4e0d\u5339\u914d\n1. train.py\u4e2dbatch_size\u53c2\u6570\u5197\u4f59\r\n\r\n2. \u547d\u4ee4\u884c\u9009\u9879gru\u914d\u7f6e\u9519\u8bef\r\n\r\n3. GRU Layer embedding\u5b66\u4e60\u7387\u9ed8\u8ba4\u914d\u7f6e\u8fc7\u5927\uff0c\u5bfc\u81f4\u6a21\u578b\u8bad\u7ec3\u8865\u8bfe\u6536\u655b\u3002\n", "before_files": [{"content": "import os\nimport six\nimport sys\nimport time\nimport unittest\nimport contextlib\n\nimport paddle\nimport paddle.fluid as fluid\n\nimport utils\nfrom nets import bow_net\nfrom nets import cnn_net\nfrom nets import lstm_net\nfrom nets import gru_net\n\n\ndef train(train_reader,\n          word_dict,\n          network,\n          use_cuda,\n          parallel,\n          save_dirname,\n          lr=0.2,\n          batch_size=128,\n          pass_num=30):\n    \"\"\"\n    train network\n    \"\"\"\n    data = fluid.layers.data(\n        name=\"words\", shape=[1], dtype=\"int64\", lod_level=1)\n\n    label = fluid.layers.data(name=\"label\", shape=[1], dtype=\"int64\")\n\n    if not parallel:\n        cost, acc, prediction = network(data, label, len(word_dict))\n    else:\n        places = fluid.layers.device.get_places(device_count=2)\n        pd = fluid.layers.ParallelDo(places)\n        with pd.do():\n            cost, acc, prediction = network(\n                pd.read_input(data), pd.read_input(label), len(word_dict))\n\n            pd.write_output(cost)\n            pd.write_output(acc)\n\n        cost, acc = pd()\n        cost = fluid.layers.mean(cost)\n        acc = fluid.layers.mean(acc)\n\n    sgd_optimizer = fluid.optimizer.Adagrad(learning_rate=lr)\n    sgd_optimizer.minimize(cost)\n\n    place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()\n    exe = fluid.Executor(place)\n    feeder = fluid.DataFeeder(feed_list=[data, label], place=place)\n\n    # For internal continuous evaluation\n    if \"CE_MODE_X\" in os.environ:\n        fluid.default_startup_program().random_seed = 110\n    exe.run(fluid.default_startup_program())\n    for pass_id in six.moves.xrange(pass_num):\n        pass_start = time.time()\n        data_size, data_count, total_acc, total_cost = 0, 0, 0.0, 0.0\n        for data in train_reader():\n            avg_cost_np, avg_acc_np = exe.run(fluid.default_main_program(),\n                                              feed=feeder.feed(data),\n                                              fetch_list=[cost, acc])\n            data_size = len(data)\n            total_acc += data_size * avg_acc_np\n            total_cost += data_size * avg_cost_np\n            data_count += data_size\n        avg_cost = total_cost / data_count\n\n        avg_acc = total_acc / data_count\n        print(\"pass_id: %d, avg_acc: %f, avg_cost: %f, pass_time_cost: %f\" %\n              (pass_id, avg_acc, avg_cost, time.time() - pass_start))\n\n        epoch_model = save_dirname + \"/\" + \"epoch\" + str(pass_id)\n        fluid.io.save_inference_model(epoch_model, [\"words\", \"label\"], acc, exe)\n\n        pass_end = time.time()\n        # For internal continuous evaluation\n        if \"CE_MODE_X\" in os.environ:\n            print(\"kpis\ttrain_acc\t%f\" % avg_acc)\n            print(\"kpis\ttrain_cost\t%f\" % avg_cost)\n            print(\"kpis\ttrain_duration\t%f\" % (pass_end - pass_start))\n\n\ndef train_net():\n    word_dict, train_reader, test_reader = utils.prepare_data(\n        \"imdb\", self_dict=False, batch_size=128, buf_size=50000)\n\n    if sys.argv[1] == \"bow\":\n        train(\n            train_reader,\n            word_dict,\n            bow_net,\n            use_cuda=False,\n            parallel=False,\n            save_dirname=\"bow_model\",\n            lr=0.002,\n            pass_num=30,\n            batch_size=4)\n    elif sys.argv[1] == \"cnn\":\n        train(\n            train_reader,\n            word_dict,\n            cnn_net,\n            use_cuda=True,\n            parallel=False,\n            save_dirname=\"cnn_model\",\n            lr=0.01,\n            pass_num=30,\n            batch_size=4)\n    elif sys.argv[1] == \"lstm\":\n        train(\n            train_reader,\n            word_dict,\n            lstm_net,\n            use_cuda=True,\n            parallel=False,\n            save_dirname=\"lstm_model\",\n            lr=0.05,\n            pass_num=30,\n            batch_size=4)\n    elif sys.argv[1] == \"gru\":\n        train(\n            train_reader,\n            word_dict,\n            lstm_net,\n            use_cuda=True,\n            parallel=False,\n            save_dirname=\"gru_model\",\n            lr=0.05,\n            pass_num=30,\n            batch_size=4)\n    else:\n        print(\"network name cannot be found!\")\n        sys.exit(1)\n\n\nif __name__ == \"__main__\":\n    train_net()\n", "path": "fluid/PaddleNLP/text_classification/train.py"}, {"content": "import sys\nimport time\nimport numpy as np\n\nimport paddle\nimport paddle.fluid as fluid\n\n\ndef bow_net(data,\n            label,\n            dict_dim,\n            emb_dim=128,\n            hid_dim=128,\n            hid_dim2=96,\n            class_dim=2):\n    \"\"\"\n    bow net\n    \"\"\"\n    emb = fluid.layers.embedding(input=data, size=[dict_dim, emb_dim])\n    bow = fluid.layers.sequence_pool(input=emb, pool_type='sum')\n    bow_tanh = fluid.layers.tanh(bow)\n    fc_1 = fluid.layers.fc(input=bow_tanh, size=hid_dim, act=\"tanh\")\n    fc_2 = fluid.layers.fc(input=fc_1, size=hid_dim2, act=\"tanh\")\n    prediction = fluid.layers.fc(input=[fc_2], size=class_dim, act=\"softmax\")\n    cost = fluid.layers.cross_entropy(input=prediction, label=label)\n    avg_cost = fluid.layers.mean(x=cost)\n    acc = fluid.layers.accuracy(input=prediction, label=label)\n\n    return avg_cost, acc, prediction\n\n\ndef cnn_net(data,\n            label,\n            dict_dim,\n            emb_dim=128,\n            hid_dim=128,\n            hid_dim2=96,\n            class_dim=2,\n            win_size=3):\n    \"\"\"\n    conv net\n    \"\"\"\n    emb = fluid.layers.embedding(input=data, size=[dict_dim, emb_dim])\n\n    conv_3 = fluid.nets.sequence_conv_pool(\n        input=emb,\n        num_filters=hid_dim,\n        filter_size=win_size,\n        act=\"tanh\",\n        pool_type=\"max\")\n\n    fc_1 = fluid.layers.fc(input=[conv_3], size=hid_dim2)\n\n    prediction = fluid.layers.fc(input=[fc_1], size=class_dim, act=\"softmax\")\n    cost = fluid.layers.cross_entropy(input=prediction, label=label)\n    avg_cost = fluid.layers.mean(x=cost)\n    acc = fluid.layers.accuracy(input=prediction, label=label)\n\n    return avg_cost, acc, prediction\n\n\ndef lstm_net(data,\n             label,\n             dict_dim,\n             emb_dim=128,\n             hid_dim=128,\n             hid_dim2=96,\n             class_dim=2,\n             emb_lr=30.0):\n    \"\"\"\n    lstm net\n    \"\"\"\n    emb = fluid.layers.embedding(\n        input=data,\n        size=[dict_dim, emb_dim],\n        param_attr=fluid.ParamAttr(learning_rate=emb_lr))\n\n    fc0 = fluid.layers.fc(input=emb, size=hid_dim * 4)\n\n    lstm_h, c = fluid.layers.dynamic_lstm(\n        input=fc0, size=hid_dim * 4, is_reverse=False)\n\n    lstm_max = fluid.layers.sequence_pool(input=lstm_h, pool_type='max')\n    lstm_max_tanh = fluid.layers.tanh(lstm_max)\n\n    fc1 = fluid.layers.fc(input=lstm_max_tanh, size=hid_dim2, act='tanh')\n\n    prediction = fluid.layers.fc(input=fc1, size=class_dim, act='softmax')\n\n    cost = fluid.layers.cross_entropy(input=prediction, label=label)\n    avg_cost = fluid.layers.mean(x=cost)\n    acc = fluid.layers.accuracy(input=prediction, label=label)\n\n    return avg_cost, acc, prediction\n\n\ndef gru_net(data,\n            label,\n            dict_dim,\n            emb_dim=128,\n            hid_dim=128,\n            hid_dim2=96,\n            class_dim=2,\n            emb_lr=400.0):\n    \"\"\"\n    gru net\n    \"\"\"\n    emb = fluid.layers.embedding(\n        input=data,\n        size=[dict_dim, emb_dim],\n        param_attr=fluid.ParamAttr(learning_rate=emb_lr))\n\n    fc0 = fluid.layers.fc(input=emb, size=hid_dim * 3)\n    gru_h = fluid.layers.dynamic_gru(input=fc0, size=hid_dim, is_reverse=False)\n    gru_max = fluid.layers.sequence_pool(input=gru_h, pool_type='max')\n    gru_max_tanh = fluid.layers.tanh(gru_max)\n    fc1 = fluid.layers.fc(input=gru_max_tanh, size=hid_dim2, act='tanh')\n    prediction = fluid.layers.fc(input=fc1, size=class_dim, act='softmax')\n\n    cost = fluid.layers.cross_entropy(input=prediction, label=label)\n    avg_cost = fluid.layers.mean(x=cost)\n    acc = fluid.layers.accuracy(input=prediction, label=label)\n\n    return avg_cost, acc, prediction\n", "path": "fluid/PaddleNLP/text_classification/nets.py"}], "after_files": [{"content": "import os\nimport six\nimport sys\nimport time\nimport unittest\nimport contextlib\n\nimport paddle\nimport paddle.fluid as fluid\n\nimport utils\nfrom nets import bow_net\nfrom nets import cnn_net\nfrom nets import lstm_net\nfrom nets import gru_net\n\n\ndef train(train_reader,\n          word_dict,\n          network,\n          use_cuda,\n          parallel,\n          save_dirname,\n          lr=0.2,\n          pass_num=30):\n    \"\"\"\n    train network\n    \"\"\"\n    data = fluid.layers.data(\n        name=\"words\", shape=[1], dtype=\"int64\", lod_level=1)\n\n    label = fluid.layers.data(name=\"label\", shape=[1], dtype=\"int64\")\n\n    if not parallel:\n        cost, acc, prediction = network(data, label, len(word_dict))\n    else:\n        places = fluid.layers.device.get_places(device_count=2)\n        pd = fluid.layers.ParallelDo(places)\n        with pd.do():\n            cost, acc, prediction = network(\n                pd.read_input(data), pd.read_input(label), len(word_dict))\n\n            pd.write_output(cost)\n            pd.write_output(acc)\n\n        cost, acc = pd()\n        cost = fluid.layers.mean(cost)\n        acc = fluid.layers.mean(acc)\n\n    sgd_optimizer = fluid.optimizer.Adagrad(learning_rate=lr)\n    sgd_optimizer.minimize(cost)\n\n    place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()\n    exe = fluid.Executor(place)\n    feeder = fluid.DataFeeder(feed_list=[data, label], place=place)\n\n    # For internal continuous evaluation\n    if \"CE_MODE_X\" in os.environ:\n        fluid.default_startup_program().random_seed = 110\n    exe.run(fluid.default_startup_program())\n    for pass_id in six.moves.xrange(pass_num):\n        pass_start = time.time()\n        data_size, data_count, total_acc, total_cost = 0, 0, 0.0, 0.0\n        for data in train_reader():\n            avg_cost_np, avg_acc_np = exe.run(fluid.default_main_program(),\n                                              feed=feeder.feed(data),\n                                              fetch_list=[cost, acc])\n            data_size = len(data)\n            total_acc += data_size * avg_acc_np\n            total_cost += data_size * avg_cost_np\n            data_count += data_size\n        avg_cost = total_cost / data_count\n\n        avg_acc = total_acc / data_count\n        print(\"pass_id: %d, avg_acc: %f, avg_cost: %f, pass_time_cost: %f\" %\n              (pass_id, avg_acc, avg_cost, time.time() - pass_start))\n\n        epoch_model = save_dirname + \"/\" + \"epoch\" + str(pass_id)\n        fluid.io.save_inference_model(epoch_model, [\"words\", \"label\"], acc, exe)\n\n        pass_end = time.time()\n        # For internal continuous evaluation\n        if \"CE_MODE_X\" in os.environ:\n            print(\"kpis\ttrain_acc\t%f\" % avg_acc)\n            print(\"kpis\ttrain_cost\t%f\" % avg_cost)\n            print(\"kpis\ttrain_duration\t%f\" % (pass_end - pass_start))\n\n\ndef train_net():\n    word_dict, train_reader, test_reader = utils.prepare_data(\n        \"imdb\", self_dict=False, batch_size=128, buf_size=50000)\n\n    if sys.argv[1] == \"bow\":\n        train(\n            train_reader,\n            word_dict,\n            bow_net,\n            use_cuda=False,\n            parallel=False,\n            save_dirname=\"bow_model\",\n            lr=0.002,\n            pass_num=30)\n    elif sys.argv[1] == \"cnn\":\n        train(\n            train_reader,\n            word_dict,\n            cnn_net,\n            use_cuda=True,\n            parallel=False,\n            save_dirname=\"cnn_model\",\n            lr=0.01,\n            pass_num=30)\n    elif sys.argv[1] == \"lstm\":\n        train(\n            train_reader,\n            word_dict,\n            lstm_net,\n            use_cuda=True,\n            parallel=False,\n            save_dirname=\"lstm_model\",\n            lr=0.05,\n            pass_num=30)\n    elif sys.argv[1] == \"gru\":\n        train(\n            train_reader,\n            word_dict,\n            gru_net,\n            use_cuda=True,\n            parallel=False,\n            save_dirname=\"gru_model\",\n            lr=0.05,\n            pass_num=30)\n    else:\n        print(\"network name cannot be found!\")\n        sys.exit(1)\n\n\nif __name__ == \"__main__\":\n    train_net()\n", "path": "fluid/PaddleNLP/text_classification/train.py"}, {"content": "import sys\nimport time\nimport numpy as np\n\nimport paddle\nimport paddle.fluid as fluid\n\n\ndef bow_net(data,\n            label,\n            dict_dim,\n            emb_dim=128,\n            hid_dim=128,\n            hid_dim2=96,\n            class_dim=2):\n    \"\"\"\n    bow net\n    \"\"\"\n    emb = fluid.layers.embedding(input=data, size=[dict_dim, emb_dim])\n    bow = fluid.layers.sequence_pool(input=emb, pool_type='sum')\n    bow_tanh = fluid.layers.tanh(bow)\n    fc_1 = fluid.layers.fc(input=bow_tanh, size=hid_dim, act=\"tanh\")\n    fc_2 = fluid.layers.fc(input=fc_1, size=hid_dim2, act=\"tanh\")\n    prediction = fluid.layers.fc(input=[fc_2], size=class_dim, act=\"softmax\")\n    cost = fluid.layers.cross_entropy(input=prediction, label=label)\n    avg_cost = fluid.layers.mean(x=cost)\n    acc = fluid.layers.accuracy(input=prediction, label=label)\n\n    return avg_cost, acc, prediction\n\n\ndef cnn_net(data,\n            label,\n            dict_dim,\n            emb_dim=128,\n            hid_dim=128,\n            hid_dim2=96,\n            class_dim=2,\n            win_size=3):\n    \"\"\"\n    conv net\n    \"\"\"\n    emb = fluid.layers.embedding(input=data, size=[dict_dim, emb_dim])\n\n    conv_3 = fluid.nets.sequence_conv_pool(\n        input=emb,\n        num_filters=hid_dim,\n        filter_size=win_size,\n        act=\"tanh\",\n        pool_type=\"max\")\n\n    fc_1 = fluid.layers.fc(input=[conv_3], size=hid_dim2)\n\n    prediction = fluid.layers.fc(input=[fc_1], size=class_dim, act=\"softmax\")\n    cost = fluid.layers.cross_entropy(input=prediction, label=label)\n    avg_cost = fluid.layers.mean(x=cost)\n    acc = fluid.layers.accuracy(input=prediction, label=label)\n\n    return avg_cost, acc, prediction\n\n\ndef lstm_net(data,\n             label,\n             dict_dim,\n             emb_dim=128,\n             hid_dim=128,\n             hid_dim2=96,\n             class_dim=2,\n             emb_lr=30.0):\n    \"\"\"\n    lstm net\n    \"\"\"\n    emb = fluid.layers.embedding(\n        input=data,\n        size=[dict_dim, emb_dim],\n        param_attr=fluid.ParamAttr(learning_rate=emb_lr))\n\n    fc0 = fluid.layers.fc(input=emb, size=hid_dim * 4)\n\n    lstm_h, c = fluid.layers.dynamic_lstm(\n        input=fc0, size=hid_dim * 4, is_reverse=False)\n\n    lstm_max = fluid.layers.sequence_pool(input=lstm_h, pool_type='max')\n    lstm_max_tanh = fluid.layers.tanh(lstm_max)\n\n    fc1 = fluid.layers.fc(input=lstm_max_tanh, size=hid_dim2, act='tanh')\n\n    prediction = fluid.layers.fc(input=fc1, size=class_dim, act='softmax')\n\n    cost = fluid.layers.cross_entropy(input=prediction, label=label)\n    avg_cost = fluid.layers.mean(x=cost)\n    acc = fluid.layers.accuracy(input=prediction, label=label)\n\n    return avg_cost, acc, prediction\n\n\ndef gru_net(data,\n            label,\n            dict_dim,\n            emb_dim=128,\n            hid_dim=128,\n            hid_dim2=96,\n            class_dim=2,\n            emb_lr=30.0):\n    \"\"\"\n    gru net\n    \"\"\"\n    emb = fluid.layers.embedding(\n        input=data,\n        size=[dict_dim, emb_dim],\n        param_attr=fluid.ParamAttr(learning_rate=emb_lr))\n\n    fc0 = fluid.layers.fc(input=emb, size=hid_dim * 3)\n    gru_h = fluid.layers.dynamic_gru(input=fc0, size=hid_dim, is_reverse=False)\n    gru_max = fluid.layers.sequence_pool(input=gru_h, pool_type='max')\n    gru_max_tanh = fluid.layers.tanh(gru_max)\n    fc1 = fluid.layers.fc(input=gru_max_tanh, size=hid_dim2, act='tanh')\n    prediction = fluid.layers.fc(input=fc1, size=class_dim, act='softmax')\n\n    cost = fluid.layers.cross_entropy(input=prediction, label=label)\n    avg_cost = fluid.layers.mean(x=cost)\n    acc = fluid.layers.accuracy(input=prediction, label=label)\n\n    return avg_cost, acc, prediction\n", "path": "fluid/PaddleNLP/text_classification/nets.py"}]}