|
| 1 | +""" |
| 2 | +Abboud, Ralph, et al. |
| 3 | +"The surprising power of graph neural networks with random node initialization." |
| 4 | +arXiv preprint arXiv:2010.01179 (2020). |
| 5 | +
|
| 6 | +Code Reference: https://github.com/ralphabb/GNN-RNI/blob/main/GNNHyb.py |
| 7 | +""" |
| 8 | + |
| 9 | +import torch |
| 10 | +from torch_geometric.data import Data as GeomData |
| 11 | + |
| 12 | +from .reader import GraphPropertyReader |
| 13 | + |
| 14 | + |
| 15 | +class RandomNodeInitializationReader(GraphPropertyReader): |
| 16 | + def __init__( |
| 17 | + self, |
| 18 | + num_node_properties: int, |
| 19 | + num_bond_properties: int, |
| 20 | + num_molecule_properties: int, |
| 21 | + distribution: str, |
| 22 | + *args, |
| 23 | + **kwargs, |
| 24 | + ): |
| 25 | + super().__init__(*args, **kwargs) |
| 26 | + self.num_node_properties = num_node_properties |
| 27 | + self.num_bond_properties = num_bond_properties |
| 28 | + self.num_molecule_properties = num_molecule_properties |
| 29 | + assert distribution in ["normal", "uniform", "xavier_normal", "xavier_uniform"] |
| 30 | + self.distribution = distribution |
| 31 | + |
| 32 | + def name(self) -> str: |
| 33 | + """ |
| 34 | + Get the name identifier of the reader. |
| 35 | +
|
| 36 | + Returns: |
| 37 | + str: The name of the reader. |
| 38 | + """ |
| 39 | + return f"gni-{self.distribution}-node{self.num_node_properties}-bond{self.num_bond_properties}-mol{self.num_molecule_properties}" |
| 40 | + |
| 41 | + def _read_data(self, raw_data): |
| 42 | + data: GeomData = super()._read_data(raw_data) |
| 43 | + random_x = torch.empty(data.x.shape[0], self.num_node_properties) |
| 44 | + random_edge_attr = torch.empty( |
| 45 | + data.edge_index.shape[1], self.num_bond_properties |
| 46 | + ) |
| 47 | + random_molecule_properties = torch.empty(1, self.num_molecule_properties) |
| 48 | + |
| 49 | + if self.distribution == "normal": |
| 50 | + torch.nn.init.normal_(random_x) |
| 51 | + torch.nn.init.normal_(random_edge_attr) |
| 52 | + torch.nn.init.normal_(random_molecule_properties) |
| 53 | + elif self.distribution == "uniform": |
| 54 | + torch.nn.init.uniform_(random_x, a=-1.0, b=1.0) |
| 55 | + torch.nn.init.uniform_(random_edge_attr, a=-1.0, b=1.0) |
| 56 | + torch.nn.init.uniform_(random_molecule_properties, a=-1.0, b=1.0) |
| 57 | + elif self.distribution == "xavier_normal": |
| 58 | + torch.nn.init.xavier_normal_(random_x) |
| 59 | + torch.nn.init.xavier_normal_(random_edge_attr) |
| 60 | + torch.nn.init.xavier_normal_(random_molecule_properties) |
| 61 | + elif self.distribution == "xavier_uniform": |
| 62 | + torch.nn.init.xavier_uniform_(random_x) |
| 63 | + torch.nn.init.xavier_uniform_(random_edge_attr) |
| 64 | + torch.nn.init.xavier_uniform_(random_molecule_properties) |
| 65 | + else: |
| 66 | + raise ValueError("Unknown distribution type") |
| 67 | + |
| 68 | + data.x = random_x |
| 69 | + data.edge_attr = random_edge_attr |
| 70 | + data.molecule_attr = random_molecule_properties |
| 71 | + return data |
| 72 | + |
| 73 | + def read_property(self, *args, **kwargs) -> Exception: |
| 74 | + """This reader does not support reading specific properties.""" |
| 75 | + raise NotImplementedError("This reader only performs random initialization.") |
0 commit comments