o
    ei                     @   sr   d Z ddlZddlm  mZ ddlmZ eeZ	G dd dejj
ZG dd dejj
ZG dd	 d	ejj
ZdS )
zcLibrary implementing activation functions.

Authors
 * Mirco Ravanelli 2020
 * Jianyuan Zhong 2020
    N)
get_loggerc                       s4   e Zd ZdZdddejf fdd	Zdd Z  ZS )	Softmaxa  Computes the softmax of a 2d, 3d, or 4d input tensor.

    Arguments
    ---------
    apply_log : bool
        Whether to apply the log function before softmax.
    dim : int
        If the dimension where softmax is applied.
    reshape: bool
        whether to apply reshaping (true by default)
    dtype: torch.dtype
        dtype of the output tensor

    Example
    -------
    >>> classifier = Softmax()
    >>> inputs = torch.rand(10, 50, 40)
    >>> output = classifier(inputs)
    >>> output.shape
    torch.Size([10, 50, 40])
    FTc                    s6   t    |rtj| _ntj| _|| _|| _|| _d S N)	super__init__Flog_softmaxactsoftmaxdimreshapedtype)self	apply_logr   r   r   	__class__ Z/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/speechbrain/nnet/activations.pyr   '   s   


zSoftmax.__init__c                 C   s   |j }| jr1t|dkr||d |d  |d }t|dkr1||d |d  |d |d }| j|| j| jd}| jrgt|dkrQ||d |d |d }t|dkrg||d |d |d |d }|S )zReturns the softmax of the input tensor.

        Arguments
        ---------
        x : torch.Tensor
            Input tensor.

        Returns
        -------
        x_act : torch.Tensor
            The softmax outputs.
           r            )r   r   )shaper   lenr
   r   r   )r   xdimsx_actr   r   r   forward5   s   " zSoftmax.forward)	__name__
__module____qualname____doc__torchfloat32r   r   __classcell__r   r   r   r   r      s
    
r   c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )GumbelSoftmaxaZ  Samples from the Gumbel-Softmax distribution and optionally discretizes.

    Reference: https://arxiv.org/abs/1611.00712, https://arxiv.org/abs/1611.01144

    Arguments
    ---------
    tau: float
        non-negative scalar temperature
    hard: bool
        if True, the returned samples will be discretized as one-hot vectors, but will be differentiated as if it is the soft sample in autograd
    apply_log: bool
        if True, returns the log of the softmax outputs.

    Example
    -------
    >>> x = torch.randn((8, 40, 120))
    >>> act = GumbelSoftmax(0.8, True)
    >>> x = act(x)
    Fc                    s    t    || _|| _|| _d S r   )r   r   tauhardr   )r   r'   r(   r   r   r   r   r   n   s   

zGumbelSoftmax.__init__c                 C   s4   | j rttj|| j| jdS tj|| j| jdS )zReturns the Gumbel softmax of the input tensor.

        Arguments
        ---------
        x : torch.Tensor
            Input tensor.

        Returns
        -------
        The Gumbel softmax output.
        )r'   r(   )r   r#   logr   gumbel_softmaxr'   r(   r   r   r   r   r   r   t   s   zGumbelSoftmax.forward)FF)r   r    r!   r"   r   r   r%   r   r   r   r   r&   Y   s    r&   c                       s0   e Zd ZdZddef fddZdd Z  ZS )	SwishaE  The class implements the Swish activation function from
    https://arxiv.org/pdf/2005.03191.pdf

    given input x. Swish(x) = x / (1 + exp(beta * x))

    Arguments
    ---------
    beta: float
        Beta value.

    Example
    -------
    >>> x = torch.randn((8, 40, 120))
    >>> act = Swish()
    >>> x = act(x)
          ?betac                    s    t    || _tj | _d S r   )r   r   r.   r#   nnSiLUsilu)r   r.   r   r   r   r      s   
zSwish.__init__c                 C   s   | j dkr
|| j  }| |S )zReturns the Swished input tensor.

        Arguments
        ---------
        x : torch.Tensor
            Input tensor.

        Returns
        -------
        The swished output.
        r   )r.   r1   r+   r   r   r   r      s   


zSwish.forward)r-   )r   r    r!   r"   floatr   r   r%   r   r   r   r   r,      s    r,   )r"   r#   torch.nn.functionalr/   
functionalr   speechbrain.utils.loggerr   r   loggerModuler   r&   r,   r   r   r   r   <module>   s    I,