GNN python code in Keras and
pytorch
YashwanthReddyGoduguchintha · Follow
3 min read · Mar 6
Graph Neural Networks (GNNs) are a class of neural networks designed to
work with graph data. Keras and PyTorch are two popular deep learning
libraries that support GNNs.
Here is an example of a simple GNN model implemented in Keras:
from keras.layers import Input, Dense
from keras.models import Model
from keras.optimizers import Adam
from keras.layers import Dropout
from keras.layers import BatchNormalization
from keras.layers import Activation
from keras.layers import Concatenate
from keras.layers import AveragePooling1D
from keras.layers import GlobalMaxPooling1D
from keras.layers import GlobalAveragePooling1D
from keras.layers import Conv1D
def GCN_block(inputs, feature_dims, activation):
# Graph Convolution Layer
outputs = Conv1D(feature_dims, 1, activation=None,
use_bias=False)(inputs)
outputs = BatchNormalization()(outputs)
outputs = Activation(activation)(outputs)
return outputs
# Define the input shape
inputs = Input(shape=(num_nodes, num_features))
# Define the model architecture
x = GCN_block(inputs, 64, 'relu')
x = Dropout(0.5)(x)
x = GCN_block(x, 64, 'relu')
x = Dropout(0.5)(x)
outputs = GCN_block(x, num_classes, 'softmax')
# Define the model and compile it
model = Model(inputs=inputs, outputs=outputs)
optimizer = Adam(lr=0.01)
model.compile(optimizer=optimizer, loss='categorical_crossentropy',
metrics=['accuracy'])
# Train the model
model.fit(X_train, y_train, validation_data=(X_val, y_val),
epochs=100, batch_size=32)
This model uses Graph Convolutional Networks (GCN) to perform node
classification on a graph. The GCN blocks take as input the node features
and output new features for each node. The output of the final GCN block is
used to predict the class labels for the nodes.
Here is an example of a simple GNN model implemented in PyTorch:
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)
def forward(self, x, adj):
x = self.linear(x)
x = torch.matmul(adj, x)
x = F.relu(x)
return x
class GCN(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super(GCN, self).__init__()
self.gcn1 = GCNBlock(input_dim, hidden_dim)
self.gcn2 = GCNBlock(hidden_dim, output_dim)
def forward(self, x, adj):
x = self.gcn1(x, adj)
x = F.dropout(x, p=0.5, training=self.training)
x = self.gcn2(x, adj)
return x
# Define the model
model = GCN(num_features, hidden_dim, num_classes)
# Define the loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
# Train the model
for epoch in range(num_epochs):
optimizer.zero_grad()
outputs = model(features, adj)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
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.
Here’s an example of how to implement a GNN using Keras and PyTorch:
GNN in Keras
from keras.layers import Input, Dense, Concatenate
from keras.models import Model
from keras.optimizers import Adam
from keras.layers import Layer
import tensorflow as tf
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
def compute_output_shape(self, input_shape):
return (input_shape[0], self.output_dim)
# Define the input layer
X_input = Input(shape=(n_features,))
# Define the graph convolutional layer
graph_conv = GraphConvolution(output_dim=n_hidden)(X_input)
# Define the output layer
output_layer = Dense(units=n_classes, activation='softmax')
(graph_conv)
# Define the model
model = Model(inputs=X_input, outputs=output_layer)
# Compile the model
optimizer = Adam(lr=learning_rate)
model.compile(optimizer=optimizer, loss='categorical_crossentropy',
metrics=['accuracy'])
# Train the model
model.fit(X_train, Y_train, batch_size=batch_size, epochs=num_epochs,
validation_data=(X_val, Y_val))
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)
def forward(self, adj_matrix, feature_matrix):
output = torch.mm(feature_matrix, self.weight)
output = torch.mm(adj_matrix, output)
return output
# Define the model
class GNN(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super(GNN, self).__init__()
self.gc1 = GraphConvolution(input_dim, hidden_dim)
self.gc2 = GraphConvolution(hidden_dim, output_dim)
self.relu = nn.ReLU()
def forward(self, adj_matrix, feature_matrix):
hidden = self.gc1(adj_matrix, feature_matrix)
hidden = self.relu(hidden)
output = self.gc2(adj_matrix, hidden)
return output
# Define the loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# Train the model
for epoch in range(num_epochs):
output = model(adj_matrix, feature_matrix)
loss = criterion(output, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
Note that in both examples, we define a GraphConvolution layer which
performs a matrix multiplication between the input features and a weight
matrix, followed by an activation function. In the Keras example, we define
the GraphConvolution layer as a custom layer using the Keras API, while in
the PyTorch example, we define it as a module using the PyTorch API. We
then define the
Gnn Pytorch Keras TensorFlow
Written by YashwanthReddyGoduguchintha Follow
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/
More from YashwanthReddyGoduguchintha
YashwanthReddyGoduguchintha YashwanthReddyGoduguchintha
Fine-Tuning GPT-3.5 on Custom Beginner’s Guide to FastAPI &
Dataset: A Step-by-Step Guide OpenAI ChatGPT API Integration
Introduction TABLE OF CONTENTS
4 min read · Nov 14 6 min read · Aug 28
YashwanthReddyGoduguchintha YashwanthReddyGoduguchintha
Building a Document-based Azure Data Bricks(DEMO)
Question Answering System with… What is a Databricks?
1. Introduction
6 min read · Apr 29 4 min read · Oct 23
8 1 1
See all from YashwanthReddyGoduguchintha
Recommended from Medium
Krishna Chaitanya Cristian Urbinati in Data Reply IT | DataTech
Graph Neural Networks and Spatio-Temporal Forecasting using
implementing in TensorFlow Temporal Graph Neural Networks
This article guide you through the process of Application of Spatio-Temporal GNN for
understanding Graph Neural Networks… traffic forecasting on a custom dataset…
7 min read · Aug 12 8 min read · Jun 5
81
Lists
Practical Guides to Machine Natural Language Processing
Learning 880 stories · 410 saves
10 stories · 705 saves
Jihwan Shubham Krishna in ML6team
TabNet —Deep Neural Net for Whisper Deployment Decisions:
Tabular Part I — Evaluating Latency, Costs,…
Deep Neural Networks (DNNs) are really good Introduction to Whisper
at training images and text, but These…
5 min read · Jun 17 3 min read · Jul 22
1 54 2
Anisatrop Ebrahim Pichka in Towards AI
PyTorch Lightning Overview Graph Attention Networks paper
explained with illustration and…
PyTorch with a Twist: A Look at PyTorch A detailed and illustrated walkthrough of the
Lightning “Graph Attention Networks” paper by…
4 min read · Jul 5 · 13 min read · Jul 26
4 106
See more recommendations