word2vec_test.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 word2vec module."""
  16. from __future__ import absolute_import
  17. from __future__ import division
  18. from __future__ import print_function
  19. import os
  20. import tensorflow as tf
  21. import word2vec
  22. flags = tf.app.flags
  23. FLAGS = flags.FLAGS
  24. class Word2VecTest(tf.test.TestCase):
  25. def setUp(self):
  26. FLAGS.train_data = os.path.join(self.get_temp_dir(), "test-text.txt")
  27. FLAGS.eval_data = os.path.join(self.get_temp_dir(), "eval-text.txt")
  28. FLAGS.save_path = self.get_temp_dir()
  29. with open(FLAGS.train_data, "w") as f:
  30. f.write(
  31. """alice was beginning to get very tired of sitting by her sister on
  32. the bank, and of having nothing to do: once or twice she had peeped
  33. into the book her sister was reading, but it had no pictures or
  34. conversations in it, 'and what is the use of a book,' thought alice
  35. 'without pictures or conversations?' So she was considering in her own
  36. mind (as well as she could, for the hot day made her feel very sleepy
  37. and stupid), whether the pleasure of making a daisy-chain would be
  38. worth the trouble of getting up and picking the daisies, when suddenly
  39. a White rabbit with pink eyes ran close by her.\n""")
  40. with open(FLAGS.eval_data, "w") as f:
  41. f.write("alice she rabbit once\n")
  42. def testWord2Vec(self):
  43. FLAGS.batch_size = 5
  44. FLAGS.num_neg_samples = 10
  45. FLAGS.epochs_to_train = 1
  46. FLAGS.min_count = 0
  47. word2vec.main([])
  48. if __name__ == "__main__":
  49. tf.test.main()