a
    ==ic                     @   s8   d dl mZmZ d dlmZ d dlZG dd deZdS )    )coreschema)
ModelLayerNc                       sF   e Zd Zd fdd	Zdd Zdd Zdd	 Zd
d Zdd Z  Z	S )LabelSmoothlabel_smoothc                    sn   t t| j|||fi | || _t|tj }| 	| | 
| ttj| jff| d| _d S )NZsmoothed_label)superr   __init__labelnparrayZastypefloat32flattenset_dimset_smooth_matrixr   ZScalardimZget_next_blob_referenceoutput_schema)selfmodelr	   smooth_matrixnamekwargs	__class__ r/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/caffe2/python/layers/label_smooth.pyr      s    

zLabelSmooth.__init__c                 C   sH   |j }|dk| _| jrd| _n&t|d |ks4J tt|| _d S )N      )sizebinary_prob_labelr   r
   sqrtint)r   r   Znum_elementsr   r   r   r   +   s    
zLabelSmooth.set_dimc                 C   sf   | j s\| jjd| j || j| jfttjd| _	| jjd| j | jttj
d| _n|| _	d S )Nz%s_label_smooth_matrix)r   dtypez%s_label_dim)r   r   Zadd_global_constantr   Zreshaper   r
   r!   r   r   int64len)r   r   r   r   r   r   4   s    


zLabelSmooth.set_smooth_matrixc                 C   sj   | j  jtjkr:|d}|j|   g|gtjj	d n|   }|j
||  d| jd | jd d d S )Nfloat32_labeltog      ?r   r   )	thresholdZ	low_valueZ
high_value)r	   
field_typebaser
   r   NextScopedBlobCastr   DataTypeFLOATZ	StumpFuncr   r   )r   netr$   r   r   r   add_ops_for_binary_prob_labelC   s    
z)LabelSmooth.add_ops_for_binary_prob_labelc                 C   sz   | j  jtjkr:|d}|j|   g|gtjj	d n|   }|d}|
|| jg|g ||| jg|   d S )Nint64_labelr%   one_hot_label)r	   r(   r)   r
   r"   r*   r+   r   r,   ZINT64ZOneHotr#   ZMatMulr   r   )r   r.   r0   r1   r   r   r   add_ops_for_categorical_labelQ   s    

z)LabelSmooth.add_ops_for_categorical_labelc                 C   s    | j r| | n
| | d S )N)r   r/   r2   )r   r.   r   r   r   add_ops[   s    zLabelSmooth.add_ops)r   )
__name__
__module____qualname__r   r   r   r/   r2   r3   __classcell__r   r   r   r   r      s    	
r   )Zcaffe2.pythonr   r   Zcaffe2.python.layers.layersr   numpyr
   r   r   r   r   r   <module>   s   