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.

test_layer_input.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from decagon_pytorch.layer import InputLayer, \
  2. OneHotInputLayer, \
  3. DecagonLayer
  4. from decagon_pytorch.data import Data
  5. import torch
  6. import pytest
  7. from decagon_pytorch.convolve import SparseDropoutGraphConvActivation, \
  8. SparseMultiDGCA, \
  9. DropoutGraphConvActivation
  10. def _some_data():
  11. d = Data()
  12. d.add_node_type('Gene', 1000)
  13. d.add_node_type('Drug', 100)
  14. d.add_relation_type('Target', 1, 0, None)
  15. d.add_relation_type('Interaction', 0, 0, None)
  16. d.add_relation_type('Side Effect: Nausea', 1, 1, None)
  17. d.add_relation_type('Side Effect: Infertility', 1, 1, None)
  18. d.add_relation_type('Side Effect: Death', 1, 1, None)
  19. return d
  20. def _some_data_with_interactions():
  21. d = Data()
  22. d.add_node_type('Gene', 1000)
  23. d.add_node_type('Drug', 100)
  24. d.add_relation_type('Target', 1, 0,
  25. torch.rand((100, 1000), dtype=torch.float32).round())
  26. d.add_relation_type('Interaction', 0, 0,
  27. torch.rand((1000, 1000), dtype=torch.float32).round())
  28. d.add_relation_type('Side Effect: Nausea', 1, 1,
  29. torch.rand((100, 100), dtype=torch.float32).round())
  30. d.add_relation_type('Side Effect: Infertility', 1, 1,
  31. torch.rand((100, 100), dtype=torch.float32).round())
  32. d.add_relation_type('Side Effect: Death', 1, 1,
  33. torch.rand((100, 100), dtype=torch.float32).round())
  34. return d
  35. def test_input_layer_01():
  36. d = _some_data()
  37. for output_dim in [32, 64, 128]:
  38. layer = InputLayer(d, output_dim)
  39. assert layer.output_dim[0] == output_dim
  40. assert len(layer.node_reps) == 2
  41. assert layer.node_reps[0].shape == (1000, output_dim)
  42. assert layer.node_reps[1].shape == (100, output_dim)
  43. assert layer.data == d
  44. def test_input_layer_02():
  45. d = _some_data()
  46. layer = InputLayer(d, 32)
  47. res = layer()
  48. assert isinstance(res[0], torch.Tensor)
  49. assert isinstance(res[1], torch.Tensor)
  50. assert res[0].shape == (1000, 32)
  51. assert res[1].shape == (100, 32)
  52. assert torch.all(res[0] == layer.node_reps[0])
  53. assert torch.all(res[1] == layer.node_reps[1])
  54. def test_input_layer_03():
  55. if torch.cuda.device_count() == 0:
  56. pytest.skip('No CUDA devices on this host')
  57. d = _some_data()
  58. layer = InputLayer(d, 32)
  59. device = torch.device('cuda:0')
  60. layer = layer.to(device)
  61. print(list(layer.parameters()))
  62. # assert layer.device.type == 'cuda:0'
  63. assert layer.node_reps[0].device == device
  64. assert layer.node_reps[1].device == device
  65. def test_one_hot_input_layer_01():
  66. d = _some_data()
  67. layer = OneHotInputLayer(d)
  68. assert layer.output_dim == [1000, 100]
  69. assert len(layer.node_reps) == 2
  70. assert layer.node_reps[0].shape == (1000, 1000)
  71. assert layer.node_reps[1].shape == (100, 100)
  72. assert layer.data == d
  73. assert layer.is_sparse
  74. def test_one_hot_input_layer_02():
  75. d = _some_data()
  76. layer = OneHotInputLayer(d)
  77. res = layer()
  78. assert isinstance(res[0], torch.Tensor)
  79. assert isinstance(res[1], torch.Tensor)
  80. assert res[0].shape == (1000, 1000)
  81. assert res[1].shape == (100, 100)
  82. assert torch.all(res[0].to_dense() == layer.node_reps[0].to_dense())
  83. assert torch.all(res[1].to_dense() == layer.node_reps[1].to_dense())
  84. def test_one_hot_input_layer_03():
  85. if torch.cuda.device_count() == 0:
  86. pytest.skip('No CUDA devices on this host')
  87. d = _some_data()
  88. layer = OneHotInputLayer(d)
  89. device = torch.device('cuda:0')
  90. layer = layer.to(device)
  91. print(list(layer.parameters()))
  92. # assert layer.device.type == 'cuda:0'
  93. assert layer.node_reps[0].device == device
  94. assert layer.node_reps[1].device == device