o
    sÒh#  ã                   @   sZ   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„ dejƒZdS )é    )ÚAnyÚDictÚIterableN)ÚTensorÚnn)ÚSentenceTransformer)Úfullnamec                	       sx   e Zd Ze ¡ e ¡ fdedejdejddf‡ fdd„Zde	e
eef  d	edefd
d„Zde
eef fdd„Z‡  ZS )ÚCosineSimilarityLossÚmodelÚloss_fctÚcos_score_transformationÚreturnNc                    s$   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__© úo/var/www/html/alpaca_bot/venv/lib/python3.10/site-packages/sentence_transformers/losses/CosineSimilarityLoss.pyr      s   ;
zCosineSimilarityLoss.__init__Úsentence_featuresÚlabelsc                    sB   ‡ fdd„|D ƒ}ˆ   t |d |d ¡¡}ˆ  || ¡  d¡¡S )Nc                    s   g | ]	}ˆ   |¡d  ‘qS )Úsentence_embedding)r
   )Ú.0Úsentence_feature©r   r   r   Ú
<listcomp>L   s    z0CosineSimilarityLoss.forward.<locals>.<listcomp>r   é   éÿÿÿÿ)r   ÚtorchÚcosine_similarityr   ÚfloatÚview)r   r   r   Ú
embeddingsÚoutputr   r   r   ÚforwardK   s   zCosineSimilarityLoss.forwardc                 C   s   dt | jƒiS )Nr   )r   r   r   r   r   r   Úget_config_dictP   s   z$CosineSimilarityLoss.get_config_dict)Ú__name__Ú
__module__Ú__qualname__r   ÚMSELossÚIdentityr   ÚModuler   r   r   Ústrr   r$   r   r%   Ú__classcell__r   r   r   r   r	   
   s    üþýüû"@r	   )Útypingr   r   r   r   r   r   Ú)sentence_transformers.SentenceTransformerr   Úsentence_transformers.utilr   r+   r	   r   r   r   r   Ú<module>   s    