IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

167 lines
4.6KB

  1. import tensorflow as tf
  2. import numpy as np
  3. from collections import defaultdict
  4. import torch
  5. import torch.utils.data
  6. from typing import List, \
  7. Union
  8. import icosagon.sampling
  9. import scipy.stats
  10. def test_unigram_01():
  11. range_max = 7
  12. distortion = 0.75
  13. batch_size = 500
  14. unigrams = [ 1, 3, 2, 1, 2, 1, 3]
  15. num_true = 1
  16. true_classes = np.zeros((batch_size, num_true), dtype=np.int64)
  17. for i in range(batch_size):
  18. true_classes[i, 0] = i % range_max
  19. true_classes = tf.convert_to_tensor(true_classes)
  20. neg_samples, _, _ = tf.nn.fixed_unigram_candidate_sampler(
  21. true_classes=true_classes,
  22. num_true=num_true,
  23. num_sampled=batch_size,
  24. unique=False,
  25. range_max=range_max,
  26. distortion=distortion,
  27. unigrams=unigrams)
  28. assert neg_samples.shape == (batch_size,)
  29. for i in range(batch_size):
  30. assert neg_samples[i] != true_classes[i, 0]
  31. counts = defaultdict(int)
  32. with tf.Session() as sess:
  33. neg_samples = neg_samples.eval()
  34. for x in neg_samples:
  35. counts[x] += 1
  36. print('counts:', counts)
  37. assert counts[0] < counts[1] and \
  38. counts[0] < counts[2] and \
  39. counts[0] < counts[4] and \
  40. counts[0] < counts[6]
  41. assert counts[2] < counts[1] and \
  42. counts[0] < counts[6]
  43. assert counts[3] < counts[1] and \
  44. counts[3] < counts[2] and \
  45. counts[3] < counts[4] and \
  46. counts[3] < counts[6]
  47. assert counts[4] < counts[1] and \
  48. counts[4] < counts[6]
  49. assert counts[5] < counts[1] and \
  50. counts[5] < counts[2] and \
  51. counts[5] < counts[4] and \
  52. counts[5] < counts[6]
  53. def test_unigram_02():
  54. range_max = 7
  55. distortion = 0.75
  56. batch_size = 500
  57. unigrams = [ 1, 3, 2, 1, 2, 1, 3]
  58. num_true = 1
  59. true_classes = np.zeros((batch_size, num_true), dtype=np.int64)
  60. for i in range(batch_size):
  61. true_classes[i, 0] = i % range_max
  62. true_classes = torch.tensor(true_classes)
  63. neg_samples = icosagon.sampling.fixed_unigram_candidate_sampler(
  64. true_classes=true_classes,
  65. unigrams=unigrams,
  66. distortion=distortion)
  67. assert neg_samples.shape == (batch_size,)
  68. for i in range(batch_size):
  69. assert neg_samples[i] != true_classes[i, 0]
  70. counts = defaultdict(int)
  71. for x in neg_samples:
  72. counts[x.item()] += 1
  73. print('counts:', counts)
  74. assert counts[0] < counts[1] and \
  75. counts[0] < counts[2] and \
  76. counts[0] < counts[4] and \
  77. counts[0] < counts[6]
  78. assert counts[2] < counts[1] and \
  79. counts[0] < counts[6]
  80. assert counts[3] < counts[1] and \
  81. counts[3] < counts[2] and \
  82. counts[3] < counts[4] and \
  83. counts[3] < counts[6]
  84. assert counts[4] < counts[1] and \
  85. counts[4] < counts[6]
  86. assert counts[5] < counts[1] and \
  87. counts[5] < counts[2] and \
  88. counts[5] < counts[4] and \
  89. counts[5] < counts[6]
  90. def test_unigram_03():
  91. range_max = 7
  92. distortion = 0.75
  93. batch_size = 2500
  94. unigrams = [ 1, 3, 2, 1, 2, 1, 3]
  95. num_true = 1
  96. true_classes = np.zeros((batch_size, num_true), dtype=np.int64)
  97. for i in range(batch_size):
  98. true_classes[i, 0] = i % range_max
  99. true_classes_tf = tf.convert_to_tensor(true_classes)
  100. true_classes_torch = torch.tensor(true_classes)
  101. counts_tf = torch.zeros(range_max)
  102. counts_torch = torch.zeros(range_max)
  103. for i in range(10):
  104. neg_samples, _, _ = tf.nn.fixed_unigram_candidate_sampler(
  105. true_classes=true_classes_tf,
  106. num_true=num_true,
  107. num_sampled=batch_size,
  108. unique=False,
  109. range_max=range_max,
  110. distortion=distortion,
  111. unigrams=unigrams)
  112. counts = torch.zeros(range_max)
  113. with tf.Session() as sess:
  114. neg_samples = neg_samples.eval()
  115. for x in neg_samples:
  116. counts[x.item()] += 1
  117. counts_tf += counts
  118. neg_samples = icosagon.sampling.fixed_unigram_candidate_sampler(
  119. true_classes=true_classes,
  120. distortion=distortion,
  121. unigrams=unigrams)
  122. counts = torch.zeros(range_max)
  123. for x in neg_samples:
  124. counts[x.item()] += 1
  125. counts_torch += counts
  126. print('counts_tf:', counts_tf)
  127. print('counts_torch:', counts_torch)
  128. distance = scipy.stats.wasserstein_distance(counts_tf, counts_torch)
  129. assert distance < 2000