
    Ng+                        d dl mZ d dlZd dlZd dlZd dlmZ d dlZd dl	Z	d dl	m
Z
mZ d dlmZmZ d dlmZ  G d dej                  Z G d	 d
ej                  Z G d d          ZdS )    )annotationsN)Iterable)Tensornn)InputExampleutil)SentenceTransformerc                  D     e Zd ZdZd fdZddZedd            Z xZS )ContrastiveTensionLossa
  
    This loss expects only single sentences, without any labels. Positive and negative pairs are automatically created via random sampling,
    such that a positive pair consists of two identical sentences and a negative pair consists of two different sentences. An independent
    copy of the encoder model is created, which is used for encoding the first sentence of each pair. The original encoder model encodes the
    second sentence. The embeddings are compared and scored using the generated labels (1 if positive, 0 if negative) using the binary cross
    entropy objective.

    Note that you must use the `ContrastiveTensionDataLoader` for this loss. The `pos_neg_ratio` of the ContrastiveTensionDataLoader can be
    used to determine the number of negative pairs per positive pair.

    Generally, :class:`ContrastiveTensionLossInBatchNegatives` is recommended over this loss, as it gives a stronger training signal.

    Args:
        model: SentenceTransformer model

    References:
        * Semantic Re-Tuning with Contrastive Tension: https://openreview.net/pdf?id=Ov_sMNau-PF
        * `Unsupervised Learning > CT <../../examples/unsupervised_learning/CT/README.html>`_

    Inputs:
        +------------------+--------+
        | Texts            | Labels |
        +==================+========+
        | single sentences | none   |
        +------------------+--------+

    Relations:
        * :class:`ContrastiveTensionLossInBatchNegatives` uses in-batch negative sampling, which gives a stronger training signal than this loss.

    Example:
        ::

            from sentence_transformers import SentenceTransformer, losses
            from sentence_transformers.losses import ContrastiveTensionDataLoader

            model = SentenceTransformer('all-MiniLM-L6-v2')
            train_examples = [
                'This is the 1st sentence',
                'This is the 2nd sentence',
                'This is the 3rd sentence',
                'This is the 4th sentence',
                'This is the 5th sentence',
                'This is the 6th sentence',
                'This is the 7th sentence',
                'This is the 8th sentence',
                'This is the 9th sentence',
                'This is the final sentence',
            ]

            train_dataloader = ContrastiveTensionDataLoader(train_examples, batch_size=3, pos_neg_ratio=3)
            train_loss = losses.ContrastiveTensionLoss(model=model)

            model.fit(
                [(train_dataloader, train_loss)],
                epochs=10,
            )
    modelr	   returnNonec                    t                                                       || _        t          j        |          | _        t          j        d          | _        d S )Nsum)	reduction)	super__init__model2copydeepcopymodel1r   BCEWithLogitsLoss	criterion)selfr   	__class__s     o/var/www/html/ai-engine/env/lib/python3.11/site-packages/sentence_transformers/losses/ContrastiveTensionLoss.pyr   zContrastiveTensionLoss.__init__K   sJ    mE**->>>    sentence_featuresIterable[dict[str, Tensor]]labelsr   c                   t          |          \  }}|                     |          d         }|                     |          d         }t          j        |d d d f         |d d d d d f                                       d                              d          }|                     ||                    |                    }|S )Nsentence_embedding)tupler   r   torchmatmulsqueezer   type_as)	r   r   r    sentence_features1sentence_features2reps_1reps_2
sim_scoreslosss	            r   forwardzContrastiveTensionLoss.forwardQ   s    167H1I1I../001EF/001EF L4&AAAt*<==EEbIIQQRTUU 	 ~~j&..*D*DEEr   strc                    dS Nar  
@inproceedings{carlsson2021semantic,
    title={Semantic Re-tuning with Contrastive Tension},
    author={Fredrik Carlsson and Amaru Cuba Gyllensten and Evangelia Gogoulou and Erik Ylip{"a}{"a} Hellqvist and Magnus Sahlgren},
    booktitle={International Conference on Learning Representations},
    year={2021},
    url={https://openreview.net/forum?id=Ov_sMNau-PF}
}
 r   s    r   citationzContrastiveTensionLoss.citation]        r   )r   r	   r   r   r   r   r    r   r   r   r   r0   )	__name__
