
    rh                    b    d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ  G d de	      Zy)	    )annotations)IterableN)Tensor)CosineSimilarityLoss)SparseEncoderc                  x     e Zd Z ej                          ej
                         f	 	 	 	 	 	 	 d fdZddZ xZS )SparseCosineSimilarityLossc                6    d|_         t        | 	  |||      S )a5
  
        SparseCosineSimilarityLoss 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: SparseEncoder 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).

        Requirements:
            - Need to be used in SpladeLoss or CSRLoss as a loss function.
            - Sentence pairs with corresponding similarity scores in range `[0, 1]`

        Inputs:
            +--------------------------------+------------------------+
            | Texts                          | Labels                 |
            +================================+========================+
            | (sentence_A, sentence_B) pairs | float similarity score |
            +--------------------------------+------------------------+

        Relations:
            - :class:`SparseAnglELoss` is :class:`SparseCoSENTLoss` with ``pairwise_angle_sim`` as the metric, rather than ``pairwise_cos_sim``.

        Example:
            ::

                from datasets import Dataset
                from sentence_transformers.sparse_encoder import SparseEncoder, SparseEncoderTrainer, losses

                model = SparseEncoder("distilbert/distilbert-base-uncased")
                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.SpladeLoss(
                    model=model,
                    loss=losses.SparseCosineSimilarityLoss(model),
                    document_regularizer_weight=5e-5,
                    use_document_regularizer_only=True,
                )

                trainer = SparseEncoderTrainer(model=model, train_dataset=train_dataset, loss=loss)
                trainer.train()
        cosine)loss_fctcos_score_transformation)similarity_fn_namesuper__init__)selfmodelr   r   	__class__s       /var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/sentence_transformers/sparse_encoder/losses/SparseCosineSimilarityLoss.pyr   z#SparseCosineSimilarityLoss.__init__   s&    v $, wSkll    c                    t        d      )NzWSparseCosineSimilarityLoss should not be used alone. Use it with SpladeLoss or CSRLoss.)AttributeError)r   sentence_featureslabelss      r   forwardz"SparseCosineSimilarityLoss.forwardK   s    vwwr   )r   r   r   	nn.Moduler   r   returnNone)r   zIterable[dict[str, Tensor]]r   r   r   r   )	__name__
__module____qualname__nnMSELossIdentityr   r   __classcell__)r   s   @r   r	   r	      sR     )bjjl.9bkkm	<m<m <m #,	<m
 
<m|xr   r	   )
__future__r   collections.abcr   torch.nnr!   torchr   1sentence_transformers.losses.CosineSimilarityLossr   2sentence_transformers.sparse_encoder.SparseEncoderr   r	    r   r   <module>r,      s(    " $   R L@x!5 @xr   