
    NgE                        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dS )
    )annotations)Iterable)AnyN)Tensornn)SentenceTransformer)fullnamec                  j     e Zd Z ej                     ej                    fd fdZddZddZ xZ	S )CosineSimilarityLossmodelr   loss_fct	nn.Modulecos_score_transformationreturnNonec                r    t                                                       || _        || _        || _        dS )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__r   r   r   )selfr   r   r   	__class__s       m/var/www/html/ai-engine/env/lib/python3.11/site-packages/sentence_transformers/losses/CosineSimilarityLoss.pyr   zCosineSimilarityLoss.__init__   s8    v 	
 (@%%%    sentence_featuresIterable[dict[str, Tensor]]labelsr   c                      fd|D             }                      t          j        |d         |d                             }                     ||                                                    d                    S )Nc                F    g | ]}                     |          d          S )sentence_embedding)r   ).0sentence_featurer   s     r   
<listcomp>z0CosineSimilarityLoss.forward.<locals>.<listcomp>O   s-    sssM]djj!1223GHsssr   r      )r   torchcosine_similarityr   floatview)r   r   r   
embeddingsoutputs   `    r   forwardzCosineSimilarityLoss.forwardN   ss    ssssarsss
..u/FzRS}V`abVc/d/dee}}VV\\^^%8%8%<%<===r   dict[str, Any]c                .    dt          | j                  iS )Nr   )r	   r   )r   s    r   get_config_dictz$CosineSimilarityLoss.get_config_dictS   s    HT]3344r   )r   r   r   r   r   r   r   r   )r   r   r   r   r   r   )r   r+   )
__name__
__module____qualname__r   MSELossIdentityr   r*   r-   __classcell__)r   s   @r   r   r      s         )bjll.9bkmm	>A >A >A >A >A >A >A@> > > >
5 5 5 5 5 5 5 5r   r   )
__future__r   collections.abcr   typingr   r$   r   r   )sentence_transformers.SentenceTransformerr   sentence_transformers.utilr	   Moduler    r   r   <module>r;      s    " " " " " " $ $ $ $ $ $                I I I I I I / / / / / /G5 G5 G5 G5 G529 G5 G5 G5 G5 G5r   