o
    {%jŽ  ã                   @   s`   d dl Z d dlmZ d dlm  mZ ddd„Zdd„ ZG dd„ dejƒZ	G d	d
„ d
ejƒZ
dS )é    Nç-Cëâ6?c                 C   sB  | j d }| j d }|j d }tj| ddd�} tj|ddd�}| | jddd� } ||jddd� }tj| |gdd�}|tj|| | jd� }|tj|| jd� }|tj|| jd� }	|j| |d  | }
| j|  |d  | }|j| |d  |	 }tj	 
|
¡\}}tj	 
|¡\}}tj	 
|¡\}}d	|| |  }|jd
d�S )z×
    Computes Mutual Information strictly between two continuous feature spaces.
    L2 normalization strictly prevents the network from cheating by scaling magnitudes.
    No discrete labels (Y) are used here.
    r   é   é   )ÚpÚdimT)Úkeepdim)r   ©Údeviceg      à?ç        )Úmin)ÚshapeÚFÚ	normalizeÚmeanÚtorchÚcatÚeyer	   ÚTÚlinalgÚslogdetÚclamp)ÚXÚZÚepsÚNÚdxZdzZXZZreg_xyZreg_xZreg_zZSigma_XZZSigma_XZSigma_ZÚ_Zld_xzZld_xZld_zÚmi© r   ú/root/disk_3tb/HAC/pid_loss.pyÚgaussian_mi_unsupervised   s&   


r    c           	      C   sú   | dur| j n	|dur|j n|j }tjd|d�}tjd|d�}| dur.|dur.|t| |ƒ8 }| dur=|dur=|t| |ƒ8 }|durL|durL|t||ƒ8 }|dur[|dur[|t||ƒ7 }|durj|durj|t||ƒ7 }|dury|dury|t||ƒ7 }||fS )z•
    Calculates PID solely based on cross-modal interactions.
    Dynamically handles missing modalities (e.g. if only video and flow are used).
    Nr
   r   )r	   r   Útensorr    )	ÚvsÚvuÚas_ÚauÚfsÚfur	   Úloss_sharedÚloss_uniquer   r   r   Úcompute_stable_pid*   s    "r*   c                       s,   e Zd ZdZd‡ fdd„	Zd	dd„Z‡  ZS )
ÚPIDProjectionHeadsz£
    Projects the high-dimensional split embeddings into a lower-dimensional
    space to ensure the covariance matrix remains full-rank given the batch size.
    é   c              
      sd   t ƒ  ¡  || _t ¡ | _| ¡ D ]\}}t t ||¡t 	|¡tj
dd�t ||¡¡| j|< qd S )NT)Úinplace)ÚsuperÚ__init__Úproj_dimÚnnÚ
ModuleDictÚheadsÚitemsÚ
SequentialÚLinearÚBatchNorm1dÚReLU)ÚselfÚdim_mapr0   ÚkÚin_dim©Ú	__class__r   r   r/   P   s   




üþzPIDProjectionHeads.__init__Nc                 C   s¤   i }|d ur| j d |ƒ|d< |d ur| j d |ƒ|d< |d ur)| j d |ƒ|d< |d ur6| j d |ƒ|d< |d urC| j d |ƒ|d< |d urP| j d |ƒ|d< |S )Nr"   r#   Úasr%   r&   r'   )r3   )r9   r"   r#   r$   r%   r&   r'   Úoutr   r   r   Úforward]   s   zPIDProjectionHeads.forward)r,   ©NNNNNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r/   rA   Ú__classcell__r   r   r=   r   r+   K   s    r+   c                       s2   e Zd Z		d‡ fdd„	Z			d	dd„Z‡  ZS )
ÚPIDSplittingLossç      ð?TNc                    s2   t ƒ  ¡  || _|| _|| _|| _|| _|| _d S )N)r.   r/   Úpid_projÚalpha_sharedÚalpha_uniqueÚ	use_videoÚ	use_audioÚuse_flow)r9   rJ   rK   rL   rM   rN   rO   Úgammar=   r   r   r/   i   s   

zPIDSplittingLoss.__init__c                 C   sœ   | j ||||||d�}| d¡}| d¡}	| d¡}
| d¡}| d¡}| d¡}t||	|
|||ƒ\}}| j| | j|  }| ¡ | ¡ | ¡ dœ}||fS )	N)r"   r#   r$   r%   r&   r'   r"   r#   r?   r%   r&   r'   )Zpid_loss_sharedZpid_loss_uniqueZpid_total_loss)rJ   Úgetr*   rK   rL   Úitem)r9   Úv_sharedÚv_uniqueÚa_sharedÚa_uniqueÚf_sharedÚf_uniqueZ
proj_featsZvs_pZvu_pZas_pZau_pZfs_pZfu_pr(   r)   Ú
total_lossÚlog_dictr   r   r   rA   u   s(   ý





ÿýzPIDSplittingLoss.forward)rI   rI   TTTNrB   )rC   rD   rE   r/   rA   rG   r   r   r=   r   rH   h   s    ÿþrH   )r   )r   Útorch.nnr1   Útorch.nn.functionalÚ
functionalr   r    r*   ÚModuler+   rH   r   r   r   r   Ú<module>   s    
%!