|
| 1 | +"""Module for the Deep Tensor Network block.""" |
| 2 | + |
| 3 | +import torch |
| 4 | +from torch_geometric.nn import MessagePassing |
| 5 | + |
| 6 | + |
| 7 | +class DeepTensorNetworkBlock(MessagePassing): |
| 8 | + """ |
| 9 | + Implementation of the Deep Tensor Network block. |
| 10 | +
|
| 11 | + This block is used to perform message-passing between nodes and edges in a |
| 12 | + graph neural network, following the scheme proposed by Schutt et al. (2017). |
| 13 | + It serves as an inner block in a larger graph neural network architecture. |
| 14 | +
|
| 15 | + The message between two nodes connected by an edge is computed by applying a |
| 16 | + linear transformation to the sender node features and the edge features, |
| 17 | + followed by a non-linear activation function. Messages are then aggregated |
| 18 | + using an aggregation scheme (e.g., sum, mean, min, max, or product). |
| 19 | +
|
| 20 | + The update step is performed by a simple addition of the incoming messages |
| 21 | + to the node features. |
| 22 | +
|
| 23 | + .. seealso:: |
| 24 | +
|
| 25 | + **Original reference**: Schutt, K., Arbabzadah, F., Chmiela, S. et al. |
| 26 | + *Quantum-Chemical Insights from Deep Tensor Neural Networks*. |
| 27 | + Nature Communications 8, 13890 (2017). |
| 28 | + DOI: `<https://doi.org/10.1038/ncomms13890>_` |
| 29 | + """ |
| 30 | + |
| 31 | + def __init__( |
| 32 | + self, |
| 33 | + node_feature_dim, |
| 34 | + edge_feature_dim, |
| 35 | + activation=torch.nn.Tanh, |
| 36 | + aggr="add", |
| 37 | + node_dim=-2, |
| 38 | + flow="source_to_target", |
| 39 | + ): |
| 40 | + """ |
| 41 | + Initialization of the :class:`AVNOBDeepTensorNetworkBlocklock` class. |
| 42 | +
|
| 43 | + :param int node_feature_dim: The dimension of the node features. |
| 44 | + :param int edge_feature_dim: The dimension of the edge features. |
| 45 | + :param torch.nn.Module activation: The activation function. |
| 46 | + Default is :class:`torch.nn.Tanh`. |
| 47 | + :param str aggr: The aggregation scheme to use for message passing. |
| 48 | + Available options are "add", "mean", "min", "max", "mul". |
| 49 | + See :class:`torch_geometric.nn.MessagePassing` for more details. |
| 50 | + Default is "add". |
| 51 | + :param int node_dim: The axis along which to propagate. Default is -2. |
| 52 | + :param str flow: The direction of message passing. |
| 53 | + See :class:`torch_geometric.nn.MessagePassing` for more details. |
| 54 | + Default is "source_to_target". |
| 55 | + """ |
| 56 | + super().__init__(aggr=aggr, node_dim=node_dim, flow=flow) |
| 57 | + |
| 58 | + self.node_feature_dim = node_feature_dim |
| 59 | + self.edge_feature_dim = edge_feature_dim |
| 60 | + self.activation = activation |
| 61 | + |
| 62 | + # Layer for processing node features |
| 63 | + self.node_layer = torch.nn.Linear( |
| 64 | + in_features=self.node_feature_dim, |
| 65 | + out_features=self.node_feature_dim, |
| 66 | + bias=True, |
| 67 | + ) |
| 68 | + |
| 69 | + # Layer for processing edge features |
| 70 | + self.edge_layer = torch.nn.Linear( |
| 71 | + in_features=self.edge_feature_dim, |
| 72 | + out_features=self.node_feature_dim, |
| 73 | + bias=True, |
| 74 | + ) |
| 75 | + |
| 76 | + # Layer for computing the message |
| 77 | + self.message_layer = torch.nn.Linear( |
| 78 | + in_features=self.node_feature_dim, |
| 79 | + out_features=self.node_feature_dim, |
| 80 | + bias=False, |
| 81 | + ) |
| 82 | + |
| 83 | + def forward(self, x, edge_index, edge_attr): |
| 84 | + """ |
| 85 | + Forward pass of the block. It performs a message-passing operation |
| 86 | + between nodes and edges. |
| 87 | +
|
| 88 | + :param x: The node features. |
| 89 | + :type x: torch.Tensor | LabelTensor |
| 90 | + :param torch.Tensor edge_index: The edge indeces. |
| 91 | + :param edge_attr: The edge attributes. |
| 92 | + :type edge_attr: torch.Tensor | LabelTensor |
| 93 | + :return: The updated node features. |
| 94 | + :rtype: torch.Tensor |
| 95 | + """ |
| 96 | + return self.propagate(edge_index=edge_index, x=x, edge_attr=edge_attr) |
| 97 | + |
| 98 | + def message(self, x_j, edge_attr): |
| 99 | + """ |
| 100 | + Compute the message to be passed between nodes and edges. |
| 101 | +
|
| 102 | + :param x_j: The node features of the sender nodes. |
| 103 | + :type x_j: torch.Tensor | LabelTensor |
| 104 | + :param edge_attr: The edge attributes. |
| 105 | + :type edge_attr: torch.Tensor | LabelTensor |
| 106 | + :return: The message to be passed. |
| 107 | + :rtype: torch.Tensor |
| 108 | + """ |
| 109 | + # Process node and edge features |
| 110 | + filter_node = self.node_layer(x_j) |
| 111 | + filter_edge = self.edge_layer(edge_attr) |
| 112 | + |
| 113 | + # Compute the message to be passed |
| 114 | + message = self.message_layer(filter_node * filter_edge) |
| 115 | + |
| 116 | + return self.activation(message) |
| 117 | + |
| 118 | + def update(self, message, x): |
| 119 | + """ |
| 120 | + Update the node features with the received messages. |
| 121 | +
|
| 122 | + :param torch.Tensor message: The message to be passed. |
| 123 | + :param x: The node features. |
| 124 | + :type x: torch.Tensor | LabelTensor |
| 125 | + :return: The updated node features. |
| 126 | + :rtype: torch.Tensor |
| 127 | + """ |
| 128 | + return x + message |
0 commit comments