speechbrain.nnet.embedding 模块

实现 embedding 的库。

作者
  • Abdelwahab Heba 2020

摘要

Embedding

计算 embedding x = wx。

参考

class speechbrain.nnet.embedding.Embedding(num_embeddings, embedding_dim=128, consider_as_one_hot=False, blank_id=0)[source]

基类:Module

计算 embedding x = wx。

参数:
  • num_embeddings (int) – embedding 字典的大小。

  • embedding_dim (int) – embedding 的维度(即输出的维度)。

  • consider_as_one_hot (bool) – 创建不可训练的 one-hot 向量。

  • blank_id (int) – 如果 consider_as_one_hot == True:将 embedding 视为 one-hot,并使用 blank_index 作为零 one-hot 向量。

示例

>>> from speechbrain.nnet.embedding import Embedding
>>> import torch
>>> emb = Embedding(
...     num_embeddings=40,
...     embedding_dim=39,
...     consider_as_one_hot=True,
...     blank_id=39
... )
>>> inputs = torch.Tensor([10,5,2,0,39]).long()
>>> output = emb(inputs)
>>> output.shape
torch.Size([5, 39])
>>> output
tensor([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0.],
        [0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0.],
        [0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0.],
        [1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0.]])
>>> emb = Embedding(num_embeddings=5, embedding_dim=3, consider_as_one_hot=False)
>>> e = emb(torch.LongTensor([[0, 1, 2], [3, 4, 2]]))
>>> e.shape
torch.Size([2, 3, 3])
forward(x)[source]

返回输入张量的 embedding。

参数:

x (torch.Tensor) – 要进行 embedding 的输入。

返回类型:

embedding 后的输出。