prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Modify inference script
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `hsigmoid/infer.py`
  Content:
  ```
  1 #!/usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 import os
  4 import logging
  5 import gzip
  6 
  7 import paddle.v2 as paddle
  8 from network_conf import ngram_lm
  9 
  10 logger = logging.getLogger("paddle")
  11 logger.setLevel(logging.WARNING)
  12 
  13 
  14 def decode_res(infer_res, dict_size):
  15     """
  16     Inferring probabilities are orginized as a complete binary tree.
  17     The actual labels are leaves (indices are counted from class number).
  18     This function travels paths decoded from inferring results.
  19     If the probability >0.5 then go to right child, otherwise go to left child.
  20 
  21     param infer_res: inferring result
  22     param dict_size: class number
  23     return predict_lbls: actual class
  24     """
  25     predict_lbls = []
  26     infer_res = infer_res > 0.5
  27     for i, probs in enumerate(infer_res):
  28         idx = 0
  29         result = 1
  30         while idx < len(probs):
  31             result <<= 1
  32             if probs[idx]:
  33                 result |= 1
  34             if probs[idx]:
  35                 idx = idx * 2 + 2  # right child
  36             else:
  37                 idx = idx * 2 + 1  # left child
  38 
  39         predict_lbl = result - dict_size
  40         predict_lbls.append(predict_lbl)
  41     return predict_lbls
  42 
  43 
  44 def predict(batch_ins, idx_word_dict, dict_size, prediction_layer, parameters):
  45     infer_res = paddle.infer(
  46         output_layer=prediction_layer, parameters=parameters, input=batch_ins)
  47 
  48     predict_lbls = decode_res(infer_res, dict_size)
  49     predict_words = [idx_word_dict[lbl] for lbl in predict_lbls]  # map to word
  50 
  51     # Ouput format: word1 word2 word3 word4 -> predict label
  52     for i, ins in enumerate(batch_ins):
  53         print(" ".join([idx_word_dict[w]
  54                         for w in ins]) + " -> " + predict_words[i])
  55 
  56 
  57 def main(model_path):
  58     assert os.path.exists(model_path), "trained model does not exist."
  59 
  60     paddle.init(use_gpu=False, trainer_count=1)
  61     word_dict = paddle.dataset.imikolov.build_dict(min_word_freq=2)
  62     dict_size = len(word_dict)
  63     prediction_layer = ngram_lm(
  64         is_train=False, hidden_size=256, embed_size=32, dict_size=dict_size)
  65 
  66     with gzip.open(model_path, "r") as f:
  67         parameters = paddle.parameters.Parameters.from_tar(f)
  68 
  69     idx_word_dict = dict((v, k) for k, v in word_dict.items())
  70     batch_size = 64
  71     batch_ins = []
  72     ins_iter = paddle.dataset.imikolov.test(word_dict, 5)
  73 
  74     for ins in ins_iter():
  75         batch_ins.append(ins[:-1])
  76         if len(batch_ins) == batch_size:
  77             predict(batch_ins, idx_word_dict, dict_size, prediction_layer,
  78                     parameters)
  79             batch_ins = []
  80 
  81     if len(batch_ins) > 0:
  82         predict(batch_ins, idx_word_dict, dict_size, prediction_layer,
  83                 parameters)
  84 
  85 
  86 if __name__ == "__main__":
  87     main("models/hsigmoid_batch_00010.tar.gz")
  88
  ```
  --- 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()
  
  ```
  
golden_diff: diff --git a/hsigmoid/infer.py b/hsigmoid/infer.py
  --- a/hsigmoid/infer.py
  +++ b/hsigmoid/infer.py
  @@ -41,9 +41,8 @@
       return predict_lbls
   
   
  -def predict(batch_ins, idx_word_dict, dict_size, prediction_layer, parameters):
  -    infer_res = paddle.infer(
  -        output_layer=prediction_layer, parameters=parameters, input=batch_ins)
  +def predict(batch_ins, idx_word_dict, dict_size, inferer):
  +    infer_res = inferer.infer(input=batch_ins)
   
       predict_lbls = decode_res(infer_res, dict_size)
       predict_words = [idx_word_dict[lbl] for lbl in predict_lbls]  # map to word
  @@ -66,6 +65,8 @@
       with gzip.open(model_path, "r") as f:
           parameters = paddle.parameters.Parameters.from_tar(f)
   
  +    inferer = paddle.inference.Inference(
  +        output_layer=prediction_layer, parameters=parameters)
       idx_word_dict = dict((v, k) for k, v in word_dict.items())
       batch_size = 64
       batch_ins = []
  @@ -74,13 +75,11 @@
       for ins in ins_iter():
           batch_ins.append(ins[:-1])
           if len(batch_ins) == batch_size:
  -            predict(batch_ins, idx_word_dict, dict_size, prediction_layer,
  -                    parameters)
  +            predict(batch_ins, idx_word_dict, dict_size, inferer)
               batch_ins = []
   
       if len(batch_ins) > 0:
  -        predict(batch_ins, idx_word_dict, dict_size, prediction_layer,
  -                parameters)
  +        predict(batch_ins, idx_word_dict, dict_size, inferer)
   
   
   if __name__ == "__main__":
  
verification_info: {"golden_diff": "diff --git a/hsigmoid/infer.py b/hsigmoid/infer.py\n--- a/hsigmoid/infer.py\n+++ b/hsigmoid/infer.py\n@@ -41,9 +41,8 @@\n     return predict_lbls\n \n \n-def predict(batch_ins, idx_word_dict, dict_size, prediction_layer, parameters):\n-    infer_res = paddle.infer(\n-        output_layer=prediction_layer, parameters=parameters, input=batch_ins)\n+def predict(batch_ins, idx_word_dict, dict_size, inferer):\n+    infer_res = inferer.infer(input=batch_ins)\n \n     predict_lbls = decode_res(infer_res, dict_size)\n     predict_words = [idx_word_dict[lbl] for lbl in predict_lbls]  # map to word\n@@ -66,6 +65,8 @@\n     with gzip.open(model_path, \"r\") as f:\n         parameters = paddle.parameters.Parameters.from_tar(f)\n \n+    inferer = paddle.inference.Inference(\n+        output_layer=prediction_layer, parameters=parameters)\n     idx_word_dict = dict((v, k) for k, v in word_dict.items())\n     batch_size = 64\n     batch_ins = []\n@@ -74,13 +75,11 @@\n     for ins in ins_iter():\n         batch_ins.append(ins[:-1])\n         if len(batch_ins) == batch_size:\n-            predict(batch_ins, idx_word_dict, dict_size, prediction_layer,\n-                    parameters)\n+            predict(batch_ins, idx_word_dict, dict_size, inferer)\n             batch_ins = []\n \n     if len(batch_ins) > 0:\n-        predict(batch_ins, idx_word_dict, dict_size, prediction_layer,\n-                parameters)\n+        predict(batch_ins, idx_word_dict, dict_size, inferer)\n \n \n if __name__ == \"__main__\":\n", "issue": "Modify inference script\n\n", "before_files": [{"content": "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\nimport os\nimport logging\nimport gzip\n\nimport paddle.v2 as paddle\nfrom network_conf import ngram_lm\n\nlogger = logging.getLogger(\"paddle\")\nlogger.setLevel(logging.WARNING)\n\n\ndef decode_res(infer_res, dict_size):\n    \"\"\"\n    Inferring probabilities are orginized as a complete binary tree.\n    The actual labels are leaves (indices are counted from class number).\n    This function travels paths decoded from inferring results.\n    If the probability >0.5 then go to right child, otherwise go to left child.\n\n    param infer_res: inferring result\n    param dict_size: class number\n    return predict_lbls: actual class\n    \"\"\"\n    predict_lbls = []\n    infer_res = infer_res > 0.5\n    for i, probs in enumerate(infer_res):\n        idx = 0\n        result = 1\n        while idx < len(probs):\n            result <<= 1\n            if probs[idx]:\n                result |= 1\n            if probs[idx]:\n                idx = idx * 2 + 2  # right child\n            else:\n                idx = idx * 2 + 1  # left child\n\n        predict_lbl = result - dict_size\n        predict_lbls.append(predict_lbl)\n    return predict_lbls\n\n\ndef predict(batch_ins, idx_word_dict, dict_size, prediction_layer, parameters):\n    infer_res = paddle.infer(\n        output_layer=prediction_layer, parameters=parameters, input=batch_ins)\n\n    predict_lbls = decode_res(infer_res, dict_size)\n    predict_words = [idx_word_dict[lbl] for lbl in predict_lbls]  # map to word\n\n    # Ouput format: word1 word2 word3 word4 -> predict label\n    for i, ins in enumerate(batch_ins):\n        print(\" \".join([idx_word_dict[w]\n                        for w in ins]) + \" -> \" + predict_words[i])\n\n\ndef main(model_path):\n    assert os.path.exists(model_path), \"trained model does not exist.\"\n\n    paddle.init(use_gpu=False, trainer_count=1)\n    word_dict = paddle.dataset.imikolov.build_dict(min_word_freq=2)\n    dict_size = len(word_dict)\n    prediction_layer = ngram_lm(\n        is_train=False, hidden_size=256, embed_size=32, dict_size=dict_size)\n\n    with gzip.open(model_path, \"r\") as f:\n        parameters = paddle.parameters.Parameters.from_tar(f)\n\n    idx_word_dict = dict((v, k) for k, v in word_dict.items())\n    batch_size = 64\n    batch_ins = []\n    ins_iter = paddle.dataset.imikolov.test(word_dict, 5)\n\n    for ins in ins_iter():\n        batch_ins.append(ins[:-1])\n        if len(batch_ins) == batch_size:\n            predict(batch_ins, idx_word_dict, dict_size, prediction_layer,\n                    parameters)\n            batch_ins = []\n\n    if len(batch_ins) > 0:\n        predict(batch_ins, idx_word_dict, dict_size, prediction_layer,\n                parameters)\n\n\nif __name__ == \"__main__\":\n    main(\"models/hsigmoid_batch_00010.tar.gz\")\n", "path": "hsigmoid/infer.py"}], "after_files": [{"content": "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\nimport os\nimport logging\nimport gzip\n\nimport paddle.v2 as paddle\nfrom network_conf import ngram_lm\n\nlogger = logging.getLogger(\"paddle\")\nlogger.setLevel(logging.WARNING)\n\n\ndef decode_res(infer_res, dict_size):\n    \"\"\"\n    Inferring probabilities are orginized as a complete binary tree.\n    The actual labels are leaves (indices are counted from class number).\n    This function travels paths decoded from inferring results.\n    If the probability >0.5 then go to right child, otherwise go to left child.\n\n    param infer_res: inferring result\n    param dict_size: class number\n    return predict_lbls: actual class\n    \"\"\"\n    predict_lbls = []\n    infer_res = infer_res > 0.5\n    for i, probs in enumerate(infer_res):\n        idx = 0\n        result = 1\n        while idx < len(probs):\n            result <<= 1\n            if probs[idx]:\n                result |= 1\n            if probs[idx]:\n                idx = idx * 2 + 2  # right child\n            else:\n                idx = idx * 2 + 1  # left child\n\n        predict_lbl = result - dict_size\n        predict_lbls.append(predict_lbl)\n    return predict_lbls\n\n\ndef predict(batch_ins, idx_word_dict, dict_size, inferer):\n    infer_res = inferer.infer(input=batch_ins)\n\n    predict_lbls = decode_res(infer_res, dict_size)\n    predict_words = [idx_word_dict[lbl] for lbl in predict_lbls]  # map to word\n\n    # Ouput format: word1 word2 word3 word4 -> predict label\n    for i, ins in enumerate(batch_ins):\n        print(\" \".join([idx_word_dict[w]\n                        for w in ins]) + \" -> \" + predict_words[i])\n\n\ndef main(model_path):\n    assert os.path.exists(model_path), \"trained model does not exist.\"\n\n    paddle.init(use_gpu=False, trainer_count=1)\n    word_dict = paddle.dataset.imikolov.build_dict(min_word_freq=2)\n    dict_size = len(word_dict)\n    prediction_layer = ngram_lm(\n        is_train=False, hidden_size=256, embed_size=32, dict_size=dict_size)\n\n    with gzip.open(model_path, \"r\") as f:\n        parameters = paddle.parameters.Parameters.from_tar(f)\n\n    inferer = paddle.inference.Inference(\n        output_layer=prediction_layer, parameters=parameters)\n    idx_word_dict = dict((v, k) for k, v in word_dict.items())\n    batch_size = 64\n    batch_ins = []\n    ins_iter = paddle.dataset.imikolov.test(word_dict, 5)\n\n    for ins in ins_iter():\n        batch_ins.append(ins[:-1])\n        if len(batch_ins) == batch_size:\n            predict(batch_ins, idx_word_dict, dict_size, inferer)\n            batch_ins = []\n\n    if len(batch_ins) > 0:\n        predict(batch_ins, idx_word_dict, dict_size, inferer)\n\n\nif __name__ == \"__main__\":\n    main(\"models/hsigmoid_batch_00010.tar.gz\")\n", "path": "hsigmoid/infer.py"}]}