o
    _{j“  ã                   @   sf   d dl Z d dlm  mZ ddlmZ ddlmZ e 	¡ G dd„ deƒƒZ
e 	¡ G dd	„ d	eƒƒZdS )
é    Né   )ÚLOSSESé   )ÚBaseWeightedLossc                       ó*   e Zd ZdZd‡ fdd„	Zdd„ Z‡  ZS )	ÚCrossEntropyLossa0  Cross Entropy Loss.

    Support two kinds of labels and their corresponding loss type. It's worth
    mentioning that loss type will be detected by the shape of ``cls_score``
    and ``label``.
    1) Hard label: This label is an integer array and all of the elements are
        in the range [0, num_classes - 1]. This label's shape should be
        ``cls_score``'s shape with the `num_classes` dimension removed.
    2) Soft label(probablity distribution over classes): This label is a
        probability distribution and all of the elements are in the range
        [0, 1]. This label's shape must be the same as ``cls_score``. For now,
        only 2-dim soft label is supported.

    Args:
        loss_weight (float): Factor scalar multiplied on the loss.
            Default: 1.0.
        class_weight (list[float] | None): Loss weight for each class. If set
            as None, use the same weight 1 for all classes. Only applies
            to CrossEntropyLoss and BCELossWithLogits (should not be set when
            using other losses). Default: None.
    ç      ð?Nc                    ó0   t ƒ j|d� d | _|d urt |¡| _d S d S ©N)Úloss_weight©ÚsuperÚ__init__Úclass_weightÚtorchÚTensor©Úselfr   r   ©Ú	__class__© ú?/root/disk_3tb/HAC/mmaction/models/losses/cross_entropy_loss.pyr       ó
   ÿzCrossEntropyLoss.__init__c                 K   sð   |  ¡ |  ¡ krV| ¡ dksJ dƒ‚t|ƒdksJ d|› �ƒ‚t |d¡}| jdur2|| j d¡ }||  d¡ }| jdurP| ¡ t | j d¡| ¡ }|S | 	¡ }|S | jdurld|vscJ dƒ‚| j 
|j¡|d< tj||fi |¤Ž}|S )	aJ  Forward function.

        Args:
            cls_score (torch.Tensor): The class score.
            label (torch.Tensor): The ground truth label.
            kwargs: Any keyword argument to be used to calculate
                CrossEntropy loss.

        Returns:
            torch.Tensor: The returned CrossEntropy loss.
        r   zOnly support 2-dim soft labelr   z=For now, no extra args are supported for soft label, but get r   NÚweightú The key 'weight' already exists.)ÚsizeÚdimÚlenÚFÚlog_softmaxr   Ú	unsqueezeÚsumr   ÚmeanÚtoÚdeviceÚcross_entropy)r   Ú	cls_scoreÚlabelÚkwargsZlsmÚloss_clsr   r   r   Ú_forward&   s2   ÿÿ


ÿö

ú
ÿzCrossEntropyLoss._forward©r   N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r*   Ú__classcell__r   r   r   r   r      s    r   c                       r   )	ÚBCELossWithLogitsa¨  Binary Cross Entropy Loss with logits.

    Args:
        loss_weight (float): Factor scalar multiplied on the loss.
            Default: 1.0.
        class_weight (list[float] | None): Loss weight for each class. If set
            as None, use the same weight 1 for all classes. Only applies
            to CrossEntropyLoss and BCELossWithLogits (should not be set when
            using other losses). Default: None.
    r   Nc                    r	   r
   r   r   r   r   r   r   `   r   zBCELossWithLogits.__init__c                 K   sD   | j durd|vsJ dƒ‚| j  |j¡|d< tj||fi |¤Ž}|S )aP  Forward function.

        Args:
            cls_score (torch.Tensor): The class score.
            label (torch.Tensor): The ground truth label.
            kwargs: Any keyword argument to be used to calculate
                bce loss with logits.

        Returns:
            torch.Tensor: The returned bce loss with logits.
        Nr   r   )r   r#   r$   r   Ú binary_cross_entropy_with_logits)r   r&   r'   r(   r)   r   r   r   r*   f   s   
ÿzBCELossWithLogits._forwardr+   r,   r   r   r   r   r2   S   s    r2   )r   Útorch.nn.functionalÚnnÚ
functionalr   Úregistryr   Úbaser   Úregister_moduler   r2   r   r   r   r   Ú<module>   s    J