U
    d                     @   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 k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   ?/tmp/pip-unpacked-wheel-ua33x9lu/caffe2/python/helpers/train.py
<listcomp>   s      z _get_weights.<locals>.<listcomp>)r   ZCurrentNameScopeweights)modelr
   r   r	   r   _get_weights   s
    r   c              	   K   sP   d|kr|d= | j jg |fdgdtjjttjdd| | jj	||f|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|kr|d nt  }|d kp*|jtjk}|sd|k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 )zAdds 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
   

