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!
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

declayer.py 3.6KB

4 anos atrás
4 anos atrás
4 anos atrás
4 anos atrás
4 anos atrás
4 anos atrás
4 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #
  2. # Copyright (C) Stanislaw Adaszewski, 2020
  3. # License: GPLv3
  4. #
  5. import torch
  6. from .data import Data
  7. from .trainprep import PreparedData, \
  8. TrainValTest
  9. from typing import Type, \
  10. List, \
  11. Callable, \
  12. Union, \
  13. Dict, \
  14. Tuple
  15. from .decode import DEDICOMDecoder
  16. from dataclasses import dataclass
  17. import time
  18. @dataclass
  19. class RelationPredictions(object):
  20. edges_pos: TrainValTest
  21. edges_neg: TrainValTest
  22. edges_back_pos: TrainValTest
  23. edges_back_neg: TrainValTest
  24. @dataclass
  25. class RelationFamilyPredictions(object):
  26. relation_types: List[RelationPredictions]
  27. @dataclass
  28. class Predictions(object):
  29. relation_families: List[RelationFamilyPredictions]
  30. class DecodeLayer(torch.nn.Module):
  31. def __init__(self,
  32. input_dim: List[int],
  33. data: PreparedData,
  34. keep_prob: float = 1.,
  35. activation: Callable[[torch.Tensor], torch.Tensor] = torch.sigmoid,
  36. **kwargs) -> None:
  37. super().__init__(**kwargs)
  38. if not isinstance(input_dim, list):
  39. raise TypeError('input_dim must be a List')
  40. if len(input_dim) != len(data.node_types):
  41. raise ValueError('input_dim must have length equal to num_node_types')
  42. if not all([ a == input_dim[0] for a in input_dim ]):
  43. raise ValueError('All elements of input_dim must have the same value')
  44. if not isinstance(data, PreparedData):
  45. raise TypeError('data must be an instance of PreparedData')
  46. self.input_dim = input_dim[0]
  47. self.output_dim = 1
  48. self.data = data
  49. self.keep_prob = keep_prob
  50. self.activation = activation
  51. self.decoders = None
  52. self.build()
  53. def build(self) -> None:
  54. self.decoders = torch.nn.ModuleList()
  55. for fam in self.data.relation_families:
  56. dec = fam.decoder_class(self.input_dim, len(fam.relation_types),
  57. self.keep_prob, self.activation)
  58. self.decoders.append(dec)
  59. def _get_tvt(self, r, edge_list_attr_names, row, column, k, last_layer_repr, dec):
  60. start_time = time.time()
  61. pred = []
  62. for p in edge_list_attr_names:
  63. tvt = []
  64. for t in ['train', 'val', 'test']:
  65. # print('r:', r)
  66. edges = getattr(getattr(r, p), t)
  67. inputs_row = last_layer_repr[row][edges[:, 0]]
  68. inputs_column = last_layer_repr[column][edges[:, 1]]
  69. tvt.append(dec(inputs_row, inputs_column, k))
  70. tvt = TrainValTest(*tvt)
  71. pred.append(tvt)
  72. print('DecodeLayer._get_tvt() took:', time.time() - start_time)
  73. return pred
  74. def forward(self, last_layer_repr: List[torch.Tensor]) -> List[List[torch.Tensor]]:
  75. t = time.time()
  76. res = []
  77. for i, fam in enumerate(self.data.relation_families):
  78. fam_pred = []
  79. for k, r in enumerate(fam.relation_types):
  80. pred = []
  81. pred += self._get_tvt(r, ['edges_pos', 'edges_neg'],
  82. r.node_type_row, r.node_type_column, k, last_layer_repr, self.decoders[i])
  83. pred += self._get_tvt(r, ['edges_back_pos', 'edges_back_neg'],
  84. r.node_type_column, r.node_type_row, k, last_layer_repr, self.decoders[i])
  85. pred = RelationPredictions(*pred)
  86. fam_pred.append(pred)
  87. fam_pred = RelationFamilyPredictions(fam_pred)
  88. res.append(fam_pred)
  89. res = Predictions(res)
  90. print('DecodeLayer.forward() took', time.time() - t)
  91. return res