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!
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

89 lignes
3.1KB

  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. class DecodeLayer(torch.nn.Module):
  17. def __init__(self,
  18. input_dim: List[int],
  19. data: PreparedData,
  20. keep_prob: float = 1.,
  21. activation: Callable[[torch.Tensor], torch.Tensor] = torch.sigmoid,
  22. **kwargs) -> None:
  23. super().__init__(**kwargs)
  24. if not isinstance(input_dim, list):
  25. raise TypeError('input_dim must be a List')
  26. if not all([ a == input_dim[0] for a in input_dim ]):
  27. raise ValueError('All elements of input_dim must have the same value')
  28. if not isinstance(data, PreparedData):
  29. raise TypeError('data must be an instance of PreparedData')
  30. self.input_dim = input_dim
  31. self.output_dim = 1
  32. self.data = data
  33. self.keep_prob = keep_prob
  34. self.activation = activation
  35. self.decoders = None
  36. self.build()
  37. def build(self) -> None:
  38. self.decoders = []
  39. for fam in self.data.relation_families:
  40. for (node_type_row, node_type_column), rels in fam.relation_types.items():
  41. for r in rels:
  42. pass
  43. dec = fam.decoder_class()
  44. self.decoders.append(dec)
  45. for (node_type_row, node_type_column), rels in self.data.relation_types.items():
  46. if len(rels) == 0:
  47. continue
  48. if isinstance(self.decoder_class, dict):
  49. if (node_type_row, node_type_column) in self.decoder_class:
  50. decoder_class = self.decoder_class[node_type_row, node_type_column]
  51. elif (node_type_column, node_type_row) in self.decoder_class:
  52. decoder_class = self.decoder_class[node_type_column, node_type_row]
  53. else:
  54. raise KeyError('Decoder not specified for edge type: %s -- %s' % (
  55. self.data.node_types[node_type_row].name,
  56. self.data.node_types[node_type_column].name))
  57. else:
  58. decoder_class = self.decoder_class
  59. self.decoders[node_type_row, node_type_column] = \
  60. decoder_class(self.input_dim[node_type_row],
  61. num_relation_types = len(rels),
  62. keep_prob = self.keep_prob,
  63. activation = self.activation)
  64. def forward(self, last_layer_repr: List[torch.Tensor]) -> Dict[Tuple[int, int], List[torch.Tensor]]:
  65. res = {}
  66. for (node_type_row, node_type_column), dec in self.decoders.items():
  67. inputs_row = last_layer_repr[node_type_row]
  68. inputs_column = last_layer_repr[node_type_column]
  69. pred_adj_matrices = [ dec(inputs_row, inputs_column, k) for k in range(dec.num_relation_types) ]
  70. res[node_type_row, node_type_column] = pred_adj_matrices
  71. return res