rasa.utils.tensorflow.crf
CrfDecodeForwardRnnCell Objects
Computes the forward decoding in a linear-chain CRF.
__init__
Initialize the CrfDecodeForwardRnnCell.
Arguments:
transition_params
- A [num_tags, num_tags] matrix of binary potentials. This matrix is expanded into a [1, num_tags, num_tags] in preparation for the broadcast summation occurring within the cell.
output_size
Returns count of tags.
build
Creates the variables of the layer.
call
Build the CrfDecodeForwardRnnCell.
Arguments:
inputs
- A [batch_size, num_tags] matrix of unary potentials.state
- A [batch_size, num_tags] matrix containing the previous step's score values.
Returns:
output
- A [batch_size, num_tags * 2] matrix of backpointers and scores.new_state
- A [batch_size, num_tags] matrix of new score values.
crf_decode_forward
Computes forward decoding in a linear-chain CRF.
Arguments:
inputs
- A [batch_size, num_tags] matrix of unary potentials.state
- A [batch_size, num_tags] matrix containing the previous step's score values.transition_params
- A [num_tags, num_tags] matrix of binary potentials.sequence_lengths
- A [batch_size] vector of true sequence lengths.
Returns:
output
- A [batch_size, num_tags * 2] matrix of backpointers and scores.new_state
- A [batch_size, num_tags] matrix of new score values.
crf_decode_backward
Computes backward decoding in a linear-chain CRF.
Arguments:
backpointers
- A [batch_size, num_tags] matrix of backpointer of next step (in time order).scores
- A [batch_size, num_tags] matrix of scores of next step (in time order).state
- A [batch_size, 1] matrix of tag index of next step.
Returns:
new_tags
- A [batch_size, num_tags] tensor containing the new tag indices.new_scores
- A [batch_size, num_tags] tensor containing the new score values.
crf_decode
Decode the highest scoring sequence of tags.
Arguments:
potentials
- A [batch_size, max_seq_len, num_tags] tensor of unary potentials.transition_params
- A [num_tags, num_tags] matrix of binary potentials.sequence_length
- A [batch_size] vector of true sequence lengths.
Returns:
decode_tags
- A [batch_size, max_seq_len] matrix, with dtypetf.int32
. Contains the highest scoring tag indices.decode_scores
- A [batch_size, max_seq_len] matrix, containing the score ofdecode_tags
.best_score
- A [batch_size] vector, containing the best score ofdecode_tags
.
crf_unary_score
Computes the unary scores of tag sequences.
Arguments:
tag_indices
- A [batch_size, max_seq_len] matrix of tag indices.sequence_lengths
- A [batch_size] vector of true sequence lengths.inputs
- A [batch_size, max_seq_len, num_tags] tensor of unary potentials.
Returns:
unary_scores
- A [batch_size] vector of unary scores.
crf_binary_score
Computes the binary scores of tag sequences.
Arguments:
tag_indices
- A [batch_size, max_seq_len] matrix of tag indices.sequence_lengths
- A [batch_size] vector of true sequence lengths.transition_params
- A [num_tags, num_tags] matrix of binary potentials.
Returns:
binary_scores
- A [batch_size] vector of binary scores.
crf_sequence_score
Computes the unnormalized score for a tag sequence.
Arguments:
inputs
- A [batch_size, max_seq_len, num_tags] tensor of unary potentials to use as input to the CRF layer.tag_indices
- A [batch_size, max_seq_len] matrix of tag indices for which we compute the unnormalized score.sequence_lengths
- A [batch_size] vector of true sequence lengths.transition_params
- A [num_tags, num_tags] transition matrix.
Returns:
sequence_scores
- A [batch_size] vector of unnormalized sequence scores.
crf_forward
Computes the alpha values in a linear-chain CRF.
See http://www.cs.columbia.edu/~mcollins/fb.pdf for reference.
Arguments:
inputs
- A [batch_size, num_tags] matrix of unary potentials.state
- A [batch_size, num_tags] matrix containing the previous alpha values.transition_params
- A [num_tags, num_tags] matrix of binary potentials. This matrix is expanded into a [1, num_tags, num_tags] in preparation for the broadcast summation occurring within the cell.sequence_lengths
- A [batch_size] vector of true sequence lengths.
Returns:
new_alphas
- A [batch_size, num_tags] matrix containing the new alpha values.
crf_log_norm
Computes the normalization for a CRF.
Arguments:
inputs
- A [batch_size, max_seq_len, num_tags] tensor of unary potentials to use as input to the CRF layer.sequence_lengths
- A [batch_size] vector of true sequence lengths.transition_params
- A [num_tags, num_tags] transition matrix.
Returns:
log_norm
- A [batch_size] vector of normalizers for a CRF.
crf_log_likelihood
Computes the log-likelihood of tag sequences in a CRF.
Arguments:
inputs
- A [batch_size, max_seq_len, num_tags] tensor of unary potentials to use as input to the CRF layer.tag_indices
- A [batch_size, max_seq_len] matrix of tag indices for which we compute the log-likelihood.sequence_lengths
- A [batch_size] vector of true sequence lengths.transition_params
- A [num_tags, num_tags] transition matrix, if available.
Returns:
log_likelihood
- A [batch_size]Tensor
containing the log-likelihood of each example, given the sequence of tag indices.transition_params
- A [num_tags, num_tags] transition matrix. This is either provided by the caller or created in this function.