§
    ’©ºjt0  ã                   ó€  — d dl mZ d dlZd dlmZmZ d dlmZ d dlmZm	Z	m
Z
mZ d dlmZ d dlmZ d dlmZ d	ed
edefd„Zd#dedee         ddfd„Zd#dededee         ddfd„Zdedededeeef         fd„Z	 	 	 d$dedededee         dedefd„Z	 	 	 d%dededed         dee         ddf
d„Z	 d#dedededee         ddf
d„Z	 d&dedededed         deeef         f
d„Z	 	 	 	 d'dededededed         dee         dedefd„Z	 	 	 	 	 d(deded ed!         dee         deded         dee         dedefd"„ZdS ))é    )ÚOptionalN)ÚTensorÚtensor)ÚLiteral)Ú_binary_confusion_matrix_formatÚ*_binary_confusion_matrix_tensor_validationÚ#_multiclass_confusion_matrix_formatÚ._multiclass_confusion_matrix_tensor_validation)Únormalize_logits_if_needed)Ú	to_onehot)ÚClassificationTaskNoMultilabelÚmeasureÚtotalÚreturnc                 ó   — | |z  S ©N© )r   r   s     ú|/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/classification/hinge.pyÚ_hinge_loss_computer      s   € Ø�U‰?Ðó    ÚsquaredÚignore_indexc                 óª   — t          | t          ¦  «        st          d| › �¦  «        ‚|�'t          |t          ¦  «        st          d|› �¦  «        ‚d S d S )Nz2Expected argument `squared` to be an bool but got zLExpected argument `ignore_index` to either be `None` or an integer, but got )Ú
isinstanceÚboolÚ
ValueErrorÚint)r   r   s     r   Ú!_binary_hinge_loss_arg_validationr   #   sg   € Ý�g�tÑ$Ô$ð YÝÐWÈgÐWÐWÑXÔXÐXØÐ­
°<ÅÑ(EÔ(EÐÝÐvÐhtÐvÐvÑwÔwÐwð  ÐÐÐr   ÚpredsÚtargetc                 ó~   — t          | ||¦  «         |                      ¦   «         st          d| j        › �¦  «        ‚d S ©NzdExpected argument `preds` to be floating tensor with probabilities/logits but got tensor with dtype )r   Úis_floating_pointr   Údtype)r   r    r   s      r   Ú$_binary_hinge_loss_tensor_validationr%   *   sX   € Ý.¨u°f¸lÑKÔKÐKØ×"Ò"Ñ$Ô$ð 
Ýð8Ø*/¬+ð8ð 8ñ
ô 
ð 	
ð
ð 
r   c                 óX  — |                      ¦   «         }t          j        | ¦  «        }| |         ||<   | |           || <   d|z
  }t          j        |d¦  «        }|r|                     d¦  «        }t          |j        d         |j        ¬¦  «        }|                     d¬¦  «        |fS )Né   r   é   ©Údevice©Údim)	r   ÚtorchÚ
