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.

74 lines
2.7KB

  1. #
  2. # Copyright (C) Stanislaw Adaszewski, 2020
  3. # License: GPLv3
  4. #
  5. import torch
  6. from ..dropout import dropout
  7. from ..weights import init_glorot
  8. from typing import List, Callable
  9. class DenseGraphConv(torch.nn.Module):
  10. def __init__(self, in_channels: int, out_channels: int,
  11. adjacency_matrix: torch.Tensor, **kwargs) -> None:
  12. super().__init__(**kwargs)
  13. self.in_channels = in_channels
  14. self.out_channels = out_channels
  15. self.weight = init_glorot(in_channels, out_channels)
  16. self.adjacency_matrix = adjacency_matrix
  17. def forward(self, x: torch.Tensor) -> torch.Tensor:
  18. x = torch.mm(x, self.weight)
  19. x = torch.mm(self.adjacency_matrix, x)
  20. return x
  21. class DenseDropoutGraphConvActivation(torch.nn.Module):
  22. def __init__(self, input_dim: int, output_dim: int,
  23. adjacency_matrix: torch.Tensor, keep_prob: float=1.,
  24. activation: Callable[[torch.Tensor], torch.Tensor]=torch.nn.functional.relu,
  25. **kwargs) -> None:
  26. super().__init__(**kwargs)
  27. self.graph_conv = DenseGraphConv(input_dim, output_dim, adjacency_matrix)
  28. self.keep_prob = keep_prob
  29. self.activation = activation
  30. def forward(self, x: torch.Tensor) -> torch.Tensor:
  31. x = dropout(x, keep_prob=self.keep_prob)
  32. x = self.graph_conv(x)
  33. x = self.activation(x)
  34. return x
  35. class DenseMultiDGCA(torch.nn.Module):
  36. def __init__(self, input_dim: List[int], output_dim: int,
  37. adjacency_matrices: List[torch.Tensor], keep_prob: float=1.,
  38. activation: Callable[[torch.Tensor], torch.Tensor]=torch.nn.functional.relu,
  39. **kwargs) -> None:
  40. super().__init__(**kwargs)
  41. self.input_dim = input_dim
  42. self.output_dim = output_dim
  43. self.adjacency_matrices = adjacency_matrices
  44. self.keep_prob = keep_prob
  45. self.activation = activation
  46. self.dgca = None
  47. self.build()
  48. def build(self):
  49. if len(self.input_dim) != len(self.adjacency_matrices):
  50. raise ValueError('input_dim must have the same length as adjacency_matrices')
  51. self.dgca = []
  52. for input_dim, adj_mat in zip(self.input_dim, self.adjacency_matrices):
  53. self.dgca.append(DenseDropoutGraphConvActivation(input_dim, self.output_dim, adj_mat, self.keep_prob, self.activation))
  54. def forward(self, x: List[torch.Tensor]) -> List[torch.Tensor]:
  55. if not isinstance(x, list):
  56. raise ValueError('x must be a list of tensors')
  57. out = torch.zeros(len(x[0]), self.output_dim, dtype=x[0].dtype)
  58. for i, f in enumerate(self.dgca):
  59. out += f(x[i])
  60. out = torch.nn.functional.normalize(out, p=2, dim=1)
  61. return out