예제 #1
0
    def test_best_span(self):
        bound = 5
        start_pl = tf.placeholder(tf.float32, (None, None))
        end_pl = tf.placeholder(tf.float32, (None, None))
        best_span, best_val = best_span_from_bounds(start_pl, end_pl, bound)
        sess = self.sess

        for i in range(0, 20):
            rng = np.random.RandomState(i)
            l = rng.randint(50, 200)
            batch = rng.randint(1, 60)

            start = rng.uniform(size=(batch, l))
            end = rng.uniform(size=(batch, l))

            # exp since the tf version uses logits and the py version use probabilities
            expected_span, expected_score = zip(*[
                get_best_span_bounded(np.exp(start[i]), np.exp(end[i]), bound)
                for i in range(batch)
            ])

            actual_span, actuals_score = sess.run([best_span, best_val], {
                start_pl: start,
                end_pl: end
            })

            self.assertTrue(np.all(np.array(expected_span) == actual_span))
            self.assertTrue(np.allclose(expected_score, np.exp(actuals_score)))
예제 #2
0
 def get_best_span(self, bound: int):
     if bound in self._bound_predictions:
         return self._bound_predictions[bound]
     else:
         pred = best_span_from_bounds(self.start_logits, self.end_logits,
                                      bound)
         self._bound_predictions[bound] = pred
         return pred
예제 #3
0
 def get_best_span(self, bound: int):
     return best_span_from_bounds(self.start_logits, self.end_logits, bound)