GNN Python Code in Keras and Pytorch - by YashwanthReddyGoduguchintha - Medium
GNN Python Code in Keras and Pytorch - by YashwanthReddyGoduguchintha - Medium
pytorch
YashwanthReddyGoduguchintha · Follow
3 min read · Mar 6
import torch
import torch.nn as nn
import torch.nn.functional as F
class GCNBlock(nn.Module):
def __init__(self, in_features, out_features):
super(GCNBlock, self).__init__()
self.linear = nn.Linear(in_features, out_features)
This model also uses GCN blocks to perform node classification on a graph.
The GCNBlock class defines a single GCN layer, and the `
Open in app
————————————————————————————————
— — — — — Search
———— Write
Graph Neural Networks (GNNs) are a type of neural network used for
processing data that is represented in graph structures. GNNs are commonly
used in applications such as recommendation systems, drug discovery, and
social network analysis.
GNN in Keras
class GraphConvolution(Layer):
def __init__(self, output_dim, **kwargs):
self.output_dim = output_dim
super(GraphConvolution, self).__init__(**kwargs)
def build(self, input_shape):
self.kernel = self.add_weight(name='kernel',
shape=(input_shape[1],
self.output_dim),
initializer='glorot_uniform',
trainable=True)
super(GraphConvolution, self).build(input_shape)
def call(self, x):
output = tf.matmul(x, self.kernel)
return output
GNN in PyTorch
import torch
import torch.nn as nn
class GraphConvolution(nn.Module):
def __init__(self, input_dim, output_dim):
super(GraphConvolution, self).__init__()
self.weight = nn.Parameter(torch.FloatTensor(input_dim,
output_dim))
nn.init.xavier_uniform_(self.weight)
95 Followers
👉 Check out my daily newsletter to learn something new about Python and Data Science
every day| Linked Group:-https://www.linkedin.com/groups/14119059/
YashwanthReddyGoduguchintha YashwanthReddyGoduguchintha
YashwanthReddyGoduguchintha YashwanthReddyGoduguchintha
8 1 1
81
Lists
1 54 2
4 106