U
    d9-                     @   sD   d dl mZmZ d dlmZ d dlmZ d dlZG dd deZ	dS )    )coreschema)
ModelLayer)TagsNc                       s6   e Zd Zd fdd	Zd	d
 Zdd Zdd Z  ZS )BatchLRLossbatch_lr_lossT              ?F皙?c                    s  t t| j|||f| || _ttdt fdt f|sFtd| _	|dkr\|dks`t|dksl|rd|ksxt| 
|| d| _	|| _|dkr|dkst|dkr|dkst||kst|| _|| _|	r|
rt|	| _|
| _|dkst|| _| jtjg ttj| d| _|| _|| _d| _|d	k rd| _| jd
| j dgdd	tjj df| j!j"d| _#| jd| j dgd|tjj df| j!j"d| _$| jd| j dgd|tjj df| j!j"d| _%d S )NlabellogitFr      
predictionToutputr	   z%s_task_gamma_curConstantFillvalueZdtype
param_nameshapeZinitializerZ	optimizerz%s_task_gammaz%s_task_gamma_lb)&superr   __init__average_lossr   Zis_schema_subsetStructZScalarAssertionErrorjsd_fuseinit_weighthomotopy_weightingpos_label_targetneg_label_targetlog_D_trickunjoined_lr_lossuncertainty_penaltytagsupdater   ZEXCLUDE_FROM_PREDICTIONnpfloat32Zget_next_blob_referenceoutput_schemafocal_gammastop_grad_in_focal_factorapply_exp_decaycreate_paramnamer   DataTypeFLOATmodelNoOptimtask_gamma_cur
task_gammatask_gamma_lb)selfr/   input_recordr,   r   
jsd_weightr   r   r   r    r!   r"   r(   r)   r2   r3   kwargs	__class__ F/tmp/pip-unpacked-wheel-ua33x9lu/caffe2/python/layers/batch_lr_loss.pyr      s    


zBatchLRLoss.__init__c                 C   s   |r| j d| j d d| jjd| _| j d| j dgddtjjdf| jjd| _| j d	| j dgdd
tjj	df| jjd| _
| j d| j dgddtjj	df| jjd| _n0| jd| j || _| jd	| j d
| | _
d S )Nz%s_mutex)ZCreateMutexr   z
%s_counterr   r   r   r   z%s_xent_weightr	   z%s_jsd_weightr   )r+   r,   r/   r0   mutexr   r-   ZINT64counterr.   xent_weightr6   Zadd_global_constant)r4   r6   r   r:   r:   r;   r   w   sZ    
  zBatchLRLoss.init_weightc                 C   s`   | | j| jg| jg |j| jg| jgddddd || jjd | jg| jg | j| jfS )Nr	   invgư>r
   )Zbase_lrpolicygammapowerZONE)	Z
AtomicIterr<   r=   ZLearningRater>   Subr/   Zglobal_constantsr6   )r4   netr:   r:   r;   update_weight   s      zBatchLRLoss.update_weightc                 C   s  | j  }|j||dtjjd}|j||ddgd}| jdksP| j	dkrn|j
||dd	| j	| jd
}|j| j  |g|d| j| jd}| jdkrl||g|dg}| j  }|||g|d}|||g|d}|j||ddd}|||g|d}|j||dt| jd}	| jdkrV||	g|dg}	|||	g|d}| jr|| j| jg| j || j| jg|d}
|||
g|d}| jr|| j  |g|d}| j r| !| |"|| j#|| j$g|d}n|}d| j j%kr| j & }|j||ddgd}|'|g|d }|j(|g|d!d"d#}|)|g|d$}|||g|d%}|j||d&t| j*d}|||g|d}|j||dd	d}d'| j j%krT| j + }| j j+, j-t.j/kr(|j||d( tjjd}||g|d)g}|||g|d*}| j0rp|1|| j23  n|4|| j23  d S )+NZlabel_float32)toZexpanded_labelr   )Zdimsr	   r   Zsmoothed_labelg      ?)	thresholdZ	low_valueZ
high_valueZcross_entropy)r    r!   r   Zlabel_stop_gradienty_plus_pyptwo_ypg       @)Zscaley_plus_p_sub_two_ypZy_plus_p_sub_two_yp_power)exponentTZfocal_factor_stop_gradientZfocallossxentZtask_gamma_cur_multiplierZexpdecayxentjsdlosslog_varianceZexpanded_log_varianceZneg_log_varianceZclipped_neg_log_varianceg      V@)maxZexp_neg_log_varianceZexp_neg_log_variance_lossZpenalized_unceratintyweightZ_float32Zweight_stop_gradientZweighted_cross_entropy)5r5   r   ZCastZNextScopedBlobr   r-   r.   Z
ExpandDimsr   r   Z	StumpFuncZSigmoidCrossEntropyWithLogitsr   r    r!   r(   ZStopGradientr   ZAddZMulZScalerC   ZPowfloatr)   r*   r1   r2   ZMaxr3   r   ZBernoulliJSDr   rE   ZWeightedSumr>   r6   fieldsrO   ZNegativeZClipZExpr"   rQ   Z
field_typebaser%   r&   r   ZAveragedLossr'   Zfield_blobsZReduceFrontSum)r4   rD   r   Zxentr   rH   rI   rJ   rK   Zfocal_factorZtask_gamma_multiplierrM   rN   Zlog_variance_blobZneg_log_variance_blobZexp_neg_log_variance_blobZexp_neg_log_variance_loss_blobZpenalized_uncertaintyZloss_2xZweight_blobr:   r:   r;   add_ops   s   


 
 

 

  

zBatchLRLoss.add_ops)r   Tr   r	   r   FFFr	   r   Fr	   r
   )__name__
__module____qualname__r   r   rE   rU   __classcell__r:   r:   r8   r;   r      s"                d1r   )
Zcaffe2.pythonr   r   Zcaffe2.python.layers.layersr   Zcaffe2.python.layers.tagsr   Znumpyr%   r   r:   r:   r:   r;   <module>   s   