o
    pi                     @   s   d Z ddlmZ ddlZddlm  mZ ddejdeej fddZ		ddejdejdeej d	ejfd
dZ
	ddejdejdeej d	ejfddZ		ddejdejdeej deej d	ejf
ddZdS )z0Frame-weighted versions of common loss functions    )OptionalNtargetweightc                 C   sF   | j d }|dur!|j d |kr!tj|dd|ddd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).
       N   linearF)sizemodealign_corners)shapeFinterpolate	transpose)r   r   
num_frames r   M/home/ubuntu/.local/lib/python3.10/site-packages/pyannote/audio/utils/loss.pyr       s   

r   
predictionreturnc                 C   sZ   t |jdkr|jdd}|du rt| | S t||d}tj| | ||jd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   dimNr   )lenr   	unsqueezer   binary_cross_entropyfloatr   expand)r   r   r   r   r   r   r   ;   s   r   c                 C   sl   t |jdkr|jdd}tj| | dd}|du r t|S t||d	|j}t
|| t
| 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)	reductionNr   )r   r   r   r   mse_lossr   torchmeanr   r   sum)r   r   r   lossesr   r   r   r   a   s   
r   class_weightc                 C   sp   | j d }tj| d||d|dd|j }|du r"t|S t||djdd}t|| t| 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   Nr   r   )	r   r   nll_lossviewr   r    r   squeezer!   )r   r   r#   r   num_classesr"   r   r   r   r%      s   


r%   )N)NN)__doc__typingr   r   torch.nn.functionalnn
functionalr   Tensorr   r   r   r%   r   r   r   r   <module>   sL   
)
+