
    Ch                        S SK Jr  S SKJr  S SKJr  S SKrS SKJrJr  S SK	J
r
  S SKJr   " S S	\R                  5      rg)
    )annotations)Iterable)AnyN)Tensornn)SentenceTransformer)fullnamec                     ^  \ rS rSr\R
                  " 5       \R                  " 5       4       SU 4S jjjrSS jrS	S jr	S
S jr
SrU =r$ )CosineSimilarityLoss   c                F   > [         TU ]  5         Xl        X l        X0l        g)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       i/var/www/html/shao/venv/lib/python3.13/site-packages/sentence_transformers/losses/CosineSimilarityLoss.pyr   CosineSimilarityLoss.__init__   s!    v 	
 (@%    c                r    U Vs/ sH  o0R                  U5      S   PM     nnU R                  XB5      $ s  snf )Nsentence_embedding)r   compute_loss_from_embeddings)r   sentence_featureslabelssentence_feature
embeddingss        r   forwardCosineSimilarityLoss.forwardN   s>    arsarM]jj!123GHar
s00DD ts   4c                    U R                  [        R                  " US   US   5      5      nU R                  X2R	                  5       R                  S5      5      $ )z
Compute the CosineSimilarity loss from embeddings.

Args:
    embeddings: List of embeddings
    labels: Labels indicating the similarity scores of the pairs

Returns:
    Loss value
r      )r   torchcosine_similarityr   floatview)r   r   r   outputs       r   r   1CosineSimilarityLoss.compute_loss_from_embeddingsS   sM     ..u/F/FzRS}V`abVc/de}}V\\^%8%8%<==r   c                0    S[        U R                  5      0$ )Nr   )r	   r   )r   s    r   get_config_dict$CosineSimilarityLoss.get_config_dicta   s    HT]]344r   )r   r   r   )r   r   r   	nn.Moduler   r-   returnNone)r   zIterable[dict[str, Tensor]]r   r   r.   r   )r   zlist[Tensor]r   r   r.   r   )r.   zdict[str, Any])__name__
__module____qualname____firstlineno__r   MSELossIdentityr   r   r   r+   __static_attributes____classcell__)r   s   @r   r   r      sc     !jjl.0kkm	>A">A >A #,	>A
 
>A >A@E
>5 5r   r   )
__future__r   collections.abcr   typingr   r$   r   r   )sentence_transformers.SentenceTransformerr   sentence_transformers.utilr	   Moduler    r   r   <module>r?      s,    " $    I /U5299 U5r   