U
    %‰dÂ
  ã                   @   s:   d dl Z dgZG dd„ de jjƒZG dd„ de jjƒZdS )é    NÚ
DeepSpeechc                       sD   e Zd ZdZd
eeeeddœ‡ fdd„Zejejdœdd	„Z	‡  Z
S )ÚFullyConnectedzh
    Args:
        n_feature: Number of input features
        n_hidden: Internal hidden unit size.
    é   N)Ú	n_featureÚn_hiddenÚdropoutÚrelu_max_clipÚreturnc                    s2   t t| ƒ ¡  tjj||dd| _|| _|| _d S )NT)Zbias)	Úsuperr   Ú__init__ÚtorchÚnnÚLinearÚfcr   r   )Úselfr   r   r   r   ©Ú	__class__© ú@/tmp/pip-unpacked-wheel-lbdmvq91/torchaudio/models/deepspeech.pyr      s    zFullyConnected.__init__©Úxr	   c                 C   sL   |   |¡}tjj |¡}tjj |d| j¡}| jrHtjj || j| j¡}|S )Nr   )	r   r   r   Ú
functionalÚreluZhardtanhr   r   Ztraining)r   r   r   r   r   Úforward   s    
zFullyConnected.forward)r   ©Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚintÚfloatr   r   ZTensorr   Ú__classcell__r   r   r   r   r      s   r   c                       sD   e Zd ZdZdeeeeddœ‡ fdd„Zejejd	œd
d„Z	‡  Z
S )r   a  
    DeepSpeech model architecture from *Deep Speech: Scaling up end-to-end speech recognition*
    [:footcite:`hannun2014deep`].

    Args:
        n_feature: Number of input features
        n_hidden: Internal hidden unit size.
        n_class: Number of output classes
    é   é(   ç        N)r   r   Ún_classr   r	   c                    sx   t t| ƒ ¡  || _t|||ƒ| _t|||ƒ| _t|||ƒ| _tj	j
||dddd| _t|||ƒ| _tj	 ||¡| _d S )Né   r   T)Z
num_layersZnonlinearityÚbidirectional)r
   r   r   r   r   Úfc1Úfc2Úfc3r   r   ZRNNÚbi_rnnÚfc4r   Úout)r   r   r   r%   r   r   r   r   r   '   s    zDeepSpeech.__init__r   c                 C   s²   |   |¡}|  |¡}|  |¡}| d¡}| dd¡}|  |¡\}}|dd…dd…d| j…f |dd…dd…| jd…f  }|  |¡}|  |¡}| 	ddd¡}t
jjj|dd}|S )zÃ
        Args:
            x (torch.Tensor): Tensor of dimension (batch, channel, time, feature).
        Returns:
            Tensor: Predictor tensor of dimension (batch, time, class).
        r&   r   Né   )Zdim)r(   r)   r*   ZsqueezeZ	transposer+   r   r,   r-   Zpermuter   r   r   Zlog_softmax)r   r   Ú_r   r   r   r   7   s    



8

zDeepSpeech.forward)r"   r#   r$   r   r   r   r   r   r      s      ûú)r   Ú__all__r   ÚModuler   r   r   r   r   r   Ú<module>   s   