Ë
    l^(h8  ã                  ó¶   — d dl mZ d dlZd dlmZ d dlmZ d dlZd dlZd dl	m
Z
 d dlmZmZ d dlmZ  ej                  e«      Z G d„ d	ej$                  «      Zy)
é    )ÚannotationsN)ÚIterable)ÚCallable)Úversion)ÚTensorÚnn)ÚSentenceTransformerc                  óŒ   ‡ — e Zd Zddd ej                  «       f	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dˆ fd„Z	 	 	 	 	 	 dd„Zedd„«       Zˆ xZ	S )	ÚSoftmaxLossTFc                óÆ  •— t         ‰	| �  «        || _        || _        || _        || _        || _        d}|r|dz  }|r|dz  }|r|dz  }t        j                  d|› �«       t        j                  ||z  ||j                  ¬«      | _        || _        t        j                  t         j"                  «      t        j                  d«      k  rt        j%                  d«       yy)	at  
        This loss was used in our SBERT publication (https://arxiv.org/abs/1908.10084) to train the SentenceTransformer
        model on NLI data. It adds a softmax classifier on top of the output of two transformer networks.

        :class:`MultipleNegativesRankingLoss` is an alternative loss function that often yields better results,
        as per https://arxiv.org/abs/2004.09813.

        Args:
            model (SentenceTransformer): The SentenceTransformer model.
            sentence_embedding_dimension (int): The dimension of the sentence embeddings.
            num_labels (int): The number of different labels.
            concatenation_sent_rep (bool): Whether to concatenate vectors u,v for the softmax classifier. Defaults to True.
            concatenation_sent_difference (bool): Whether to add abs(u-v) for the softmax classifier. Defaults to True.
            concatenation_sent_multiplication (bool): Whether to add u*v for the softmax classifier. Defaults to False.
            loss_fct (Callable): Custom pytorch loss function. If not set, uses nn.CrossEntropyLoss(). Defaults to nn.CrossEntropyLoss().

        References:
            - Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks: https://arxiv.org/abs/1908.10084
            - `Training Examples > Natural Language Inference <../../../examples/sentence_transformer/training/nli/README.html>`_

        Requirements:
            1. sentence pairs with a class label

        Inputs:
            +---------------------------------------+--------+
            | Texts                                 | Labels |
            +=======================================+========+
            | (sentence_A, sentence_B) pairs        | class  |
            +---------------------------------------+--------+

        Example:
            ::

                from sentence_transformers import SentenceTransformer, SentenceTransformerTrainer, losses
                from datasets import Dataset

                model = SentenceTransformer("microsoft/mpnet-base")
                train_dataset = Dataset.from_dict({
                    "sentence1": [
                        "A person on a horse jumps over a broken down airplane.",
                        "A person on a horse jumps over a broken down airplane.",
                        "A person on a horse jumps over a broken down airplane.",
                        "Children smiling and waving at camera",
                    ],
                    "sentence2": [
                        "A person is training his horse for a competition.",
                        "A person is at a diner, ordering an omelette.",
                        "A person is outdoors, on a horse.",
                        "There are children present.",
                    ],
                    "label": [1, 2, 0, 0],
                })
                loss = losses.SoftmaxLoss(model, model.get_sentence_embedding_dimension(), num_labels=3)

                trainer = SentenceTransformerTrainer(
                    model=model,
                    train_dataset=train_dataset,
                    loss=loss,
                )
                trainer.train()
        r   é   é   z%Softmax loss: #Vectors concatenated: )Údevicez4.43.0zÜSoftmaxLoss requires transformers >= 4.43.0 to work correctly. Otherwise, the classifier layer that maps embeddings to the labels cannot be updated. Consider updating transformers with `pip install transformers>=4.43.0`.N)ÚsuperÚ__init__ÚmodelÚ
num_labelsÚconcatenation_sent_repÚconcatenation_sent_differenceÚ!concatenation_sent_multiplicationÚloggerÚinfor   ÚLinearr   Ú
classifierÚloss_fctr   ÚparseÚtransformersÚ__version__Úwarning)
Úselfr   Úsentence_embedding_dimensionr   r   r   r   r   Únum_vectors_concatenatedÚ	__class__s
            €úf/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/sentence_transformers/losses/SoftmaxLoss.pyr   zSoftmaxLoss.__init__   sâ   ø€ ôN 	‰ÑÔØˆŒ
