a
    ãÀh  ã                   @  sj   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	„ d	ejƒZdS )
é    )Úannotations)ÚIterable)ÚAnyN)ÚTensorÚnn)ÚSentenceTransformer)Úfullnamec                      sh   e Zd Ze ¡ e ¡ fdddddœ‡ fdd„Zdddd	œd
d„Zddddœdd„Zddœdd„Z	‡  Z
S )ÚCosineSimilarityLossr   z	nn.ModuleÚNone)ÚmodelÚloss_fctÚcos_score_transformationÚreturnc                   s    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/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__r   r   r   )Úselfr   r   r   ©Ú	__class__© úm/var/www/html/assistant/venv/lib/python3.9/site-packages/sentence_transformers/losses/CosineSimilarityLoss.pyr      s    ;
zCosineSimilarityLoss.__init__zIterable[dict[str, Tensor]]r   )Úsentence_featuresÚlabelsr   c                   s   ‡ fdd„|D ƒ}ˆ   ||¡S )Nc                   s   g | ]}ˆ   |¡d  ‘qS )Zsentence_embedding)r   )Ú.0Zsentence_feature©r   r   r   Ú
<listcomp>O   ó    z0CosineSimilarityLoss.forward.<locals>.<listcomp>)Úcompute_loss_from_embeddings)r   r   r   Ú
embeddingsr   r   r   ÚforwardN   s    zCosineSimilarityLoss.forwardzlist[Tensor])r   r   r   c                 C  s0   |   t |d |d ¡¡}|  || ¡  d¡¡S )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   ÚtorchZcosine_similarityr   ÚfloatÚview)r   r   r   Úoutputr   r   r   r   S   s    z1CosineSimilarityLoss.compute_loss_from_embeddingszdict[str, Any])r   c                 C  s   dt | jƒiS )Nr   )r   r   r   r   r   r   Úget_config_dicta   s    z$CosineSimilarityLoss.get_config_dict)Ú__name__Ú
__module__Ú__qualname__r   ZMSELossZIdentityr   r   r   r%   Ú__classcell__r   r   r   r   r	      s   ü@r	   )Ú
__future__r   Úcollections.abcr   Útypingr   r!   r   r   Z)sentence_transformers.SentenceTransformerr   Zsentence_transformers.utilr   ÚModuler	   r   r   r   r   Ú<module>   s   