
    Ch8                        S SK Jr  S SKrS SKJr  S SKJr  S SKrS SKrS SK	J
r
  S SKJrJr  S SKJr  \R                  " \5      r " S S	\R$                  5      rg)
    )annotationsN)Iterable)Callable)version)Tensornn)SentenceTransformerc                     ^  \ rS rSrSSS\R
                  " 5       4               SU 4S jjjr      S	S jr\S
S j5       r	Sr
U =r$ )SoftmaxLoss   TFc                  > [         T	U ]  5         Xl        X0l        X@l        XPl        X`l        SnU(       a  US-  nU(       a  US-  nU(       a  US-  n[        R                  SU 35        [        R                  " X-  X1R                  S9U l        Xpl        [        R                  " [         R"                  5      [        R                  " S5      :  a  [        R%                  S5        gg)	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/sentence_transformer/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: )devicez4.43.0zSoftmaxLoss requires transformers >= 4.43.0 to work correctly. Otherwise, the classifier layer that maps embeddings to the labels cannot be updated. Consider updating transformers with `pip install transformers>=4.43.0`.N)super__init__model
num_labelsconcatenation_sent_repconcatenation_sent_difference!concatenation_sent_multiplicationloggerinfor   Linearr   
classifierloss_fctr   parsetransformers__version__warning)
selfr   sentence_embedding_dimensionr   r   r   r   r   num_vectors_concatenated	__class__s
            `/var/www/html/shao/venv/lib/python3.13/site-packages/sentence_transformers/losses/SoftmaxLoss.pyr   SoftmaxLoss.__init__   s    N 	
$&<#-J*1R.#$ !$)$($)$,$)$;<T;UVW))$CZXdXd
 !==112W]]85LLNNZ M    c                   U Vs/ sH  o0R                  U5      S   PM     nnUu  pV/ nU R                  (       a"  UR                  U5        UR                  U5        U R                  (       a'  UR                  [        R
                  " XV-
  5      5        U R                  (       a  UR                  XV-  5        [        R                  " US5      nU R                  U5      n	Ub"  U R                  XR                  S5      5      n
U
$ XI4$ s  snf )Nsentence_embeddingr   )r   r   appendr   torchabsr   catr   r   view)r!   sentence_featureslabelssentence_featurerepsrep_arep_bvectors_concatfeaturesoutputlosss              r%   forwardSoftmaxLoss.forwardt   s     \mm[lGW

+,-AB[lm&&!!%(!!%(--!!%))EM":;11!!%-099^Q/*==R9DK<- ns   Dc                    g)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%   citationSoftmaxLoss.citation   s    
r'   )r   r   r   r   r   r   r   )r   r	   r"   intr   r@   r   boolr   rA   r   rA   r   r   returnNone)r0   zIterable[dict[str, Tensor]]r1   r   rB   zTensor | tuple[Tensor, Tensor])rB   str)__name__
__module____qualname____firstlineno__r   CrossEntropyLossr   r:   propertyr>   __static_attributes____classcell__)r$   s   @r%   r   r      s     (,.227002`"` '*` 	`
 !%` (,` ,0` ` 
` `D !< FL 	' 6  r'   r   )
__future__r   loggingcollections.abcr   typingr   r,   r   	packagingr   r   r   )sentence_transformers.SentenceTransformerr	   	getLoggerrE   r   Moduler   r=   r'   r%   <module>rU      sB    "  $      I			8	$J")) Jr'   