zeros_likeÚclampÚpowr   Úshaper*   Úsum)r   r    r   ÚmarginÚmeasuresr   s         r   Ú_binary_hinge_loss_updater5   3   s¥   € ð
 �[Š[‰]Œ]€FÝÔ˜eÑ$Ô$€FØ˜6”]€Fˆ6�NØ˜f˜W”~�o€FˆFˆ7�Oà�6‰z€HÝŒ{˜8 QÑ'Ô'€Hàð #Ø—<’< ‘?”?ˆå�6”< ”?¨6¬=Ð9Ñ9Ô9€EØ�<Š<˜Aˆ<ÑÔ Ð%Ð%r   FÚvalidate_argsc                 ó¾   — |r!t          ||¦  «         t          | ||¦  «         t          | |d|d¬¦  «        \  } }t          | ||¦  «        \  }}t	          ||¦  «        S )aó  Compute the mean `Hinge loss`_ typically used for Support Vector Machines (SVMs) for binary tasks.

    .. math::
        \text{Hinge loss} = \max(0, 1 - y \times \hat{y})

    Where :math:`y \in {-1, 1}` is the target, and :math:`\hat{y} \in \mathbb{R}` is the prediction.

    Accepts the following input tensors:

    - ``preds`` (float tensor): ``(N, ...)``. Preds should be a tensor containing probabilities or logits for each
      observation. If preds has values outside [0,1] range we consider the input to be logits and will auto apply
      sigmoid per element.
    - ``target`` (int tensor): ``(N, ...)``. Target should be a tensor containing ground truth labels, and therefore
      only contain {0,1} values (except if `ignore_index` is specified). The value 1 always encodes the positive class.

    Additional dimension ``...`` will be flattened into the batch dimension.

    Args:
        preds: Tensor with predictions
        target: Tensor with true labels
        squared:
            If True, this will compute the squared hinge loss. Otherwise, computes the regular hinge loss.
        ignore_index:
            Specifies a target value that is ignored and does not contribute to the metric calculation
        validate_args: bool indicating if input arguments and tensors should be validated for correctness.
            Set to ``False`` for faster computations.

    Example:
        >>> from torch import tensor
        >>> from torchmetrics.functional.classification import binary_hinge_loss
        >>> preds = tensor([0.25, 0.25, 0.55, 0.75, 0.75])
        >>> target = tensor([0, 0, 1, 1, 1])
        >>> binary_hinge_loss(preds, target)
        tensor(0.6900)
        >>> binary_hinge_loss(preds, target, squared=True)
        tensor(0.6905)

    g        F)Ú	thresholdr   Úconvert_to_labels)r   r%   r   r5   r   )r   r    r   r   r6   r4   r   s          r   Úbinary_hinge_lossr:   G   s{   € ðZ ð JÝ)¨'°<Ñ@Ô@Ð@Ý,¨U°F¸LÑIÔIÐIÝ3Øˆv °<ÐSXðñ ô �M€Eˆ6õ 0°°v¸wÑGÔG�O€HˆeÝ˜x¨Ñ/Ô/Ð/r   úcrammer-singerÚnum_classesÚmulticlass_mode©r;   z
