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!
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

input.py 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #
  2. # Copyright (C) Stanislaw Adaszewski, 2020
  3. # License: GPLv3
  4. #
  5. import torch
  6. from typing import Union, \
  7. List
  8. from .data import Data
  9. class InputLayer(torch.nn.Module):
  10. def __init__(self, data: Data, output_dim: Union[int, List[int]] = None,
  11. **kwargs) -> None:
  12. output_dim = output_dim or \
  13. list(map(lambda a: a.count, data.node_types))
  14. if not isinstance(output_dim, list):
  15. output_dim = [output_dim,] * len(data.node_types)
  16. super().__init__(**kwargs)
  17. self.output_dim = output_dim
  18. self.data = data
  19. self.is_sparse=False
  20. self.node_reps = None
  21. self.build()
  22. def build(self) -> None:
  23. self.node_reps = []
  24. for i, nt in enumerate(self.data.node_types):
  25. reps = torch.rand(nt.count, self.output_dim[i])
  26. reps = torch.nn.Parameter(reps)
  27. self.register_parameter('node_reps[%d]' % i, reps)
  28. self.node_reps.append(reps)
  29. def forward(self, x) -> List[torch.nn.Parameter]:
  30. return self.node_reps
  31. def __repr__(self) -> str:
  32. s = ''
  33. s += 'Icosagon input layer with output_dim: %s\n' % self.output_dim
  34. s += ' # of node types: %d\n' % len(self.data.node_types)
  35. for nt in self.data.node_types:
  36. s += ' - %s (%d)\n' % (nt.name, nt.count)
  37. return s.strip()
  38. class OneHotInputLayer(torch.nn.Module):
  39. def __init__(self, data: Data, **kwargs) -> None:
  40. output_dim = [ a.count for a in data.node_types ]
  41. super().__init__(**kwargs)
  42. self.output_dim = output_dim
  43. self.data = data
  44. self.is_sparse=True
  45. self.node_reps = None
  46. self.build()
  47. def build(self) -> None:
  48. self.node_reps = []
  49. for i, nt in enumerate(self.data.node_types):
  50. reps = torch.eye(nt.count).to_sparse()
  51. reps = torch.nn.Parameter(reps)
  52. self.register_parameter('node_reps[%d]' % i, reps)
  53. self.node_reps.append(reps)
  54. def forward(self, x) -> List[torch.nn.Parameter]:
  55. return self.node_reps
  56. def __repr__(self) -> str:
  57. s = ''
  58. s += 'Icosagon one-hot input layer\n'
  59. s += ' # of node types: %d\n' % len(self.data.node_types)
  60. for nt in self.data.node_types:
  61. s += ' - %s (%d)\n' % (nt.name, nt.count)
  62. return s.strip()