I am using pytorch-geometric. Here is the dataset creation code, following this I ran torch.save('dataset.pt')
.
import torch
import numpy as np
from scipy.sparse import coo_matrix
from torch_geometric.data import Data, Dataset, download_url
def graph_data(A, X, labels):
tg_graphs = []
labels = torch.FloatTensor(labels)
for i in range(len(A)):
coo = coo_matrix(A[i])
indices = np.vstack((coo.row, coo.col))
x = [ord(i) for i in X[i]]
index = torch.LongTensor(indices)
feature = torch.Tensor(x)
graph = Data(x=feature, edge_index=index, y=labels[i])
tg_graphs.append(graph)
return tg_graphs
After that, I ran my model:
import torch
dataset = torch.load('data/dataset.pt')
#%%
data = dataset[0] # Get the first graph object.
print()
print(data)
print('=============================================================')
# Gather some statistics about the first graph.
print(f'Number of nodes: {data.num_nodes}')
print(f'Number of edges: {data.num_edges}')
print(f'Average node degree: {data.num_edges / data.num_nodes:.2f}')
print(f'Has isolated nodes: {data.has_isolated_nodes()}')
print(f'Has self-loops: {data.has_self_loops()}')
print(f'Is undirected: {data.is_undirected()}')
#%%
train_dataset = dataset[:5000]
test_dataset = dataset[5000:]
print(f'Number of training graphs: {len(train_dataset)}')
print(f'Number of test graphs: {len(test_dataset)}')
#%%
from torch_geometric.loader import DataLoader
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False)
for step, data in enumerate(train_loader):
print(f'Step {step + 1}:')
print('=======')
print(f'Number of graphs in the current batch: {data.num_graphs}')
print(data)
print()
#%%
from torch.nn import Linear
import torch.nn.functional as F
from torch_geometric.nn import GCNConv
from torch_geometric.nn import global_mean_pool
class GCN(torch.nn.Module):
def __init__(self, hidden_channels):
super(GCN, self).__init__()
torch.manual_seed(12345)
self.conv1 = GCNConv(-1, hidden_channels)
self.conv2 = GCNConv(hidden_channels, hidden_channels)
self.conv3 = GCNConv(hidden_channels, hidden_channels)
self.lin = Linear(hidden_channels, 35609)
def forward(self, x, edge_index, batch):
# 1. Obtain node embeddings
x = self.conv1(x, edge_index)
x = x.relu()
x = self.conv2(x, edge_index)
x = x.relu()
x = self.conv3(x, edge_index)
# 2. Readout layer
x = global_mean_pool(x, batch) # [batch_size, hidden_channels]
# 3. Apply a final classifier
x = F.dropout(x, p=0.5, training=self.training)
x = self.lin(x)
return x
model = GCN(hidden_channels=64)
print(model)
#%%
model = GCN(hidden_channels=64)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
criterion = torch.nn.CrossEntropyLoss()
def train():
model.train()
for data in train_loader.dataset: # Iterate in batches over the training dataset.
out = model(data.x.reshape(-1,1), data.edge_index, data.batch) # Perform a single forward pass.
loss = criterion(out, data.y) # Compute the loss.
loss.backward() # Derive gradients.
optimizer.step() # Update parameters based on gradients.
optimizer.zero_grad() # Clear gradients.
def test(loader):
model.eval()
correct = 0
for data in loader: # Iterate in batches over the training/test dataset.
out = model(data.x, data.edge_index, data.batch)
pred = out.argmax(dim=1) # Use the class with highest probability.
correct += int((pred == data.y).sum()) # Check against ground-truth labels.
return correct / len(loader.dataset) # Derive ratio of correct predictions.
#%%
for epoch in range(1, 171):
train()
train_acc = test(train_loader)
test_acc = test(test_loader)
print(f'Epoch: {epoch:03d}, Train Acc: {train_acc:.4f}, Test Acc: {test_acc:.4f}')
And received the following error:
ValueError Traceback (most recent call last)
Input In [7], in <cell line: 1>()
1 for epoch in range(1, 171):
----> 2 train()
3 train_acc = test(train_loader)
4 test_acc = test(test_loader)
Input In [6], in train()
8 for data in train_loader.dataset: # Iterate in batches over the training dataset.
9 out = model(data.x.reshape(-1,1), data.edge_index, data.batch) # Perform a single forward pass.
---> 10 loss = criterion(out, data.y) # Compute the loss.
11 loss.backward() # Derive gradients.
12 optimizer.step() # Update parameters based on gradients.
File ~\anaconda3\lib\site-packages\torch\nn\modules\module.py:1110, in Module._call_impl(self, *input, **kwargs)
1106 # If we don't have any hooks, we want to skip the rest of the logic in
1107 # this function, and just call forward.
1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1109 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110 return forward_call(*input, **kwargs)
1111 # Do not call functions when jit is used
1112 full_backward_hooks, non_full_backward_hooks = [], []
File ~\anaconda3\lib\site-packages\torch\nn\modules\loss.py:1163, in CrossEntropyLoss.forward(self, input, target)
1162 def forward(self, input: Tensor, target: Tensor) -> Tensor:
-> 1163 return F.cross_entropy(input, target, weight=self.weight,
1164 ignore_index=self.ignore_index, reduction=self.reduction,
1165 label_smoothing=self.label_smoothing)
File ~\anaconda3\lib\site-packages\torch\nn\functional.py:2996, in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction, label_smoothing)
2994 if size_average is not None or reduce is not None:
2995 reduction = _Reduction.legacy_get_string(size_average, reduce)
-> 2996 return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)
ValueError: Expected input batch_size (1) to match target batch_size (0).
Does it have to do with the cross entropy or the way my dataset is created?
My data looks like this:
[Data(x=[18], edge_index=[2, 42], y=1.0),
Data(x=[18], edge_index=[2, 42], y=1.0),
Data(x=[17], edge_index=[2, 40], y=1.0),
Data(x=[17], edge_index=[2, 40], y=1.0),
Data(x=[18], edge_index=[2, 42], y=1.0),
Data(x=[19], edge_index=[2, 40], y=1.0),
Data(x=[19], edge_index=[2, 40], y=1.0),
Data(x=[19], edge_index=[2, 40], y=1.0),
Data(x=[19], edge_index=[2, 40], y=1.0),
Data(x=[20], edge_index=[2, 42], y=1.0),
Data(x=[19], edge_index=[2, 40], y=1.0),
Data(x=[20], edge_index=[2, 42], y=1.0),
Data(x=[18], edge_index=[2, 38], y=1.0),
Data(x=[19], edge_index=[2, 40], y=1.0),
...
I have 43,000+ graphs with 2 classes.