We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Enable the log of gradient clipping in training


--- 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/train.py`
Content:
```
1 """Trainer for DeepSpeech2 model."""
2 from __future__ import absolute_import
3 from __future__ import division
4 from __future__ import print_function
5 
6 import argparse
7 import functools
8 import paddle.v2 as paddle
9 from model_utils.model import DeepSpeech2Model
10 from data_utils.data import DataGenerator
11 from utils.utility import add_arguments, print_arguments
12 
13 parser = argparse.ArgumentParser(description=__doc__)
14 add_arg = functools.partial(add_arguments, argparser=parser)
15 # yapf: disable
16 add_arg('batch_size',       int,    256,    "Minibatch size.")
17 add_arg('trainer_count',    int,    8,      "# of Trainers (CPUs or GPUs).")
18 add_arg('num_passes',       int,    200,    "# of training epochs.")
19 add_arg('num_proc_data',    int,    12,     "# of CPUs for data preprocessing.")
20 add_arg('num_conv_layers',  int,    2,      "# of convolution layers.")
21 add_arg('num_rnn_layers',   int,    3,      "# of recurrent layers.")
22 add_arg('rnn_layer_size',   int,    2048,   "# of recurrent cells per layer.")
23 add_arg('num_iter_print',   int,    100,    "Every # iterations for printing "
24                                             "train cost.")
25 add_arg('learning_rate',    float,  5e-4,   "Learning rate.")
26 add_arg('max_duration',     float,  27.0,   "Longest audio duration allowed.")
27 add_arg('min_duration',     float,  0.0,    "Shortest audio duration allowed.")
28 add_arg('test_off',         bool,   False,  "Turn off testing.")
29 add_arg('use_sortagrad',    bool,   True,   "Use SortaGrad or not.")
30 add_arg('use_gpu',          bool,   True,   "Use GPU or not.")
31 add_arg('use_gru',          bool,   False,  "Use GRUs instead of simple RNNs.")
32 add_arg('is_local',         bool,   True,   "Use pserver or not.")
33 add_arg('share_rnn_weights',bool,   True,   "Share input-hidden weights across "
34                                             "bi-directional RNNs. Not for GRU.")
35 add_arg('train_manifest',   str,
36         'data/librispeech/manifest.train',
37         "Filepath of train manifest.")
38 add_arg('dev_manifest',     str,
39         'data/librispeech/manifest.dev-clean',
40         "Filepath of validation manifest.")
41 add_arg('mean_std_path',    str,
42         'data/librispeech/mean_std.npz',
43         "Filepath of normalizer's mean & std.")
44 add_arg('vocab_path',       str,
45         'data/librispeech/vocab.txt',
46         "Filepath of vocabulary.")
47 add_arg('init_model_path',  str,
48         None,
49         "If None, the training starts from scratch, "
50         "otherwise, it resumes from the pre-trained model.")
51 add_arg('output_model_dir', str,
52         "./checkpoints/libri",
53         "Directory for saving checkpoints.")
54 add_arg('augment_conf_path',str,
55         'conf/augmentation.config',
56         "Filepath of augmentation configuration file (json-format).")
57 add_arg('specgram_type',    str,
58         'linear',
59         "Audio feature type. Options: linear, mfcc.",
60         choices=['linear', 'mfcc'])
61 add_arg('shuffle_method',   str,
62         'batch_shuffle_clipped',
63         "Shuffle method.",
64         choices=['instance_shuffle', 'batch_shuffle', 'batch_shuffle_clipped'])
65 # yapf: disable
66 args = parser.parse_args()
67 
68 
69 def train():
70     """DeepSpeech2 training."""
71     train_generator = DataGenerator(
72         vocab_filepath=args.vocab_path,
73         mean_std_filepath=args.mean_std_path,
74         augmentation_config=open(args.augment_conf_path, 'r').read(),
75         max_duration=args.max_duration,
76         min_duration=args.min_duration,
77         specgram_type=args.specgram_type,
78         num_threads=args.num_proc_data)
79     dev_generator = DataGenerator(
80         vocab_filepath=args.vocab_path,
81         mean_std_filepath=args.mean_std_path,
82         augmentation_config="{}",
83         specgram_type=args.specgram_type,
84         num_threads=args.num_proc_data)
85     train_batch_reader = train_generator.batch_reader_creator(
86         manifest_path=args.train_manifest,
87         batch_size=args.batch_size,
88         min_batch_size=args.trainer_count,
89         sortagrad=args.use_sortagrad if args.init_model_path is None else False,
90         shuffle_method=args.shuffle_method)
91     dev_batch_reader = dev_generator.batch_reader_creator(
92         manifest_path=args.dev_manifest,
93         batch_size=args.batch_size,
94         min_batch_size=1,  # must be 1, but will have errors.
95         sortagrad=False,
96         shuffle_method=None)
97 
98     ds2_model = DeepSpeech2Model(
99         vocab_size=train_generator.vocab_size,
100         num_conv_layers=args.num_conv_layers,
101         num_rnn_layers=args.num_rnn_layers,
102         rnn_layer_size=args.rnn_layer_size,
103         use_gru=args.use_gru,
104         pretrained_model_path=args.init_model_path,
105         share_rnn_weights=args.share_rnn_weights)
106     ds2_model.train(
107         train_batch_reader=train_batch_reader,
108         dev_batch_reader=dev_batch_reader,
109         feeding_dict=train_generator.feeding,
110         learning_rate=args.learning_rate,
111         gradient_clipping=400,
112         num_passes=args.num_passes,
113         num_iterations_print=args.num_iter_print,
114         output_model_dir=args.output_model_dir,
115         is_local=args.is_local,
116         test_off=args.test_off)
117 
118 
119 def main():
120     print_arguments(args)
121     paddle.init(use_gpu=args.use_gpu, trainer_count=args.trainer_count)
122     train()
123 
124 
125 if __name__ == '__main__':
126     main()
127
```

Path: `deep_speech_2/decoders/swig_wrapper.py`
Content:
```
1 """Wrapper for various CTC decoders in SWIG."""
2 from __future__ import absolute_import
3 from __future__ import division
4 from __future__ import print_function
5 
6 import swig_decoders
7 
8 
9 class Scorer(swig_decoders.Scorer):
10     """Wrapper for Scorer.
11 
12     :param alpha: Parameter associated with language model. Don't use
13                   language model when alpha = 0.
14     :type alpha: float
15     :param beta: Parameter associated with word count. Don't use word
16                  count when beta = 0.
17     :type beta: float
18     :model_path: Path to load language model.
19     :type model_path: basestring
20     """
21 
22     def __init__(self, alpha, beta, model_path, vocabulary):
23         swig_decoders.Scorer.__init__(self, alpha, beta, model_path, vocabulary)
24 
25 
26 def ctc_greedy_decoder(probs_seq, vocabulary):
27     """Wrapper for ctc best path decoder in swig.
28 
29     :param probs_seq: 2-D list of probability distributions over each time
30                       step, with each element being a list of normalized
31                       probabilities over vocabulary and blank.
32     :type probs_seq: 2-D list
33     :param vocabulary: Vocabulary list.
34     :type vocabulary: list
35     :return: Decoding result string.
36     :rtype: basestring
37     """
38     return swig_decoders.ctc_greedy_decoder(probs_seq.tolist(), vocabulary)
39 
40 
41 def ctc_beam_search_decoder(probs_seq,
42                             vocabulary,
43                             beam_size,
44                             cutoff_prob=1.0,
45                             cutoff_top_n=40,
46                             ext_scoring_func=None):
47     """Wrapper for the CTC Beam Search Decoder.
48 
49     :param probs_seq: 2-D list of probability distributions over each time
50                       step, with each element being a list of normalized
51                       probabilities over vocabulary and blank.
52     :type probs_seq: 2-D list
53     :param vocabulary: Vocabulary list.
54     :type vocabulary: list
55     :param beam_size: Width for beam search.
56     :type beam_size: int
57     :param cutoff_prob: Cutoff probability in pruning,
58                         default 1.0, no pruning.
59     :type cutoff_prob: float
60     :param cutoff_top_n: Cutoff number in pruning, only top cutoff_top_n
61                          characters with highest probs in vocabulary will be
62                          used in beam search, default 40.
63     :type cutoff_top_n: int
64     :param ext_scoring_func: External scoring function for
65                              partially decoded sentence, e.g. word count
66                              or language model.
67     :type external_scoring_func: callable
68     :return: List of tuples of log probability and sentence as decoding
69              results, in descending order of the probability.
70     :rtype: list
71     """
72     return swig_decoders.ctc_beam_search_decoder(probs_seq.tolist(), vocabulary,
73                                                  beam_size, cutoff_prob,
74                                                  cutoff_top_n, ext_scoring_func)
75 
76 
77 def ctc_beam_search_decoder_batch(probs_split,
78                                   vocabulary,
79                                   beam_size,
80                                   num_processes,
81                                   cutoff_prob=1.0,
82                                   cutoff_top_n=40,
83                                   ext_scoring_func=None):
84     """Wrapper for the batched CTC beam search decoder.
85 
86     :param probs_seq: 3-D list with each element as an instance of 2-D list
87                       of probabilities used by ctc_beam_search_decoder().
88     :type probs_seq: 3-D list
89     :param vocabulary: Vocabulary list.
90     :type vocabulary: list
91     :param beam_size: Width for beam search.
92     :type beam_size: int
93     :param num_processes: Number of parallel processes.
94     :type num_processes: int
95     :param cutoff_prob: Cutoff probability in vocabulary pruning,
96                         default 1.0, no pruning.
97     :type cutoff_prob: float
98     :param cutoff_top_n: Cutoff number in pruning, only top cutoff_top_n
99                          characters with highest probs in vocabulary will be
100                          used in beam search, default 40.
101     :type cutoff_top_n: int
102     :param num_processes: Number of parallel processes.
103     :type num_processes: int
104     :param ext_scoring_func: External scoring function for
105                              partially decoded sentence, e.g. word count
106                              or language model.
107     :type external_scoring_function: callable
108     :return: List of tuples of log probability and sentence as decoding
109              results, in descending order of the probability.
110     :rtype: list
111     """
112     probs_split = [probs_seq.tolist() for probs_seq in probs_split]
113 
114     return swig_decoders.ctc_beam_search_decoder_batch(
115         probs_split, vocabulary, beam_size, num_processes, cutoff_prob,
116         cutoff_top_n, ext_scoring_func)
117
```
--- 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/decoders/swig_wrapper.py b/deep_speech_2/decoders/swig_wrapper.py
--- a/deep_speech_2/decoders/swig_wrapper.py
+++ b/deep_speech_2/decoders/swig_wrapper.py
@@ -35,7 +35,8 @@
     :return: Decoding result string.
     :rtype: basestring
     """
-    return swig_decoders.ctc_greedy_decoder(probs_seq.tolist(), vocabulary)
+    result = swig_decoders.ctc_greedy_decoder(probs_seq.tolist(), vocabulary)
+    return result.decode('utf-8')
 
 
 def ctc_beam_search_decoder(probs_seq,
@@ -69,9 +70,11 @@
              results, in descending order of the probability.
     :rtype: list
     """
-    return swig_decoders.ctc_beam_search_decoder(probs_seq.tolist(), vocabulary,
-                                                 beam_size, cutoff_prob,
-                                                 cutoff_top_n, ext_scoring_func)
+    beam_results = swig_decoders.ctc_beam_search_decoder(
+        probs_seq.tolist(), vocabulary, beam_size, cutoff_prob, cutoff_top_n,
+        ext_scoring_func)
+    beam_results = [(res[0], res[1].decode('utf-8')) for res in beam_results]
+    return beam_results
 
 
 def ctc_beam_search_decoder_batch(probs_split,
@@ -111,6 +114,11 @@
     """
     probs_split = [probs_seq.tolist() for probs_seq in probs_split]
 
-    return swig_decoders.ctc_beam_search_decoder_batch(
+    batch_beam_results = swig_decoders.ctc_beam_search_decoder_batch(
         probs_split, vocabulary, beam_size, num_processes, cutoff_prob,
         cutoff_top_n, ext_scoring_func)
+    batch_beam_results = [
+        [(res[0], res[1].decode("utf-8")) for res in beam_results]
+        for beam_results in batch_beam_results
+    ]
+    return batch_beam_results
diff --git a/deep_speech_2/train.py b/deep_speech_2/train.py
--- a/deep_speech_2/train.py
+++ b/deep_speech_2/train.py
@@ -118,7 +118,9 @@
 
 def main():
     print_arguments(args)
-    paddle.init(use_gpu=args.use_gpu, trainer_count=args.trainer_count)
+    paddle.init(use_gpu=args.use_gpu,
+                trainer_count=args.trainer_count,
+                log_clipping=True)
     train()

{"golden_diff": "diff --git a/deep_speech_2/decoders/swig_wrapper.py b/deep_speech_2/decoders/swig_wrapper.py\n--- a/deep_speech_2/decoders/swig_wrapper.py\n+++ b/deep_speech_2/decoders/swig_wrapper.py\n@@ -35,7 +35,8 @@\n     :return: Decoding result string.\n     :rtype: basestring\n     \"\"\"\n-    return swig_decoders.ctc_greedy_decoder(probs_seq.tolist(), vocabulary)\n+    result = swig_decoders.ctc_greedy_decoder(probs_seq.tolist(), vocabulary)\n+    return result.decode('utf-8')\n \n \n def ctc_beam_search_decoder(probs_seq,\n@@ -69,9 +70,11 @@\n              results, in descending order of the probability.\n     :rtype: list\n     \"\"\"\n-    return swig_decoders.ctc_beam_search_decoder(probs_seq.tolist(), vocabulary,\n-                                                 beam_size, cutoff_prob,\n-                                                 cutoff_top_n, ext_scoring_func)\n+    beam_results = swig_decoders.ctc_beam_search_decoder(\n+        probs_seq.tolist(), vocabulary, beam_size, cutoff_prob, cutoff_top_n,\n+        ext_scoring_func)\n+    beam_results = [(res[0], res[1].decode('utf-8')) for res in beam_results]\n+    return beam_results\n \n \n def ctc_beam_search_decoder_batch(probs_split,\n@@ -111,6 +114,11 @@\n     \"\"\"\n     probs_split = [probs_seq.tolist() for probs_seq in probs_split]\n \n-    return swig_decoders.ctc_beam_search_decoder_batch(\n+    batch_beam_results = swig_decoders.ctc_beam_search_decoder_batch(\n         probs_split, vocabulary, beam_size, num_processes, cutoff_prob,\n         cutoff_top_n, ext_scoring_func)\n+    batch_beam_results = [\n+        [(res[0], res[1].decode(\"utf-8\")) for res in beam_results]\n+        for beam_results in batch_beam_results\n+    ]\n+    return batch_beam_results\ndiff --git a/deep_speech_2/train.py b/deep_speech_2/train.py\n--- a/deep_speech_2/train.py\n+++ b/deep_speech_2/train.py\n@@ -118,7 +118,9 @@\n \n def main():\n     print_arguments(args)\n-    paddle.init(use_gpu=args.use_gpu, trainer_count=args.trainer_count)\n+    paddle.init(use_gpu=args.use_gpu,\n+                trainer_count=args.trainer_count,\n+                log_clipping=True)\n     train()\n", "issue": "Enable the log of gradient clipping in training\n\n", "before_files": [{"content": "\"\"\"Trainer for DeepSpeech2 model.\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport argparse\nimport functools\nimport paddle.v2 as paddle\nfrom model_utils.model import DeepSpeech2Model\nfrom data_utils.data import DataGenerator\nfrom utils.utility import add_arguments, print_arguments\n\nparser = argparse.ArgumentParser(description=__doc__)\nadd_arg = functools.partial(add_arguments, argparser=parser)\n# yapf: disable\nadd_arg('batch_size',       int,    256,    \"Minibatch size.\")\nadd_arg('trainer_count',    int,    8,      \"# of Trainers (CPUs or GPUs).\")\nadd_arg('num_passes',       int,    200,    \"# of training epochs.\")\nadd_arg('num_proc_data',    int,    12,     \"# of CPUs for data preprocessing.\")\nadd_arg('num_conv_layers',  int,    2,      \"# of convolution layers.\")\nadd_arg('num_rnn_layers',   int,    3,      \"# of recurrent layers.\")\nadd_arg('rnn_layer_size',   int,    2048,   \"# of recurrent cells per layer.\")\nadd_arg('num_iter_print',   int,    100,    \"Every # iterations for printing \"\n                                            \"train cost.\")\nadd_arg('learning_rate',    float,  5e-4,   \"Learning rate.\")\nadd_arg('max_duration',     float,  27.0,   \"Longest audio duration allowed.\")\nadd_arg('min_duration',     float,  0.0,    \"Shortest audio duration allowed.\")\nadd_arg('test_off',         bool,   False,  \"Turn off testing.\")\nadd_arg('use_sortagrad',    bool,   True,   \"Use SortaGrad or not.\")\nadd_arg('use_gpu',          bool,   True,   \"Use GPU or not.\")\nadd_arg('use_gru',          bool,   False,  \"Use GRUs instead of simple RNNs.\")\nadd_arg('is_local',         bool,   True,   \"Use pserver or not.\")\nadd_arg('share_rnn_weights',bool,   True,   \"Share input-hidden weights across \"\n                                            \"bi-directional RNNs. Not for GRU.\")\nadd_arg('train_manifest',   str,\n        'data/librispeech/manifest.train',\n        \"Filepath of train manifest.\")\nadd_arg('dev_manifest',     str,\n        'data/librispeech/manifest.dev-clean',\n        \"Filepath of validation manifest.\")\nadd_arg('mean_std_path',    str,\n        'data/librispeech/mean_std.npz',\n        \"Filepath of normalizer's mean & std.\")\nadd_arg('vocab_path',       str,\n        'data/librispeech/vocab.txt',\n        \"Filepath of vocabulary.\")\nadd_arg('init_model_path',  str,\n        None,\n        \"If None, the training starts from scratch, \"\n        \"otherwise, it resumes from the pre-trained model.\")\nadd_arg('output_model_dir', str,\n        \"./checkpoints/libri\",\n        \"Directory for saving checkpoints.\")\nadd_arg('augment_conf_path',str,\n        'conf/augmentation.config',\n        \"Filepath of augmentation configuration file (json-format).\")\nadd_arg('specgram_type',    str,\n        'linear',\n        \"Audio feature type. Options: linear, mfcc.\",\n        choices=['linear', 'mfcc'])\nadd_arg('shuffle_method',   str,\n        'batch_shuffle_clipped',\n        \"Shuffle method.\",\n        choices=['instance_shuffle', 'batch_shuffle', 'batch_shuffle_clipped'])\n# yapf: disable\nargs = parser.parse_args()\n\n\ndef train():\n    \"\"\"DeepSpeech2 training.\"\"\"\n    train_generator = DataGenerator(\n        vocab_filepath=args.vocab_path,\n        mean_std_filepath=args.mean_std_path,\n        augmentation_config=open(args.augment_conf_path, 'r').read(),\n        max_duration=args.max_duration,\n        min_duration=args.min_duration,\n        specgram_type=args.specgram_type,\n        num_threads=args.num_proc_data)\n    dev_generator = DataGenerator(\n        vocab_filepath=args.vocab_path,\n        mean_std_filepath=args.mean_std_path,\n        augmentation_config=\"{}\",\n        specgram_type=args.specgram_type,\n        num_threads=args.num_proc_data)\n    train_batch_reader = train_generator.batch_reader_creator(\n        manifest_path=args.train_manifest,\n        batch_size=args.batch_size,\n        min_batch_size=args.trainer_count,\n        sortagrad=args.use_sortagrad if args.init_model_path is None else False,\n        shuffle_method=args.shuffle_method)\n    dev_batch_reader = dev_generator.batch_reader_creator(\n        manifest_path=args.dev_manifest,\n        batch_size=args.batch_size,\n        min_batch_size=1,  # must be 1, but will have errors.\n        sortagrad=False,\n        shuffle_method=None)\n\n    ds2_model = DeepSpeech2Model(\n        vocab_size=train_generator.vocab_size,\n        num_conv_layers=args.num_conv_layers,\n        num_rnn_layers=args.num_rnn_layers,\n        rnn_layer_size=args.rnn_layer_size,\n        use_gru=args.use_gru,\n        pretrained_model_path=args.init_model_path,\n        share_rnn_weights=args.share_rnn_weights)\n    ds2_model.train(\n        train_batch_reader=train_batch_reader,\n        dev_batch_reader=dev_batch_reader,\n        feeding_dict=train_generator.feeding,\n        learning_rate=args.learning_rate,\n        gradient_clipping=400,\n        num_passes=args.num_passes,\n        num_iterations_print=args.num_iter_print,\n        output_model_dir=args.output_model_dir,\n        is_local=args.is_local,\n        test_off=args.test_off)\n\n\ndef main():\n    print_arguments(args)\n    paddle.init(use_gpu=args.use_gpu, trainer_count=args.trainer_count)\n    train()\n\n\nif __name__ == '__main__':\n    main()\n", "path": "deep_speech_2/train.py"}, {"content": "\"\"\"Wrapper for various CTC decoders in SWIG.\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport swig_decoders\n\n\nclass Scorer(swig_decoders.Scorer):\n    \"\"\"Wrapper for Scorer.\n\n    :param alpha: Parameter associated with language model. Don't use\n                  language model when alpha = 0.\n    :type alpha: float\n    :param beta: Parameter associated with word count. Don't use word\n                 count when beta = 0.\n    :type beta: float\n    :model_path: Path to load language model.\n    :type model_path: basestring\n    \"\"\"\n\n    def __init__(self, alpha, beta, model_path, vocabulary):\n        swig_decoders.Scorer.__init__(self, alpha, beta, model_path, vocabulary)\n\n\ndef ctc_greedy_decoder(probs_seq, vocabulary):\n    \"\"\"Wrapper for ctc best path decoder in swig.\n\n    :param probs_seq: 2-D list of probability distributions over each time\n                      step, with each element being a list of normalized\n                      probabilities over vocabulary and blank.\n    :type probs_seq: 2-D list\n    :param vocabulary: Vocabulary list.\n    :type vocabulary: list\n    :return: Decoding result string.\n    :rtype: basestring\n    \"\"\"\n    return swig_decoders.ctc_greedy_decoder(probs_seq.tolist(), vocabulary)\n\n\ndef ctc_beam_search_decoder(probs_seq,\n                            vocabulary,\n                            beam_size,\n                            cutoff_prob=1.0,\n                            cutoff_top_n=40,\n                            ext_scoring_func=None):\n    \"\"\"Wrapper for the CTC Beam Search Decoder.\n\n    :param probs_seq: 2-D list of probability distributions over each time\n                      step, with each element being a list of normalized\n                      probabilities over vocabulary and blank.\n    :type probs_seq: 2-D list\n    :param vocabulary: Vocabulary list.\n    :type vocabulary: list\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 ext_scoring_func: External scoring function for\n                             partially decoded sentence, e.g. word count\n                             or language model.\n    :type external_scoring_func: callable\n    :return: List of tuples of log probability and sentence as decoding\n             results, in descending order of the probability.\n    :rtype: list\n    \"\"\"\n    return swig_decoders.ctc_beam_search_decoder(probs_seq.tolist(), vocabulary,\n                                                 beam_size, cutoff_prob,\n                                                 cutoff_top_n, ext_scoring_func)\n\n\ndef ctc_beam_search_decoder_batch(probs_split,\n                                  vocabulary,\n                                  beam_size,\n                                  num_processes,\n                                  cutoff_prob=1.0,\n                                  cutoff_top_n=40,\n                                  ext_scoring_func=None):\n    \"\"\"Wrapper for the batched CTC beam search decoder.\n\n    :param probs_seq: 3-D list with each element as an instance of 2-D list\n                      of probabilities used by ctc_beam_search_decoder().\n    :type probs_seq: 3-D list\n    :param vocabulary: Vocabulary list.\n    :type vocabulary: list\n    :param beam_size: Width for beam search.\n    :type beam_size: int\n    :param num_processes: Number of parallel processes.\n    :type num_processes: int\n    :param cutoff_prob: Cutoff probability in vocabulary 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 num_processes: Number of parallel processes.\n    :type num_processes: int\n    :param ext_scoring_func: External scoring function for\n                             partially decoded sentence, e.g. word count\n                             or language model.\n    :type external_scoring_function: callable\n    :return: List of tuples of log probability and sentence as decoding\n             results, in descending order of the probability.\n    :rtype: list\n    \"\"\"\n    probs_split = [probs_seq.tolist() for probs_seq in probs_split]\n\n    return swig_decoders.ctc_beam_search_decoder_batch(\n        probs_split, vocabulary, beam_size, num_processes, cutoff_prob,\n        cutoff_top_n, ext_scoring_func)\n", "path": "deep_speech_2/decoders/swig_wrapper.py"}], "after_files": [{"content": "\"\"\"Trainer for DeepSpeech2 model.\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport argparse\nimport functools\nimport paddle.v2 as paddle\nfrom model_utils.model import DeepSpeech2Model\nfrom data_utils.data import DataGenerator\nfrom utils.utility import add_arguments, print_arguments\n\nparser = argparse.ArgumentParser(description=__doc__)\nadd_arg = functools.partial(add_arguments, argparser=parser)\n# yapf: disable\nadd_arg('batch_size',       int,    256,    \"Minibatch size.\")\nadd_arg('trainer_count',    int,    8,      \"# of Trainers (CPUs or GPUs).\")\nadd_arg('num_passes',       int,    200,    \"# of training epochs.\")\nadd_arg('num_proc_data',    int,    12,     \"# of CPUs for data preprocessing.\")\nadd_arg('num_conv_layers',  int,    2,      \"# of convolution layers.\")\nadd_arg('num_rnn_layers',   int,    3,      \"# of recurrent layers.\")\nadd_arg('rnn_layer_size',   int,    2048,   \"# of recurrent cells per layer.\")\nadd_arg('num_iter_print',   int,    100,    \"Every # iterations for printing \"\n                                            \"train cost.\")\nadd_arg('learning_rate',    float,  5e-4,   \"Learning rate.\")\nadd_arg('max_duration',     float,  27.0,   \"Longest audio duration allowed.\")\nadd_arg('min_duration',     float,  0.0,    \"Shortest audio duration allowed.\")\nadd_arg('test_off',         bool,   False,  \"Turn off testing.\")\nadd_arg('use_sortagrad',    bool,   True,   \"Use SortaGrad or not.\")\nadd_arg('use_gpu',          bool,   True,   \"Use GPU or not.\")\nadd_arg('use_gru',          bool,   False,  \"Use GRUs instead of simple RNNs.\")\nadd_arg('is_local',         bool,   True,   \"Use pserver or not.\")\nadd_arg('share_rnn_weights',bool,   True,   \"Share input-hidden weights across \"\n                                            \"bi-directional RNNs. Not for GRU.\")\nadd_arg('train_manifest',   str,\n        'data/librispeech/manifest.train',\n        \"Filepath of train manifest.\")\nadd_arg('dev_manifest',     str,\n        'data/librispeech/manifest.dev-clean',\n        \"Filepath of validation manifest.\")\nadd_arg('mean_std_path',    str,\n        'data/librispeech/mean_std.npz',\n        \"Filepath of normalizer's mean & std.\")\nadd_arg('vocab_path',       str,\n        'data/librispeech/vocab.txt',\n        \"Filepath of vocabulary.\")\nadd_arg('init_model_path',  str,\n        None,\n        \"If None, the training starts from scratch, \"\n        \"otherwise, it resumes from the pre-trained model.\")\nadd_arg('output_model_dir', str,\n        \"./checkpoints/libri\",\n        \"Directory for saving checkpoints.\")\nadd_arg('augment_conf_path',str,\n        'conf/augmentation.config',\n        \"Filepath of augmentation configuration file (json-format).\")\nadd_arg('specgram_type',    str,\n        'linear',\n        \"Audio feature type. Options: linear, mfcc.\",\n        choices=['linear', 'mfcc'])\nadd_arg('shuffle_method',   str,\n        'batch_shuffle_clipped',\n        \"Shuffle method.\",\n        choices=['instance_shuffle', 'batch_shuffle', 'batch_shuffle_clipped'])\n# yapf: disable\nargs = parser.parse_args()\n\n\ndef train():\n    \"\"\"DeepSpeech2 training.\"\"\"\n    train_generator = DataGenerator(\n        vocab_filepath=args.vocab_path,\n        mean_std_filepath=args.mean_std_path,\n        augmentation_config=open(args.augment_conf_path, 'r').read(),\n        max_duration=args.max_duration,\n        min_duration=args.min_duration,\n        specgram_type=args.specgram_type,\n        num_threads=args.num_proc_data)\n    dev_generator = DataGenerator(\n        vocab_filepath=args.vocab_path,\n        mean_std_filepath=args.mean_std_path,\n        augmentation_config=\"{}\",\n        specgram_type=args.specgram_type,\n        num_threads=args.num_proc_data)\n    train_batch_reader = train_generator.batch_reader_creator(\n        manifest_path=args.train_manifest,\n        batch_size=args.batch_size,\n        min_batch_size=args.trainer_count,\n        sortagrad=args.use_sortagrad if args.init_model_path is None else False,\n        shuffle_method=args.shuffle_method)\n    dev_batch_reader = dev_generator.batch_reader_creator(\n        manifest_path=args.dev_manifest,\n        batch_size=args.batch_size,\n        min_batch_size=1,  # must be 1, but will have errors.\n        sortagrad=False,\n        shuffle_method=None)\n\n    ds2_model = DeepSpeech2Model(\n        vocab_size=train_generator.vocab_size,\n        num_conv_layers=args.num_conv_layers,\n        num_rnn_layers=args.num_rnn_layers,\n        rnn_layer_size=args.rnn_layer_size,\n        use_gru=args.use_gru,\n        pretrained_model_path=args.init_model_path,\n        share_rnn_weights=args.share_rnn_weights)\n    ds2_model.train(\n        train_batch_reader=train_batch_reader,\n        dev_batch_reader=dev_batch_reader,\n        feeding_dict=train_generator.feeding,\n        learning_rate=args.learning_rate,\n        gradient_clipping=400,\n        num_passes=args.num_passes,\n        num_iterations_print=args.num_iter_print,\n        output_model_dir=args.output_model_dir,\n        is_local=args.is_local,\n        test_off=args.test_off)\n\n\ndef main():\n    print_arguments(args)\n    paddle.init(use_gpu=args.use_gpu,\n                trainer_count=args.trainer_count,\n                log_clipping=True)\n    train()\n\n\nif __name__ == '__main__':\n    main()\n", "path": "deep_speech_2/train.py"}, {"content": "\"\"\"Wrapper for various CTC decoders in SWIG.\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport swig_decoders\n\n\nclass Scorer(swig_decoders.Scorer):\n    \"\"\"Wrapper for Scorer.\n\n    :param alpha: Parameter associated with language model. Don't use\n                  language model when alpha = 0.\n    :type alpha: float\n    :param beta: Parameter associated with word count. Don't use word\n                 count when beta = 0.\n    :type beta: float\n    :model_path: Path to load language model.\n    :type model_path: basestring\n    \"\"\"\n\n    def __init__(self, alpha, beta, model_path, vocabulary):\n        swig_decoders.Scorer.__init__(self, alpha, beta, model_path, vocabulary)\n\n\ndef ctc_greedy_decoder(probs_seq, vocabulary):\n    \"\"\"Wrapper for ctc best path decoder in swig.\n\n    :param probs_seq: 2-D list of probability distributions over each time\n                      step, with each element being a list of normalized\n                      probabilities over vocabulary and blank.\n    :type probs_seq: 2-D list\n    :param vocabulary: Vocabulary list.\n    :type vocabulary: list\n    :return: Decoding result string.\n    :rtype: basestring\n    \"\"\"\n    result = swig_decoders.ctc_greedy_decoder(probs_seq.tolist(), vocabulary)\n    return result.decode('utf-8')\n\n\ndef ctc_beam_search_decoder(probs_seq,\n                            vocabulary,\n                            beam_size,\n                            cutoff_prob=1.0,\n                            cutoff_top_n=40,\n                            ext_scoring_func=None):\n    \"\"\"Wrapper for the CTC Beam Search Decoder.\n\n    :param probs_seq: 2-D list of probability distributions over each time\n                      step, with each element being a list of normalized\n                      probabilities over vocabulary and blank.\n    :type probs_seq: 2-D list\n    :param vocabulary: Vocabulary list.\n    :type vocabulary: list\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 ext_scoring_func: External scoring function for\n                             partially decoded sentence, e.g. word count\n                             or language model.\n    :type external_scoring_func: callable\n    :return: List of tuples of log probability and sentence as decoding\n             results, in descending order of the probability.\n    :rtype: list\n    \"\"\"\n    beam_results = swig_decoders.ctc_beam_search_decoder(\n        probs_seq.tolist(), vocabulary, beam_size, cutoff_prob, cutoff_top_n,\n        ext_scoring_func)\n    beam_results = [(res[0], res[1].decode('utf-8')) for res in beam_results]\n    return beam_results\n\n\ndef ctc_beam_search_decoder_batch(probs_split,\n                                  vocabulary,\n                                  beam_size,\n                                  num_processes,\n                                  cutoff_prob=1.0,\n                                  cutoff_top_n=40,\n                                  ext_scoring_func=None):\n    \"\"\"Wrapper for the batched CTC beam search decoder.\n\n    :param probs_seq: 3-D list with each element as an instance of 2-D list\n                      of probabilities used by ctc_beam_search_decoder().\n    :type probs_seq: 3-D list\n    :param vocabulary: Vocabulary list.\n    :type vocabulary: list\n    :param beam_size: Width for beam search.\n    :type beam_size: int\n    :param num_processes: Number of parallel processes.\n    :type num_processes: int\n    :param cutoff_prob: Cutoff probability in vocabulary 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 num_processes: Number of parallel processes.\n    :type num_processes: int\n    :param ext_scoring_func: External scoring function for\n                             partially decoded sentence, e.g. word count\n                             or language model.\n    :type external_scoring_function: callable\n    :return: List of tuples of log probability and sentence as decoding\n             results, in descending order of the probability.\n    :rtype: list\n    \"\"\"\n    probs_split = [probs_seq.tolist() for probs_seq in probs_split]\n\n    batch_beam_results = swig_decoders.ctc_beam_search_decoder_batch(\n        probs_split, vocabulary, beam_size, num_processes, cutoff_prob,\n        cutoff_top_n, ext_scoring_func)\n    batch_beam_results = [\n        [(res[0], res[1].decode(\"utf-8\")) for res in beam_results]\n        for beam_results in batch_beam_results\n    ]\n    return batch_beam_results\n", "path": "deep_speech_2/decoders/swig_wrapper.py"}]}