o
    پiy                     @   sR   d Z ddlZddlmZ ddlm  mZ G dd dejZG dd dejZ	dS )z_ Cross Entropy w/ smoothing or soft targets

Hacked together by / Copyright 2021 Ross Wightman
    Nc                       s>   e Zd ZdZd
 fdd	Zdejdejdejfdd	Z  ZS )LabelSmoothingCrossEntropyz$ NLL loss with label smoothing.
    皙?c                    s.   t t|   |dk sJ || _d| | _d S )Ng      ?)superr   __init__	smoothing
confidence)selfr   	__class__ K/home/ubuntu/.local/lib/python3.10/site-packages/timm/loss/cross_entropy.pyr      s   z#LabelSmoothingCrossEntropy.__init__xtargetreturnc                 C   sX   t j|dd}|jd|dd }|d}|jdd }| j| | j|  }| S )Ndim   )r   index)Flog_softmaxgather	unsqueezesqueezemeanr   r   )r   r   r   logprobsnll_losssmooth_losslossr   r   r   forward   s   
z"LabelSmoothingCrossEntropy.forward)r   )	__name__
__module____qualname____doc__r   torchTensorr   __classcell__r   r   r	   r   r      s    $r   c                       s8   e Zd Z fddZdejdejdejfddZ  ZS )SoftTargetCrossEntropyc                    s   t t|   d S )N)r   r'   r   )r   r	   r   r   r      s   zSoftTargetCrossEntropy.__init__r   r   r   c                 C   s&   t j| tj|dd dd}| S )Nr   r   )r$   sumr   r   r   )r   r   r   r   r   r   r   r   "   s   zSoftTargetCrossEntropy.forward)r    r!   r"   r   r$   r%   r   r&   r   r   r	   r   r'      s    $r'   )
r#   r$   torch.nnnntorch.nn.functional
functionalr   Moduler   r'   r   r   r   r   <module>   s    