Example #1
0
 def testCrfUnaryScore(self):
     inputs = np.array([[4, 5, -3], [3, -1, 3], [-1, 2, 1], [0, 0, 0]],
                       dtype=np.float32)
     tag_indices = np.array([1, 2, 1, 0], dtype=np.int32)
     sequence_lengths = np.array(3, dtype=np.int32)
     with self.test_session() as sess:
         unary_score = crf.crf_unary_score(
             tag_indices=array_ops.expand_dims(tag_indices, 0),
             sequence_lengths=array_ops.expand_dims(sequence_lengths, 0),
             inputs=array_ops.expand_dims(inputs, 0))
         unary_score = array_ops.squeeze(unary_score, [0])
         tf_unary_score = sess.run(unary_score)
         expected_unary_score = sum(inputs[i][tag_indices[i]]
                                    for i in range(sequence_lengths))
         self.assertAllClose(tf_unary_score, expected_unary_score)
Example #2
0
 def testCrfUnaryScore(self):
   inputs = np.array(
       [[4, 5, -3], [3, -1, 3], [-1, 2, 1], [0, 0, 0]], dtype=np.float32)
   tag_indices = np.array([1, 2, 1, 0], dtype=np.int32)
   sequence_lengths = np.array(3, dtype=np.int32)
   with self.test_session() as sess:
     unary_score = crf.crf_unary_score(
         tag_indices=array_ops.expand_dims(tag_indices, 0),
         sequence_lengths=array_ops.expand_dims(sequence_lengths, 0),
         inputs=array_ops.expand_dims(inputs, 0))
     unary_score = array_ops.squeeze(unary_score, [0])
     tf_unary_score = sess.run(unary_score)
     expected_unary_score = sum(inputs[i][tag_indices[i]]
                                for i in range(sequence_lengths))
     self.assertAllClose(tf_unary_score, expected_unary_score)
Example #3
0
def crf_sequence_score(inputs, tag_indices, sequence_lengths, transitions):
    """Computes the unnormalized score for a tag sequence.

  Args:
    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.
  """

    # Compute the scores of the given tag sequence.
    unary_scores = crf_unary_score(tag_indices, sequence_lengths, inputs)
    binary_scores = crf_binary_score(tag_indices, sequence_lengths,
                                     transitions)
    sequence_scores = unary_scores + binary_scores
    return sequence_scores