3
I4W^4  ã               @   sL   d dl mZmZmZmZ d dlZd dlmZm	Z	m
Z
mZ G dd„ deƒZdS )é    )Úabsolute_importÚdivisionÚprint_functionÚunicode_literalsN)ÚbrewÚcoreÚmodel_helperÚ	recurrentc               @   sd   e Zd Zddd„Zddd„Zddd„Zdd	„ Zdd
d„Zdd„ Zdd„ Z	e
dd„ ƒZe
dd„ ƒZdS )ÚCRFWithLossNc             C   s\   || _ || _|d | _|sB| j jjg tjdƒg| j| jgddd�}|| _| j jj	| jƒ d S )Né   Zcrf_transitionsg      ð?)ÚshapeÚminÚmaxg      ð¿)
ÚmodelÚnum_classesÚnum_classes_paddedÚparam_init_netZUniformFillr   ÚScopedBlobReferenceÚtransitionsÚparamsÚappend)Úselfr   r   Ztransitions_blob© r   ú2/tmp/pip-build-e66j97b2/torch/caffe2/python/crf.pyÚ__init__   s    


zCRFWithLoss.__init__c             C   s4  | j jj| jtjdƒƒ}| j||| jƒ}tj	|| j j
| j j| jƒ}tj|| j j
| j j| jƒ}| j|||ƒ}| j jj||gtjdƒƒ}| j j
jg dgdd�}| j jj||gtjdƒdd�}	| j jj|gddd	d
�\}
}| j jj|
gtjdƒdgd�}
| j jj|tjdƒƒ}| j|
|	|ƒ}| j jj||gtjdƒƒ}|S )NÚtransitions_snapshotZ
path_totalé   r   )r   ÚvalueZrnn_initialT)Údense_gradientr   )Úpadding_widthÚend_padding_widthÚoutputsZrnn_input_data)ÚdimsÚtransitions_copyÚcrf_loss)r   ÚnetZCopyr   r   r   Ú_gather_entries_sumr   r
   Úpad_predictionsr   Ú
pad_labelsÚ_path_binary_scoresÚAddÚConstantFillÚGatherÚRemovePaddingÚ
ExpandDimsÚ_crf_forwardÚSub)r   ÚpredictionsÚlabelsÚseq_lengthsr   Zpath_unary_scoreZpath_binary_scoreZpath_total_scoreÚ
zero_indexÚinitial_stateZ
input_dataÚ_r#   Zall_paths_scoresZlossr   r   r   r$       s:    

zCRFWithLoss.crf_lossc             C   s¤   | j jj|gdddd�\}}| j jj|gdddd�\}}| j jj|g| jd�}| j jj||gƒ}| j jj||gƒ}| j jj|gƒ}	| j jj|	|gdd�}
| j j	|
ƒS )Nr   r   r   )r!   r   r    )r   T)r   )
r   r%   r-   r+   r   ZMulr*   ÚFlattenToVecr,   ÚReduceFrontSum)r   r2   r   r3   Z
column_idsr6   Zrow_idsZnum_columns_blobZflattened_idsZflattened_transitionsÚentriesr   r   r   r)   T   s    zCRFWithLoss._path_binary_scoresc       
      C   s~   | j jj|gdd�}| j jjg dg|d�}| j jj||gƒ}| j jj|ƒ}| j jj|ƒ}| j jj||gƒ}| j jj|gƒ}	|	S )NÚint64)Útor   )r   r   )	r   r%   ÚCastr   r+   ZOneHotr7   Z
DotProductr8   )
r   Zin_dataÚindicesZ
index_sizeZindex_size_blobZquery_one_hotZflattend_queryZflattend_dataZquery_scoresZ	final_sumr   r   r   r&   i   s    zCRFWithLoss._gather_entries_sumc       	      C   sx   | j |||ƒ}| jjj|gd| jfd�\}}| jjjg d| jgtjj	d�}| jjj
||gƒ}| jjj|df d�\}}|S )Nr   )r!   r   r   )r   r   Údtype)Úbuild_crf_netr   r%   ÚReshaper   r   r+   r   ÚDataTypeÚINT32ÚSortedSegmentRangeLogSumExp)	r   Ú
input_blobr5   r#   r3   Úout_lastr6   Úzero_segment_idZaccum_scorer   r   r   r/   u   s    
zCRFWithLoss._crf_forwardc          	      s€  d‰ ‡ fdd„}t jd| jd�}|jjtjdƒtjdƒ|ƒ\}}}|jjg |dƒgd	| j	gtj
jd
�}	|jj|	ƒ tj|||dƒgdd�}
|jj|
|dƒg| j	dd�}|jj||dƒg| j	dd�}|jj||g|dƒgƒ}|jj||g|dƒgddd�}|jj||dƒ|dƒg| j	| j	fd�\}}|jj||	g|dƒgƒ}|jj|ƒ |}tj| jj|j||fg||fg||iˆ dd�\}}|S )a©  
            Adds the crf_net recurrent operator to the model.

            model: model_helper.ModelHelper object new operators would be added
            to

            input_blob: the input sequence in a format T x N x D
            where T is sequence size, N - batch size and D - input dimention
            ##Only supports batch-size 1##

            seq_lengths: blob containing sequence lengths (unused)
            Zcrf_netc                s   dj tˆ ƒt| ƒƒS )Ú z{}/{})ÚformatÚstr)Úname)Úscoper   r   Ús˜   s    z$CRFWithLoss.build_crf_net.<locals>.sZcrf_step)rJ   Zparam_modelÚinput_tÚcell_t_prevrF   r   )r   r   r>   Úprev_transposer   r   )ZaxesÚ
