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.

214 lines
6.7KB

  1. from icosagon.data import Data, \
  2. _equal
  3. from icosagon.model import Model
  4. from icosagon.trainprep import PreparedData, \
  5. PreparedRelationFamily, \
  6. PreparedRelationType, \
  7. TrainValTest, \
  8. norm_adj_mat_one_node_type, \
  9. prepare_training
  10. import torch
  11. from icosagon.input import OneHotInputLayer
  12. from icosagon.convlayer import DecagonLayer
  13. from icosagon.declayer import DecodeLayer
  14. import pytest
  15. def _is_identity_function(f):
  16. for x in range(-100, 101):
  17. if f(x) != x:
  18. return False
  19. return True
  20. def test_model_01():
  21. d = Data()
  22. d.add_node_type('Dummy', 10)
  23. fam = d.add_relation_family('Dummy-Dummy', 0, 0, False)
  24. fam.add_relation_type('Dummy Rel', torch.rand(10, 10).round())
  25. prep_d = prepare_training(d, TrainValTest(.8, .1, .1))
  26. m = Model(prep_d)
  27. assert m.prep_d == prep_d
  28. assert m.layer_dimensions == [32, 64]
  29. assert m.keep_prob == 1.
  30. assert _is_identity_function(m.rel_activation)
  31. assert m.layer_activation == torch.nn.functional.relu
  32. assert _is_identity_function(m.dec_activation)
  33. assert m.lr == 0.001
  34. assert m.loss == torch.nn.functional.binary_cross_entropy_with_logits
  35. assert m.batch_size == 100
  36. assert isinstance(m.seq, torch.nn.Sequential)
  37. assert isinstance(m.opt, torch.optim.Optimizer)
  38. def test_model_02():
  39. d = Data()
  40. d.add_node_type('Dummy', 10)
  41. fam = d.add_relation_family('Dummy-Dummy', 0, 0, False)
  42. mat = torch.rand(10, 10).round().to_sparse()
  43. fam.add_relation_type('Dummy Rel', mat)
  44. prep_d = prepare_training(d, TrainValTest(1., 0., 0.))
  45. m = Model(prep_d)
  46. assert isinstance(m.prep_d, PreparedData)
  47. assert isinstance(m.prep_d.relation_families, list)
  48. assert len(m.prep_d.relation_families) == 1
  49. assert isinstance(m.prep_d.relation_families[0], PreparedRelationFamily)
  50. assert len(m.prep_d.relation_families[0].relation_types) == 1
  51. assert isinstance(m.prep_d.relation_families[0].relation_types[0], PreparedRelationType)
  52. assert m.prep_d.relation_families[0].relation_types[0].adjacency_matrix_backward is None
  53. assert torch.all(_equal(m.prep_d.relation_families[0].relation_types[0].adjacency_matrix,
  54. norm_adj_mat_one_node_type(mat)))
  55. assert isinstance(m.seq[0], OneHotInputLayer)
  56. assert isinstance(m.seq[1], DecagonLayer)
  57. assert isinstance(m.seq[2], DecagonLayer)
  58. assert isinstance(m.seq[3], DecodeLayer)
  59. assert len(m.seq) == 4
  60. def test_model_03():
  61. d = Data()
  62. d.add_node_type('Dummy', 10)
  63. fam = d.add_relation_family('Dummy-Dummy', 0, 0, False)
  64. mat = torch.rand(10, 10).round().to_sparse()
  65. fam.add_relation_type('Dummy Rel', mat)
  66. prep_d = prepare_training(d, TrainValTest(1., 0., 0.))
  67. m = Model(prep_d)
  68. state_dict = m.opt.state_dict()
  69. assert isinstance(state_dict, dict)
  70. # print(state_dict['param_groups'])
  71. # print(list(m.seq.parameters()))
  72. assert len(list(m.seq[0].parameters())) == 1
  73. assert len(list(m.seq[1].parameters())) == 1
  74. assert len(list(m.seq[2].parameters())) == 1
  75. assert len(list(m.seq[3].parameters())) == 2
  76. # print(list(m.seq[1].parameters()))
  77. def test_model_04():
  78. d = Data()
  79. d.add_node_type('Dummy', 10)
  80. fam = d.add_relation_family('Dummy-Dummy', 0, 0, False)
  81. mat = torch.rand(10, 10).round().to_sparse()
  82. fam.add_relation_type('Dummy Rel 1', mat)
  83. fam.add_relation_type('Dummy Rel 2', mat.clone())
  84. prep_d = prepare_training(d, TrainValTest(1., 0., 0.))
  85. m = Model(prep_d)
  86. assert len(list(m.seq[0].parameters())) == 1
  87. assert len(list(m.seq[1].parameters())) == 2
  88. assert len(list(m.seq[2].parameters())) == 2
  89. assert len(list(m.seq[3].parameters())) == 3
  90. def test_model_05():
  91. d = Data()
  92. d.add_node_type('Dummy', 10)
  93. fam = d.add_relation_family('Dummy-Dummy', 0, 0, False)
  94. mat = torch.rand(10, 10).round().to_sparse()
  95. fam.add_relation_type('Dummy Rel 1', mat)
  96. fam.add_relation_type('Dummy Rel 2', mat.clone())
  97. fam = d.add_relation_family('Dummy-Dummy 2', 0, 0, False)
  98. mat = torch.rand(10, 10).round().to_sparse()
  99. fam.add_relation_type('Dummy Rel 2-1', mat)
  100. fam.add_relation_type('Dummy Rel 2-2', mat.clone())
  101. prep_d = prepare_training(d, TrainValTest(1., 0., 0.))
  102. m = Model(prep_d)
  103. assert len(list(m.seq[0].parameters())) == 1
  104. assert len(list(m.seq[1].parameters())) == 4
  105. assert len(list(m.seq[2].parameters())) == 4
  106. assert len(list(m.seq[3].parameters())) == 6
  107. def test_model_06():
  108. d = Data()
  109. d.add_node_type('Dummy', 10)
  110. d.add_node_type('Foobar', 20)
  111. fam = d.add_relation_family('Dummy-Foobar', 0, 1, True)
  112. mat = torch.rand(10, 20).round().to_sparse()
  113. fam.add_relation_type('Dummy Rel 1', mat)
  114. fam.add_relation_type('Dummy Rel 2', mat.clone())
  115. fam = d.add_relation_family('Dummy-Dummy 2', 0, 0, False)
  116. mat = torch.rand(10, 10).round().to_sparse()
  117. fam.add_relation_type('Dummy Rel 2-1', mat)
  118. fam.add_relation_type('Dummy Rel 2-2', mat.clone())
  119. prep_d = prepare_training(d, TrainValTest(1., 0., 0.))
  120. m = Model(prep_d)
  121. assert len(list(m.seq[0].parameters())) == 2
  122. assert len(list(m.seq[1].parameters())) == 6
  123. assert len(list(m.seq[2].parameters())) == 6
  124. assert len(list(m.seq[3].parameters())) == 6
  125. def test_model_07():
  126. d = Data()
  127. d.add_node_type('Dummy', 10)
  128. fam = d.add_relation_family('Dummy-Dummy', 0, 0, False)
  129. fam.add_relation_type('Dummy Rel', torch.rand(10, 10).round())
  130. prep_d = prepare_training(d, TrainValTest(.8, .1, .1))
  131. with pytest.raises(TypeError):
  132. m = Model(1)
  133. with pytest.raises(TypeError):
  134. m = Model(prep_d, layer_dimensions=1)
  135. with pytest.raises(TypeError):
  136. m = Model(prep_d, ratios=1)
  137. with pytest.raises(ValueError):
  138. m = Model(prep_d, keep_prob='x')
  139. with pytest.raises(TypeError):
  140. m = Model(prep_d, rel_activation='x')
  141. with pytest.raises(TypeError):
  142. m = Model(prep_d, layer_activation='x')
  143. with pytest.raises(TypeError):
  144. m = Model(prep_d, dec_activation='x')
  145. with pytest.raises(ValueError):
  146. m = Model(prep_d, lr='x')
  147. with pytest.raises(TypeError):
  148. m = Model(prep_d, loss=1)
  149. with pytest.raises(ValueError):
  150. m = Model(prep_d, batch_size='x')
  151. def test_model_08():
  152. d = Data()
  153. d.add_node_type('Dummy', 10)
  154. fam = d.add_relation_family('Dummy-Dummy', 0, 0, False)
  155. fam.add_relation_type('Dummy Rel', torch.rand(10, 10).round())
  156. prep_d = prepare_training(d, TrainValTest(.8, .1, .1))
  157. m = Model(prep_d)
  158. m.run_epoch()