def testCrfBinaryScore(self): tag_indices = np.array([1, 2, 1, 0], dtype=np.int32) transition_params = np.array([[-3, 5, -2], [3, 4, 1], [1, 2, 1]], dtype=np.float32) sequence_lengths = np.array(3, dtype=np.int32) binary_score = text.crf_binary_score( tag_indices=tf.expand_dims(tag_indices, 0), sequence_lengths=tf.expand_dims(sequence_lengths, 0), transition_params=tf.constant(transition_params)) binary_score = tf.squeeze(binary_score, [0]) tf_binary_score = self.evaluate(binary_score) expected_binary_score = sum(transition_params[tag_indices[i], tag_indices[i + 1]] for i in range(sequence_lengths - 1)) self.assertAllClose(tf_binary_score, expected_binary_score)
def test_crf_binary_score(dtype): tag_indices = np.array([1, 2, 1, 0], dtype=np.int32) transition_params = np.array([[-3, 5, -2], [3, 4, 1], [1, 2, 1]], dtype=dtype) sequence_lengths = np.array(3, dtype=np.int32) binary_score = text.crf_binary_score( tag_indices=tf.expand_dims(tag_indices, 0), sequence_lengths=tf.expand_dims(sequence_lengths, 0), transition_params=tf.constant(transition_params), ) binary_score = tf.squeeze(binary_score, [0]) expected_binary_score = sum( transition_params[tag_indices[i], tag_indices[i + 1]] for i in range(sequence_lengths - 1) ) test_utils.assert_allclose_according_to_type(binary_score, expected_binary_score)