diff options
author | Benoit Steiner <bsteiner@google.com> | 2016-07-22 12:54:03 -0800 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2016-07-22 14:04:04 -0700 |
commit | 075868639f79d77657a7d268fc4762d7a90c370b (patch) | |
tree | 08fcb3e79bde8fa9a0006be04233f8067bd29b1e /tensorflow/python/kernel_tests/xent_op_test.py | |
parent | ef5d941c164b22a9be47e4f5bd7c90ba7c83e984 (diff) |
Use 32bit floats to compute cross entropies. 16 bit floats aren't accurate
enough to deal with more than a few labels
Change: 128208074
Diffstat (limited to 'tensorflow/python/kernel_tests/xent_op_test.py')
-rw-r--r-- | tensorflow/python/kernel_tests/xent_op_test.py | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/tensorflow/python/kernel_tests/xent_op_test.py b/tensorflow/python/kernel_tests/xent_op_test.py index 9e7c563547..70b3bdcbb4 100644 --- a/tensorflow/python/kernel_tests/xent_op_test.py +++ b/tensorflow/python/kernel_tests/xent_op_test.py @@ -21,6 +21,8 @@ from __future__ import print_function import numpy as np import tensorflow as tf +from tensorflow.python.ops import gen_nn_ops + class XentTest(tf.test.TestCase): @@ -38,8 +40,8 @@ class XentTest(tf.test.TestCase): def _testXent(self, np_features, np_labels, use_gpu=False): np_loss, np_backprop = self._npXent(np_features, np_labels) with self.test_session(use_gpu=use_gpu) as sess: - loss = tf.nn.softmax_cross_entropy_with_logits(np_features, np_labels) - backprop = loss.op.outputs[1] + loss, backprop = gen_nn_ops._softmax_cross_entropy_with_logits( + np_features, np_labels) tf_loss, tf_backprop = sess.run([loss, backprop]) self.assertAllCloseAccordingToType(np_loss, tf_loss) self.assertAllCloseAccordingToType(np_backprop, tf_backprop) @@ -51,10 +53,9 @@ class XentTest(tf.test.TestCase): def _testSingleClass(self, use_gpu=False): for dtype in np.float16, np.float32: with self.test_session(use_gpu=use_gpu) as sess: - loss = tf.nn.softmax_cross_entropy_with_logits( + loss, backprop = gen_nn_ops._softmax_cross_entropy_with_logits( np.array([[1.], [-1.], [0.]]).astype(dtype), np.array([[-1.], [0.], [1.]]).astype(dtype)) - backprop = loss.op.outputs[1] tf_loss, tf_backprop = sess.run([loss, backprop]) self.assertAllClose([0.0, 0.0, 0.0], tf_loss) self.assertAllClose([[2.0], [1.0], [0.0]], tf_backprop) @@ -69,9 +70,9 @@ class XentTest(tf.test.TestCase): [[[1., 1., 1., 1.]], [[1., 2., 3., 4.]]]).astype(dtype) np_labels = np.array( [[[0., 0., 0., 1.]], [[0., .5, .5, 0.]]]).astype(dtype) - self.assertRaisesRegexp( - ValueError, "must have rank 2", - tf.nn.softmax_cross_entropy_with_logits, np_features, np_labels) + self.assertRaisesRegexp(ValueError, "must have rank 2", + gen_nn_ops._softmax_cross_entropy_with_logits, + np_features, np_labels) def testNpXent(self): # We create 2 batches of logits for testing. @@ -110,14 +111,14 @@ class XentTest(tf.test.TestCase): def testShapeMismatch(self): with self.test_session(): with self.assertRaises(ValueError): - tf.nn.softmax_cross_entropy_with_logits( + gen_nn_ops._softmax_cross_entropy_with_logits( [[0., 1.], [2., 3.]], [[0., 1., 0.], [1., 0., 0.]]) def testNotMatrix(self): with self.test_session(): with self.assertRaises(ValueError): - tf.nn.softmax_cross_entropy_with_logits([0., 1., 2., 3.], - [0., 1., 0., 1.]) + gen_nn_ops._softmax_cross_entropy_with_logits([0., 1., 2., 3.], + [0., 1., 0., 1.]) def testHalf(self): self._testAll( |