
    si#                     p    d dl mZmZ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 )    )AnyDictIterableN)Tensornn)SentenceTransformer)fullnamec            	            e Zd Z ej                     ej                    fdedej        dej        ddf fdZde	e
eef                  dedefd	Zde
eef         fd
Z xZS )CosineSimilarityLossmodelloss_fctcos_score_transformationreturnNc                     t          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]`

        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.

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

        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)superr   __init__r   r   r   )selfr   r   r   	__class__s       d/var/www/icac/venv/lib/python3.11/site-packages/sentence_transformers/losses/CosineSimilarityLoss.pyr   zCosineSimilarityLoss.__init__   s?    v 	"D))22444
 (@%%%    sentence_featureslabelsc                       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>L   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.forwardK   ss    ssssarsss
..u/FzRS}V`abVc/d/dee}}VV\\^^%8%8%<%<===r   c                 .    dt          | j                  iS )Nr   )r	   r   )r   s    r   get_config_dictz$CosineSimilarityLoss.get_config_dictP   s    HT]3344r   )__name__
__module____qualname__r   MSELossIdentityr   Moduler   r   r   strr   r'   r   r)   __classcell__)r   s   @r   r   r   
   s         )bjll.9bkmm	>A >A">A )>A #%)	>A
 
>A >A >A >A >A >A@>$sF{2C)D >f >Y_ > > > >
5c3h 5 5 5 5 5 5 5 5r   r   )typingr   r   r   r!   r   r   )sentence_transformers.SentenceTransformerr   sentence_transformers.utilr	   r/   r    r   r   <module>r6      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   