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개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 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. d.add_relation_type('Dummy Relation 1', 0, 0,
  17. torch.rand((100, 100), dtype=torch.float32).round().to_sparse())
  18. prep_d = prepare_training(d, TrainValTest(.8, .1, .1))
  19. in_layer = OneHotInputLayer(d)
  20. d_layer = DecagonLayer(in_layer.output_dim, 32, d)
  21. seq = torch.nn.Sequential(in_layer, d_layer)
  22. last_layer_repr = seq(None)
  23. dec = DecodeLayer(input_dim=d_layer.output_dim, data=prep_d, keep_prob=1.,
  24. decoder_class=DEDICOMDecoder, activation=lambda x: x)
  25. pred_adj_matrices = dec(last_layer_repr)
  26. assert isinstance(pred_adj_matrices, dict)
  27. assert len(pred_adj_matrices) == 1
  28. assert isinstance(pred_adj_matrices[0, 0], list)
  29. assert len(pred_adj_matrices[0, 0]) == 1
  30. def test_decode_layer_02():
  31. d = Data()
  32. d.add_node_type('Dummy', 100)
  33. d.add_relation_type('Dummy Relation 1', 0, 0,
  34. torch.rand((100, 100), dtype=torch.float32).round().to_sparse())
  35. prep_d = prepare_training(d, TrainValTest(.8, .1, .1))
  36. in_layer = OneHotInputLayer(d)
  37. d_layer = DecagonLayer(in_layer.output_dim, 32, d)
  38. dec_layer = DecodeLayer(input_dim=d_layer.output_dim, data=prep_d, keep_prob=1.,
  39. decoder_class=DEDICOMDecoder, activation=lambda x: x)
  40. seq = torch.nn.Sequential(in_layer, d_layer, dec_layer)
  41. pred_adj_matrices = seq(None)
  42. assert isinstance(pred_adj_matrices, dict)
  43. assert len(pred_adj_matrices) == 1
  44. assert isinstance(pred_adj_matrices[0, 0], list)
  45. assert len(pred_adj_matrices[0, 0]) == 1
  46. def test_decode_layer_03():
  47. d = Data()
  48. d.add_node_type('Dummy 1', 100)
  49. d.add_node_type('Dummy 2', 100)
  50. d.add_relation_type('Dummy Relation 1', 0, 1,
  51. torch.rand((100, 100), dtype=torch.float32).round().to_sparse())
  52. prep_d = prepare_training(d, TrainValTest(.8, .1, .1))
  53. in_layer = OneHotInputLayer(d)
  54. d_layer = DecagonLayer(in_layer.output_dim, 32, d)
  55. dec_layer = DecodeLayer(input_dim=d_layer.output_dim, data=prep_d, keep_prob=1.,
  56. decoder_class={(0, 1): DEDICOMDecoder}, activation=lambda x: x)
  57. seq = torch.nn.Sequential(in_layer, d_layer, dec_layer)
  58. pred_adj_matrices = seq(None)
  59. assert isinstance(pred_adj_matrices, dict)
  60. assert len(pred_adj_matrices) == 2
  61. assert isinstance(pred_adj_matrices[0, 1], list)
  62. assert isinstance(pred_adj_matrices[1, 0], list)
  63. assert len(pred_adj_matrices[0, 1]) == 1
  64. assert len(pred_adj_matrices[1, 0]) == 1
  65. def test_decode_layer_04():
  66. d = Data()
  67. d.add_node_type('Dummy', 100)
  68. assert len(d.relation_types[0, 0]) == 0
  69. prep_d = prepare_training(d, TrainValTest(.8, .1, .1))
  70. in_layer = OneHotInputLayer(d)
  71. d_layer = DecagonLayer(in_layer.output_dim, 32, d)
  72. dec_layer = DecodeLayer(input_dim=d_layer.output_dim, data=prep_d, keep_prob=1.,
  73. decoder_class=DEDICOMDecoder, activation=lambda x: x)
  74. seq = torch.nn.Sequential(in_layer, d_layer, dec_layer)
  75. pred_adj_matrices = seq(None)
  76. assert isinstance(pred_adj_matrices, dict)
  77. assert len(pred_adj_matrices) == 0