one-vs-allc                 ó¸   — t          ||¦  «         t          | t          ¦  «        r| dk     rt          d| › �¦  «        ‚d}||vrt          d|› d|› d�¦  «        ‚d S )Nr(   zHExpected argument `num_classes` to be an integer larger than 1, but got r>   z1Expected argument `multiclass_mode` to be one of z
, but got ú.)r   r   r   r   )r<   r   r=   r   Ú
allowed_mms        r   Ú%_multiclass_hinge_loss_arg_validationrB   ~   s‚   € õ & g¨|Ñ<Ô<Ð<Ý�k¥3Ñ'Ô'ð s¨;¸ª?¨?ÝÐqÐdoÐqÐqÑrÔrÐrØ1€JØ˜jÐ(Ð(ÝÐuÈZÐuÐuÐcrÐuÐuÐuÑvÔvÐvð )Ð(r   c                 ó€   — t          | |||¦  «         |                      ¦   «         st          d| j        › �¦  «        ‚d S r"   )r
   r#   r   r$   )r   r    r<   r   s       r   Ú(_multiclass_hinge_loss_tensor_validationrD   Œ   s\   € õ 3°5¸&À+È|Ñ\Ô\Ð\Ø×"Ò"Ñ$Ô$ð 
Ýð8Ø*/¬+ð8ð 8ñ
ô 
ð 	
ð
ð 
r   c                 ó–  — t          | d¦  «        } t          |t          d| j        d         ¦  «        ¦  «                             ¦   «         }|dk    rN| |         }|t          j        | |                               | j        d         d¦  «        d¬¦  «        d         z  }nA|                     ¦   «         }t          j        | ¦  «        }| |         ||<   | |           || <   d|z
  }t          j        |d¦  «        }|r| 	                    d¦  «        }t          |j        d         |j        ¬¦  «        }|                     d¬¦  «        |fS )	NÚsoftmaxr(   r'   r;   r   éÿÿÿÿr+   r)   )r   r   Úmaxr1   r   r-   Úviewr.   r/   r0   r   r*   r2   )r   r    r   r=   r3   r4   r   s          r   Ú_multiclass_hinge_loss_updaterJ   —   s0  € õ ' u¨iÑ8Ô8€EÝ�v�s 1 e¤k°!¤nÑ5Ô5Ñ6Ô6×;Ò;Ñ=Ô=€FØÐ*Ò*Ð*Ø�v”ˆØ•%”)˜E 6 'œN×/Ò/°´¸A´ÀÑCÔCÈÐKÑKÔKÈAÔNÑNˆˆà—’‘”ˆÝÔ! %Ñ(Ô(ˆØ˜vœˆˆv‰Ø  & œ>˜/ˆ�ˆw‰à�6‰z€HÝŒ{˜8 QÑ'Ô'€Hàð #Ø—<’< ‘?”?ˆå�6”< ”?¨6¬=Ð9Ñ9Ô9€EØ�<Š<˜Aˆ<ÑÔ Ð%Ð%r   c                 óÄ   — |r$t          ||||¦  «         t          | |||¦  «         t          | ||d¬¦  «        \  } }t          | |||¦  «        \  }}t	          ||¦  «        S )a
  Compute the mean `Hinge loss`_ typically used for Support Vector Machines (SVMs) for multiclass tasks.

    The metric can be computed in two ways. Either, the definition by Crammer and Singer is used:

    .. math::
        \text{Hinge loss} = \max\left(0, 1 - \hat{y}_y + \max_{i \ne y} (\hat{y}_i)\right)

    Where :math:`y \in {0, ..., \mathrm{C}}` is the target class (where :math:`\mathrm{C}` is the number of classes),
    and :math:`\hat{y} \in \mathbb{R}^\mathrm{C}` is the predicted output per class. Alternatively, the metric can
    also be computed in one-vs-all approach, where each class is valued against all other classes in a binary fashion.

    Accepts the following input tensors:

    - ``preds`` (float tensor): ``(N, C, ...)``. Preds should be a tensor containing probabilities or logits for each
      observation. If preds has values outside [0,1] range we consider the input to be logits and will auto apply
      softmax per sample.
    - ``target`` (int tensor): ``(N, ...)``. Target should be a tensor containing ground truth labels, and therefore
      only contain values in the [0, n_classes-1] range (except if `ignore_index` is specified).

    Additional dimension ``...`` will be flattened into the batch dimension.

    Args:
        preds: Tensor with predictions
        target: Tensor with true labels
        num_classes: Integer specifying the number of classes
        squared:
            If True, this will compute the squared hinge loss. Otherwise, computes the regular hinge loss.
        multiclass_mode:
            Determines how to compute the metric
        ignore_index:
            Specifies a target value that is ignored and does not contribute to the metric calculation
        validate_args: bool indicating if input arguments and tensors should be validated for correctness.
            Set to ``False`` for faster computations.

    Example:
        >>> from torch import tensor
        >>> from torchmetrics.functional.classification import multiclass_hinge_loss
        >>> preds = tensor([[0.25, 0.20, 0.55],
        ...                 [0.55, 0.05, 0.40],
        ...                 [0.10, 0.30, 0.60],
        ...                 [0.90, 0.05, 0.05]])
        >>> target = tensor([0, 1, 2, 0])
        >>> multiclass_hinge_loss(preds, target, num_classes=3)
        tensor(0.9125)
        >>> multiclass_hinge_loss(preds, target, num_classes=3, squared=True)
        tensor(1.1131)
        >>> multiclass_hinge_loss(preds, target, num_classes=3, multiclass_mode='one-vs-all')
        tensor([0.8750, 1.1250, 1.1000])

    F)r9   )rB   rD   r	   rJ   r   )	r   r    r<   r   r=   r   r6   r4   r   s	            r   Úmulticlass_hinge_lossrL   ²   sy   € ðv ð [Ý-¨k¸7ÀOÐUaÑbÔbÐbÝ0°¸ÀÈ\ÑZÔZÐZÝ7¸¸vÀ|ÐglÐmÑmÔm�M€Eˆ6Ý3°E¸6À7ÈOÑ\Ô\�O€HˆeÝ˜x¨Ñ/Ô/Ð/r   TÚtask)ÚbinaryÚ
