MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / _validateTopK

Method _validateTopK

tensorflow/python/kernel_tests/topk_op_test.py:42–94  ·  view source on GitHub ↗
(self,
                    inputs,
                    k,
                    expected_values,
                    expected_indices,
                    sorted=True)

Source from the content-addressed store, hash-verified

40class TopKTest(test.TestCase):
41
42 def _validateTopK(self,
43 inputs,
44 k,
45 expected_values,
46 expected_indices,
47 sorted=True): # pylint: disable=redefined-builtin
48 np_expected_values = np.array(expected_values)
49 np_expected_indices = np.array(expected_indices)
50 with self.cached_session(use_gpu=True) as sess:
51 values_op, indices_op = nn_ops.top_k(inputs, k, sorted=sorted)
52 values, indices = self.evaluate([values_op, indices_op])
53
54 self.assertShapeEqual(np_expected_values, values_op)
55 self.assertShapeEqual(np_expected_indices, indices_op)
56
57 if sorted:
58 self.assertAllClose(np_expected_values, values)
59 # Do some special casing of equality of indices: if indices
60 # are not the same, but values are floating type, ensure that
61 # the values are within epsilon of each other.
62 if not np.issubdtype(np_expected_values.dtype, np.floating):
63 # Values are not floating point type; check indices exactly
64 self.assertAllEqual(np_expected_indices, indices)
65 else:
66 # Values are floating point; indices may be swapped for
67 # values near each other.
68 indices_not_equal = np_expected_indices != indices
69 if np.any(indices_not_equal):
70 values_unsure = values[indices_not_equal]
71 expected_values_unsure = expected_values[indices_not_equal]
72 self.assertAllClose(expected_values_unsure, values_unsure)
73 else:
74 np_inputs = np.array(inputs)
75
76 # Check that the indices are valid.
77 for result_index, src_index in np.ndenumerate(indices):
78 value = values[result_index]
79 expected_value = np_inputs[result_index[0], src_index]
80 np.testing.assert_almost_equal(value, expected_value)
81
82 # Check that if two elements are equal, the lower-index element appears
83 # first.
84 shape = values.shape
85 for batch_index in range(shape[0]):
86 for index in range(shape[1] - 1):
87 if np.isclose(values[batch_index, index],
88 values[batch_index, index + 1]):
89 self.assertLess(indices[batch_index, index],
90 indices[batch_index, index + 1])
91
92 # Now check the results, ignoring order.
93 self.assertAllEqual(np.sort(np_expected_indices), np.sort(indices))
94 self.assertAllClose(np.sort(np_expected_values), np.sort(values))
95
96 def testTop1(self):
97 inputs = [[0.1, 0.3, 0.2, 0.4], [0.1, 0.3, 0.3, 0.2]]

Callers 13

testTop1Method · 0.95
testTop2Method · 0.95
testTop3Method · 0.95
testTop1AllNanMethod · 0.95
_testLargeSortMethod · 0.95
_testLargeTopKMethod · 0.95
_testMediumTopKMethod · 0.95
testStableSortMethod · 0.95
testTopAllMethod · 0.95
testTop3UnsortedMethod · 0.95
testTop3VectorMethod · 0.95
testTensorKMethod · 0.95

Calls 7

assertShapeEqualMethod · 0.80
rangeFunction · 0.50
cached_sessionMethod · 0.45
evaluateMethod · 0.45
assertAllCloseMethod · 0.45
assertAllEqualMethod · 0.45
sortMethod · 0.45

Tested by

no test coverage detected