CTCDecoder
1.0.0
更新2021:可安装的Python软件包
Python实现了一些共同的连接派时间分类(CTC)解码算法。提供了简约的语言模型。
pip install .tests/执行pytest以检查安装是否有效这是一个简约的可执行示例:
import numpy as np
from ctc_decoder import best_path , beam_search
mat = np . array ([[ 0.4 , 0 , 0.6 ], [ 0.4 , 0 , 0.6 ]])
chars = 'ab'
print ( f'Best path: " { best_path ( mat , chars ) } "' )
print ( f'Beam search: " { beam_search ( mat , chars ) } "' ) CTC训练的神经网络的输出mat (NUMPY阵列,已应用的SoftMax)有望具有Shape TXC,并将第一个参数传递给解码器。 t是时间步长的数量,c字符数(CTC-Blank是最后一个元素)。神经网络可以预测的字符作为chars传递给解码器。解码器返回解码的字符串。
运行代码输出:
Best path: ""
Beam search: "a"
要查看有关如何使用解码器的更多示例,请查看tests/文件夹中的脚本。
光束搜索可以选择整合字符级的语言模型。 Beam搜索使用文本统计(Bigrams)来提高阅读精度。
from ctc_decoder import beam_search , LanguageModel
# create language model instance from a (large) text
lm = LanguageModel ( 'this is some text' , chars )
# and use it in the beam search decoder
res = beam_search ( mat , chars , lm = lm )词典搜索解码器以最佳路径解码计算第一个近似值。然后,它使用BK-Tree检索类似的单词,得分并最终返回最佳得分单词。通过提供字典单词列表来创建BK-TREE。公差参数定义了从查询单词到返回的字典单词的最大编辑距离。
from ctc_decoder import lexicon_search , BKTree
# create BK-tree from a list of words
bk_tree = BKTree ([ 'words' , 'from' , 'a' , 'dictionary' ])
# and use the tree in the lexicon search
res = lexicon_search ( mat , chars , bk_tree , tolerance = 2 )一些注释:
rnn_outputmat = rnn_output[:, 0, :]推荐的解码器:
best_path :最佳路径(或贪婪)解码器,所有算法中最快的解码器,但是,其他解码器的表现通常更好beam_search :光束搜索解码器,可选地集成了字符级的语言模型,可以通过梁宽度参数调整lexicon_search :词典搜索解码器,返回词典中的最佳评分单词根据我的经验,其他解码器并不真正适合实际目的,而是用于实验或研究:
prefix_search :前缀搜索解码器token_passing :令牌传递算法extras/文件夹)本文提供了何时使用最佳路径解码,梁搜索解码和令牌传递的建议。