We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Fix bugs for model_utils/model.py
Always print 0 for ValidataionCost

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `deep_speech_2/model_utils/model.py`
Content:
```
1 """Contains DeepSpeech2 model."""
2 from __future__ import absolute_import
3 from __future__ import division
4 from __future__ import print_function
5 
6 import sys
7 import os
8 import time
9 import logging
10 import gzip
11 from distutils.dir_util import mkpath
12 import paddle.v2 as paddle
13 from decoders.swig_wrapper import Scorer
14 from decoders.swig_wrapper import ctc_greedy_decoder
15 from decoders.swig_wrapper import ctc_beam_search_decoder_batch
16 from model_utils.network import deep_speech_v2_network
17 
18 logging.basicConfig(
19     format='[%(levelname)s %(asctime)s %(filename)s:%(lineno)d] %(message)s')
20 
21 
22 class DeepSpeech2Model(object):
23     """DeepSpeech2Model class.
24 
25     :param vocab_size: Decoding vocabulary size.
26     :type vocab_size: int
27     :param num_conv_layers: Number of stacking convolution layers.
28     :type num_conv_layers: int
29     :param num_rnn_layers: Number of stacking RNN layers.
30     :type num_rnn_layers: int
31     :param rnn_layer_size: RNN layer size (number of RNN cells).
32     :type rnn_layer_size: int
33     :param pretrained_model_path: Pretrained model path. If None, will train
34                                   from stratch.
35     :type pretrained_model_path: basestring|None
36     :param share_rnn_weights: Whether to share input-hidden weights between
37                               forward and backward directional RNNs.Notice that
38                               for GRU, weight sharing is not supported.
39     :type share_rnn_weights: bool
40     """
41 
42     def __init__(self, vocab_size, num_conv_layers, num_rnn_layers,
43                  rnn_layer_size, use_gru, pretrained_model_path,
44                  share_rnn_weights):
45         self._create_network(vocab_size, num_conv_layers, num_rnn_layers,
46                              rnn_layer_size, use_gru, share_rnn_weights)
47         self._create_parameters(pretrained_model_path)
48         self._inferer = None
49         self._loss_inferer = None
50         self._ext_scorer = None
51         self.logger = logging.getLogger("")
52         self.logger.setLevel(level=logging.INFO)
53 
54     def train(self,
55               train_batch_reader,
56               dev_batch_reader,
57               feeding_dict,
58               learning_rate,
59               gradient_clipping,
60               num_passes,
61               output_model_dir,
62               is_local=True,
63               num_iterations_print=100,
64               test_off=False):
65         """Train the model.
66 
67         :param train_batch_reader: Train data reader.
68         :type train_batch_reader: callable
69         :param dev_batch_reader: Validation data reader.
70         :type dev_batch_reader: callable
71         :param feeding_dict: Feeding is a map of field name and tuple index
72                              of the data that reader returns.
73         :type feeding_dict: dict|list
74         :param learning_rate: Learning rate for ADAM optimizer.
75         :type learning_rate: float
76         :param gradient_clipping: Gradient clipping threshold.
77         :type gradient_clipping: float
78         :param num_passes: Number of training epochs.
79         :type num_passes: int
80         :param num_iterations_print: Number of training iterations for printing
81                                      a training loss.
82         :type rnn_iteratons_print: int
83         :param is_local: Set to False if running with pserver with multi-nodes.
84         :type is_local: bool
85         :param output_model_dir: Directory for saving the model (every pass).
86         :type output_model_dir: basestring
87         :param test_off: Turn off testing.
88         :type test_off: bool
89         """
90         # prepare model output directory
91         if not os.path.exists(output_model_dir):
92             mkpath(output_model_dir)
93 
94         # prepare optimizer and trainer
95         optimizer = paddle.optimizer.Adam(
96             learning_rate=learning_rate,
97             gradient_clipping_threshold=gradient_clipping)
98         trainer = paddle.trainer.SGD(
99             cost=self._loss,
100             parameters=self._parameters,
101             update_equation=optimizer,
102             is_local=is_local)
103 
104         # create event handler
105         def event_handler(event):
106             global start_time, cost_sum, cost_counter
107             if isinstance(event, paddle.event.EndIteration):
108                 cost_sum += event.cost
109                 cost_counter += 1
110                 if (event.batch_id + 1) % num_iterations_print == 0:
111                     output_model_path = os.path.join(output_model_dir,
112                                                      "params.latest.tar.gz")
113                     with gzip.open(output_model_path, 'w') as f:
114                         self._parameters.to_tar(f)
115                     print("\nPass: %d, Batch: %d, TrainCost: %f" %
116                           (event.pass_id, event.batch_id + 1,
117                            cost_sum / cost_counter))
118                     cost_sum, cost_counter = 0.0, 0
119                 else:
120                     sys.stdout.write('.')
121                     sys.stdout.flush()
122             if isinstance(event, paddle.event.BeginPass):
123                 start_time = time.time()
124                 cost_sum, cost_counter = 0.0, 0
125             if isinstance(event, paddle.event.EndPass):
126                 if test_off:
127                     print("\n------- Time: %d sec,  Pass: %d" %
128                           (time.time() - start_time, event.pass_id))
129                 else:
130                     result = trainer.test(
131                         reader=dev_batch_reader, feeding=feeding_dict)
132                     print("\n------- Time: %d sec,  Pass: %d, "
133                           "ValidationCost: %s" %
134                           (time.time() - start_time, event.pass_id, 0))
135                 output_model_path = os.path.join(
136                     output_model_dir, "params.pass-%d.tar.gz" % event.pass_id)
137                 with gzip.open(output_model_path, 'w') as f:
138                     self._parameters.to_tar(f)
139 
140         # run train
141         trainer.train(
142             reader=train_batch_reader,
143             event_handler=event_handler,
144             num_passes=num_passes,
145             feeding=feeding_dict)
146 
147     def infer_loss_batch(self, infer_data):
148         """Model inference. Infer the ctc loss for a batch of speech
149         utterances.
150 
151         :param infer_data: List of utterances to infer, with each utterance a
152                            tuple of audio features and transcription text (empty
153                            string).
154         :type infer_data: list
155         :return: List of ctc loss.
156         :rtype: List of float
157         """
158         # define inferer
159         if self._loss_inferer == None:
160             self._loss_inferer = paddle.inference.Inference(
161                 output_layer=self._loss, parameters=self._parameters)
162         # run inference
163         return self._loss_inferer.infer(input=infer_data)
164 
165     def infer_batch(self, infer_data, decoding_method, beam_alpha, beam_beta,
166                     beam_size, cutoff_prob, cutoff_top_n, vocab_list,
167                     language_model_path, num_processes):
168         """Model inference. Infer the transcription for a batch of speech
169         utterances.
170 
171         :param infer_data: List of utterances to infer, with each utterance
172                            consisting of a tuple of audio features and
173                            transcription text (empty string).
174         :type infer_data: list
175         :param decoding_method: Decoding method name, 'ctc_greedy' or
176                                 'ctc_beam_search'.
177         :param decoding_method: string
178         :param beam_alpha: Parameter associated with language model.
179         :type beam_alpha: float
180         :param beam_beta: Parameter associated with word count.
181         :type beam_beta: float
182         :param beam_size: Width for Beam search.
183         :type beam_size: int
184         :param cutoff_prob: Cutoff probability in pruning,
185                             default 1.0, no pruning.
186         :type cutoff_prob: float
187         :param cutoff_top_n: Cutoff number in pruning, only top cutoff_top_n
188                         characters with highest probs in vocabulary will be
189                         used in beam search, default 40.
190         :type cutoff_top_n: int
191         :param vocab_list: List of tokens in the vocabulary, for decoding.
192         :type vocab_list: list
193         :param language_model_path: Filepath for language model.
194         :type language_model_path: basestring|None
195         :param num_processes: Number of processes (CPU) for decoder.
196         :type num_processes: int
197         :return: List of transcription texts.
198         :rtype: List of basestring
199         """
200         # define inferer
201         if self._inferer == None:
202             self._inferer = paddle.inference.Inference(
203                 output_layer=self._log_probs, parameters=self._parameters)
204         # run inference
205         infer_results = self._inferer.infer(input=infer_data)
206         num_steps = len(infer_results) // len(infer_data)
207         probs_split = [
208             infer_results[i * num_steps:(i + 1) * num_steps]
209             for i in xrange(0, len(infer_data))
210         ]
211         # run decoder
212         results = []
213         if decoding_method == "ctc_greedy":
214             # best path decode
215             for i, probs in enumerate(probs_split):
216                 output_transcription = ctc_greedy_decoder(
217                     probs_seq=probs, vocabulary=vocab_list)
218                 results.append(output_transcription)
219         elif decoding_method == "ctc_beam_search":
220             # initialize external scorer
221             if self._ext_scorer == None:
222                 self._loaded_lm_path = language_model_path
223                 self.logger.info("begin to initialize the external scorer "
224                                  "for decoding")
225                 self._ext_scorer = Scorer(beam_alpha, beam_beta,
226                                           language_model_path, vocab_list)
227 
228                 lm_char_based = self._ext_scorer.is_character_based()
229                 lm_max_order = self._ext_scorer.get_max_order()
230                 lm_dict_size = self._ext_scorer.get_dict_size()
231                 self.logger.info("language model: "
232                                  "is_character_based = %d," % lm_char_based +
233                                  " max_order = %d," % lm_max_order +
234                                  " dict_size = %d" % lm_dict_size)
235                 self.logger.info("end initializing scorer. Start decoding ...")
236             else:
237                 self._ext_scorer.reset_params(beam_alpha, beam_beta)
238                 assert self._loaded_lm_path == language_model_path
239             # beam search decode
240             num_processes = min(num_processes, len(probs_split))
241             beam_search_results = ctc_beam_search_decoder_batch(
242                 probs_split=probs_split,
243                 vocabulary=vocab_list,
244                 beam_size=beam_size,
245                 num_processes=num_processes,
246                 ext_scoring_func=self._ext_scorer,
247                 cutoff_prob=cutoff_prob,
248                 cutoff_top_n=cutoff_top_n)
249 
250             results = [result[0][1] for result in beam_search_results]
251         else:
252             raise ValueError("Decoding method [%s] is not supported." %
253                              decoding_method)
254         return results
255 
256     def _create_parameters(self, model_path=None):
257         """Load or create model parameters."""
258         if model_path is None:
259             self._parameters = paddle.parameters.create(self._loss)
260         else:
261             self._parameters = paddle.parameters.Parameters.from_tar(
262                 gzip.open(model_path))
263 
264     def _create_network(self, vocab_size, num_conv_layers, num_rnn_layers,
265                         rnn_layer_size, use_gru, share_rnn_weights):
266         """Create data layers and model network."""
267         # paddle.data_type.dense_array is used for variable batch input.
268         # The size 161 * 161 is only an placeholder value and the real shape
269         # of input batch data will be induced during training.
270         audio_data = paddle.layer.data(
271             name="audio_spectrogram",
272             type=paddle.data_type.dense_array(161 * 161))
273         text_data = paddle.layer.data(
274             name="transcript_text",
275             type=paddle.data_type.integer_value_sequence(vocab_size))
276         self._log_probs, self._loss = deep_speech_v2_network(
277             audio_data=audio_data,
278             text_data=text_data,
279             dict_size=vocab_size,
280             num_conv_layers=num_conv_layers,
281             num_rnn_layers=num_rnn_layers,
282             rnn_size=rnn_layer_size,
283             use_gru=use_gru,
284             share_rnn_weights=share_rnn_weights)
285
```
--- 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/deep_speech_2/model_utils/model.py b/deep_speech_2/model_utils/model.py
--- a/deep_speech_2/model_utils/model.py
+++ b/deep_speech_2/model_utils/model.py
@@ -129,9 +129,10 @@
                 else:
                     result = trainer.test(
                         reader=dev_batch_reader, feeding=feeding_dict)
