o
    S"g                     @  s   d dl mZ d dlmZ d dlmZ d dlmZ d dlm	  m
Z d dlmZm	Z	 d dlmZ G dd	 d	eZG d
d de	jZdS )    )annotations)Iterable)Enum)AnyN)Tensornn)SentenceTransformerc                   @  s(   e Zd ZdZdd Zdd Zdd ZdS )SiameseDistanceMetricz#The metric for the contrastive lossc                 C     t j| |ddS )N   pFpairwise_distancexy r   n/mnt/skqttb/ctump_chatbot/chatbot/lib/python3.10/site-packages/sentence_transformers/losses/ContrastiveLoss.py<lambda>       zSiameseDistanceMetric.<lambda>c                 C  r
   )N   r   r   r   r   r   r   r      r   c                 C  s   dt | | S )Nr   )r   cosine_similarityr   r   r   r   r      r   N)__name__
__module____qualname____doc__	EUCLIDEAN	MANHATTANCOSINE_DISTANCEr   r   r   r   r	      s
    r	   c                      sJ   e Zd Zejddfd fddZdddZdddZedddZ	  Z
S )ContrastiveLoss      ?Tmodelr   marginfloatsize_averageboolreturnNonec                   s&   t    || _|| _|| _|| _dS )a	  
        Contrastive loss. Expects as input two texts and a label of either 0 or 1. If the label == 1, then the distance between the
        two embeddings is reduced. If the label == 0, then the distance between the embeddings is increased.

        Args:
            model: SentenceTransformer model
            distance_metric: Function that returns a distance between
                two embeddings. The class SiameseDistanceMetric contains
                pre-defined metrices that can be used
            margin: Negative samples (label == 0) should have a distance
                of at least the margin value.
            size_average: Average by the size of the mini-batch.

        References:
            * Further information: http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf
            * `Training Examples > Quora Duplicate Questions <../../../examples/sentence_transformer/training/quora_duplicate_questions/README.html>`_

        Requirements:
            1. (anchor, positive/negative) pairs

        Inputs:
            +-----------------------------------------------+------------------------------+
            | Texts                                         | Labels                       |
            +===============================================+==============================+
            | (anchor, positive/negative) pairs             | 1 if positive, 0 if negative |
            +-----------------------------------------------+------------------------------+

        Relations:
            - :class:`OnlineContrastiveLoss` is similar, but uses hard positive and hard negative pairs.
              It often yields better results.

        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."],
                    "label": [1, 0],
                })
                loss = losses.ContrastiveLoss(model)

                trainer = SentenceTransformerTrainer(
                    model=model,
                    train_dataset=train_dataset,
                    loss=loss,
                )
                trainer.train()
        N)super__init__distance_metricr$   r#   r&   )selfr#   r,   r$   r&   	__class__r   r   r+      s
   
;
zContrastiveLoss.__init__dict[str, Any]c                 C  sF   | j j}tt D ]\}}|| j krd| } nq
|| j| jdS )NzSiameseDistanceMetric.)r,   r$   r&   )r,   r   varsr	   itemsr$   r&   )r-   distance_metric_namenamevaluer   r   r   get_config_dictW   s   

zContrastiveLoss.get_config_dictsentence_featuresIterable[dict[str, Tensor]]labelsr   c                   s    fdd|D }t |dksJ |\}} ||}d| |d d|  t j| d   } jr>| S |	 S )Nc                   s   g | ]	}  |d  qS )sentence_embedding)r#   ).0sentence_featurer-   r   r   
<listcomp>a   s    z+ContrastiveLoss.forward.<locals>.<listcomp>r   r"   r   )
lenr,   r%   powr   relur$   r&   meansum)r-   r7   r9   reps
rep_anchor	rep_other	distanceslossesr   r=   r   forward`   s   2zContrastiveLoss.forwardstrc                 C  s   dS )Na~  
@inproceedings{hadsell2006dimensionality,
    author={Hadsell, R. and Chopra, S. and LeCun, Y.},
    booktitle={2006 IEEE Computer Society Conference on Computer Vision and Pattern Recognition (CVPR'06)},
    title={Dimensionality Reduction by Learning an Invariant Mapping},
    year={2006},
    volume={2},
    number={},
    pages={1735-1742},
    doi={10.1109/CVPR.2006.100}
}
r   r=   r   r   r   citationj   s   zContrastiveLoss.citation)r#   r   r$   r%   r&   r'   r(   r)   )r(   r0   )r7   r8   r9   r   r(   r   )r(   rJ   )r   r   r   r	   r    r+   r6   rI   propertyrK   __classcell__r   r   r.   r   r!      s    
A
	
r!   )
__future__r   collections.abcr   enumr   typingr   torch.nn.functionalr   
functionalr   torchr   )sentence_transformers.SentenceTransformerr   r	   Moduler!   r   r   r   r   <module>   s    