
    +sgE                        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/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       d/var/www/html/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sM]djj!123GHs
s..u/F/FzRS}V`abVc/de}}VV\\^%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    HT]]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     )bjjl.9bkkm	>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5299 G5r   