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 kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

90 Zeilen
3.1KB

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