o  iã@s4ddlmZddlZddlmZGdd„deƒZdS)é)Ú annotationsN)Ú_Losscs.eZdZdZdd‡fdd„ Zdd d „Z‡ZS)Ú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?ÚlambdÚfloatÚreturnÚNonecstƒ ¡||_dS)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__©ú[/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/losses/barlow_twins.pyr $s zBarlowTwinsLoss.__init__Úinputú torch.TensorÚtargetc Cs<t|jƒdkst|jƒdkrtd|j›d|j›dƒ‚|j|jkr.td|j›d|j›dƒ‚| d¡dkr?td | d¡›ƒ‚t |j¡ |j¡}|jd}||  d¡|  d¡  d ¡}||  d¡|  d¡  d ¡}t  |  ¡|¡|}|tj| d¡|jd  d¡}|tj| d¡|jd  ¡|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 rrZ lambd_tensorÚ batch_sizeZ input_normZ target_normÚcZc_diffrrrÚforward2s,ÿÿÿ ÿ  (zBarlowTwinsLoss.forward)r)rrrr )rrrrrr)Ú__name__Ú __module__Ú __qualname__Ú__doc__r r*Ú __classcell__rrr rrsr)Ú __future__rrÚtorch.nn.modules.lossrrrrrrÚs