speechbrain.nnet.embedding 模块

实现嵌入的库。

Authors
  • 阿卜杜勒瓦哈布·赫巴 2020

摘要

类:

Embedding

计算嵌入 x = wx。

参考

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

基础:Module

计算嵌入 x = wx。

Parameters:
  • num_embeddings (int) – 嵌入字典的大小。

  • embedding_dim (int) – 这是嵌入的维度(即输出的维度)。

  • consider_as_one_hot (bool) – 创建不可训练的一热向量。

  • blank_id (int) – 如果 consider_as_one_hot == True:将嵌入视为 one_hot 并使用 blank_index 作为零 one_hot 向量。

Example

>>> 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]

返回输入张量的嵌入。

Parameters:

x (torch.Tensor) – 输入的嵌入。

Return type:

嵌入的输出。