a
    ==ic                     @   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   l/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/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}|sd|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 )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
   