prev_tiled)ZtilesÚaxisÚinput_t_tiledÚinput_with_prevZprev_with_transitions)Ú	broadcastZuse_grad_hackÚall_with_transitions_reshapedZall_with_transitions_orig)r   Úcell_t)r%   Zcell_netÚinputsZinitial_cell_inputsÚlinksrK   Zoutputs_with_grads)r   r   r   )r   )r   ZModelHelperr   r%   ZAddExternalInputsr   r   r   r+   r   rA   rB   ZAddExternalOutputr   Z	transposeZTiler*   r@   rC   ZAddExternalOutputsr	   Zrecurrent_net)r   rD   r5   r   rL   Z
step_modelrM   rN   r6   rF   rO   rP   rR   rS   Zall_with_transitionsrU   rV   Zcell_input_blobZout_allrE   r   )rK   r   r?   ˆ   sX    zCRFWithLoss.build_crf_netc             C   sF   dd„ }t j|| jj| jj| jƒ}| jjj|ƒ|| jgtj	dƒƒ}|S )Nc             S   sž  | d j }| d j }| d j }| d j}|d j|ƒ tj|ƒ}tj|tjd�}|d |d< xVtd|d ƒD ]D}tj||d  dƒ| }|| tj|dƒ ||< tj	|dƒ||< qlW tj	|d ƒg}	x*t
|dd … ƒD ]}
|	j|
|	d  ƒ qÖW |	jƒ  tj|ƒ}g }x`t|ƒD ]T\}}|| ||< tj	|ƒ}|j|ƒ || ||	|   ||	| < ||< |||< �qW |dd…dd	…f }|d j|jƒ ||d j d< d S )
Nr   r   )r>   r   .éÿÿÿÿrY   rY   éþÿÿÿ)Údatar   ZreshapeÚnpÚzerosÚint32ÚrangeZexpand_dimsr   ZargmaxÚreversedr   ÚreverseÚ	enumerate)rW   r!   r1   r   Zpredictions_shapeZtrellisZbackpointersÚtÚvZviterbiZbpZnew_predictionsZ	old_bestsÚiZw_predictionsZold_bestZorig_predictionsr   r   r   Úcrf_update_predictions_opÙ   s8    







zACRFWithLoss.update_predictions.<locals>.crf_update_predictions_opZpost_crf_classes)
r
   r'   r   r   r%   r   ÚPythonr   r   r   )r   Úclassesrf   Zpadded_classesZnew_classesr   r   r   Úupdate_predictionsØ   s    +zCRFWithLoss.update_predictionsc       
      C   s^   |}|d }|j g dg|d�}|j g dg|d�}|j| gdd�} |j|| |gddd�\}}	|S )Nr   )r   r   r:   )r;   r   r   )rQ   r!   )r+   r<   ÚConcat)
r2   Úinit_netr%   r   Zbos_iZeos_iZbos_i_bZeos_i_bZpadded_labelsr6   r   r   r   r(     s    zCRFWithLoss.pad_labelsc             C   s  d}t j|g| d|g gƒjt jƒ}t j|g| |dg gƒjt jƒ}|jg dd|d g|d�}|jg dd|d g|d�}|jg dgdd�}|j|j| gƒ|gƒ}|j|d	d
�}|j	|ƒ}	|j|	g|d�}
|j
|
dgd�}
|j| |
|
gddd�\}}|j|||gddd�\}}|S )Ng     @�@r   Úb_scoresr   r   )r   ÚvaluesÚe_scores)r   r   r^   )r;   )r   )r"   )r!   rQ   g     @�À)r\   ÚarrayZastypeZfloat32ZGivenTensorFillr+   r,   ZShaper<   ZLengthsRangeFillr.   rj   )r1   rk   r%   r   Z	low_scorerl   rn   r4   ÚlengthZt_rangeÚpaddingZpadded_predictionsr6   Zpadded_predictions_concatr   r   r   r'     s(    
zCRFWithLoss.pad_predictions)N)N)N)N)Ú__name__Ú
__module__Ú__qualname__r   r$   r)   r&   r/   r?   ri   Ústaticmethodr(   r'   r   r   r   r   r
      s   

4

P5
r
   )Ú
__future__r   r   r   r   Znumpyr\   Zcaffe2.pythonr   r   r   r	   Úobjectr
   r   r   r   r   Ú<module>   s   
