def testBadTarget(self): predictions = [[0.1, 0.3, 0.2, 0.4], [0.1, 0.2, 0.3, 0.4]] target = [0, 80000] with self.test_session(): with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, "target.*out of range"): nn_ops.in_top_k(predictions, target, 2).eval()
def _validateInTopK(self, predictions, target, k, expected): np_ans = np.array(expected) with self.cached_session(use_gpu=True): precision = nn_ops.in_top_k(predictions, target, k) out = self.evaluate(precision) self.assertAllClose(np_ans, out) self.assertShapeEqual(np_ans, precision)
def _validateInTopK(self, predictions, target, k, expected): np_ans = np.array(expected) with self.test_session(): precision = nn_ops.in_top_k(predictions, target, k) out = precision.eval() self.assertAllClose(np_ans, out) self.assertShapeEqual(np_ans, precision)
def testTensorK(self): predictions = [[0.1, 0.3, 0.2, 0.4], [0.1, 0.2, 0.3, 0.4]] target = [0, 2] k = constant_op.constant(3) np_ans = np.array([False, True]) with self.cached_session(): precision = nn_ops.in_top_k(predictions, target, k) out = self.evaluate(precision) self.assertAllClose(np_ans, out) self.assertShapeEqual(np_ans, precision)
def testTensorK(self): predictions = [[0.1, 0.3, 0.2, 0.4], [0.1, 0.2, 0.3, 0.4]] target = [0, 2] k = constant_op.constant(3) np_ans = np.array([False, True]) with self.cached_session(): precision = nn_ops.in_top_k(predictions, target, k) out = precision.eval() self.assertAllClose(np_ans, out) self.assertShapeEqual(np_ans, precision)
def in_topk(predictions, targets, k=k): return nn_ops.in_top_k(predictions, targets, k)