
    RЦiy                         S r SSKrSSKJr  SSKJs  Jr   " S S\R                  5      r " S S\R                  5      r	g)z^Cross Entropy w/ smoothing or soft targets

Hacked together by / Copyright 2021 Ross Wightman
    Nc                      ^  \ rS rSrSrS	U 4S jjrS\R                  S\R                  S\R                  4S jrSr	U =r
$ )
LabelSmoothingCrossEntropy   z#NLL loss with label smoothing.
    c                 Z   > [         [        U ]  5         US:  d   eXl        SU-
  U l        g )Ng      ?)superr   __init__	smoothing
confidence)selfr	   	__class__s     V/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/timm/loss/cross_entropy.pyr   #LabelSmoothingCrossEntropy.__init__   s-    ($8:3"y.    xtargetreturnc                    [         R                  " USS9nUR                  SUR                  S5      S9* nUR	                  S5      nUR                  SS9* nU R                  U-  U R                  U-  -   nUR                  5       $ )Ndim   )r   index)Flog_softmaxgather	unsqueezesqueezemeanr
   r	   )r   r   r   logprobsnll_losssmooth_losslosss          r   forward"LabelSmoothingCrossEntropy.forward   s}    ==+OO&2B2B12EOFF##A&}}},,)DNN[,HHyy{r   )r
   r	   )g?)__name__
__module____qualname____firstlineno____doc__r   torchTensorr#   __static_attributes____classcell__r   s   @r   r   r      s8    ) u||   r   r   c                   z   ^  \ rS rSrU 4S jrS\R                  S\R                  S\R                  4S jrSrU =r	$ )SoftTargetCrossEntropy   c                 *   > [         [        U ]  5         g )N)r   r0   r   )r   r   s    r   r   SoftTargetCrossEntropy.__init__   s    $d46r   r   r   r   c                 z    [         R                  " U* [        R                  " USS9-  SS9nUR	                  5       $ )Nr   r   )r*   sumr   r   r   )r   r   r   r"   s       r   r#   SoftTargetCrossEntropy.forward"   s0    yy&1==#;;Dyy{r    )
r%   r&   r'   r(   r   r*   r+   r#   r,   r-   r.   s   @r   r0   r0      s3    7 u||   r   r0   )
r)   r*   torch.nnnntorch.nn.functional
functionalr   Moduler   r0   r7   r   r   <module>r=      s8   
     $RYY r   