def crf_log_norm(inputs, sequence_lengths, transition_params):
"""Computes the normalization for a CRF.
Args:
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.
"""
# Split up the first and rest of the inputs in preparation for the forward
# algorithm.
first_input = array_ops.slice(inputs, [0, 0, 0], [-1, 1, -1])
first_input = array_ops.squeeze(first_input, [1])
rest_of_input = array_ops.slice(inputs, [0, 1, 0], [-1, -1, -1])
# Compute the alpha values in the forward algorithm in order to get the
# partition function.
forward_cell = CrfForwardRnnCell(transition_params)
'''
tf.nn.rnn creates an unrolled graph for a fixed RNN length. That means,
if you call tf.nn.rnn with inputs having 200 time steps you are creating a
static graph with 200 RNN steps. First, graph creation is slow. Second,
you’re unable to pass in longer sequences (> 200) than you’ve originally
specified.tf.nn.dynamic_rnn solves this. It uses a tf.While loop to dynamically
construct the graph when it is executed. That means graph creation is faster
and you can feed batches of variable size.
'''
_, alphas = rnn.dynamic_rnn(
cell=forward_cell,
inputs=rest_of_input,
sequence_length=sequence_lengths - 1,
initial_state=first_input,
dtype=dtypes.float32)
'''
'''
log_norm = math_ops.reduce_logsumexp(alphas, [1])
return log_norm
crf.py 文件源码
python
阅读 24
收藏 0
点赞 0
评论 0
评论列表
文章目录