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!
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

test_decode.py 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. from icosagon.decode import DEDICOMDecoder, \
  2. DistMultDecoder, \
  3. BilinearDecoder, \
  4. InnerProductDecoder
  5. import decagon_pytorch.decode.pairwise
  6. import torch
  7. def test_dedicom_decoder_01():
  8. repr_ = torch.rand(20, 32)
  9. dec_1 = DEDICOMDecoder(32, 7, keep_prob=1.,
  10. activation=torch.sigmoid)
  11. dec_2 = decagon_pytorch.decode.pairwise.DEDICOMDecoder(32, 7, drop_prob=0.,
  12. activation=torch.sigmoid)
  13. dec_2.global_interaction = dec_1.global_interaction
  14. dec_2.local_variation = dec_1.local_variation
  15. res_1 = [ dec_1(repr_, repr_, k) for k in range(7) ]
  16. res_2 = dec_2(repr_, repr_)
  17. assert isinstance(res_1, list)
  18. assert isinstance(res_2, list)
  19. assert len(res_1) == len(res_2)
  20. for i in range(len(res_1)):
  21. assert torch.all(res_1[i] == res_2[i])
  22. def test_dist_mult_decoder_01():
  23. repr_ = torch.rand(20, 32)
  24. dec_1 = DistMultDecoder(32, 7, keep_prob=1.,
  25. activation=torch.sigmoid)
  26. dec_2 = decagon_pytorch.decode.pairwise.DistMultDecoder(32, 7, drop_prob=0.,
  27. activation=torch.sigmoid)
  28. dec_2.relation = dec_1.relation
  29. res_1 = [ dec_1(repr_, repr_, k) for k in range(7) ]
  30. res_2 = dec_2(repr_, repr_)
  31. assert isinstance(res_1, list)
  32. assert isinstance(res_2, list)
  33. assert len(res_1) == len(res_2)
  34. for i in range(len(res_1)):
  35. assert torch.all(res_1[i] == res_2[i])
  36. def test_bilinear_decoder_01():
  37. repr_ = torch.rand(20, 32)
  38. dec_1 = BilinearDecoder(32, 7, keep_prob=1.,
  39. activation=torch.sigmoid)
  40. dec_2 = decagon_pytorch.decode.pairwise.BilinearDecoder(32, 7, drop_prob=0.,
  41. activation=torch.sigmoid)
  42. dec_2.relation = dec_1.relation
  43. res_1 = [ dec_1(repr_, repr_, k) for k in range(7) ]
  44. res_2 = dec_2(repr_, repr_)
  45. assert isinstance(res_1, list)
  46. assert isinstance(res_2, list)
  47. assert len(res_1) == len(res_2)
  48. for i in range(len(res_1)):
  49. assert torch.all(res_1[i] == res_2[i])
  50. def test_inner_product_decoder_01():
  51. repr_ = torch.rand(20, 32)
  52. dec_1 = InnerProductDecoder(32, 7, keep_prob=1.,
  53. activation=torch.sigmoid)
  54. dec_2 = decagon_pytorch.decode.pairwise.InnerProductDecoder(32, 7, drop_prob=0.,
  55. activation=torch.sigmoid)
  56. res_1 = [ dec_1(repr_, repr_, k) for k in range(7) ]
  57. res_2 = dec_2(repr_, repr_)
  58. assert isinstance(res_1, list)
  59. assert isinstance(res_2, list)
  60. assert len(res_1) == len(res_2)
  61. for i in range(len(res_1)):
  62. assert torch.all(res_1[i] == res_2[i])
  63. def test_is_dedicom_not_symmetric_01():
  64. repr_1 = torch.rand(20, 32)
  65. repr_2 = torch.rand(20, 32)
  66. dec = DEDICOMDecoder(32, 7, keep_prob=1.,
  67. activation=torch.sigmoid)
  68. res_1 = [ dec(repr_1, repr_2, k) for k in range(7) ]
  69. res_2 = [ dec(repr_2, repr_1, k) for k in range(7) ]
  70. assert isinstance(res_1, list)
  71. assert isinstance(res_2, list)
  72. assert len(res_1) == len(res_2)
  73. for i in range(len(res_1)):
  74. assert not torch.all(res_1[i] - res_2[i] < 0.000001)
  75. def test_is_dist_mult_symmetric_01():
  76. repr_1 = torch.rand(20, 32)
  77. repr_2 = torch.rand(20, 32)
  78. dec = DistMultDecoder(32, 7, keep_prob=1.,
  79. activation=torch.sigmoid)
  80. res_1 = [ dec(repr_1, repr_2, k) for k in range(7) ]
  81. res_2 = [ dec(repr_2, repr_1, k) for k in range(7) ]
  82. assert isinstance(res_1, list)
  83. assert isinstance(res_2, list)
  84. assert len(res_1) == len(res_2)
  85. for i in range(len(res_1)):
  86. assert torch.all(res_1[i] - res_2[i] < 0.000001)
  87. def test_is_bilinear_not_symmetric_01():
  88. repr_1 = torch.rand(20, 32)
  89. repr_2 = torch.rand(20, 32)
  90. dec = BilinearDecoder(32, 7, keep_prob=1.,
  91. activation=torch.sigmoid)
  92. res_1 = [ dec(repr_1, repr_2, k) for k in range(7) ]
  93. res_2 = [ dec(repr_2, repr_1, k) for k in range(7) ]
  94. assert isinstance(res_1, list)
  95. assert isinstance(res_2, list)
  96. assert len(res_1) == len(res_2)
  97. for i in range(len(res_1)):
  98. assert not torch.all(res_1[i] - res_2[i] < 0.000001)
  99. def test_is_inner_product_symmetric_01():
  100. repr_1 = torch.rand(20, 32)
  101. repr_2 = torch.rand(20, 32)
  102. dec = InnerProductDecoder(32, 7, keep_prob=1.,
  103. activation=torch.sigmoid)
  104. res_1 = [ dec(repr_1, repr_2, k) for k in range(7) ]
  105. res_2 = [ dec(repr_2, repr_1, k) for k in range(7) ]
  106. assert isinstance(res_1, list)
  107. assert isinstance(res_2, list)
  108. assert len(res_1) == len(res_2)
  109. for i in range(len(res_1)):
  110. assert torch.all(res_1[i] - res_2[i] < 0.000001)
  111. def test_empty_dedicom_decoder_01():
  112. repr_ = torch.rand(0, 32)
  113. dec = DEDICOMDecoder(32, 7, keep_prob=1.,
  114. activation=torch.sigmoid)
  115. res = [ dec(repr_, repr_, k) for k in range(7) ]
  116. assert isinstance(res, list)
  117. for i in range(len(res)):
  118. assert res[i].shape == (0,)
  119. def test_empty_dist_mult_decoder_01():
  120. repr_ = torch.rand(0, 32)
  121. dec = DistMultDecoder(32, 7, keep_prob=1.,
  122. activation=torch.sigmoid)
  123. res = [ dec(repr_, repr_, k) for k in range(7) ]
  124. assert isinstance(res, list)
  125. for i in range(len(res)):
  126. assert res[i].shape == (0,)
  127. def test_empty_bilinear_decoder_01():
  128. repr_ = torch.rand(0, 32)
  129. dec = BilinearDecoder(32, 7, keep_prob=1.,
  130. activation=torch.sigmoid)
  131. res = [ dec(repr_, repr_, k) for k in range(7) ]
  132. assert isinstance(res, list)
  133. for i in range(len(res)):
  134. assert res[i].shape == (0,)
  135. def test_empty_inner_product_decoder_01():
  136. repr_ = torch.rand(0, 32)
  137. dec = InnerProductDecoder(32, 7, keep_prob=1.,
  138. activation=torch.sigmoid)
  139. res = [ dec(repr_, repr_, k) for k in range(7) ]
  140. assert isinstance(res, list)
  141. for i in range(len(res)):
  142. assert res[i].shape == (0,)
  143. def test_dedicom_decoder_parameter_count_01():
  144. dec = DEDICOMDecoder(32, 7, keep_prob=1.,
  145. activation=torch.sigmoid)
  146. assert len(list(dec.parameters())) == 8
  147. def test_dist_mult_decoder_parameter_count_01():
  148. dec = DistMultDecoder(32, 7, keep_prob=1.,
  149. activation=torch.sigmoid)
  150. assert len(list(dec.parameters())) == 7
  151. def test_bilinear_decoder_parameter_count_01():
  152. dec = BilinearDecoder(32, 7, keep_prob=1.,
  153. activation=torch.sigmoid)
  154. assert len(list(dec.parameters())) == 7
  155. def test_inner_product_decoder_parameter_count_01():
  156. dec = InnerProductDecoder(32, 7, keep_prob=1.,
  157. activation=torch.sigmoid)
  158. assert len(list(dec.parameters())) == 0