Would anyone help me solve my code error plz?

i wrote a code for GCN such as follower

but, this code show me an Error that i cannot know how to fix …

would anyone help me? plz …

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')

dataset = TUDataset(root='/python/GCN/ENZYMES', name='ENZYMES')
num_train = int(len(dataset) * 0.8)
train_dataset = dataset[:num_train]
test_dataset = dataset[num_train:]


class GCN(nn.Module):

    def __init__(self, in_channels, hidden_channels, out_channels):   
        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)
        x = self.conv1(x, edge_index)
        print(x.shape)
        x = F.relu(x)
        print(x.shape)
        x = F.dropout(x, p=0.5, training=self.training)
        print(x.shape)
        x = self.conv2(x, edge_index)
        print(x.shape)
        x = F.log_softmax(x, dim=1)
        print(x.shape)
        return x



model = GCN(dataset.num_features, 32, dataset.num_classes).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
criterion = nn.NLLLoss()


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)


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_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 96, in <module>
    train_loss = train(model, device, train_loader, optimizer, criterion)
  File "E:/python/GCN/GCN_EX_TMP.py", line 68, 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 (897) to match target batch_size (32).

Is this a double post from here or what’s the difference between these two issues?

i posted a code using backtick “”. it is not different

However, I tried checking the input output size and it seems that the GCN model for this code is determined by the class of the ENZYMES data regardless of the batch size. I know the batch size usually by writing 32,64,128, but I wonder if this code is good.

The output size of the GCN model should be the batch size of 32, but I cannot adjust the size in the GCN class. What should I do? Help m