MCPcopy Create free account
hub / github.com/pytorch/examples / GAT

Class GAT

gat/main.py:143–203  ·  view source on GitHub ↗

Graph Attention Network (GAT) as described in the paper `"Graph Attention Networks" `. Consists of a 2-layer stack of Graph Attention Layers (GATs). The fist GAT Layer is followed by an ELU activation. And the second (final) layer is a GAT layer wit

Source from the content-addressed store, hash-verified

141################################
142
143class GAT(nn.Module):
144 """
145 Graph Attention Network (GAT) as described in the paper `"Graph Attention Networks" <https://arxiv.org/pdf/1710.10903.pdf>`.
146 Consists of a 2-layer stack of Graph Attention Layers (GATs). The fist GAT Layer is followed by an ELU activation.
147 And the second (final) layer is a GAT layer with a single attention head and softmax activation function.
148 """
149 def __init__(self,
150 in_features,
151 n_hidden,
152 n_heads,
153 num_classes,
154 concat=False,
155 dropout=0.4,
156 leaky_relu_slope=0.2):
157 """ Initializes the GAT model.
158
159 Args:
160 in_features (int): number of input features per node.
161 n_hidden (int): output size of the first Graph Attention Layer.
162 n_heads (int): number of attention heads in the first Graph Attention Layer.
163 num_classes (int): number of classes to predict for each node.
164 concat (bool, optional): Wether to concatinate attention heads or take an average over them for the
165 output of the first Graph Attention Layer. Defaults to False.
166 dropout (float, optional): dropout rate. Defaults to 0.4.
167 leaky_relu_slope (float, optional): alpha (slope) of the leaky relu activation. Defaults to 0.2.
168 """
169
170 super(GAT, self).__init__()
171
172 # Define the Graph Attention layers
173 self.gat1 = GraphAttentionLayer(
174 in_features=in_features, out_features=n_hidden, n_heads=n_heads,
175 concat=concat, dropout=dropout, leaky_relu_slope=leaky_relu_slope
176 )
177
178 self.gat2 = GraphAttentionLayer(
179 in_features=n_hidden, out_features=num_classes, n_heads=1,
180 concat=False, dropout=dropout, leaky_relu_slope=leaky_relu_slope
181 )
182
183
184 def forward(self, input_tensor: torch.Tensor , adj_mat: torch.Tensor):
185 """
186 Performs a forward pass through the network.
187
188 Args:
189 input_tensor (torch.Tensor): Input tensor representing node features.
190 adj_mat (torch.Tensor): Adjacency matrix representing graph structure.
191
192 Returns:
193 torch.Tensor: Output tensor after the forward pass.
194 """
195
196 # Apply the first Graph Attention layer
197 x = self.gat1(input_tensor, adj_mat)
198 x = F.elu(x) # Apply ELU activation function to the output of the first layer
199
200 # Apply the second Graph Attention layer

Callers 1

main.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected