
     i                        d Z ddlmZ ddlZddlmc mZ ddej                  deej                     fdZ		 ddej                  dej                  deej                     dej                  fd	Z
	 ddej                  dej                  deej                     dej                  fd
Z	 	 ddej                  dej                  deej                     deej                     dej                  f
dZy)z0Frame-weighted versions of common loss functions    )OptionalNtargetweightc                     | j                   d   }|K|j                   d   |k7  r9t        j                  |j                  dd      |dd      j                  dd      }|S )a  Interpolate weight to match target frame resolution

    Parameters
    ----------
    target : torch.Tensor
        Target with shape (batch_size, num_frames) or (batch_size, num_frames, num_classes)
    weight : torch.Tensor, optional
        Frame weight with shape (batch_size, num_frames_weight, 1).

    Returns
    -------
    weight : torch.Tensor
        Interpolated frame weight with shape (batch_size, num_frames, 1).
          linearF)sizemodealign_corners)shapeFinterpolate	transpose)r   r   
num_framess      n/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/pyannote/audio/utils/loss.pyr   r       sd      aJfll1o;Q"	

 )Aq/ 	 M    
predictionreturnc                 :   t        |j                        dk(  r|j                  d      }|$t        j                  | |j                               S t        ||      }t        j                  | |j                         |j                  |j                              S )a   Frame-weighted binary cross entropy

    Parameters
    ----------
    prediction : torch.Tensor
        Prediction with shape (batch_size, num_frames, num_classes).
    target : torch.Tensor
        Target with shape (batch_size, num_frames) for binary or multi-class classification,
        or (batch_size, num_frames, num_classes) for multi-label classification.
    weight : (batch_size, num_frames, 1) torch.Tensor, optional
        Frame weight with shape (batch_size, num_frames, 1).

    Returns
    -------
    loss : torch.Tensor
    r   dimr   )lenr   	unsqueezer   binary_cross_entropyfloatr   expand)r   r   r   s      r   r   r   ;   s    . 6<<A!!a!(~%%j&,,.AA VF3%%v}}V\\/J
 	
r   c                 x   t        |j                        dk(  r|j                  d      }t        j                  | |j                         d      }|t        j                  |      S t        ||      j                  |j                        }t        j                  ||z        t        j                  |      z  S )a#  Frame-weighted mean-squared error loss

    Parameters
    ----------
    prediction : torch.Tensor
        Prediction with shape (batch_size, num_frames, num_classes).
    target : torch.Tensor
        Target with shape (batch_size, num_frames) for binary or multi-class classification,
        or (batch_size, num_frames, num_classes) for multi-label classification.
    weight : (batch_size, num_frames, 1) torch.Tensor, optional
        Frame weight with shape (batch_size, num_frames, 1).

    Returns
    -------
    loss : torch.Tensor
    r   r   none)	reductionr   )r   r   r   r   mse_lossr   torchmeanr   r   sum)r   r   r   lossess       r   r"   r"   a   s    . 6<<A!!a!(ZZ
FLLNfEF ~zz&!! VF3::6<<H yy&)EIIf,===r   class_weightc                    | j                   d   }t        j                  | j                  d|      |j                  d      |d      j                  |j                         }|t	        j
                  |      S t        ||      j                  d      }t	        j                  ||z        t	        j                  |      z  S )a  Frame-weighted negative log-likelihood loss

    Parameters
    ----------
    prediction : torch.Tensor
        Prediction with shape (batch_size, num_frames, num_classes).
    target : torch.Tensor
        Target with shape (batch_size, num_frames)
    class_weight : (num_classes, ) torch.Tensor, optional
        Class weight with shape (num_classes,  )
    weight : (batch_size, num_frames, 1) torch.Tensor, optional
        Frame weight with shape (batch_size, num_frames, 1).

    Returns
    -------
    loss : torch.Tensor
    r   r    )r   r!   r   r   )	r   r   nll_lossviewr#   r$   r   squeezer%   )r   r   r'   r   num_classesr&   s         r   r*   r*      s    0 ""1%KZZK(B d6<<  ~zz&!! VF3;;;B yy&)EIIf,===r   )N)NN)__doc__typingr   r#   torch.nn.functionalnn
functionalr   Tensorr   r   r"   r*    r   r   <module>r5      s  0 7     hu||.D < &*#
#
LL#
 U\\"#
 \\	#
R &*%>%>LL%> U\\"%> \\	%>V ,0%)	->->LL-> 5<<(-> U\\"	->
 \\->r   