o
    ei                     @   s.   d Z ddlZddlZedjZdddZdS )z
# Authors:
 * Szu-Wei, Fu 2021
 * Mirco Ravanelli 2020
 * Samuele Cornell 2020
 * Hwidong Na 2020
 * Yan Gao 2020
 * Titouan Parcollet 2020
    Nfloatmeanc                 C   s  t j| dd} t j|dd}| jd }t |}td|D ]c}||dt|| | jd  f }| |dt|| | jd  f }t j|| ddd}	t j|d dddt }
|	| |
 }|| }t j|d ddt j|d ddt  }dt |t  ||< q|d	kr|	  S | S )
a  Compute the si_snr score and return -1 * that score.

    This function can be used as a loss function for training
    with SGD-based updates.

    Arguments
    ---------
    y_pred_batch : torch.Tensor
        The degraded (enhanced) waveforms.
    y_true_batch : torch.Tensor
        The clean (reference) waveforms.
    lens : torch.Tensor
        The relative lengths of the waveforms within the batch.
    reduction : str
        The type of reduction ("mean" or "batch") to use.

    Returns
    -------
    Computed si_snr loss.
    )dimr      T)r   keepdim   
   r   )
torchsqueezeshapezerosrangeintsumsmallVallog10r   )y_pred_batchy_true_batchlens	reduction
batch_sizeSI_SNRis_target
s_estimatedots_target_energyproje_noisesi_snr_beforelog r!   _/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/speechbrain/nnet/loss/si_snr_loss.pysi_snr_loss   s$   

""
r#   )r   )__doc__numpynpr
   finfoepsr   r#   r!   r!   r!   r"   <module>   s
    
