o
    åÊi½  ã                   @   sP   d dl Z d dl mZmZmZ d dlmZ G dd„ dejƒZG dd„ dejƒZdS )é    N)ÚnnÚsinÚpow)Ú	Parameterc                       ó,   e Zd ZdZ	d	‡ fdd„	Zdd„ Z‡  ZS )
ÚSnakeaÓ  
    Implementation of a sine-based periodic activation function
    Shape:
        - Input: (B, C, T)
        - Output: (B, C, T), same shape as the input
    Parameters:
        - alpha - trainable parameter
    References:
        - This activation function is from this paper by Liu Ziyin, Tilman Hartwig, Masahito Ueda:
        https://arxiv.org/abs/2006.08195
    Examples:
        >>> a1 = snake(256)
        >>> x = torch.randn(256)
        >>> x = a1(x)
    ç      ð?TFc                    s\   t t| ƒ ¡  || _|| _| jrtt |¡| ƒ| _n
tt 	|¡| ƒ| _|| j_
d| _dS )a   
        Initialization.
        INPUT:
            - in_features: shape of the input
            - alpha: trainable parameter
            alpha is initialized to 1 by default, higher values = higher-frequency.
            alpha will be trained along with the rest of your model.
        ç•Ö&è.>N)Úsuperr   Ú__init__Úin_featuresÚalpha_logscaler   ÚtorchÚzerosÚalphaÚonesÚrequires_gradÚno_div_by_zero©Úselfr   r   Úalpha_trainabler   ©Ú	__class__© úH/home/ubuntu/.local/lib/python3.10/site-packages/neucodec/activations.pyr      s   
zSnake.__init__c                 C   sJ   | j  d¡ d¡}| jrt |¡}|d|| j  tt|| ƒdƒ  }|S )uŽ   
        Forward pass of the function.
        Applies the function to the input elementwise.
        Snake âˆ¶= x + 1/a * sin^2 (xa)
        r   éÿÿÿÿr   é   )r   Ú	unsqueezer   r   Úexpr   r   r   )r   Úxr   r   r   r   Úforward3   s
   
$zSnake.forward©r   TF©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    Ú__classcell__r   r   r   r   r   	   s
    ÿr   c                       r   )
Ú	SnakeBetaam  
    A modified Snake function which uses separate parameters for the magnitude of the periodic components
    Shape:
        - Input: (B, C, T)
        - Output: (B, C, T), same shape as the input
    Parameters:
        - alpha - trainable parameter that controls frequency
        - beta - trainable parameter that controls magnitude
    References:
        - This activation function is a modified version based on this paper by Liu Ziyin, Tilman Hartwig, Masahito Ueda:
        https://arxiv.org/abs/2006.08195
    Examples:
        >>> a1 = snakebeta(256)
        >>> x = torch.randn(256)
        >>> x = a1(x)
    r   TFc                    sŒ   t t| ƒ ¡  || _|| _| jr%tt |¡| ƒ| _tt |¡| ƒ| _	ntt 
|¡| ƒ| _tt 
|¡| ƒ| _	|| j_|| j	_d| _dS )aÍ  
        Initialization.
        INPUT:
            - in_features: shape of the input
            - alpha - trainable parameter that controls frequency
            - beta - trainable parameter that controls magnitude
            alpha is initialized to 1 by default, higher values = higher-frequency.
            beta is initialized to 1 by default, higher values = higher-magnitude.
            alpha will be trained along with the rest of your model.
        r	   N)r
   r(   r   r   r   r   r   r   r   Úbetar   r   r   r   r   r   r   r   S   s   
zSnakeBeta.__init__c                 C   sf   | j  d¡ d¡}| j d¡ d¡}| jrt |¡}t |¡}|d|| j  tt|| ƒdƒ  }|S )u’   
        Forward pass of the function.
        Applies the function to the input elementwise.
        SnakeBeta âˆ¶= x + 1/b * sin^2 (xa)
        r   r   r   r   )	r   r   r)   r   r   r   r   r   r   )r   r   r   r)   r   r   r   r    q   s   

$zSnakeBeta.forwardr!   r"   r   r   r   r   r(   A   s
    ÿr(   )	r   r   r   r   Útorch.nnr   ÚModuler   r(   r   r   r   r   Ú<module>   s
   8