o
    ei                     @   sB   d Z ddlZddlmZ ddlmZ eeZG dd dejZ	dS )zALibrary implementing embedding.

Authors
 * Abdelwahab Heba 2020
    N)
get_loggerc                       s0   e Zd ZdZ			d	 fdd	Zdd Z  ZS )
	Embeddinga  Computes an embedding x = wx.

    Arguments
    ---------
    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])
       Fr   c                    s   t    || _|| _| jr| jd | _n|| _|| _| jrdtj| j| j| jd| _t	| j}| jd | jkrI|| jd  | jj
j| jd d < | jdkr]|d | j | jj
jd | j< d| jj
_d S t| j| j| _d S )N   )padding_idxr   F)super__init__num_embeddingsconsider_as_one_hotembedding_dimblank_idnnr   torcheyeweightdatarequires_grad)selfr	   r   r
   r   one_hot	__class__ X/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/speechbrain/nnet/embedding.pyr   B   s4   


zEmbedding.__init__c                 C   s   |  | S )zReturns the embedding of input tensor.

        Arguments
        ---------
        x : torch.Tensor
           Input to embed.

        Returns
        -------
        The embedded outputs.
        )r   long)r   xr   r   r   forwardi   s   zEmbedding.forward)r   Fr   )__name__
__module____qualname____doc__r   r   __classcell__r   r   r   r   r      s    5'r   )
r   r   torch.nnr   speechbrain.utils.loggerr   r   loggerModuler   r   r   r   r   <module>   s    