You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_nlp.py 2.4 kB

4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import unittest
  5. os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
  6. import tensorflow as tf
  7. import tensorlayer as tl
  8. from tensorflow.python.platform import gfile
  9. from tests.utils import CustomTestCase
  10. import nltk
  11. nltk.download('punkt')
  12. class Test_Leaky_ReLUs(CustomTestCase):
  13. @classmethod
  14. def setUpClass(cls):
  15. pass
  16. @classmethod
  17. def tearDownClass(cls):
  18. pass
  19. def test_as_bytes(self):
  20. origin_str = "str"
  21. origin_bytes = b'bytes'
  22. converted_str = tl.nlp.as_bytes(origin_str)
  23. converted_bytes = tl.nlp.as_bytes(origin_bytes)
  24. print('str after using as_bytes:', converted_str)
  25. print('bytes after using as_bytes:', converted_bytes)
  26. def test_as_text(self):
  27. origin_str = "str"
  28. origin_bytes = b'bytes'
  29. converted_str = tl.nlp.as_text(origin_str)
  30. converted_bytes = tl.nlp.as_text(origin_bytes)
  31. print('str after using as_text:', converted_str)
  32. print('bytes after using as_text:', converted_bytes)
  33. def test_save_vocab(self):
  34. words = tl.files.load_matt_mahoney_text8_dataset()
  35. vocabulary_size = 50000
  36. data, count, dictionary, reverse_dictionary = tl.nlp.build_words_dataset(words, vocabulary_size, True)
  37. tl.nlp.save_vocab(count, name='vocab_text8.txt')
  38. def test_basic_tokenizer(self):
  39. c = "how are you?"
  40. tokens = tl.nlp.basic_tokenizer(c)
  41. print(tokens)
  42. def test_generate_skip_gram_batch(self):
  43. data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
  44. batch, labels, data_index = tl.nlp.generate_skip_gram_batch(
  45. data=data, batch_size=8, num_skips=2, skip_window=1, data_index=0
  46. )
  47. print(batch)
  48. print(labels)
  49. def test_process_sentence(self):
  50. c = "how are you?"
  51. c = tl.nlp.process_sentence(c)
  52. print(c)
  53. def test_words_to_word_id(self):
  54. words = tl.files.load_matt_mahoney_text8_dataset()
  55. vocabulary_size = 50000
  56. data, count, dictionary, reverse_dictionary = tl.nlp.build_words_dataset(words, vocabulary_size, True)
  57. ids = tl.nlp.words_to_word_ids(words, dictionary)
  58. context = tl.nlp.word_ids_to_words(ids, reverse_dictionary)
  59. # print(ids)
  60. # print(context)
  61. if __name__ == '__main__':
  62. unittest.main()

TensorLayer3.0 是一款兼容多种深度学习框架为计算后端的深度学习库。计划兼容TensorFlow, Pytorch, MindSpore, Paddle.