reader_test.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Copyright 2015 The TensorFlow Authors. 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 models.tutorials.rnn.ptb.reader."""
  16. from __future__ import absolute_import
  17. from __future__ import division
  18. from __future__ import print_function
  19. import os.path
  20. import tensorflow as tf
  21. import reader
  22. class PtbReaderTest(tf.test.TestCase):
  23. def setUp(self):
  24. self._string_data = "\n".join(
  25. [" hello there i am",
  26. " rain as day",
  27. " want some cheesy puffs ?"])
  28. def testPtbRawData(self):
  29. tmpdir = tf.test.get_temp_dir()
  30. for suffix in "train", "valid", "test":
  31. filename = os.path.join(tmpdir, "ptb.%s.txt" % suffix)
  32. with tf.gfile.GFile(filename, "w") as fh:
  33. fh.write(self._string_data)
  34. # Smoke test
  35. output = reader.ptb_raw_data(tmpdir)
  36. self.assertEqual(len(output), 4)
  37. def testPtbProducer(self):
  38. raw_data = [4, 3, 2, 1, 0, 5, 6, 1, 1, 1, 1, 0, 3, 4, 1]
  39. batch_size = 3
  40. num_steps = 2
  41. x, y = reader.ptb_producer(raw_data, batch_size, num_steps)
  42. with self.test_session() as session:
  43. coord = tf.train.Coordinator()
  44. tf.train.start_queue_runners(session, coord=coord)
  45. try:
  46. xval, yval = session.run([x, y])
  47. self.assertAllEqual(xval, [[4, 3], [5, 6], [1, 0]])
  48. self.assertAllEqual(yval, [[3, 2], [6, 1], [0, 3]])
  49. xval, yval = session.run([x, y])
  50. self.assertAllEqual(xval, [[2, 1], [1, 1], [3, 4]])
  51. self.assertAllEqual(yval, [[1, 0], [1, 1], [4, 1]])
  52. finally:
  53. coord.request_stop()
  54. coord.join()
  55. if __name__ == "__main__":
  56. tf.test.main()