U
    d                     @   s,   d dl mZ d dlmZ G dd deZdS )    )schema)
ModelLayerc                       s*   e Zd ZdZd fdd	Zdd Z  ZS )	BlobWeightedSumz_
    This layer implements the weighted sum:
    weighted element-wise sum of input blobs.
    Nblob_weighted_sumc                    s
  t tj||f| j _tj_jdksBtdt	 d j
dks`tdtfddtdjD std	 d j
 rjt kstdjt nd	gj   fd
dtjD _t	 d d_d S )N   z1BlobWeightedSum expects more than one input blobsr   z>BlobWeightedSum expects limited dimensions of the input tensorc                 3   s*   | ]"}   d  j   | jkV  qdS )r   N)field_typesshape).0i)input_record J/tmp/pip-unpacked-wheel-ua33x9lu/caffe2/python/layers/blob_weighted_sum.py	<genexpr>'   s   z+BlobWeightedSum.__init__.<locals>.<genexpr>z0Shape of input blobs should be the same shape {}zNthe size of init_weights should be the same as input blobs, expects {}, got {}g      ?c              
      s6   g | ].}j d |dgddt | ifdqS )zw_{}r   ZConstantFillvalue)
param_namer   ZinitializerZ	optimizer)Zcreate_paramformatfloat)r	   idx)init_weightsselfweight_optimr   r   
<listcomp>6   s   z,BlobWeightedSum.__init__.<locals>.<listcomp>Zblob_weighted_sum_out)superr   __init__r   Zfield_blobsblobslenZnum_weightsAssertionErrorr   r   allranger   weightsr   ZScalarZget_next_blob_referenceoutput_schema)r   modelr   r   r   namekwargs	__class__)r   r   r   r   r   r      s>    	

 	
zBlobWeightedSum.__init__c                 C   s,   |j dd t| j| jD |  dd d S )Nc                 S   s   g | ]}|D ]}|qqS r   r   )r	   pairxr   r   r   r   F   s       z+BlobWeightedSum.add_ops.<locals>.<listcomp>T)Z	grad_on_w)ZWeightedSumzipr   r   r    )r   netr   r   r   add_opsD   s
    zBlobWeightedSum.add_ops)NNr   )__name__
__module____qualname____doc__r   r*   __classcell__r   r   r$   r   r      s      3r   N)Zcaffe2.pythonr   Zcaffe2.python.layers.layersr   r   r   r   r   r   <module>   s   