speechbrain.nnet.embedding module

Library implementing embedding.

Authors
  • Abdelwahab Heba 2020

Summary

Classes:

Embedding

Computes an embedding x = wx.

Reference

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

Bases: torch.nn.modules.module.Module

Computes an embedding x = wx.

Parameters
  • num_embeddings (int) – Size of the dictionary of embeddings.

  • embedding_dim (int) – It is the dim of embedding (i.e, the dimensionality of the output).

  • consider_as_one_hot (bool) – Create non-trainable one-hot vector.

  • blank_id (int) – If consider_as_one_hot == True: consider the embedding as one_hot and use blank_index as zero one_hot vector.

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])
training: bool
forward(x)[source]

Returns the embedding of input tensor.

Parameters

x (torch.Tensor) – Input to embed.