Upload models/gnn.py with huggingface_hub
Browse files- models/gnn.py +84 -0
models/gnn.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2023 DeepMind Technologies Limited
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
# ==============================================================================
|
| 15 |
+
|
| 16 |
+
"""Implementation of Graph Convolutional Neural Networks."""
|
| 17 |
+
|
| 18 |
+
import copy
|
| 19 |
+
import math
|
| 20 |
+
import torch
|
| 21 |
+
from torch import nn
|
| 22 |
+
import torch.nn.functional as F
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def clones(module, n):
|
| 26 |
+
return nn.ModuleList([copy.deepcopy(module) for _ in range(n)])
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class GraphConvolution(nn.Module):
|
| 30 |
+
"""Simple GCN layer, similar to https://arxiv.org/abs/1609.02907."""
|
| 31 |
+
|
| 32 |
+
def __init__(self, in_features, out_features, bias=True):
|
| 33 |
+
super().__init__()
|
| 34 |
+
self.in_features = in_features
|
| 35 |
+
self.out_features = out_features
|
| 36 |
+
self.weight = nn.Parameter(torch.FloatTensor(in_features, out_features))
|
| 37 |
+
if bias:
|
| 38 |
+
self.bias = nn.Parameter(torch.FloatTensor(out_features))
|
| 39 |
+
else:
|
| 40 |
+
self.register_parameter('bias', None)
|
| 41 |
+
self.reset_parameters()
|
| 42 |
+
|
| 43 |
+
def reset_parameters(self):
|
| 44 |
+
stdv = 1.0 / math.sqrt(self.weight.size(1))
|
| 45 |
+
self.weight.data.uniform_(-stdv, stdv)
|
| 46 |
+
if self.bias is not None:
|
| 47 |
+
self.bias.data.uniform_(-stdv, stdv)
|
| 48 |
+
|
| 49 |
+
def forward(self, inp, adj):
|
| 50 |
+
support = torch.matmul(inp, self.weight)
|
| 51 |
+
output = torch.matmul(adj.to_dense() if adj.is_sparse else adj, support)
|
| 52 |
+
if self.bias is not None:
|
| 53 |
+
return output + self.bias
|
| 54 |
+
else:
|
| 55 |
+
return output
|
| 56 |
+
|
| 57 |
+
def __repr__(self):
|
| 58 |
+
return (
|
| 59 |
+
self.__class__.__name__
|
| 60 |
+
+ ' ('
|
| 61 |
+
+ str(self.in_features)
|
| 62 |
+
+ ' -> '
|
| 63 |
+
+ str(self.out_features)
|
| 64 |
+
+ ')'
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
class GCN(nn.Module):
|
| 69 |
+
"""Graph Convolutional Neural Network class."""
|
| 70 |
+
|
| 71 |
+
def __init__(self, nfeat, nhid, nout, dropout, num_hidden):
|
| 72 |
+
super().__init__()
|
| 73 |
+
|
| 74 |
+
self.gc0 = GraphConvolution(nfeat, nhid)
|
| 75 |
+
self.gc_layers = clones(GraphConvolution(nhid, nhid), num_hidden)
|
| 76 |
+
self.out = nn.Linear(nhid, nout)
|
| 77 |
+
self.dropout = dropout
|
| 78 |
+
|
| 79 |
+
def forward(self, x, adj):
|
| 80 |
+
x = F.relu(self.gc0(x, adj))
|
| 81 |
+
|
| 82 |
+
for i, _ in enumerate(self.gc_layers):
|
| 83 |
+
x = F.relu(self.gc_layers[i](x, adj))
|
| 84 |
+
return self.out(x)
|