o
    hN
                     @   sZ   d Z ddlZddlZddlmZ ddlm  mZ ddlm  mZ	 G dd dej
ZdS )a  
LargeMarginInSoftmax, from the article

@inproceedings{kobayashi2019bmvc,
  title={Large Margin In Softmax Cross-Entropy Loss},
  author={Takumi Kobayashi},
  booktitle={Proceedings of the British Machine Vision Conference (BMVC)},
  year={2019}
}

implementation from

https://github.com/tk1980/LargeMarginInSoftmax

There is no license specifically chosen; they just ask people to cite the paper if the work is useful.
    Nc                       s.   e Zd ZdZ		d
 fdd	Zdd	 Z  ZS )LargeMarginInSoftmaxLossa  
    This combines the Softmax Cross-Entropy Loss (nn.CrossEntropyLoss) and the large-margin inducing
    regularization proposed in
       T. Kobayashi, "Large-Margin In Softmax Cross-Entropy Loss." In BMVC2019.

    This loss function inherits the parameters from nn.CrossEntropyLoss except for `reg_lambda` and `deg_logit`.
    Args:
         reg_lambda (float, optional): a regularization parameter. (default: 0.3)
         deg_logit (bool, optional): underestimate (degrade) the target logit by -1 or not. (default: False)
                                     If True, it realizes the method that incorporates the modified loss into ours
                                     as described in the above paper (Table 4).
    333333?Nmeanc                    s*   t t| j|||||d || _|| _d S )N)weightsize_averageignore_indexreduce	reduction)superr   __init__
reg_lambda	deg_logit)selfr   r   r   r   r   r	   r
   	__class__ a/var/www/html/env_mimamsha/lib/python3.10/site-packages/stanza/models/common/large_margin_loss.pyr   (   s
   
z!LargeMarginInSoftmaxLoss.__init__c           	      C   s   | d}| d}tj|dd}d|t||f< | jd ur%|| j|  }tj||| j| j| j	d}|d|  }dtj
|ddd	|d   tj|dd d	|  jdd }| j	d
kr`| }n| j	dkrj| }n| j	dkrq|}|| j|  S )Nr      F)requires_grad)r   r   r
   g    .Ag      ?)dimg      ?sumr   none)sizetorch
zeros_likeranger   Fcross_entropyr   r   r
   softmaxlog_softmaxr   r   r   )	r   inputtargetNCMasklossXregr   r   r   forward/   s$   


<




z LargeMarginInSoftmaxLoss.forward)r   NNNr   Nr   )__name__
__module____qualname____doc__r   r)   __classcell__r   r   r   r   r      s    r   )r-   mathr   torch.nnnntorch.nn.initinittorch.nn.functional
functionalr   CrossEntropyLossr   r   r   r   r   <module>   s    