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_static_embedding.py 7.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import unittest
  2. from fastNLP.embeddings import StaticEmbedding
  3. from fastNLP import Vocabulary
  4. import torch
  5. import os
  6. class TestLoad(unittest.TestCase):
  7. def test_norm1(self):
  8. # 测试只对可以找到的norm
  9. vocab = Vocabulary().add_word_lst(['the', 'a', 'notinfile'])
  10. embed = StaticEmbedding(vocab, model_dir_or_name='test/data_for_tests/glove.6B.50d_test.txt',
  11. only_norm_found_vector=True)
  12. self.assertEqual(round(torch.norm(embed(torch.LongTensor([[2]]))).item(), 4), 1)
  13. self.assertNotEqual(torch.norm(embed(torch.LongTensor([[4]]))).item(), 1)
  14. def test_norm2(self):
  15. # 测试对所有都norm
  16. vocab = Vocabulary().add_word_lst(['the', 'a', 'notinfile'])
  17. embed = StaticEmbedding(vocab, model_dir_or_name='test/data_for_tests/glove.6B.50d_test.txt',
  18. normalize=True)
  19. self.assertEqual(round(torch.norm(embed(torch.LongTensor([[2]]))).item(), 4), 1)
  20. self.assertEqual(round(torch.norm(embed(torch.LongTensor([[4]]))).item(), 4), 1)
  21. def test_dropword(self):
  22. # 测试是否可以通过drop word
  23. vocab = Vocabulary().add_word_lst([chr(i) for i in range(1, 200)])
  24. embed = StaticEmbedding(vocab, model_dir_or_name=None, embedding_dim=10, dropout=0.1, word_dropout=0.4)
  25. for i in range(10):
  26. length = torch.randint(1, 50, (1,)).item()
  27. batch = torch.randint(1, 4, (1,)).item()
  28. words = torch.randint(1, 200, (batch, length)).long()
  29. embed(words)
  30. class TestRandomSameEntry(unittest.TestCase):
  31. def test_same_vector(self):
  32. vocab = Vocabulary().add_word_lst(["The", "the", "THE", 'a', "A"])
  33. embed = StaticEmbedding(vocab, model_dir_or_name=None, embedding_dim=5, lower=True)
  34. words = torch.LongTensor([[vocab.to_index(word) for word in ["The", "the", "THE", 'a', 'A']]])
  35. words = embed(words)
  36. embed_0 = words[0, 0]
  37. for i in range(1, 3):
  38. assert torch.sum(embed_0==words[0, i]).eq(len(embed_0))
  39. embed_0 = words[0, 3]
  40. for i in range(3, 5):
  41. assert torch.sum(embed_0 == words[0, i]).eq(len(embed_0))
  42. @unittest.skipIf('TRAVIS' in os.environ, "Skip in travis")
  43. def test_same_vector2(self):
  44. vocab = Vocabulary().add_word_lst(["The", 'a', 'b', "the", "THE", "B", 'a', "A"])
  45. embed = StaticEmbedding(vocab, model_dir_or_name='en-glove-6B-100d',
  46. lower=True)
  47. words = torch.LongTensor([[vocab.to_index(word) for word in ["The", "the", "THE", 'b', "B", 'a', 'A']]])
  48. words = embed(words)
  49. embed_0 = words[0, 0]
  50. for i in range(1, 3):
  51. assert torch.sum(embed_0==words[0, i]).eq(len(embed_0))
  52. embed_0 = words[0, 3]
  53. for i in range(3, 5):
  54. assert torch.sum(embed_0 == words[0, i]).eq(len(embed_0))
  55. @unittest.skipIf('TRAVIS' in os.environ, "Skip in travis")
  56. def test_same_vector3(self):
  57. # 验证lower
  58. word_lst = ["The", "the"]
  59. no_create_word_lst = ['of', 'Of', 'With', 'with']
  60. vocab = Vocabulary().add_word_lst(word_lst)
  61. vocab.add_word_lst(no_create_word_lst, no_create_entry=True)
  62. embed = StaticEmbedding(vocab, model_dir_or_name='en-glove-6B-100d',
  63. lower=True)
  64. words = torch.LongTensor([[vocab.to_index(word) for word in word_lst+no_create_word_lst]])
  65. words = embed(words)
  66. lowered_word_lst = [word.lower() for word in word_lst]
  67. lowered_no_create_word_lst = [word.lower() for word in no_create_word_lst]
  68. lowered_vocab = Vocabulary().add_word_lst(lowered_word_lst)
  69. lowered_vocab.add_word_lst(lowered_no_create_word_lst, no_create_entry=True)
  70. lowered_embed = StaticEmbedding(lowered_vocab, model_dir_or_name='en-glove-6B-100d',
  71. lower=False)
  72. lowered_words = torch.LongTensor([[lowered_vocab.to_index(word) for word in lowered_word_lst+lowered_no_create_word_lst]])
  73. lowered_words = lowered_embed(lowered_words)
  74. all_words = word_lst + no_create_word_lst
  75. for idx, (word_i, word_j) in enumerate(zip(words[0], lowered_words[0])):
  76. with self.subTest(idx=idx, word=all_words[idx]):
  77. assert torch.sum(word_i == word_j).eq(lowered_embed.embed_size)
  78. @unittest.skipIf('TRAVIS' in os.environ, "Skip in travis")
  79. def test_same_vector4(self):
  80. # 验证在有min_freq下的lower
  81. word_lst = ["The", "the", "the", "The", "a", "A"]
  82. no_create_word_lst = ['of', 'Of', "Of", "of", 'With', 'with']
  83. all_words = word_lst[:-2] + no_create_word_lst[:-2]
  84. vocab = Vocabulary(min_freq=2).add_word_lst(word_lst)
  85. vocab.add_word_lst(no_create_word_lst, no_create_entry=True)
  86. embed = StaticEmbedding(vocab, model_dir_or_name='en-glove-6B-100d',
  87. lower=True)
  88. words = torch.LongTensor([[vocab.to_index(word) for word in all_words]])
  89. words = embed(words)
  90. lowered_word_lst = [word.lower() for word in word_lst]
  91. lowered_no_create_word_lst = [word.lower() for word in no_create_word_lst]
  92. lowered_vocab = Vocabulary().add_word_lst(lowered_word_lst)
  93. lowered_vocab.add_word_lst(lowered_no_create_word_lst, no_create_entry=True)
  94. lowered_embed = StaticEmbedding(lowered_vocab, model_dir_or_name='en-glove-6B-100d',
  95. lower=False)
  96. lowered_words = torch.LongTensor([[lowered_vocab.to_index(word.lower()) for word in all_words]])
  97. lowered_words = lowered_embed(lowered_words)
  98. for idx in range(len(all_words)):
  99. word_i, word_j = words[0, idx], lowered_words[0, idx]
  100. with self.subTest(idx=idx, word=all_words[idx]):
  101. assert torch.sum(word_i == word_j).eq(lowered_embed.embed_size)
  102. @unittest.skipIf('TRAVIS' in os.environ, "Skip in travis")
  103. def test_same_vector5(self):
  104. # 检查通过使用min_freq后的word是否内容一致
  105. word_lst = ["they", "the", "they", "the", 'he', 'he', "a", "A"]
  106. no_create_word_lst = ['of', "of", "she", "she", 'With', 'with']
  107. all_words = word_lst[:-2] + no_create_word_lst[:-2]
  108. vocab = Vocabulary().add_word_lst(word_lst)
  109. vocab.add_word_lst(no_create_word_lst, no_create_entry=True)
  110. embed = StaticEmbedding(vocab, model_dir_or_name='en-glove-6B-100d',
  111. lower=False, min_freq=2)
  112. words = torch.LongTensor([[vocab.to_index(word) for word in all_words]])
  113. words = embed(words)
  114. min_freq_vocab = Vocabulary(min_freq=2).add_word_lst(word_lst)
  115. min_freq_vocab.add_word_lst(no_create_word_lst, no_create_entry=True)
  116. min_freq_embed = StaticEmbedding(min_freq_vocab, model_dir_or_name='en-glove-6B-100d',
  117. lower=False)
  118. min_freq_words = torch.LongTensor([[min_freq_vocab.to_index(word.lower()) for word in all_words]])
  119. min_freq_words = min_freq_embed(min_freq_words)
  120. for idx in range(len(all_words)):
  121. word_i, word_j = words[0, idx], min_freq_words[0, idx]
  122. with self.subTest(idx=idx, word=all_words[idx]):
  123. assert torch.sum(word_i == word_j).eq(min_freq_embed.embed_size)