__module____qualname____doc__r   r/   propertyr5   __classcell__r   s   @r   r   r      s|        8 8t? ? ? ? ? ?
 
 
 
 	 	 	 X	 	 	 	 	r   r   c                  P     e Zd Zdej        fd fdZddZedd            Z xZ	S )&ContrastiveTensionLossInBatchNegativesg      4@r   r	   scalefloatr   r   c                F   t                                                       || _        t          j        |          | _        || _        t          j                    | _	        t          j
        t          j        g           t          j        |          z            | _        dS )a\  
        This loss expects only single sentences, without any labels. Positive and negative pairs are automatically created via random sampling,
        such that a positive pair consists of two identical sentences and a negative pair consists of two different sentences. An independent
        copy of the encoder model is created, which is used for encoding the first sentence of each pair. The original encoder model encodes the
        second sentence. Unlike :class:`ContrastiveTensionLoss`, this loss uses the batch negative sampling strategy, i.e. the negative pairs
        are sampled from the batch. Using in-batch negative sampling gives a stronger training signal than the original :class:`ContrastiveTensionLoss`.
        The performance usually increases with increasing batch sizes.

        Note that you should not use the `ContrastiveTensionDataLoader` for this loss, but just a normal DataLoader with `InputExample` instances.
        The two texts of each `InputExample` instance should be identical.

        Args:
            model: SentenceTransformer model
            scale: Output of similarity function is multiplied by scale
                value
            similarity_fct: similarity function between sentence
                embeddings. By default, cos_sim. Can also be set to dot
                product (and then set scale to 1)

        References:
            - Semantic Re-Tuning with Contrastive Tension: https://openreview.net/pdf?id=Ov_sMNau-PF
            - `Unsupervised Learning > CT (In-Batch Negatives) <../../examples/unsupervised_learning/CT_In-Batch_Negatives/README.html>`_

        Relations:
            * :class:`ContrastiveTensionLoss` does not select negative pairs in-batch, resulting in a weaker training signal than this loss.

        Inputs:
            +------------------------+--------+
            | Texts                  | Labels |
            +========================+========+
            | (anchor, anchor) pairs | none   |
            +------------------------+--------+

        Example:
            ::

                from sentence_transformers import SentenceTransformer, losses
                from torch.utils.data import DataLoader

                model = SentenceTransformer('all-MiniLM-L6-v2')
                train_examples = [
                    InputExample(texts=['This is a positive pair', 'Where the distance will be minimized'], label=1),
                    InputExample(texts=['This is a negative pair', 'Their distance will be increased'], label=0),
                ]
                train_examples = [
                    InputExample(texts=['This is the 1st sentence', 'This is the 1st sentence']),
                    InputExample(texts=['This is the 2nd sentence', 'This is the 2nd sentence']),
                    InputExample(texts=['This is the 3rd sentence', 'This is the 3rd sentence']),
                    InputExample(texts=['This is the 4th sentence', 'This is the 4th sentence']),
                    InputExample(texts=['This is the 5th sentence', 'This is the 5th sentence']),
                ]

                train_dataloader = DataLoader(train_examples, shuffle=True, batch_size=32)
                train_loss = losses.ContrastiveTensionLossInBatchNegatives(model=model)

                model.fit(
                    [(train_dataloader, train_loss)],
                    epochs=10,
                )
        N)r   r   r   r   r   r   similarity_fctr   CrossEntropyLosscross_entropy_loss	Parameterr%   onesnploglogit_scale)r   r   rB   rE   r   s       r   r   z/ContrastiveTensionLossInBatchNegatives.__init__k   sz    z 	mE**,"$"5"7"7<
2(FGGr   r   r   r    r   c                   t          |          \  }}|                     |          d         }|                     |          d         }|                     ||          | j                                        z  }t          j        t          t          |                    t          j
        |j                  }|                     ||          |                     |                                |          z   dz  S )Nr"   )dtypedevice   )r$   r   r   rE   rL   expr%   tensorrangelenlongrO   rG   t)r   r   r    r)   r*   embeddings_aembeddings_bscoress           r   r/   z.ContrastiveTensionLossInBatchNegatives.forward   s    167H1I1I..{{#5667KL{{#5667KL$$\<@@4CSCWCWCYCYYeCKK00
6=YYY''77$:Q:QRXRZRZR\R\^d:e:eeijjjr   r0   c                    dS r2   r3   r4   s    r   r5   z/ContrastiveTensionLossInBatchNegatives.citation   r6   r   )r   r	   rB   rC   r   r   r7   r8   )
r9   r:   r;   r   cos_simr   r/   r=   r5   r>   r?   s   @r   rA   rA   j   s        BFW[Wc BH BH BH BH BH BH BHHk k k k 	 	 	 X	 	 	 	 	r   rA   c                  "    e Zd ZddZd Zd ZdS )ContrastiveTensionDataLoader   c                    || _         || _        || _        d | _        | j        | j        z  dk    rt	          d| d| d          d S )Nr   z@ContrastiveTensionDataLoader was loaded with a pos_neg_ratio of z and a batch size of z7. The batch size must be divisible by the pos_neg_ratio)	sentences
batch_sizepos_neg_ratio
collate_fn
ValueError)r   r`   ra   rb   s       r   r   z%ContrastiveTensionDataLoader.__init__   s|    "$*?T//144 {S`  {  {  xB  {  {  {   54r   c              #    K   t          j        | j                   d}g }|dz   t          | j                  k     r| j        |         }t          |          | j        z  dk    r|dz  }| j        |         }d}n| j        |         }d}|dz  }|                    t          ||g|                     t          |          | j        k    r"| j        |                     |          n|V  g }|dz   t          | j                  k     d S d S )Nr      )textslabel)	randomshuffler`   rT   rb   appendr   ra   rc   )r   sentence_idxbatchs1s2rh   s         r   __iter__z%ContrastiveTensionDataLoader.__iter__   s!     t~&&&QT^!4!444-B5zzD..22!^L1^L1ALLLRHEBBBCCC5zzT_,,040Kdooe,,,QVVVV QT^!4!4444444r   c                d    t          j        t          | j                  d| j        z  z            S )NrP   )mathfloorrT   r`   ra   r4   s    r   __len__z$ContrastiveTensionDataLoader.__len__   s(    z#dn--T_1DEFFFr   N)r^   )r9   r:   r;   r   rp   rt   r3   r   r   r]   r]      sK        	 	 	 	  ,G G G G Gr   r]   )
__future__r   r   rr   ri   collections.abcr   numpyrJ   r%   r   r   sentence_transformersr   r   )sentence_transformers.SentenceTransformerr	   Moduler   rA   r]   r3   r   r   <module>r{      sA   " " " " " "    $ $ $ $ $ $              4 4 4 4 4 4 4 4 I I I I I IW W W W WRY W W WtX X X X XRY X X XB#G #G #G #G #G #G #G #G #G #Gr   