-                    print("\n------- Time: %d sec,  Pass: %d, "
-                          "ValidationCost: %s" %
-                          (time.time() - start_time, event.pass_id, 0))
+                    print(
+                        "\n------- Time: %d sec,  Pass: %d, "
+                        "ValidationCost: %s" %
+                        (time.time() - start_time, event.pass_id, result.cost))
                 output_model_path = os.path.join(
                     output_model_dir, "params.pass-%d.tar.gz" % event.pass_id)
                 with gzip.open(output_model_path, 'w') as f:

{"golden_diff": "diff --git a/deep_speech_2/model_utils/model.py b/deep_speech_2/model_utils/model.py\n--- a/deep_speech_2/model_utils/model.py\n+++ b/deep_speech_2/model_utils/model.py\n@@ -129,9 +129,10 @@\n                 else:\n                     result = trainer.test(\n                         reader=dev_batch_reader, feeding=feeding_dict)\n-                    print(\"\\n------- Time: %d sec,  Pass: %d, \"\n-                          \"ValidationCost: %s\" %\n-                          (time.time() - start_time, event.pass_id, 0))\n+                    print(\n+                        \"\\n------- Time: %d sec,  Pass: %d, \"\n+                        \"ValidationCost: %s\" %\n+                        (time.time() - start_time, event.pass_id, result.cost))\n                 output_model_path = os.path.join(\n                     output_model_dir, \"params.pass-%d.tar.gz\" % event.pass_id)\n                 with gzip.open(output_model_path, 'w') as f:\n", "issue": "Fix bugs for model_utils/model.py\nAlways print 0 for ValidataionCost\n", "before_files": [{"content": "\"\"\"Contains DeepSpeech2 model.\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport sys\nimport os\nimport time\nimport logging\nimport gzip\nfrom distutils.dir_util import mkpath\nimport paddle.v2 as paddle\nfrom decoders.swig_wrapper import Scorer\nfrom decoders.swig_wrapper import ctc_greedy_decoder\nfrom decoders.swig_wrapper import ctc_beam_search_decoder_batch\nfrom model_utils.network import deep_speech_v2_network\n\nlogging.basicConfig(\n    format='[%(levelname)s %(asctime)s %(filename)s:%(lineno)d] %(message)s')\n\n\nclass DeepSpeech2Model(object):\n    \"\"\"DeepSpeech2Model class.\n\n    :param vocab_size: Decoding vocabulary size.\n    :type vocab_size: int\n    :param num_conv_layers: Number of stacking convolution layers.\n    :type num_conv_layers: int\n    :param num_rnn_layers: Number of stacking RNN layers.\n    :type num_rnn_layers: int\n    :param rnn_layer_size: RNN layer size (number of RNN cells).\n    :type rnn_layer_size: int\n    :param pretrained_model_path: Pretrained model path. If None, will train\n                                  from stratch.\n    :type pretrained_model_path: basestring|None\n    :param share_rnn_weights: Whether to share input-hidden weights between\n                              forward and backward directional RNNs.Notice that\n                              for GRU, weight sharing is not supported.\n    :type share_rnn_weights: bool\n    \"\"\"\n\n    def __init__(self, vocab_size, num_conv_layers, num_rnn_layers,\n                 rnn_layer_size, use_gru, pretrained_model_path,\n                 share_rnn_weights):\n        self._create_network(vocab_size, num_conv_layers, num_rnn_layers,\n                             rnn_layer_size, use_gru, share_rnn_weights)\n        self._create_parameters(pretrained_model_path)\n        self._inferer = None\n        self._loss_inferer = None\n        self._ext_scorer = None\n        self.logger = logging.getLogger(\"\")\n        self.logger.setLevel(level=logging.INFO)\n\n    def train(self,\n              train_batch_reader,\n              dev_batch_reader,\n              feeding_dict,\n              learning_rate,\n              gradient_clipping,\n              num_passes,\n              output_model_dir,\n              is_local=True,\n              num_iterations_print=100,\n              test_off=False):\n        \"\"\"Train the model.\n\n        :param train_batch_reader: Train data reader.\n        :type train_batch_reader: callable\n        :param dev_batch_reader: Validation data reader.\n        :type dev_batch_reader: callable\n        :param feeding_dict: Feeding is a map of field name and tuple index\n                             of the data that reader returns.\n        :type feeding_dict: dict|list\n        :param learning_rate: Learning rate for ADAM optimizer.\n        :type learning_rate: float\n        :param gradient_clipping: Gradient clipping threshold.\n        :type gradient_clipping: float\n        :param num_passes: Number of training epochs.\n        :type num_passes: int\n        :param num_iterations_print: Number of training iterations for printing\n                                     a training loss.\n        :type rnn_iteratons_print: int\n        :param is_local: Set to False if running with pserver with multi-nodes.\n        :type is_local: bool\n        :param output_model_dir: Directory for saving the model (every pass).\n        :type output_model_dir: basestring\n        :param test_off: Turn off testing.\n        :type test_off: bool\n        \"\"\"\n        # prepare model output directory\n        if not os.path.exists(output_model_dir):\n            mkpath(output_model_dir)\n\n        # prepare optimizer and trainer\n        optimizer = paddle.optimizer.Adam(\n            learning_rate=learning_rate,\n            gradient_clipping_threshold=gradient_clipping)\n        trainer = paddle.trainer.SGD(\n            cost=self._loss,\n            parameters=self._parameters,\n            update_equation=optimizer,\n            is_local=is_local)\n\n        # create event handler\n        def event_handler(event):\n            global start_time, cost_sum, cost_counter\n            if isinstance(event, paddle.event.EndIteration):\n                cost_sum += event.cost\n                cost_counter += 1\n                if (event.batch_id + 1) % num_iterations_print == 0:\n                    output_model_path = os.path.join(output_model_dir,\n                                                     \"params.latest.tar.gz\")\n                    with gzip.open(output_model_path, 'w') as f:\n                        self._parameters.to_tar(f)\n                    print(\"\\nPass: %d, Batch: %d, TrainCost: %f\" %\n                          (event.pass_id, event.batch_id + 1,\n                           cost_sum / cost_counter))\n                    cost_sum, cost_counter = 0.0, 0\n                else:\n                    sys.stdout.write('.')\n                    sys.stdout.flush()\n            if isinstance(event, paddle.event.BeginPass):\n                start_time = time.time()\n                cost_sum, cost_counter = 0.0, 0\n            if isinstance(event, paddle.event.EndPass):\n                if test_off:\n                    print(\"\\n------- Time: %d sec,  Pass: %d\" %\n                          (time.time() - start_time, event.pass_id))\n                else:\n                    result = trainer.test(\n                        reader=dev_batch_reader, feeding=feeding_dict)\n                    print(\"\\n------- Time: %d sec,  Pass: %d, \"\n                          \"ValidationCost: %s\" %\n                          (time.time() - start_time, event.pass_id, 0))\n                output_model_path = os.path.join(\n                    output_model_dir, \"params.pass-%d.tar.gz\" % event.pass_id)\n                with gzip.open(output_model_path, 'w') as f:\n                    self._parameters.to_tar(f)\n\n        # run train\n        trainer.train(\n            reader=train_batch_reader,\n            event_handler=event_handler,\n            num_passes=num_passes,\n            feeding=feeding_dict)\n\n    def infer_loss_batch(self, infer_data):\n        \"\"\"Model inference. Infer the ctc loss for a batch of speech\n        utterances.\n\n        :param infer_data: List of utterances to infer, with each utterance a\n                           tuple of audio features and transcription text (empty\n                           string).\n        :type infer_data: list\n        :return: List of ctc loss.\n        :rtype: List of float\n        \"\"\"\n        # define inferer\n        if self._loss_inferer == None:\n            self._loss_inferer = paddle.inference.Inference(\n                output_layer=self._loss, parameters=self._parameters)\n        # run inference\n        return self._loss_inferer.infer(input=infer_data)\n\n    def infer_batch(self, infer_data, decoding_method, beam_alpha, beam_beta,\n                    beam_size, cutoff_prob, cutoff_top_n, vocab_list,\n                    language_model_path, num_processes):\n        \"\"\"Model inference. Infer the transcription for a batch of speech\n        utterances.\n\n        :param infer_data: List of utterances to infer, with each utterance\n                           consisting of a tuple of audio features and\n                           transcription text (empty string).\n        :type infer_data: list\n        :param decoding_method: Decoding method name, 'ctc_greedy' or\n                                'ctc_beam_search'.\n        :param decoding_method: string\n        :param beam_alpha: Parameter associated with language model.\n        :type beam_alpha: float\n        :param beam_beta: Parameter associated with word count.\n        :type beam_beta: float\n        :param beam_size: Width for Beam search.\n        :type beam_size: int\n        :param cutoff_prob: Cutoff probability in pruning,\n                            default 1.0, no pruning.\n        :type cutoff_prob: float\n        :param cutoff_top_n: Cutoff number in pruning, only top cutoff_top_n\n                        characters with highest probs in vocabulary will be\n                        used in beam search, default 40.\n        :type cutoff_top_n: int\n        :param vocab_list: List of tokens in the vocabulary, for decoding.\n        :type vocab_list: list\n        :param language_model_path: Filepath for language model.\n        :type language_model_path: basestring|None\n        :param num_processes: Number of processes (CPU) for decoder.\n        :type num_processes: int\n        :return: List of transcription texts.\n        :rtype: List of basestring\n        \"\"\"\n        # define inferer\n        if self._inferer == None:\n            self._inferer = paddle.inference.Inference(\n                output_layer=self._log_probs, parameters=self._parameters)\n        # run inference\n        infer_results = self._inferer.infer(input=infer_data)\n        num_steps = len(infer_results) // len(infer_data)\n        probs_split = [\n            infer_results[i * num_steps:(i + 1) * num_steps]\n            for i in xrange(0, len(infer_data))\n        ]\n        # run decoder\n        results = []\n        if decoding_method == \"ctc_greedy\":\n            # best path decode\n            for i, probs in enumerate(probs_split):\n                output_transcription = ctc_greedy_decoder(\n                    probs_seq=probs, vocabulary=vocab_list)\n                results.append(output_transcription)\n        elif decoding_method == \"ctc_beam_search\":\n            # initialize external scorer\n            if self._ext_scorer == None:\n                self._loaded_lm_path = language_model_path\n                self.logger.info(\"begin to initialize the external scorer \"\n                                 \"for decoding\")\n                self._ext_scorer = Scorer(beam_alpha, beam_beta,\n                                          language_model_path, vocab_list)\n\n                lm_char_based = self._ext_scorer.is_character_based()\n                lm_max_order = self._ext_scorer.get_max_order()\n                lm_dict_size = self._ext_scorer.get_dict_size()\n                self.logger.info(\"language model: \"\n                                 \"is_character_based = %d,\" % lm_char_based +\n                                 \" max_order = %d,\" % lm_max_order +\n                                 \" dict_size = %d\" % lm_dict_size)\n                self.logger.info(\"end initializing scorer. Start decoding ...\")\n            else:\n                self._ext_scorer.reset_params(beam_alpha, beam_beta)\n                assert self._loaded_lm_path == language_model_path\n            # beam search decode\n            num_processes = min(num_processes, len(probs_split))\n            beam_search_results = ctc_beam_search_decoder_batch(\n                probs_split=probs_split,\n                vocabulary=vocab_list,\n                beam_size=beam_size,\n                num_processes=num_processes,\n                ext_scoring_func=self._ext_scorer,\n                cutoff_prob=cutoff_prob,\n                cutoff_top_n=cutoff_top_n)\n\n            results = [result[0][1] for result in beam_search_results]\n        else:\n            raise ValueError(\"Decoding method [%s] is not supported.\" %\n                             decoding_method)\n        return results\n\n    def _create_parameters(self, model_path=None):\n        \"\"\"Load or create model parameters.\"\"\"\n        if model_path is None:\n            self._parameters = paddle.parameters.create(self._loss)\n        else:\n            self._parameters = paddle.parameters.Parameters.from_tar(\n                gzip.open(model_path))\n\n    def _create_network(self, vocab_size, num_conv_layers, num_rnn_layers,\n                        rnn_layer_size, use_gru, share_rnn_weights):\n        \"\"\"Create data layers and model network.\"\"\"\n        # paddle.data_type.dense_array is used for variable batch input.\n        # The size 161 * 161 is only an placeholder value and the real shape\n        # of input batch data will be induced during training.\n        audio_data = paddle.layer.data(\n            name=\"audio_spectrogram\",\n            type=paddle.data_type.dense_array(161 * 161))\n        text_data = paddle.layer.data(\n            name=\"transcript_text\",\n            type=paddle.data_type.integer_value_sequence(vocab_size))\n        self._log_probs, self._loss = deep_speech_v2_network(\n            audio_data=audio_data,\n            text_data=text_data,\n            dict_size=vocab_size,\n            num_conv_layers=num_conv_layers,\n            num_rnn_layers=num_rnn_layers,\n            rnn_size=rnn_layer_size,\n            use_gru=use_gru,\n            share_rnn_weights=share_rnn_weights)\n", "path": "deep_speech_2/model_utils/model.py"}], "after_files": [{"content": "\"\"\"Contains DeepSpeech2 model.\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport sys\nimport os\nimport time\nimport logging\nimport gzip\nfrom distutils.dir_util import mkpath\nimport paddle.v2 as paddle\nfrom decoders.swig_wrapper import Scorer\nfrom decoders.swig_wrapper import ctc_greedy_decoder\nfrom decoders.swig_wrapper import ctc_beam_search_decoder_batch\nfrom model_utils.network import deep_speech_v2_network\n\nlogging.basicConfig(\n    format='[%(levelname)s %(asctime)s %(filename)s:%(lineno)d] %(message)s')\n\n\nclass DeepSpeech2Model(object):\n    \"\"\"DeepSpeech2Model class.\n\n    :param vocab_size: Decoding vocabulary size.\n    :type vocab_size: int\n    :param num_conv_layers: Number of stacking convolution layers.\n    :type num_conv_layers: int\n    :param num_rnn_layers: Number of stacking RNN layers.\n    :type num_rnn_layers: int\n    :param rnn_layer_size: RNN layer size (number of RNN cells).\n    :type rnn_layer_size: int\n    :param pretrained_model_path: Pretrained model path. If None, will train\n                                  from stratch.\n    :type pretrained_model_path: basestring|None\n    :param share_rnn_weights: Whether to share input-hidden weights between\n                              forward and backward directional RNNs.Notice that\n                              for GRU, weight sharing is not supported.\n    :type share_rnn_weights: bool\n    \"\"\"\n\n    def __init__(self, vocab_size, num_conv_layers, num_rnn_layers,\n                 rnn_layer_size, use_gru, pretrained_model_path,\n                 share_rnn_weights):\n        self._create_network(vocab_size, num_conv_layers, num_rnn_layers,\n                             rnn_layer_size, use_gru, share_rnn_weights)\n        self._create_parameters(pretrained_model_path)\n        self._inferer = None\n        self._loss_inferer = None\n        self._ext_scorer = None\n        self.logger = logging.getLogger(\"\")\n        self.logger.setLevel(level=logging.INFO)\n\n    def train(self,\n              train_batch_reader,\n              dev_batch_reader,\n              feeding_dict,\n              learning_rate,\n              gradient_clipping,\n              num_passes,\n              output_model_dir,\n              is_local=True,\n              num_iterations_print=100,\n              test_off=False):\n        \"\"\"Train the model.\n\n        :param train_batch_reader: Train data reader.\n        :type train_batch_reader: callable\n        :param dev_batch_reader: Validation data reader.\n        :type dev_batch_reader: callable\n        :param feeding_dict: Feeding is a map of field name and tuple index\n                             of the data that reader returns.\n        :type feeding_dict: dict|list\n        :param learning_rate: Learning rate for ADAM optimizer.\n        :type learning_rate: float\n        :param gradient_clipping: Gradient clipping threshold.\n        :type gradient_clipping: float\n        :param num_passes: Number of training epochs.\n        :type num_passes: int\n        :param num_iterations_print: Number of training iterations for printing\n                                     a training loss.\n        :type rnn_iteratons_print: int\n        :param is_local: Set to False if running with pserver with multi-nodes.\n        :type is_local: bool\n        :param output_model_dir: Directory for saving the model (every pass).\n        :type output_model_dir: basestring\n        :param test_off: Turn off testing.\n        :type test_off: bool\n        \"\"\"\n        # prepare model output directory\n        if not os.path.exists(output_model_dir):\n            mkpath(output_model_dir)\n\n        # prepare optimizer and trainer\n        optimizer = paddle.optimizer.Adam(\n            learning_rate=learning_rate,\n            gradient_clipping_threshold=gradient_clipping)\n        trainer = paddle.trainer.SGD(\n            cost=self._loss,\n            parameters=self._parameters,\n            update_equation=optimizer,\n            is_local=is_local)\n\n        # create event handler\n        def event_handler(event):\n            global start_time, cost_sum, cost_counter\n            if isinstance(event, paddle.event.EndIteration):\n                cost_sum += event.cost\n                cost_counter += 1\n                if (event.batch_id + 1) % num_iterations_print == 0:\n                    output_model_path = os.path.join(output_model_dir,\n                                                     \"params.latest.tar.gz\")\n                    with gzip.open(output_model_path, 'w') as f:\n                        self._parameters.to_tar(f)\n                    print(\"\\nPass: %d, Batch: %d, TrainCost: %f\" %\n                          (event.pass_id, event.batch_id + 1,\n                           cost_sum / cost_counter))\n                    cost_sum, cost_counter = 0.0, 0\n                else:\n                    sys.stdout.write('.')\n                    sys.stdout.flush()\n            if isinstance(event, paddle.event.BeginPass):\n                start_time = time.time()\n                cost_sum, cost_counter = 0.0, 0\n            if isinstance(event, paddle.event.EndPass):\n                if test_off:\n                    print(\"\\n------- Time: %d sec,  Pass: %d\" %\n                          (time.time() - start_time, event.pass_id))\n                else:\n                    result = trainer.test(\n                        reader=dev_batch_reader, feeding=feeding_dict)\n                    print(\n                        \"\\n------- Time: %d sec,  Pass: %d, \"\n                        \"ValidationCost: %s\" %\n                        (time.time() - start_time, event.pass_id, result.cost))\n                output_model_path = os.path.join(\n                    output_model_dir, \"params.pass-%d.tar.gz\" % event.pass_id)\n                with gzip.open(output_model_path, 'w') as f:\n                    self._parameters.to_tar(f)\n\n        # run train\n        trainer.train(\n            reader=train_batch_reader,\n            event_handler=event_handler,\n            num_passes=num_passes,\n            feeding=feeding_dict)\n\n    def infer_loss_batch(self, infer_data):\n        \"\"\"Model inference. Infer the ctc loss for a batch of speech\n        utterances.\n\n        :param infer_data: List of utterances to infer, with each utterance a\n                           tuple of audio features and transcription text (empty\n                           string).\n        :type infer_data: list\n        :return: List of ctc loss.\n        :rtype: List of float\n        \"\"\"\n        # define inferer\n        if self._loss_inferer == None:\n            self._loss_inferer = paddle.inference.Inference(\n                output_layer=self._loss, parameters=self._parameters)\n        # run inference\n        return self._loss_inferer.infer(input=infer_data)\n\n    def infer_batch(self, infer_data, decoding_method, beam_alpha, beam_beta,\n                    beam_size, cutoff_prob, cutoff_top_n, vocab_list,\n                    language_model_path, num_processes):\n        \"\"\"Model inference. Infer the transcription for a batch of speech\n        utterances.\n\n        :param infer_data: List of utterances to infer, with each utterance\n                           consisting of a tuple of audio features and\n                           transcription text (empty string).\n        :type infer_data: list\n        :param decoding_method: Decoding method name, 'ctc_greedy' or\n                                'ctc_beam_search'.\n        :param decoding_method: string\n        :param beam_alpha: Parameter associated with language model.\n        :type beam_alpha: float\n        :param beam_beta: Parameter associated with word count.\n        :type beam_beta: float\n        :param beam_size: Width for Beam search.\n        :type beam_size: int\n        :param cutoff_prob: Cutoff probability in pruning,\n                            default 1.0, no pruning.\n        :type cutoff_prob: float\n        :param cutoff_top_n: Cutoff number in pruning, only top cutoff_top_n\n                        characters with highest probs in vocabulary will be\n                        used in beam search, default 40.\n        :type cutoff_top_n: int\n        :param vocab_list: List of tokens in the vocabulary, for decoding.\n        :type vocab_list: list\n        :param language_model_path: Filepath for language model.\n        :type language_model_path: basestring|None\n        :param num_processes: Number of processes (CPU) for decoder.\n        :type num_processes: int\n        :return: List of transcription texts.\n        :rtype: List of basestring\n        \"\"\"\n        # define inferer\n        if self._inferer == None:\n            self._inferer = paddle.inference.Inference(\n                output_layer=self._log_probs, parameters=self._parameters)\n        # run inference\n        infer_results = self._inferer.infer(input=infer_data)\n        num_steps = len(infer_results) // len(infer_data)\n        probs_split = [\n            infer_results[i * num_steps:(i + 1) * num_steps]\n            for i in xrange(0, len(infer_data))\n        ]\n        # run decoder\n        results = []\n        if decoding_method == \"ctc_greedy\":\n            # best path decode\n            for i, probs in enumerate(probs_split):\n                output_transcription = ctc_greedy_decoder(\n                    probs_seq=probs, vocabulary=vocab_list)\n                results.append(output_transcription)\n        elif decoding_method == \"ctc_beam_search\":\n            # initialize external scorer\n            if self._ext_scorer == None:\n                self._loaded_lm_path = language_model_path\n                self.logger.info(\"begin to initialize the external scorer \"\n                                 \"for decoding\")\n                self._ext_scorer = Scorer(beam_alpha, beam_beta,\n                                          language_model_path, vocab_list)\n\n                lm_char_based = self._ext_scorer.is_character_based()\n                lm_max_order = self._ext_scorer.get_max_order()\n                lm_dict_size = self._ext_scorer.get_dict_size()\n                self.logger.info(\"language model: \"\n                                 \"is_character_based = %d,\" % lm_char_based +\n                                 \" max_order = %d,\" % lm_max_order +\n                                 \" dict_size = %d\" % lm_dict_size)\n                self.logger.info(\"end initializing scorer. Start decoding ...\")\n            else:\n                self._ext_scorer.reset_params(beam_alpha, beam_beta)\n                assert self._loaded_lm_path == language_model_path\n            # beam search decode\n            num_processes = min(num_processes, len(probs_split))\n            beam_search_results = ctc_beam_search_decoder_batch(\n                probs_split=probs_split,\n                vocabulary=vocab_list,\n                beam_size=beam_size,\n                num_processes=num_processes,\n                ext_scoring_func=self._ext_scorer,\n                cutoff_prob=cutoff_prob,\n                cutoff_top_n=cutoff_top_n)\n\n            results = [result[0][1] for result in beam_search_results]\n        else:\n            raise ValueError(\"Decoding method [%s] is not supported.\" %\n                             decoding_method)\n        return results\n\n    def _create_parameters(self, model_path=None):\n        \"\"\"Load or create model parameters.\"\"\"\n        if model_path is None:\n            self._parameters = paddle.parameters.create(self._loss)\n        else:\n            self._parameters = paddle.parameters.Parameters.from_tar(\n                gzip.open(model_path))\n\n    def _create_network(self, vocab_size, num_conv_layers, num_rnn_layers,\n                        rnn_layer_size, use_gru, share_rnn_weights):\n        \"\"\"Create data layers and model network.\"\"\"\n        # paddle.data_type.dense_array is used for variable batch input.\n        # The size 161 * 161 is only an placeholder value and the real shape\n        # of input batch data will be induced during training.\n        audio_data = paddle.layer.data(\n            name=\"audio_spectrogram\",\n            type=paddle.data_type.dense_array(161 * 161))\n        text_data = paddle.layer.data(\n            name=\"transcript_text\",\n            type=paddle.data_type.integer_value_sequence(vocab_size))\n        self._log_probs, self._loss = deep_speech_v2_network(\n            audio_data=audio_data,\n            text_data=text_data,\n            dict_size=vocab_size,\n            num_conv_layers=num_conv_layers,\n            num_rnn_layers=num_rnn_layers,\n            rnn_size=rnn_layer_size,\n            use_gru=use_gru,\n            share_rnn_weights=share_rnn_weights)\n", "path": "deep_speech_2/model_utils/model.py"}]}