Ë
    l^(h]  ã                  ó‚   — d dl mZ d dlmZ 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  G d„ d	ej                  «      Zy)
é    )Úannotations)ÚIterable)ÚAnyN)ÚTensorÚnn)ÚSentenceTransformer)Úfullnamec                  ó€   ‡ — e Zd Z ej                  «        ej
                  «       f	 	 	 	 	 	 	 dˆ fd„Zdd„Zdd„Zˆ xZ	S )ÚCosineSimilarityLossc                óL   •— t         ‰| �  «        || _        || _        || _        y)a–
  
        CosineSimilarityLoss expects that the InputExamples consists of two texts and a float label. It computes the
        vectors ``u = model(sentence_A)`` and ``v = model(sentence_B)`` and measures the cosine-similarity between the two.
        By default, it minimizes the following loss: ``||input_label - cos_score_transformation(cosine_sim(u,v))||_2``.

        Args:
            model: SentenceTransformer model
            loss_fct: Which pytorch loss function should be used to
                compare the ``cosine_similarity(u, v)`` with the
                input_label? By default, MSE is used: ``||input_label -
                cosine_sim(u, v)||_2``
            cos_score_transformation: The cos_score_transformation
                function is applied on top of cosine_similarity. By
                default, the identify function is used (i.e. no change).

        References:
            - `Training Examples > Semantic Textual Similarity <../../../examples/sentence_transformer/training/sts/README.html>`_

        Requirements:
            1. Sentence pairs with corresponding similarity scores in range `[0, 1]`

        Inputs:
            +--------------------------------+------------------------+
            | Texts                          | Labels                 |
            +================================+========================+
            | (sentence_A, sentence_B) pairs | float similarity score |
            +--------------------------------+------------------------+

        Relations:
            - :class:`CoSENTLoss` seems to produce a stronger training signal than CosineSimilarityLoss. In our experiments, CoSENTLoss is recommended.
            - :class:`AnglELoss` is :class:`CoSENTLoss` with ``pairwise_angle_sim`` as the metric, rather than ``pairwise_cos_sim``. It also produces a stronger training signal than CosineSimilarityLoss.

        Example:
            ::

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

                model = SentenceTransformer("microsoft/mpnet-base")
                train_dataset = Dataset.from_dict({
                    "sentence1": ["It's nice weather outside today.", "He drove to work."],
                    "sentence2": ["It's so sunny.", "She walked to the store."],
                    "score": [1.0, 0.3],
                })
                loss = losses.CosineSimilarityLoss(model)

                trainer = SentenceTransformerTrainer(
                    model=model,
                    train_dataset=train_dataset,
                    loss=loss,
                )
                trainer.train()
        N)ÚsuperÚ__init__ÚmodelÚloss_fctÚcos_score_transformation)Úselfr   r   r   Ú	__class__s       €úo/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/sentence_transformers/losses/CosineSimilarityLoss.pyr   zCosineSimilarityLoss.__init__   s'   ø€ ôv 	‰ÑÔØˆŒ
Ø ˆŒØ(@ˆÕ%ó    c                ó  — |D �cg c]  }| j                  |«      d   ‘Œ }}| j                  t        j                  |d   |d   «      «      }| j	                  ||j                  «       j                  d«      «      S c c}w )NÚsentence_embeddingr   é   éÿÿÿÿ)r   r   ÚtorchÚcosine_similarityr   ÚfloatÚview)r   Úsentence_featuresÚlabelsÚsentence_featureÚ
embeddingsÚoutputs         r   ÚforwardzCosineSimilarityLoss.forwardN   sw   € ØarÖsÐM]�d—j‘jÐ!1Ó2Ð3GÓHÐsˆ
ÐsØ×.Ñ.¬u×/FÑ/FÀzÐRSÁ}ÐV`ÐabÑVcÓ/dÓeˆØ�}‰}˜V V§\¡\£^×%8Ñ%8¸Ó%<Ó=Ð=ùò ts   …A<c                ó0   — dt        | j                  «      iS )Nr   )r	   r   )r   s    r   Úget_config_dictz$CosineSimilarityLoss.get_config_dictS   s   € ØœH T§]¡]Ó3Ð4Ð4r   )r   r   r   ú	nn.Moduler   r&   ÚreturnÚNone)r   zIterable[dict[str, Tensor]]r   r   r'   r   )r'   zdict[str, Any])
Ú__name__Ú
__module__Ú__qualname__r   ÚMSELossÚIdentityr   r#   r%   Ú__classcell__)r   s   @r   r   r      sV   ø„ ð )˜bŸj™j›lØ.9¨b¯k©k«mð	>Aà"ð>Að ð>Að #,ð	>Að
 
õ>Aó@>÷
5r   r   )Ú
__future__r   Úcollections.abcr   Útypingr   r   r   r   Ú)sentence_transformers.SentenceTransformerr   Úsentence_transformers.utilr	   ÚModuler   © r   r   ú<module>r6      s,   ðÝ "å $Ý ã ß å IÝ /ôG5˜2Ÿ9™9õ G5r   