U
    tPÓh  ã                   @  s4   d dl mZ d dlZd dlmZ G dd„ deƒZdS )é    )ÚannotationsN)Ú_Lossc                      s<   e Zd ZdZddddœ‡ fdd„Zdddd	œd
d„Z‡  ZS )ÚBarlowTwinsLossaÄ  
    The Barlow Twins cost function takes the representations extracted by a neural network from two
    distorted views and seeks to make the cross-correlation matrix of the two representations tend
    towards identity. This encourages the neural network to learn similar representations with the least
    amount of redundancy. This cost function can be used in particular in multimodal learning to work on
    representations from two modalities. The most common use case is for unsupervised learning, where data
    augmentations are used to generate 2 distorted views of the same sample to force the encoder to
    extract useful features for downstream tasks.

    Zbontar, Jure, et al. "Barlow Twins: Self-Supervised Learning via Redundancy Reduction" International
    conference on machine learning. PMLR, 2020. (http://proceedings.mlr.press/v139/zbontar21a/zbontar21a.pdf)

    Adapted from:
        https://github.com/facebookresearch/barlowtwins

    ç{®Gázt?ÚfloatÚNone)ÚlambdÚreturnc                   s   t ƒ  ¡  || _dS )aj  
        Args:
            lamb: Can be any float to handle the informativeness and invariance trade-off. Ideally set to 5e-3.

        Raises:
            ValueError: When an input of dimension length > 2 is passed
            ValueError: When input and target are of different shapes
            ValueError: When batch size is less than or equal to 1

        N)ÚsuperÚ__init__r   )Úselfr   ©Ú	__class__© úN/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/losses/barlow_twins.pyr   $   s    
zBarlowTwinsLoss.__init__ztorch.Tensor)ÚinputÚtargetr	   c           	      C  s<  t |jƒdkst |jƒdkr6td|j› d|j› dƒ‚|j|jkr\td|j› d|j› dƒ‚| d¡dkr~td	| d¡› ƒ‚t | j¡ |j¡}|jd }|| 	d¡ | 
d¡ d
¡ }|| 	d¡ | 
d¡ d
¡ }t | ¡ |¡| }|tj| d¡|jd  d¡}|tj| d¡|jd ¡    |9  < | ¡ S )zr
        Args:
            input: the shape should be B[F].
            target: the shape should be B[F].
        é   zLEither target or input has dimensions greater than 2 where target shape is (z) and input shape is (ú)z"ground truth has differing shape (z) from input (r   é   zHBatch size must be greater than 1 to compute Barlow Twins Loss, but got gíµ ÷Æ°>)Údevice)ÚlenÚshapeÚ
ValueErrorÚsizeÚtorchÚ	as_tensorr   Útor   ÚmeanÚstdÚaddÚmmÚtÚeyeÚpow_ÚboolÚsum)	r   r   r   Zlambd_tensorÚ
batch_sizeÚ
input_normZtarget_normÚcZc_diffr   r   r   Úforward2   s$    ÿÿ
 (zBarlowTwinsLoss.forward)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r*   Ú__classcell__r   r   r   r   r      s   r   )Ú
__future__r   r   Útorch.nn.modules.lossr   r   r   r   r   r   Ú<module>   s   