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!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.5KB

  1. #
  2. # Copyright (C) Stanislaw Adaszewski, 2020
  3. # License: GPLv3
  4. #
  5. from icosagon.input import OneHotInputLayer
  6. from icosagon.convlayer import DecagonLayer
  7. from icosagon.declayer import DecodeLayer
  8. from icosagon.decode import DEDICOMDecoder
  9. from icosagon.data import Data
  10. from icosagon.trainprep import prepare_training, \
  11. TrainValTest
  12. import torch
  13. def test_decode_layer_01():
  14. d = Data()
  15. d.add_node_type('Dummy', 100)
  16. fam = d.add_relation_family('Dummy-Dummy', 0, 0, False)
  17. fam.add_relation_type('Dummy Relation 1', 0, 0,
  18. torch.rand((100, 100), dtype=torch.float32).round().to_sparse())
  19. prep_d = prepare_training(d, TrainValTest(.8, .1, .1))
  20. in_layer = OneHotInputLayer(d)
  21. d_layer = DecagonLayer(in_layer.output_dim, 32, d)
  22. seq = torch.nn.Sequential(in_layer, d_layer)
  23. last_layer_repr = seq(None)
  24. dec = DecodeLayer(input_dim=d_layer.output_dim, data=prep_d, keep_prob=1.,
  25. decoder_class=DEDICOMDecoder, activation=lambda x: x)
  26. pred_adj_matrices = dec(last_layer_repr)
  27. assert isinstance(pred_adj_matrices, dict)
  28. assert len(pred_adj_matrices) == 1
  29. assert isinstance(pred_adj_matrices[0, 0], list)
  30. assert len(pred_adj_matrices[0, 0]) == 1
  31. def test_decode_layer_02():
  32. d = Data()
  33. d.add_node_type('Dummy', 100)
  34. d.add_relation_type('Dummy Relation 1', 0, 0,
  35. torch.rand((100, 100), dtype=torch.float32).round().to_sparse())
  36. prep_d = prepare_training(d, TrainValTest(.8, .1, .1))
  37. in_layer = OneHotInputLayer(d)
  38. d_layer = DecagonLayer(in_layer.output_dim, 32, d)
  39. dec_layer = DecodeLayer(input_dim=d_layer.output_dim, data=prep_d, keep_prob=1.,
  40. decoder_class=DEDICOMDecoder, activation=lambda x: x)
  41. seq = torch.nn.Sequential(in_layer, d_layer, dec_layer)
  42. pred_adj_matrices = seq(None)
  43. assert isinstance(pred_adj_matrices, dict)
  44. assert len(pred_adj_matrices) == 1
  45. assert isinstance(pred_adj_matrices[0, 0], list)
  46. assert len(pred_adj_matrices[0, 0]) == 1
  47. def test_decode_layer_03():
  48. d = Data()
  49. d.add_node_type('Dummy 1', 100)
  50. d.add_node_type('Dummy 2', 100)
  51. d.add_relation_type('Dummy Relation 1', 0, 1,
  52. torch.rand((100, 100), dtype=torch.float32).round().to_sparse())
  53. prep_d = prepare_training(d, TrainValTest(.8, .1, .1))
  54. in_layer = OneHotInputLayer(d)
  55. d_layer = DecagonLayer(in_layer.output_dim, 32, d)
  56. dec_layer = DecodeLayer(input_dim=d_layer.output_dim, data=prep_d, keep_prob=1.,
  57. decoder_class={(0, 1): DEDICOMDecoder}, activation=lambda x: x)
  58. seq = torch.nn.Sequential(in_layer, d_layer, dec_layer)
  59. pred_adj_matrices = seq(None)
  60. assert isinstance(pred_adj_matrices, dict)
  61. assert len(pred_adj_matrices) == 2
  62. assert isinstance(pred_adj_matrices[0, 1], list)
  63. assert isinstance(pred_adj_matrices[1, 0], list)
  64. assert len(pred_adj_matrices[0, 1]) == 1
  65. assert len(pred_adj_matrices[1, 0]) == 1
  66. def test_decode_layer_04():
  67. d = Data()
  68. d.add_node_type('Dummy', 100)
  69. assert len(d.relation_types[0, 0]) == 0
  70. prep_d = prepare_training(d, TrainValTest(.8, .1, .1))
  71. in_layer = OneHotInputLayer(d)
  72. d_layer = DecagonLayer(in_layer.output_dim, 32, d)
  73. dec_layer = DecodeLayer(input_dim=d_layer.output_dim, data=prep_d, keep_prob=1.,
  74. decoder_class=DEDICOMDecoder, activation=lambda x: x)
  75. seq = torch.nn.Sequential(in_layer, d_layer, dec_layer)
  76. pred_adj_matrices = seq(None)
  77. assert isinstance(pred_adj_matrices, dict)
  78. assert len(pred_adj_matrices) == 0