
    siH                         d dl Z d dlmZmZmZmZmZ d dlZd dlmZm	Z	 d dl
mZ  e j        e          Z G d de	j                  ZdS )    N)CallableDictIterableTupleUnion)Tensornn)SentenceTransformerc                        e Zd Zddd ej                    fdedededededed	ed
df fdZ	de
eeef                  ded
eeeeef         f         fdZed
efd            Z xZS )SoftmaxLossTFmodelsentence_embedding_dimension
num_labelsconcatenation_sent_repconcatenation_sent_difference!concatenation_sent_multiplicationloss_fctreturnNc                 z   t          t          |                                            || _        || _        || _        || _        || _        d}|r|dz  }|r|dz  }|r|dz  }t          	                    d
                    |                     t          j        ||z  ||j                  | _        || _        dS )a\  
        This loss was used in our SBERT publication (https://arxiv.org/abs/1908.10084) to train the SentenceTransformer
        model on NLI data. It adds a softmax classifier on top of the output of two transformer networks.

        :class:`MultipleNegativesRankingLoss` is an alternative loss function that often yields better results,
        as per https://arxiv.org/abs/2004.09813.

        Args:
            model (SentenceTransformer): The SentenceTransformer model.
            sentence_embedding_dimension (int): The dimension of the sentence embeddings.
            num_labels (int): The number of different labels.
            concatenation_sent_rep (bool): Whether to concatenate vectors u,v for the softmax classifier. Defaults to True.
            concatenation_sent_difference (bool): Whether to add abs(u-v) for the softmax classifier. Defaults to True.
            concatenation_sent_multiplication (bool): Whether to add u*v for the softmax classifier. Defaults to False.
            loss_fct (Callable): Custom pytorch loss function. If not set, uses nn.CrossEntropyLoss(). Defaults to nn.CrossEntropyLoss().

        References:
            - Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks: https://arxiv.org/abs/1908.10084
            - `Training Examples > Natural Language Inference <../../examples/training/nli/README.html>`_

        Requirements:
            1. sentence pairs with a class label

        Inputs:
            +---------------------------------------+--------+
            | Texts                                 | Labels |
            +=======================================+========+
            | (sentence_A, sentence_B) pairs        | class  |
            +---------------------------------------+--------+

        Example:
            ::

                from sentence_transformers import SentenceTransformer, SentenceTransformerTrainer, losses
                from datasets import Dataset

                model = SentenceTransformer("microsoft/mpnet-base")
                train_dataset = Dataset.from_dict({
                    "sentence1": [
                        "A person on a horse jumps over a broken down airplane.",
                        "A person on a horse jumps over a broken down airplane.",
                        "A person on a horse jumps over a broken down airplane.",
                        "Children smiling and waving at camera",
                    ],
                    "sentence2": [
                        "A person is training his horse for a competition.",
                        "A person is at a diner, ordering an omelette.",
                        "A person is outdoors, on a horse.",
                        "There are children present.",
                    ],
                    "label": [1, 2, 0, 0],
                })
                loss = losses.SoftmaxLoss(model, model.get_sentence_embedding_dimension(), num_labels=3)

                trainer = SentenceTransformerTrainer(
                    model=model,
                    train_dataset=train_dataset,
                    loss=loss,
                )
                trainer.train()
        r         z'Softmax loss: #Vectors concatenated: {})deviceN)superr   __init__r   r   r   r   r   loggerinfoformatr	   Linearr   
classifierr   )
selfr   r   r   r   r   r   r   num_vectors_concatenated	__class__s
            [/var/www/icac/venv/lib/python3.11/site-packages/sentence_transformers/losses/SoftmaxLoss.pyr   zSoftmaxLoss.__init__   s    N 	k4  ))+++
$&<#-J*1R.#$ ! 	*$)$( 	*$)$, 	*$)$=DDE]^^___)$'CCZX]Xd
 
 
 !    sentence_featureslabelsc                      fd|D             }|\  }}g } j         r*|                    |           |                    |            j        r*|                    t          j        ||z
                        j        r|                    ||z             t          j        |d          }                     |          }|+                     ||	                    d                    }	|	S ||fS )Nc                 F    g | ]}                     |          d          S )sentence_embedding)r   ).0sentence_featurer    s     r#   
<listcomp>z'SoftmaxLoss.forward.<locals>.<listcomp>k   s-    mmmGW

+,,-ABmmmr$   r   )
r   appendr   torchabsr   catr   r   view)
r    r%   r&   repsrep_arep_bvectors_concatfeaturesoutputlosss
   `         r#   forwardzSoftmaxLoss.forwardh   s    nmmm[lmmmu& 	)!!%(((!!%(((- 	<!!%)EEM":":;;;1 	1!!%%-0009^Q//**==R99DK<r$   c                     dS )Na  
@inproceedings{reimers-2019-sentence-bert,
    title = "Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks",
    author = "Reimers, Nils and Gurevych, Iryna",
    booktitle = "Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing",
    month = "11",
    year = "2019",
    publisher = "Association for Computational Linguistics",
    url = "https://arxiv.org/abs/1908.10084",
}
 )r    s    r#   citationzSoftmaxLoss.citation   s    
 
r$   )__name__
__module____qualname__r	   CrossEntropyLossr
   intboolr   r   r   r   strr   r   r   r:   propertyr=   __classcell__)r"   s   @r#   r   r      s-        (,.2270R022Y! Y!"Y! '*Y! 	Y!
 !%Y! (,Y! ,0Y! Y! 
Y! Y! Y! Y! Y! Y!v !)$sF{*;!< FL 	vuVV^,,	-       6 #    X    r$   r   )loggingtypingr   r   r   r   r   r/   r   r	   )sentence_transformers.SentenceTransformerr
   	getLoggerr>   r   Moduler   r<   r$   r#   <module>rL      s     9 9 9 9 9 9 9 9 9 9 9 9 9 9          I I I I I I		8	$	$C C C C C") C C C C Cr$   