multiclassc           	      óH  — t          j        |¦  «        }|t           j        k    rt          | ||||¦  «        S |t           j        k    rJt          |t          ¦  «        s t          dt          |¦  «        › d�¦  «        ‚t          | ||||||¦  «        S t          d|› �¦  «        ‚)a¿  Compute the mean `Hinge loss`_ typically used for Support Vector Machines (SVMs).

    This function is a simple wrapper to get the task specific versions of this metric, which is done by setting the
    ``task`` argument to either ``'binary'`` or ``'multiclass'``. See the documentation of
    :func:`~torchmetrics.functional.classification.binary_hinge_loss` and
    :func:`~torchmetrics.functional.classification.multiclass_hinge_loss` for the specific details of
    each argument influence and examples.

    Legacy Example:
        >>> from torch import tensor
        >>> target = tensor([0, 1, 1])
        >>> preds = tensor([0.5, 0.7, 0.1])
        >>> hinge_loss(preds, target, task="binary")
        tensor(0.9000)

        >>> target = tensor([0, 1, 2])
        >>> preds = tensor([[-1.0, 0.9, 0.2], [0.5, -1.1, 0.8], [2.2, -0.5, 0.3]])
        >>> hinge_loss(preds, target, task="multiclass", num_classes=3)
        tensor(1.5551)

        >>> target = tensor([0, 1, 2])
        >>> preds = tensor([[-1.0, 0.9, 0.2], [0.5, -1.1, 0.8], [2.2, -0.5, 0.3]])
        >>> hinge_loss(preds, target, task="multiclass", num_classes=3, multiclass_mode="one-vs-all")
        tensor([1.3743, 1.1945, 1.2359])

    z+`num_classes` is expected to be `int` but `z was passed.`zNot handled value: )
r   Úfrom_strÚBINARYr:   Ú
MULTICLASSr   r   r   ÚtyperL   )r   r    rM   r<   r   r=   r   r6   s           r   Ú
hinge_lossrU   õ   s²   € õH *Ô2°4Ñ8Ô8€DØÕ-Ô4Ò4Ð4Ý  ¨°¸À}ÑUÔUÐUØÕ-Ô8Ò8Ð8Ý˜+¥sÑ+Ô+ð 	mÝÐkÍ4ÐP[ÑK\ÔK\ÐkÐkÐkÑlÔlÐlÝ$ U¨F°KÀÈ/Ð[gÐivÑwÔwÐwÝ
Ð1¨4Ð1Ð1Ñ
2Ô
2Ð2r   r   )FNF)Fr;   N)r;   )Fr;   NF)NFr;   NT)Útypingr   r-   r   r   Útyping_extensionsr   Ú7torchmetrics.functional.classification.confusion_matrixr   r   r	   r
   Útorchmetrics.utilities.computer   Útorchmetrics.utilities.datar   Útorchmetrics.utilities.enumsr   r   r   r   r   r%   Útupler5   r:   rB   rD   rJ   rL   rU   r   r   r   ú<module>r]      s<  ðð Ð Ð Ð Ð Ð à €€€Ø  Ð  Ð  Ð  Ð  Ð  Ð  Ð  Ø %Ð %Ð %Ð %Ð %Ð %ðð ð ð ð ð ð ð ð ð ð ð ð FÐ EÐ EÐ EÐ EÐ EØ 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø GÐ GÐ GÐ GÐ GÐ Gð ð °ð ¸6ð ð ð ð ðxð x¨tð xÀ8ÈCÄ=ð xÐ\`ð xð xð xð xð
ð 
°ð 
Àð 
ÐV^Ð_bÔVcð 
Ðosð 
ð 
ð 
ð 
ð&Øð&àð&ð ð&ð ˆ6�6ˆ>Ôð	&ð &ð &ð &ð. Ø"&Øð40ð 40Øð40àð40ð ð40ð ˜3”-ð	40ð
 ð40ð ð40ð 40ð 40ð 40ðr Ø?OØ"&ð	wð wØðwàðwð Ð;Ô<ðwð ˜3”-ð	wð
 
ðwð wð wð wð TXð
ð 
Øð
Ø!ð
Ø03ð
ØCKÈCÄ=ð
à	ð
ð 
ð 
ð 
ð @Pð	&ð &Øð&àð&ð ð&ð Ð;Ô<ð	&ð
 ˆ6�6ˆ>Ôð&ð &ð &ð &ð> Ø?OØ"&Øð@0ð @0Øð@0àð@0ð ð@0ð ð	@0ð
 Ð;Ô<ð@0ð ˜3”-ð@0ð ð@0ð ð@0ð @0ð @0ð @0ðN "&ØØ?OØ"&Øð+3ð +3Øð+3àð+3ð Ð(Ô
)ð+3ð ˜#”ð	+3ð
 ð+3ð Ð;Ô<ð+3ð ˜3”-ð+3ð ð+3ð ð+3ð +3ð +3ð +3ð +3ð +3r   