MCPcopy Create free account
hub / github.com/RolnickLab/climart / GraphNetwork

Class GraphNetwork

climart/models/GraphNet/graph_network.py:14–100  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12
13
14class GraphNetwork(BaseModel):
15 def __init__(self,
16 # input_dim: Dict[str, int],
17 hidden_dims: Sequence[int],
18 input_transform: AbstractGraphTransform,
19 readout_which_output: Optional[str] = NODES,
20 update_mlp_n_layers: int = 1,
21 aggregator_funcs: Union[str, Dict[AggregationTypes, int]] = 'sum',
22 net_normalization: str = 'layer_norm',
23 residual: Union[bool, Dict[str, bool]] = True,
24 activation_function: str = 'Gelu',
25 output_activation_function: Optional[str] = None,
26 output_net_normalization: bool = True,
27 dropout: float = 0.0,
28 *args, **kwargs):
29 """
30 Args:
31 readout_which_output: Which graph part to return (default: edges),
32 can be {EDGES, NODES, GLOBALS, 'graph', None}
33 If None or 'graph', the whole graph is returned.
34 """
35 super().__init__(input_transform=input_transform, *args, **kwargs)
36 self.save_hyperparameters(ignore="verbose_mlp")
37 assert len(self.hparams.hidden_dims) >= 1
38 assert update_mlp_n_layers >= 1
39 self.input_transform: AbstractGraphTransform = self.input_transform
40 in_dim = self.input_transform.output_dim
41
42 senders, receivers = self.input_transform.get_edge_idxs()
43 gn_layers = []
44 dims = [in_dim] + list(hidden_dims)
45 for i in range(1, len(dims)):
46 out_activation_function = output_activation_function if i == len(dims) - 1 else activation_function
47 out_net_norm = output_net_normalization if i == len(dims) - 1 else True
48 gn_layers += [
49 GraphNetBlock(
50 in_dims=in_dim,
51 out_dims=dims[i],
52 senders=senders,
53 receivers=receivers,
54 n_layers=update_mlp_n_layers,
55 residual=residual,
56 net_norm=net_normalization,
57 activation=activation_function,
58 dropout=dropout,
59 output_normalization=out_net_norm,
60 output_activation_function=out_activation_function,
61 aggregator_funcs=aggregator_funcs,
62 )]
63 in_dim = dims[i]
64
65 self.layers: nn.ModuleList[GraphNetBlock] = nn.ModuleList(gn_layers)
66 self.output_type = readout_which_output
67 if self.output_type not in [NODES, EDGES, GLOBALS, 'graph', None]:
68 raise ValueError("Unsupported argument for GraphNetwork `output_type`", readout_which_output)
69 if hasattr(self.input_transform, "n_edges"):
70 err_msg = f"GN inferred {self.n_edges} edges, but input_tranform refers to {self.input_transform.n_edges}"
71 assert self.n_edges == self.input_transform.n_edges, err_msg

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected