speechbrain.wordemb.util 模块

用于词嵌入的工具

作者 * Artem Ploujnikov 2021

摘要

函数:

expand_to_chars

将词嵌入扩展到字符嵌入序列,为每个字符分配其所属词的词嵌入

参考

speechbrain.wordemb.util.expand_to_chars(emb, seq, seq_len, word_separator)[source]

将词嵌入扩展到字符嵌入序列,为每个字符分配其所属词的词嵌入

Parameters:
  • emb (torch.Tensor) – 一个词嵌入的张量

  • seq (torch.Tensor) – 一个字符嵌入的张量

  • seq_len (torch.Tensor) – 一个字符嵌入长度的张量

  • word_separator (torch.Tensor) – 正在使用的单词分隔符

Returns:

char_word_emb – 一个结合了字符和词嵌入的张量

Return type:

torch.Tensor

Example

>>> import torch
>>> emb = torch.tensor(
...     [[[1., 2., 3.],
...       [3., 1., 2.],
...       [0., 0., 0.]],
...      [[1., 3., 2.],
...       [3., 2., 1.],
...       [2., 3., 1.]]]
... )
>>> seq = torch.tensor(
...     [[1, 2, 0, 2, 1, 0],
...      [1, 0, 1, 2, 0, 2]]
... )
>>> seq_len = torch.tensor([4, 5])
>>> word_separator = 0
>>> expand_to_chars(emb, seq, seq_len, word_separator)
tensor([[[1., 2., 3.],
         [1., 2., 3.],
         [0., 0., 0.],
         [3., 1., 2.],
         [3., 1., 2.],
         [0., 0., 0.]],

        [[1., 3., 2.],
         [0., 0., 0.],
         [3., 2., 1.],
         [3., 2., 1.],
         [0., 0., 0.],
         [2., 3., 1.]]])