o
    i                     @   sN   d dl Z d dlZd dlmZ d dlmZ d dlZ	G dd dej
Zdd ZdS )    Nc                       s0   e Zd ZdZ fddZdd Zdd Z  ZS )CTCzChainer implementation of ctc layer.

    Args:
        odim (int): The output dimension.
        eprojs (int | None): Dimension of input vectors from encoder.
        dropout_rate (float): Dropout rate.

    c                    sV   t t|   || _d | _|   t||| _W d    d S 1 s$w   Y  d S )N)	superr   __init__dropout_rateloss
init_scopeLLinearctc_lo)selfodimeprojsr   	__class__ S/home/ubuntu/.local/lib/python3.10/site-packages/espnet/nets/chainer_backend/ctc.pyr      s   
"zCTC.__init__c           	      C   s   d| _ dd |D }dd |D }| jtjt|| jddd}tj|dd	}tj|d
d}t| j	j
|tjd}t| j	j
|tjd}t| jjd t|j  t| jjd t|j  t||d||| _ tdt| j j  | j S )a^  CTC forward.

        Args:
            hs (list of chainer.Variable | N-dimension array):
                Input variable from encoder.
            ys (list of chainer.Variable | N-dimension array):
                Input variable of decoder.

        Returns:
            chainer.Variable: A variable holding a scalar value of the CTC loss.

        Nc                 S      g | ]}|j d  qS r   shape.0xr   r   r   
<listcomp>)       z CTC.__call__.<locals>.<listcomp>c                 S   r   r   r   r   r   r   r   r   *   r   )ratio   n_batch_axes   )axis)padding)dtypez input lengths:  z output lengths: r   z	ctc loss:)r   r
   Fdropoutpad_sequencer   separatechainerVariablexparraynpint32logginginfor   __name__strdata%connectionist_temporal_classification)	r   hsysilensolensy_haty_trueinput_lengthlabel_lengthr   r   r   __call__   s*   
zCTC.__call__c                 C   s4   | j t|dd}t|d|jd |jS )zLog_softmax of frame activations.

        Args:
            hs (list of chainer.Variable | N-dimension array):
                Input variable from encoder.

        Returns:
            chainer.Variable: A n-dimension float array.

        r   r   r!   )r
   r$   r&   log_softmaxreshaper   )r   r4   r8   r   r   r   r=   G   s    zCTC.log_softmax)r0   
__module____qualname____doc__r   r<   r=   __classcell__r   r   r   r   r   	   s
    	,r   c                 C   s:   | j }|dkrtd t|| j| j}|S td|)zReturn the CTC layer corresponding to the args.

    Args:
        args (Namespace): The program arguments.
        odim (int): The output dimension.

    Returns:
        The CTC module.

    builtinz Using chainer CTC implementationzctc_type must be "builtin": {})ctc_typer.   r/   r   r   r   
ValueErrorformat)argsr   rD   ctcr   r   r   ctc_forV   s   
rI   )r.   r(   chainer.functions	functionsr$   chainer.linkslinksr   numpyr,   Chainr   rI   r   r   r   r   <module>   s    M