a
    –¥»a�  ã                   @   sB   d dl mZmZ d dlmZ ddd„Zdd„ Zdd	„ Zd
d„ ZdS )é    )ÚcoreÚscope)Ú
caffe2_pb2Nc                    s>   ˆ d u rt  ¡ ‰ ˆ dkr&| jd d … S ‡ fdd„| jD ƒS d S )NÚ c                    s   g | ]}|  ¡ ˆ kr|‘qS © )ZGetNameScope)Ú.0Úw©Ú	namescoper   úU/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/caffe2/python/helpers/train.pyÚ
<listcomp>   ó    z _get_weights.<locals>.<listcomp>)r   ZCurrentNameScopeÚweights)Úmodelr
   r   r	   r   Ú_get_weights   s
    r   c              	   K   sT   d|v r|d= | j jg |fdgdtjjt tjd¡dœ|¤Ž | jj	||fi |¤ŽS )NÚdevice_optioné   r   )ÚshapeÚvalueZdtyper   )
Úparam_init_netÚConstantFillr   ZDataTypeZINT64ÚDeviceOptionr   ÚCPUÚnetZIter)r   Úblob_outÚkwargsr   r   r   Úiter   s    þúù	r   c                 K   s´   d|v r|d nt  ¡ }|d u p*|jtjk}|s¢d|v r¢|d dkr¢| j |d |d d ¡}| j |d |d d ¡}| jj||g|fdt 	tjd¡i|¤Ž n| j ||¡ d S )Nr   Ztop_kr   r   Z_host)
r   ZCurrentDeviceScopeZdevice_typer   r   r   ZCopyGPUToCPUZAccuracyr   r   )r   Zblob_inr   r   ÚdevZis_cpuZ	pred_hostZ
label_hostr   r   r   Úaccuracy%   s     ÿþýür   c                 C   sj   |dkrdS | j jg ddg|d�}| j jg ddgdd�}t| ƒD ]$}| j| }| j ||||g|¡ q@dS )z”Adds a decay to weights in the model.

    This is a form of L2 regularization.

    Args:
        weight_decay: strength of the regularization
    g        NÚwdr   )r   r   ÚONEg      ð?)r   r   r   Zparam_to_gradr   ZWeightedSum)r   Zweight_decayr   r    ÚparamZgradr   r   r   Úadd_weight_decay:   s    
ÿ

þr"   )N)	Zcaffe2.pythonr   r   Zcaffe2.protor   r   r   r   r"   r   r   r   r   Ú<module>   s
   

