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!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.4KB

  1. #
  2. # This module implements a single layer of the Decagon
  3. # model. This is going to be already quite complex, as
  4. # we will be using all the graph convolutional building
  5. # blocks.
  6. #
  7. # h_{i}^(k+1) = ϕ(∑_r ∑_{j∈N{r}^{i}} c_{r}^{ij} * \
  8. # W_{r}^(k) h_{j}^{k} + c_{r}^{i} h_{i}^(k))
  9. #
  10. # N{r}^{i} - set of neighbors of node i under relation r
  11. # W_{r}^(k) - relation-type specific weight matrix
  12. # h_{i}^(k) - hidden state of node i in layer k
  13. # h_{i}^(k)∈R^{d(k)} where d(k) is the dimensionality
  14. # of the representation in k-th layer
  15. # ϕ - activation function
  16. # c_{r}^{ij} - normalization constants
  17. # c_{r}^{ij} = 1/sqrt(|N_{r}^{i}| |N_{r}^{j}|)
  18. # c_{r}^{i} - normalization constants
  19. # c_{r}^{i} = 1/|N_{r}^{i}|
  20. #
  21. import torch
  22. from .convolve import SparseMultiDGCA
  23. class InputLayer(torch.nn.Module):
  24. def __init__(self, data, dimensionality=32, **kwargs):
  25. super().__init__(**kwargs)
  26. self.data = data
  27. self.dimensionality = dimensionality
  28. self.node_reps = None
  29. self.build()
  30. def build(self):
  31. self.node_reps = []
  32. for i, nt in enumerate(self.data.node_types):
  33. reps = torch.rand(nt.count, self.dimensionality)
  34. reps = torch.nn.Parameter(reps)
  35. self.register_parameter('node_reps[%d]' % i, reps)
  36. self.node_reps.append(reps)
  37. def forward(self):
  38. return self.node_reps
  39. def __repr__(self):
  40. s = ''
  41. s += 'GNN input layer with dimensionality: %d\n' % self.dimensionality
  42. s += ' # of node types: %d\n' % len(self.data.node_types)
  43. for nt in self.data.node_types:
  44. s += ' - %s (%d)\n' % (nt.name, nt.count)
  45. return s.strip()
  46. class DecagonLayer(torch.nn.Module):
  47. def __init__(self, data,
  48. input_dim, output_dim,
  49. keep_prob=1.,
  50. rel_activation=lambda x: x,
  51. layer_activation=torch.nn.functional.relu,
  52. **kwargs):
  53. super().__init__(**kwargs)
  54. self.data = data
  55. self.input_dim = input_dim
  56. self.output_dim = output_dim
  57. self.keep_prob = keep_prob
  58. self.rel_activation = rel_activation
  59. self.layer_activation = layer_activation
  60. self.convolutions = None
  61. self.build()
  62. def build(self):
  63. self.convolutions = {}
  64. for key in self.data.relation_types.keys():
  65. adjacency_matrices = \
  66. self.data.get_adjacency_matrices(*key)
  67. self.convolutions[key] = SparseMultiDGCA(self.input_dim,
  68. self.output_dim, adjacency_matrices,
  69. self.keep_prob, self.rel_activation)
  70. # for node_type_row, node_type_col in enumerate(self.data.node_
  71. # if rt.node_type_row == i or rt.node_type_col == i:
  72. def __call__(self, prev_layer_repr):
  73. new_layer_repr = []
  74. for i, nt in enumerate(self.data.node_types):
  75. new_repr = []
  76. for key in self.data.relation_types.keys():
  77. nt_row, nt_col = key
  78. if nt_row != i and nt_col != i:
  79. continue
  80. if nt_row == i:
  81. x = prev_layer_repr[nt_col]
  82. else:
  83. x = prev_layer_repr[nt_row]
  84. conv = self.convolutions[key]
  85. new_repr.append(conv(x))
  86. new_repr = sum(new_repr)
  87. new_layer_repr.append(new_repr)
  88. return new_layer_repr