Ø$ˆŒØ&<ˆÔ#Ø-JˆÔ*Ø1RˆÔ.à#$Ð Ù!Ø$¨Ñ)Ð$Ù(Ø$¨Ñ)Ð$Ù,Ø$¨Ñ)Ð$Ü�‰Ð;Ð<TÐ;UÐVÔWÜŸ)™)Ø$Ð'CÑCÀZÐX]×XdÑXdô
ˆŒð !ˆŒä�=‰=œ×1Ñ1Ó2´W·]±]À8Ó5LÒLÜ�N‰NðZõð Mó    c                óþ  — |D �cg c]  }| j                  |«      d   ‘Œ }}|\  }}g }| j                  r"|j                  |«       |j                  |«       | j                  r'|j                  t	        j
                  ||z
  «      «       | j                  r|j                  ||z  «       t	        j                  |d«      }| j                  |«      }	|�#| j                  |	|j                  d«      «      }
|
S ||	fS c c}w )NÚsentence_embeddingr   éÿÿÿÿ)r   r   Úappendr   ÚtorchÚabsr   Úcatr   r   Úview)r    Úsentence_featuresÚlabelsÚsentence_featureÚrepsÚrep_aÚrep_bÚvectors_concatÚfeaturesÚoutputÚlosss              r$   ÚforwardzSoftmaxLoss.forwardt   sí   € ð \mÖmÐGW�—
‘
Ð+Ó,Ð-AÓBÐmˆÐmØ‰ˆˆuàˆØ×&Ò&Ø×!Ñ! %Ô(Ø×!Ñ! %Ô(à×-Ò-Ø×!Ñ!¤%§)¡)¨E°E©MÓ":Ô;à×1Ò1Ø×!Ñ! %¨%¡-Ô0ä—9‘9˜^¨QÓ/ˆà—‘ Ó*ˆàÐØ—=‘= ¨¯©°R«Ó9ˆDØˆKà˜�<Ðùò- ns   …C:c                 ó   — y)Na¨  
@inproceedings{reimers-2019-sentence-bert,
    title = "Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks",
    author = "Reimers, Nils and Gurevych, Iryna",
    booktitle = "Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing",
    month = "11",
    year = "2019",
    publisher = "Association for Computational Linguistics",
    url = "https://arxiv.org/abs/1908.10084",
}
© )r    s    r$   ÚcitationzSoftmaxLoss.citation�   s   € ð
r%   )r   r	   r!   Úintr   r<   r   Úboolr   r=   r   r=   r   r   ÚreturnÚNone)r.   zIterable[dict[str, Tensor]]r/   r   r>   zTensor | tuple[Tensor, Tensor])r>   Ústr)
Ú__name__Ú
__module__Ú__qualname__r   ÚCrossEntropyLossr   r8   Úpropertyr;   Ú__classcell__)r#   s   @r$   r   r      sª   ø„ ð (,Ø.2Ø27Ø0˜R×0Ñ0Ó2ð`à"ð`ð '*ð`ð ð	`ð
 !%ð`ð (,ð`ð ,0ð`ð ð`ð 
õ`ðD Ø!<ð ØFLð à	'ó ð6 òó ôr%   r   )Ú
__future__r   ÚloggingÚcollections.abcr   Útypingr   r*   r   Ú	packagingr   r   r   Ú)sentence_transformers.SentenceTransformerr	   Ú	getLoggerrA   r   ÚModuler   r:   r%   r$   ú<module>rO      sD   ðÝ "ã Ý $Ý ã Û Ý ß å Ià	ˆ×	Ñ	˜8Ó	$€ôJ�"—)‘)õ Jr%   