How to implement ReadOut layer based on torch_geometric ? #6428
-
My job is to complete a graph classification task based on GCN and I want to do it on torch_geometric . For example, a batch of a training contains G graphs, a total of n nodes. After the node feature matrix is convolved , the node feature matrix of row n needs to be converted into graph feature of row G. The feature of each graph needs to integrate the attributes belonging to the graph node in a specific way, such as taking the maximum value of each component as the value of the graph feature on this component, or directly taking the average value . The following is a concrete implementation of my model import numpy as np
import torch
import torch_geometric
from torch_geometric.datasets import TUDataset
from torch_geometric.loader import DataLoader
from torch_geometric.graphgym import register_pooling
from torch_geometric.nn import GCNConv
import torch_scatter
import scipy
# Prepare ENZYMES data set
enzymes_dataset = TUDataset(
root=r'C:\Users\Administrator\Desktop\data',
name='ENZYMES'
)
enzymes_loader = DataLoader(
batch_size=16,
shuffle=False
)
# Convolution layer definition
class Net(torch.nn.Module):
def __init__(self):
super(Net,self).__init__()
self.conv1 = GCNConv(enzymes_dataset.num_node_features,16)
self.conv2 = GCNConv(16, 32)
self.fc1 = torch.nn.Linear(32,16)
self.fc2 = torch.nn.Linear(16,enzymes_dataset.num_classes)
def forward(self,data):
x, edge_index = data.x, data.edge_index
x = self.conv1(x,edge_index)
x = torch.nn.functional.relu(x)
x = self.conv2(x,edge_index)
x = torch.nn.functional.relu(x)
# x = Read_Out (x) This's the function I'm asking for help
x = self.fc1(x)
x = self.fc2(x)
return torch.nn.functional.log_softmax(x,dim=-1) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
You can leverage any |
Beta Was this translation helpful? Give feedback.
You can leverage any
torch_geoemtric.Aggregation
module for the final readout. Here is an example that usesglobal_add_pool
.