batch_norm_test.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # MIT License
  2. #
  3. # Copyright (c) 2016 David Sandberg
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in all
  13. # copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. import unittest
  23. import tensorflow as tf
  24. import models
  25. import numpy as np
  26. import numpy.testing as testing
  27. class BatchNormTest(unittest.TestCase):
  28. @unittest.skip("Skip batch norm test case")
  29. def testBatchNorm(self):
  30. tf.set_random_seed(123)
  31. x = tf.placeholder(tf.float32, [None, 20, 20, 10], name='input')
  32. phase_train = tf.placeholder(tf.bool, name='phase_train')
  33. # generate random noise to pass into batch norm
  34. #x_gen = tf.random_normal([50,20,20,10])
  35. bn = models.network.batch_norm(x, phase_train)
  36. init = tf.global_variables_initializer()
  37. sess = tf.Session(config=tf.ConfigProto())
  38. sess.run(init)
  39. with sess.as_default():
  40. #generate a constant variable to pass into batch norm
  41. y = np.random.normal(0, 1, size=(50,20,20,10))
  42. feed_dict = {x: y, phase_train: True}
  43. sess.run(bn, feed_dict=feed_dict)
  44. feed_dict = {x: y, phase_train: False}
  45. y1 = sess.run(bn, feed_dict=feed_dict)
  46. y2 = sess.run(bn, feed_dict=feed_dict)
  47. testing.assert_almost_equal(y1, y2, 10, 'Output from two forward passes with phase_train==false should be equal')
  48. if __name__ == "__main__":
  49. unittest.main()