Skip to content

Instantly share code, notes, and snippets.

View rohithteja's full-sized avatar

Rohith Teja M rohithteja

  • CEA (Commissariat à l'énergie atomique et aux énergies alternatives)
  • Paris
View GitHub Profile
@rohithteja
rohithteja / karateclub.py
Last active September 24, 2022 02:56
Graph statistics
import networkx as nx
import numpy as np
import torch
from sklearn.preprocessing import StandardScaler
# load graph from networkx library
G = nx.karate_club_graph()
# retrieve the labels for each node
labels = np.asarray([G.nodes[i]['club'] != 'Mr. Hi' for i in G.nodes]).astype(np.int64)
@rohithteja
rohithteja / pyg-karateclub.py
Last active October 9, 2022 22:00
Pytorch Geometric custom dataset
import torch
import pandas as pd
from torch_geometric.data import InMemoryDataset, Data
from sklearn.model_selection import train_test_split
import torch_geometric.transforms as T
# custom dataset
class KarateDataset(InMemoryDataset):
def __init__(self, transform=None):
super(KarateDataset, self).__init__('.', transform, None, None)
@rohithteja
rohithteja / gcn-karateclub.py
Created August 10, 2021 11:37
Graph Convolutional Network
import torch.nn as nn
import torch.nn.functional as F
from torch_geometric.nn import GCNConv
# GCN model with 2 layers
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = GCNConv(data.num_features, 16)
self.conv2 = GCNConv(16, int(data.num_classes))
@rohithteja
rohithteja / train-gcn.py
Created August 10, 2021 12:10
Train GCN model
torch.manual_seed(42)
optimizer_name = "Adam"
lr = 1e-1
optimizer = getattr(torch.optim, optimizer_name)(model.parameters(), lr=lr)
epochs = 200
def train():
model.train()
optimizer.zero_grad()
@rohithteja
rohithteja / deepwalk-karate.py
Last active February 19, 2024 16:24
DeepWalk embeddings on Karate Club dataset
from ge import DeepWalk
# load graph from networkx library
G = nx.karate_club_graph()
labels = np.asarray([G.nodes[i]['club'] != 'Mr. Hi' for i in G.nodes]).astype(np.int64)
# convert nodes from int to str format
keys = np.arange(0,34)
values = [str(i) for i in keys]
@rohithteja
rohithteja / tsne-karate.py
Created August 14, 2021 13:35
Karate club embedding visualization
import numpy as np
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
# retrieve the labels for each node
labels = np.asarray([G.nodes[i]['club'] != 'Mr. Hi' for i in G.nodes]).astype(np.int64)
# assigning colours to node labels
color_map = []
for i in labels:
@rohithteja
rohithteja / dataprep-karate.py
Last active August 21, 2021 12:54
Data Preparation
import networkx as nx
import numpy as np
import torch
# load graph from networkx library
G = nx.karate_club_graph()
# retrieve the labels for each node
labels = np.asarray([G.nodes[i]['club'] != 'Mr. Hi' for i in G.nodes]).astype(np.int64)
@rohithteja
rohithteja / pyg-karate.py
Created August 14, 2021 15:31
Custom Dataset PyG
import torch
import pandas as pd
from torch_geometric.data import InMemoryDataset, Data
from sklearn.model_selection import train_test_split
import torch_geometric.transforms as T
# custom dataset
class KarateDataset(InMemoryDataset):
def __init__(self, transform=None):
super(KarateDataset, self).__init__('.', transform, None, None)
@rohithteja
rohithteja / dgl-custom-karate.py
Created August 21, 2021 13:20
DGL Custom Dataset
import pandas as pd
import numpy as np
import dgl
import torch
from dgl.data import DGLDataset
from sklearn.model_selection import train_test_split
# prepare the embeddings corresponding to each node
nodes = pd.DataFrame(list(H.nodes()))
nodes.columns = ['nodes']
@rohithteja
rohithteja / spektral-custom-karate.py
Created August 21, 2021 14:07
Spektral Custom Dataset
import torch
import networkx as nx
from spektral.data import Dataset
from sklearn.preprocessing import OneHotEncoder
from sklearn.model_selection import train_test_split
from spektral.transforms import AdjToSpTensor, LayerPreprocess
from spektral.layers import GCNConv
# spektral custom dataset class
class KarateDataset(Dataset):