3
I4W^'  ã               @   sz   d dl mZ d dl mZ d dl mZ d dl mZ d dlmZ d dlmZ d dl	m
Z
 d dlZd dlZG d	d
„ d
e
eƒZdS )é    )Úabsolute_import)Údivision)Úprint_function)Úunicode_literals)Úschema)Ú
ModelLayer)ÚSamplingTrainableMixinNc            
       s>   e Zd Zd‡ fdd„	Zedd„ ƒZdd	„ Zed
d„ ƒZ‡  Z	S )ÚFCNÚfcé   c          	      s¤  t t| ƒj|||f|Ž t|tjƒs2tdj|ƒƒ‚t|j	ƒ d j
ƒdksPtdƒ‚|dksftdj|ƒƒ‚|| _tj|j	ƒ d j
|d d … ƒ}|dks¢tdj|ƒƒ‚d | _|d k	�r6t|ƒdksÆtdƒ‚|\}}|d k	sæ|d k	sætd	ƒ‚|d kpü|d kpü||k �std
ƒ‚i | _|d k	�r"|| jd< |d k	�r6|| jd< tjd| ƒ}|�rN|nd| |dœf}|�rh|nd| |dœf}tj|||ƒ| _| jd k�s¤t| jƒdk�rØ| jd||g|||	d�| _| jd|g|||
d�| _npg | _g | _xbt| jƒD ]T\}}| jj| jdj|ƒ||g|||	d�ƒ | jj| jdj|ƒ|g|||	d�ƒ �qðW |dk�rZ|f}n,t|j	ƒ d j
ƒd|d … }t||g ƒ}tjtj|f| jdƒƒ| _d S )NzIncorrect input type {}r   z1FC expects limited dimensions of the input tensorr   zaxis {} should >= 1.z'FC expects input dimensions > 0, got {}é   zUclip_param must be a tuple / list of length 2 and in the form of (clip_min, clip max)z8clip_min, and clip_max in clip_param cannot both be Nonez?clip_param = [clip_min, clip_max] must have clip_min < clip_maxÚminÚmaxg      ð?ZUniformFill)r   r   Úw)Ú
param_nameÚshapeZinitializerZ	optimizerZregularizerÚbzw_sub_{}zb_sub_{}Úoutput)Úsuperr	   Ú__init__Ú
isinstancer   ZScalarÚAssertionErrorÚformatÚlenZfield_typesr   ÚaxisÚnpÚprodÚ	clip_argsÚmathÚsqrtÚcalculate_fc_output_dimsÚoutput_dim_vecZcreate_paramr   r   Úw_vecÚb_vecÚ	enumerateÚappendÚlistÚtupleZfloat32Zget_next_blob_referenceÚoutput_schema)ÚselfÚmodelÚinput_recordZoutput_dimsZweight_initZ	bias_initZweight_optimZ
bias_optimÚnameZ
weight_regZbias_regZ
clip_paramÚmax_fc_sizer   ÚkwargsZ
input_dimsZclip_minZclip_maxZscaleÚidxÚ
output_dimZoutput_shape)Ú	__class__© ú8/tmp/pip-build-e66j97b2/torch/caffe2/python/layers/fc.pyr      s|     










zFC.__init__c             C   sz   |  s| dk rd S | |ks*t dj| |ƒƒ‚ttj| | ƒƒ}ttj|d | ƒd ƒ}|g|d  }|j|t|ƒ ƒ |S )Nr   zwCurrently we split along the output dimension. So we need max_fc_size >= input_dim. But, max_fc_size: {}, input_dim: {}r   )r   r   Úintr   Úfloorr%   Úsum)r-   Z	input_dimr0   Zoutput_dim_allowedZnum_fcr!   r2   r2   r3   r    g   s    
zFC.calculate_fc_output_dimsc                sh  | j d k	rN‡ fdd„|D ƒ}x,t||ƒD ]\}}ˆ j|g|gf| j Ž q(W |}| jd ksft| jƒdkr”ˆ j| jjƒ | | jjƒ fd| j	i| j
—Ž nÐ|d tt|ƒd ƒ… }|tt|ƒd ƒd … }t|ƒt|ƒksØt‚g }x`tt| jƒƒD ]N}	ˆ jdj|	ƒƒ}
|jˆ j| jjƒ ||	 ||	 g |
gfd| j	i| j
—Žƒ qìW ˆ j|| jjƒ | jjƒ d d g ƒ d S )	Nc                s   g | ]}ˆ j d t|ƒ ƒ‘qS )z
clipped_%s)ÚNextScopedBlobÚstr)Ú.0Úp)Únetr2   r3   ú
<listcomp>|   s   zFC._add_ops.<locals>.<listcomp>r   r   r   zoutput_sub_{}r   Z_concat_dims)r   ÚzipZClipr!   r   r	   r+   Zfield_blobsr(   r   r.   r4   r   Úranger7   r   r%   ZConcat)r)   r;   ÚparamsZclipped_paramsr:   Úcpr"   r#   Zoutput_blob_vecÚiZoutput_blobr2   )r;   r3   Ú_add_opsz   s.    

zFC._add_opsc             C   s4   | j d kst| j ƒdkr$| j| jgS | j| j S d S )Nr   )r!   r   r   r   r"   r#   )r)   r2   r2   r3   Úparam_blobsš   s    zFC.param_blobs)
NNNNr
   NNNNr   )
Ú__name__Ú
__module__Ú__qualname__r   Ústaticmethodr    rB   ÚpropertyrC   Ú__classcell__r2   r2   )r1   r3   r	      s      S r	   )Ú
__future__r   r   r   r   Zcaffe2.pythonr   Zcaffe2.python.layers.layersr   Z-caffe2.python.layers.sampling_trainable_mixinr   r   Znumpyr   r	   r2   r2   r2   r3   Ú<module>   s   