o
    Ni                     @   s<   d dl Zd dlZd dlm  mZ G dd dejjj	Z
dS )    Nc                       s@   e Zd ZdZd fdd	Z fddZdd	 ZdddZ  ZS )CRFa  
    Conditional Random Field layer (tf.keras)
    `CRF` can be used as the last layer in a network (as a classifier). Input shape (features)
    must be equal to the number of classes the CRF can predict (a linear layer is recommended).

    Args:
        units: Positive integer, dimensionality of the output space. If it is None, the last dim of input must be num_classes
        chain_initializer: the initialize method for transitions, default orthogonal.
        regularizer: Regularizer for crf transitions, can be 'l1', 'l2' or other tensorflow regularizers.

    Input shape:
        nD tensor with shape `(batch_size, sentence length, features)` or `(batch_size, sentence length, num_classes)`.

    Output shape:
        in training:
            viterbi_sequence: the predicted sequence tags with shape `(batch_size, sentence length)`
            inputs: the input tensor of the CRF layer with shape `(batch_size, sentence length, num_classes)`
            sequence_lengths: true sequence length of inputs with shape `(batch_size)`
            self.transitions: the internal transition parameters of CRF with shape `(num_classes, num_classes)`
        in predicting:
            viterbi_sequence: the predicted sequence tags with shape `(batch_size, sentence length)`

    Masking
        This layer supports keras masking for input data with a variable number
        of timesteps. To introduce masks to your data,
        use an embedding layer with the `mask_zero` parameter
        set to `True` or add a Masking Layer before this Layer
    N
orthogonalc                    sr   t t| jdi | tjj|| _|| _d | _	d| _
d | _tjj | _|| _|d ur7tjj|| _d S d S )NT )superr   __init__tfkerasinitializersgetchain_initializerregularizertransitionssupports_maskingmaskmetricsAccuracyaccuracy_fnunitslayersDensedense)selfr   r   r   kwargs	__class__r   >/home/ubuntu/.local/lib/python3.10/site-packages/tf2crf/crf.pyr   $   s   zCRF.__init__c                    s    t t|  }|ddi |S )Nr   r   )r   r   
get_configupdate)r   configr   r   r   r   0   s
   zCRF.get_configc                 C   sF   t |dksJ | jr| j}n|d }| jd||g| j| jd| _d S )N   r   )nameshapeinitializerr   )lenr   
add_weightr   r   r   )r   input_shaper   r   r   r   build7   s   z	CRF.buildFc                 C   st   |d u rt t |dgdg}t |}tjt|ddd}| jr(| |}t	j
|| j|\}}|||| jfS )Nr      int32r    )axis)r   slicer"   onesKsumcastr   r   tfatext
crf_decoder   )r   inputsr   trainingraw_input_shapesequence_lengthsviterbi_sequence_r   r   r   callD   s   

zCRF.call)Nr   N)NF)	__name__
__module____qualname____doc__r   r   r'   r9   __classcell__r   r   r   r   r      s    r   )
tensorflowr   tensorflow_addonsr0   tensorflow.keras.backendr   backendr-   r   Layerr   r   r   r   r   <module>   s    