losses_test.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 RegularizersTest(tf.test.TestCase):
  41. def testL1Regularizer(self):
  42. with self.test_session():
  43. shape = [5, 5, 5]
  44. num_elem = 5 * 5 * 5
  45. tensor = tf.constant(1.0, shape=shape)
  46. loss = losses.l1_regularizer()(tensor)
  47. self.assertEquals(loss.op.name, 'L1Regularizer/value')
  48. self.assertAlmostEqual(loss.eval(), num_elem, 5)
  49. def testL1RegularizerWithScope(self):
  50. with self.test_session():
  51. shape = [5, 5, 5]
  52. num_elem = 5 * 5 * 5
  53. tensor = tf.constant(1.0, shape=shape)
  54. loss = losses.l1_regularizer(scope='L1')(tensor)
  55. self.assertEquals(loss.op.name, 'L1/value')
  56. self.assertAlmostEqual(loss.eval(), num_elem, 5)
  57. def testL1RegularizerWithWeight(self):
  58. with self.test_session():
  59. shape = [5, 5, 5]
  60. num_elem = 5 * 5 * 5
  61. tensor = tf.constant(1.0, shape=shape)
  62. weight = 0.01
  63. loss = losses.l1_regularizer(weight)(tensor)
  64. self.assertEquals(loss.op.name, 'L1Regularizer/value')
  65. self.assertAlmostEqual(loss.eval(), num_elem * weight, 5)
  66. def testL2Regularizer(self):
  67. with self.test_session():
  68. shape = [5, 5, 5]
  69. num_elem = 5 * 5 * 5
  70. tensor = tf.constant(1.0, shape=shape)
  71. loss = losses.l2_regularizer()(tensor)
  72. self.assertEquals(loss.op.name, 'L2Regularizer/value')
  73. self.assertAlmostEqual(loss.eval(), num_elem / 2, 5)
  74. def testL2RegularizerWithScope(self):
  75. with self.test_session():
  76. shape = [5, 5, 5]
  77. num_elem = 5 * 5 * 5
  78. tensor = tf.constant(1.0, shape=shape)
  79. loss = losses.l2_regularizer(scope='L2')(tensor)
  80. self.assertEquals(loss.op.name, 'L2/value')
  81. self.assertAlmostEqual(loss.eval(), num_elem / 2, 5)
  82. def testL2RegularizerWithWeight(self):
  83. with self.test_session():
  84. shape = [5, 5, 5]
  85. num_elem = 5 * 5 * 5
  86. tensor = tf.constant(1.0, shape=shape)
  87. weight = 0.01
  88. loss = losses.l2_regularizer(weight)(tensor)
  89. self.assertEquals(loss.op.name, 'L2Regularizer/value')
  90. self.assertAlmostEqual(loss.eval(), num_elem * weight / 2, 5)
  91. def testL1L2Regularizer(self):
  92. with self.test_session():
  93. shape = [5, 5, 5]
  94. num_elem = 5 * 5 * 5
  95. tensor = tf.constant(1.0, shape=shape)
  96. loss = losses.l1_l2_regularizer()(tensor)
  97. self.assertEquals(loss.op.name, 'L1L2Regularizer/value')
  98. self.assertAlmostEqual(loss.eval(), num_elem + num_elem / 2, 5)
  99. def testL1L2RegularizerWithScope(self):
  100. with self.test_session():
  101. shape = [5, 5, 5]
  102. num_elem = 5 * 5 * 5
  103. tensor = tf.constant(1.0, shape=shape)
  104. loss = losses.l1_l2_regularizer(scope='L1L2')(tensor)
  105. self.assertEquals(loss.op.name, 'L1L2/value')
  106. self.assertAlmostEqual(loss.eval(), num_elem + num_elem / 2, 5)
  107. def testL1L2RegularizerWithWeights(self):
  108. with self.test_session():
  109. shape = [5, 5, 5]
  110. num_elem = 5 * 5 * 5
  111. tensor = tf.constant(1.0, shape=shape)
  112. weight_l1 = 0.01
  113. weight_l2 = 0.05
  114. loss = losses.l1_l2_regularizer(weight_l1, weight_l2)(tensor)
  115. self.assertEquals(loss.op.name, 'L1L2Regularizer/value')
  116. self.assertAlmostEqual(loss.eval(),
  117. num_elem * weight_l1 + num_elem * weight_l2 / 2, 5)
  118. class CrossEntropyLossTest(tf.test.TestCase):
  119. def testCrossEntropyLossAllCorrect(self):
  120. with self.test_session():
  121. logits = tf.constant([[10.0, 0.0, 0.0],
  122. [0.0, 10.0, 0.0],
  123. [0.0, 0.0, 10.0]])
  124. labels = tf.constant([[1, 0, 0],
  125. [0, 1, 0],
  126. [0, 0, 1]])
  127. loss = losses.cross_entropy_loss(logits, labels)
  128. self.assertEquals(loss.op.name, 'CrossEntropyLoss/value')
  129. self.assertAlmostEqual(loss.eval(), 0.0, 3)
  130. def testCrossEntropyLossAllWrong(self):
  131. with self.test_session():
  132. logits = tf.constant([[10.0, 0.0, 0.0],
  133. [0.0, 10.0, 0.0],
  134. [0.0, 0.0, 10.0]])
  135. labels = tf.constant([[0, 0, 1],
  136. [1, 0, 0],
  137. [0, 1, 0]])
  138. loss = losses.cross_entropy_loss(logits, labels)
  139. self.assertEquals(loss.op.name, 'CrossEntropyLoss/value')
  140. self.assertAlmostEqual(loss.eval(), 10.0, 3)
  141. def testCrossEntropyLossAllWrongWithWeight(self):
  142. with self.test_session():
  143. logits = tf.constant([[10.0, 0.0, 0.0],
  144. [0.0, 10.0, 0.0],
  145. [0.0, 0.0, 10.0]])
  146. labels = tf.constant([[0, 0, 1],
  147. [1, 0, 0],
  148. [0, 1, 0]])
  149. loss = losses.cross_entropy_loss(logits, labels, weight=0.5)
  150. self.assertEquals(loss.op.name, 'CrossEntropyLoss/value')
  151. self.assertAlmostEqual(loss.eval(), 5.0, 3)
  152. if __name__ == '__main__':
  153. tf.test.main()