losses_test.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Copyright 2016 Google Inc. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """Tests for slim.losses."""
  16. from __future__ import absolute_import
  17. from __future__ import division
  18. from __future__ import print_function
  19. import tensorflow as tf
  20. from inception.slim import losses
  21. class LossesTest(tf.test.TestCase):
  22. def testL1Loss(self):
  23. with self.test_session():
  24. shape = [5, 5, 5]
  25. num_elem = 5 * 5 * 5
  26. weights = tf.constant(1.0, shape=shape)
  27. wd = 0.01
  28. loss = losses.l1_loss(weights, wd)
  29. self.assertEquals(loss.op.name, 'L1Loss/value')
  30. self.assertAlmostEqual(loss.eval(), num_elem * wd, 5)
  31. def testL2Loss(self):
  32. with self.test_session():
  33. shape = [5, 5, 5]
  34. num_elem = 5 * 5 * 5
  35. weights = tf.constant(1.0, shape=shape)
  36. wd = 0.01
  37. loss = losses.l2_loss(weights, wd)
  38. self.assertEquals(loss.op.name, 'L2Loss/value')
  39. self.assertAlmostEqual(loss.eval(), num_elem * wd / 2, 5)
  40. class CrossEntropyLossTest(tf.test.TestCase):
  41. def testCrossEntropyLossAllCorrect(self):
  42. with self.test_session():
  43. logits = tf.constant([[10.0, 0.0, 0.0],
  44. [0.0, 10.0, 0.0],
  45. [0.0, 0.0, 10.0]])
  46. labels = tf.constant([[1, 0, 0],
  47. [0, 1, 0],
  48. [0, 0, 1]])
  49. loss = losses.cross_entropy_loss(logits, labels)
  50. self.assertEquals(loss.op.name, 'CrossEntropyLoss/value')
  51. self.assertAlmostEqual(loss.eval(), 0.0, 3)
  52. def testCrossEntropyLossAllWrong(self):
  53. with self.test_session():
  54. logits = tf.constant([[10.0, 0.0, 0.0],
  55. [0.0, 10.0, 0.0],
  56. [0.0, 0.0, 10.0]])
  57. labels = tf.constant([[0, 0, 1],
  58. [1, 0, 0],
  59. [0, 1, 0]])
  60. loss = losses.cross_entropy_loss(logits, labels)
  61. self.assertEquals(loss.op.name, 'CrossEntropyLoss/value')
  62. self.assertAlmostEqual(loss.eval(), 10.0, 3)
  63. def testCrossEntropyLossAllWrongWithWeight(self):
  64. with self.test_session():
  65. logits = tf.constant([[10.0, 0.0, 0.0],
  66. [0.0, 10.0, 0.0],
  67. [0.0, 0.0, 10.0]])
  68. labels = tf.constant([[0, 0, 1],
  69. [1, 0, 0],
  70. [0, 1, 0]])
  71. loss = losses.cross_entropy_loss(logits, labels, weight=0.5)
  72. self.assertEquals(loss.op.name, 'CrossEntropyLoss/value')
  73. self.assertAlmostEqual(loss.eval(), 5.0, 3)
  74. if __name__ == '__main__':
  75. tf.test.main()