Would anyone help me solve my code error?

i wrote a code for GCN such as follower

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torch_geometric.datasets import TUDataset
from torch_geometric.data import DataLoader as GeoDataLoader
from torch_geometric.nn import GCNConv

device = torch.device(‘cuda’ if torch.cuda.is_available() else ‘cpu’)

Load the ENZYMES dataset

dataset = TUDataset(root=‘/python/GCN/ENZYMES’, name=‘ENZYMES’)
data_1 = dataset[0]

Split the dataset into train and test sets

num_train = int(len(dataset) * 0.8)
train_dataset = dataset[:num_train]
test_dataset = dataset[num_train:]

Define a GCN model

class GCN(nn.Module):
def init(self, in_channels, hidden_channels, out_channels): #in_channel : 노드 특징, hidden_channel : 엣지 특성, out_channels : num_classes 수
super().init()
self.conv1 = GCNConv(in_channels, hidden_channels)
self.conv2 = GCNConv(hidden_channels, out_channels)

def forward(self, data):
    x, edge_index, batch = data.x, data.edge_index, data.batch
    print(x.shape)
    print(edge_index.shape)
    print('-----------------1-------------------')
    x = self.conv1(x, edge_index)
    print(x.shape)
    print('-----------------2-------------------')
    x = F.relu(x)
    print(x.shape)
    print('-----------------3-------------------')
    x = F.dropout(x, p=0.5, training=self.training)
    print(x.shape)
    print('------------------4------------------')
    x = self.conv2(x, edge_index)
    print(x.shape)
    print('-----------------5-------------------')
    x = F.log_softmax(x, dim=1)
    print(x.shape)
    print('------------------6------------------')
    return x

Create a GCN model with 32 hidden units

model = GCN(dataset.num_features, 32, dataset.num_classes).to(device)
print(‘model run’)

Define the optimizer and loss function

optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
criterion = nn.NLLLoss()

Define the training loop

def train(model, device, loader, optimizer, criterion):
model.train()
total_loss = 0
for data in loader:
data = data.to(device)
optimizer.zero_grad()
output = model(data)
print(output.shape)
print(data.y.shape)
loss = criterion(output, data.y)
loss.backward()
optimizer.step()
total_loss += loss.item() * data.num_graphs
return total_loss / len(loader.dataset)

Define the evaluation loop

def evaluate(model, device, loader, criterion):
model.eval()
total_loss = 0
total_correct = 0
for data in loader:
data = data.to(device)
with torch.no_grad():
output = model(data)
loss = criterion(output, data.y)
total_loss += loss.item() * data.num_graphs
pred = output.argmax(dim=1)
total_correct += pred.eq(data.y).sum().item()
return total_loss / len(loader.dataset), total_correct / len(loader.dataset)

Train the GCN model

train_loader = GeoDataLoader(train_dataset, batch_size=32, shuffle=True)
test_loader = GeoDataLoader(test_dataset, batch_size=32, shuffle=False)
for epoch in range(1, 201):
print({epoch})
train_loss = train(model, device, train_loader, optimizer, criterion)
test_loss, test_acc = evaluate(model, device, test_loader, criterion)
print(f’Epoch: {epoch:03d}, Train Loss: {train_loss:.4f}, Test Loss: {test_loss:.4f}, Test Acc: {test_acc:.4f}')

Traceback (most recent call last):
File “E:/python/GCN/GCN_EX_TMP.py”, line 97, in
train_loss = train(model, device, train_loader, optimizer, criterion)
File “E:/python/GCN/GCN_EX_TMP.py”, line 69, in train
loss = criterion(output, data.y)
File “E:\python\job_python\nlp_env\lib\site-packages\torch\nn\modules\module.py”, line 727, in _call_impl
result = self.forward(*input, **kwargs)
File “E:\python\job_python\nlp_env\lib\site-packages\torch\nn\modules\loss.py”, line 213, in forward
return F.nll_loss(input, target, weight=self.weight, ignore_index=self.ignore_index, reduction=self.reduction)
File “E:\python\job_python\nlp_env\lib\site-packages\torch\nn\functional.py”, line 2262, in nll_loss
.format(input.size(0), target.size(0)))

ValueError: Expected input batch_size (961) to match target batch_size (32).
but, this code show me an Error that i cannot know how to fix ㅠ…

would anyone help me? plz …

Based on the error message and the code showing a batch size of 32 is used it seems the model output was reshaped wrongfully as it shows a batch size of 961.
I don’t know what the shape of the intermediate activations is, but you should check that the model input has a batch size of 32 and check which operation is creating the wrong shape.

PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier.

very, thank you so much. i will try some checks