Beispiel #1
0
 def testCrfUnaryScore(self):
     inputs = np.array([[4, 5, -3], [3, -1, 3], [-1, 2, 1], [0, 0, 0]],
                       dtype=np.float32)
     for dtype in (np.int32, np.int64):
         tag_indices = np.array([1, 2, 1, 0], dtype=dtype)
         sequence_lengths = np.array(3, dtype=np.int32)
         unary_score = text.crf_unary_score(
             tag_indices=tf.expand_dims(tag_indices, 0),
             sequence_lengths=tf.expand_dims(sequence_lengths, 0),
             inputs=tf.expand_dims(inputs, 0))
         unary_score = tf.squeeze(unary_score, [0])
         tf_unary_score = self.evaluate(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)
Beispiel #2
0
def test_crf_unary_score(dtype):
    inputs = np.array([[4, 5, -3], [3, -1, 3], [-1, 2, 1], [0, 0, 0]], dtype=dtype)
    for dtype in (np.int32, np.int64):
        tag_indices = np.array([1, 2, 1, 0], dtype=dtype)
        sequence_lengths = np.array(3, dtype=np.int32)
        unary_score = text.crf_unary_score(
            tag_indices=tf.expand_dims(tag_indices, 0),
            sequence_lengths=tf.expand_dims(sequence_lengths, 0),
            inputs=tf.expand_dims(inputs, 0),
        )
        unary_score = tf.squeeze(unary_score, [0])
        expected_unary_score = sum(
            inputs[i][tag_indices[i]] for i in range(sequence_lengths)
        )
        test_utils.assert_allclose_according_to_type(unary_score, expected_unary_score)