目录

torchaudio.models.decoder

解码器类

CTCDecoder

class torchaudio.models.decoder.CTCDecoder(nbest: int, lexicon: Optional[Dict], word_dict: torchaudio._torchaudio_decoder._Dictionary, tokens_dict: torchaudio._torchaudio_decoder._Dictionary, lm: torchaudio._torchaudio_decoder._LM, decoder_options: Union[torchaudio._torchaudio_decoder._LexiconDecoderOptions, torchaudio._torchaudio_decoder._LexiconFreeDecoderOptions], blank_token: str, sil_token: str, unk_word: str)[source]
This feature supports the following devices: CPU

来自 Flashlight 的 CTC 束搜索解码器 [1]。

注意

要构建解码器,请使用工厂函数 ctc_decoder()

Parameters
  • nbest (int) – 要返回的最佳解码数量

  • lexicon (DictNone) – 单词到拼写的词典映射,或 None 表示无词典解码器

  • word_dict (_Dictionary) – 单词字典

  • tokens_dict (_Dictionary) – 标记字典

  • lm (_LM) – 语言模型

  • decoder_options (_LexiconDecoderOptions_LexiconFreeDecoderOptions) – 用于束搜索解码的参数

  • blank_token (str) – 对应空格的标记

  • sil_token (str) – 对应静音的 token

  • unk_word (str) – 对应未知词的单词

Tutorials using CTCDecoder:
__call__(self, emissions: torch.FloatTensor, lengths: Optional[torch.Tensor] = None)List[List[torchaudio.models.decoder.CTCHypothesis]][source]
Parameters
  • emissions (torch.FloatTensor) – 形状为 (batch, frame, num_tokens) 的 CPU 张量,存储标签上的概率分布序列;声学模型的输出。

  • lengths (TensorNone, 可选) – 形状为 (batch, ) 的 CPU 张量,用于存储每个批次中输出张量在时间轴上的有效长度。

Returns

批次中每个音频序列的排序最佳假设列表。

Return type

List[List[CTCHypothesis]]

idxs_to_tokens(idxs: torch.LongTensor)List[source]

将原始 token ID 映射到对应的 token

Parameters

idxs (LongTensor) – 由解码器生成的原始令牌 ID

Returns

与输入 ID 对应的标记

Return type

列表

CTCHypothesis

class torchaudio.models.decoder.CTCHypothesis(tokens: torch.LongTensor, words: List[str], score: float, timesteps: torch.IntTensor)[source]

表示由 CTC 束搜索解码器生成的假设 CTCDecoder()

Variables
  • tokens (torch.LongTensor) – 预测的 token ID 序列。形状为 (L, ),其中 L 是输出序列的长度

  • words (List[str]) – 预测单词列表

  • score (float) – 对应假设的分数

  • timesteps (torch.IntTensor) – 与 token 对应的时间步。形状为 (L, ), 其中 L 是输出序列的长度

Tutorials using CTCHypothesis:

工厂函数

ctc_decoder

class torchaudio.models.decoder.ctc_decoder(lexicon: Optional[str], tokens: Union[str, List[str]], lm: Optional[str] = None, nbest: int = 1, beam_size: int = 50, beam_size_token: Optional[int] = None, beam_threshold: float = 50, lm_weight: float = 2, word_score: float = 0, unk_score: float = - inf, sil_score: float = 0, log_add: bool = False, blank_token: str = '-', sil_token: str = '|', unk_word: str = '<unk>')[source]

Flashlight [1] 构建 CTC 束搜索解码器。

Parameters
  • lexicon (strNone) – 包含可能单词及其对应拼写的词典文件。 每行由一个单词及其空格分隔的拼写组成。如果为 None,则使用无词典解码。

  • tokens (str or List[str]) – 包含有效标记的文件或列表。如果使用文件,期望的格式是将映射到同一索引的标记放在同一行

  • lm (strNone, 可选) – 包含语言模型的文件,或者如果不使用语言模型则为 None

  • nbest (int, optional) – 要返回的最佳解码数量(默认值:1)

  • beam_size (int, optional) – 每个解码步骤后保留的最大假设数量(默认值:50)

  • beam_size_token (int, optional) – 在每一步解码中考虑的最大token数量。 如果为 None,则设置为token的总数(默认值:None)

  • beam_threshold (float, optional) – 用于剪枝假设的阈值(默认值:50)

  • lm_weight (float, optional) – 语言模型的权重(默认值:2)

  • word_score (float, optional) – 词插入分数(默认值:0)

  • unk_score (float, optional) – 未知词插入分数(默认值:-inf)

  • sil_score (float, optional) – 静音插入分数(默认值:0)

  • log_add (bool, optional) – 在合并假设时是否使用 logadd(默认值:False)

  • blank_token (str, optional) – 对应空白符的标记(默认值:“-“)

  • sil_token (str, optional) – 对应静音的 token(默认值:“|”)

  • unk_word (str, optional) – 对应未知词的单词(默认值:“<unk>”)

Returns

解码器

Return type

CTCDecoder

Example
>>> decoder = ctc_decoder(
>>>     lexicon="lexicon.txt",
>>>     tokens="tokens.txt",
>>>     lm="kenlm.bin",
>>> )
>>> results = decoder(emissions) # List of shape (B, nbest) of Hypotheses
Tutorials using ctc_decoder:

实用函数

download_pretrained_files

class torchaudio.models.decoder.download_pretrained_files(model: str)[source]

检索用于 CTC 解码器的预训练数据文件。

Parameters

model (str) – 要下载的预训练语言模型。 选项:[“librispeech-3-gram”, “librispeech-4-gram”, “librispeech”]

Returns

Object with the following attributes
lm:

对应于已下载语言模型的路径,或者如果该模型未与 lm 关联则为 None

lexicon:

对应于已下载词典文件的路径

tokens:

对应于已下载令牌文件的路径

Tutorials using download_pretrained_files:

参考文献

1(1,2)

Jacob Kahn, Vineel Pratap, Tatiana Likhomanenko, Qiantong Xu, Awni Hannun, Jeff Cai, Paden Tomasello, Ann Lee, Edouard Grave, Gilad Avidov, 和其他人员。Flashlight:推动机器学习工具的创新。arXiv预印本 arXiv:2201.12465, 2022.

文档

访问 PyTorch 的全面开发人员文档

查看文档

教程

获取面向初学者和高级开发人员的深入教程

查看教程

资源

查找开发资源并解答您的问题

查看资源