Would anyone help me solve my code error?

username: dfdf_pakr
cooked: <p>i wrote a code for GCN such as follower</p>
<p>import torch<br>
import torch.nn as nn<br>
import torch.nn.functional as F<br>
from torch.utils.data import DataLoader<br>
from torch_geometric.datasets import TUDataset<br>
from torch_geometric.data import DataLoader as GeoDataLoader<br>
from torch_geometric.nn import GCNConv</p>
<p>device = torch.device(‘cuda’ if torch.cuda.is_available() else ‘cpu’)</p>
<h1>
<a name="load-the-enzymes-dataset-1" class="anchor" href="#load-the-enzymes-dataset-1"></a>Load the ENZYMES dataset</h1>
<p>dataset = TUDataset(root=‘/python/GCN/ENZYMES’, name=‘ENZYMES’)<br>
data_1 = dataset[0]</p>
<h1>
<a name="split-the-dataset-into-train-and-test-sets-2" class="anchor" href="#split-the-dataset-into-train-and-test-sets-2"></a>Split the dataset into train and test sets</h1>
<p>num_train = int(len(dataset) * 0.8)<br>
train_dataset = dataset[:num_train]<br>
test_dataset = dataset[num_train:]</p>
<h1>
<a name="define-a-gcn-model-3" class="anchor" href="#define-a-gcn-model-3"></a>Define a GCN model</h1>
<p>class GCN(nn.Module):<br>
def <strong>init</strong>(self, in_channels, hidden_channels, out_channels):   <span class="hashtag">#in_channel</span> : 노드 특징, hidden_channel : 엣지 특성, out_channels : num_classes 수<br>
super().<strong>init</strong>()<br>
self.conv1 = GCNConv(in_channels, hidden_channels)<br>
self.conv2 = GCNConv(hidden_channels, out_channels)</p>
<pre><code>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
</code></pre>
<h1>
<a name="create-a-gcn-model-with-32-hidden-units-4" class="anchor" href="#create-a-gcn-model-with-32-hidden-units-4"></a>Create a GCN model with 32 hidden units</h1>
<p>model = GCN(dataset.num_features, 32, dataset.num_classes).to(device)<br>
print(‘model run’)</p>
<h1>
<a name="define-the-optimizer-and-loss-function-5" class="anchor" href="#define-the-optimizer-and-loss-function-5"></a>Define the optimizer and loss function</h1>
<p>optimizer = torch.optim.Adam(model.parameters(), lr=0.01)<br>
criterion = nn.NLLLoss()</p>
<h1>
<a name="define-the-training-loop-6" class="anchor" href="#define-the-training-loop-6"></a>Define the training loop</h1>
<p>def train(model, device, loader, optimizer, criterion):<br>
model.train()<br>
total_loss = 0<br>
for data in loader:<br>
data = data.to(device)<br>
optimizer.zero_grad()<br>
output = model(data)<br>
print(output.shape)<br>
print(data.y.shape)<br>
loss = criterion(output, data.y)<br>
loss.backward()<br>
optimizer.step()<br>
total_loss += loss.item() * data.num_graphs<br>
return total_loss / len(loader.dataset)</p>
<h1>
<a name="define-the-evaluation-loop-7" class="anchor" href="#define-the-evaluation-loop-7"></a>Define the evaluation loop</h1>
<p>def evaluate(model, device, loader, criterion):<br>
model.eval()<br>
total_loss = 0<br>
total_correct = 0<br>
for data in loader:<br>
data = data.to(device)<br>
with torch.no_grad():<br>
output = model(data)<br>
loss = criterion(output, data.y)<br>
total_loss += loss.item() * data.num_graphs<br>
pred = output.argmax(dim=1)<br>
total_correct += pred.eq(data.y).sum().item()<br>
return total_loss / len(loader.dataset), total_correct / len(loader.dataset)</p>
<h1>
<a name="train-the-gcn-model-8" class="anchor" href="#train-the-gcn-model-8"></a>Train the GCN model</h1>
<h2>
<a name="train_loader-geodataloadertrain_dataset-batch_size32-shuffletrue-test_loader-geodataloadertest_dataset-batch_size32-shufflefalse-for-epoch-in-range1-201-printepoch-train_loss-trainmodel-device-train_loader-optimizer-criterion-test_loss-test_acc-evaluatemodel-device-test_loader-criterion-printfepoch-epoch03d-train-loss-train_loss4f-test-loss-test_loss4f-test-acc-test_acc4f-9" class="anchor" href="#train_loader-geodataloadertrain_dataset-batch_size32-shuffletrue-test_loader-geodataloadertest_dataset-batch_size32-shufflefalse-for-epoch-in-range1-201-printepoch-train_loss-trainmodel-device-train_loader-optimizer-criterion-test_loss-test_acc-evaluatemodel-device-test_loader-criterion-printfepoch-epoch03d-train-loss-train_loss4f-test-loss-test_loss4f-test-acc-test_acc4f-9"></a>train_loader = GeoDataLoader(train_dataset, batch_size=32, shuffle=True)<br>
test_loader = GeoDataLoader(test_dataset, batch_size=32, shuffle=False)<br>
for epoch in range(1, 201):<br>
print({epoch})<br>
train_loss = train(model, device, train_loader, optimizer, criterion)<br>
test_loss, test_acc = evaluate(model, device, test_loader, criterion)<br>
print(f’Epoch: {epoch:03d}, Train Loss: {train_loss:.4f}, Test Loss: {test_loss:.4f}, Test Acc: {test_acc:.4f}')</h2>
<h2>
<a name="traceback-most-recent-call-last-file-epythongcngcn_ex_tmppy-line-97-in-module-train_loss-trainmodel-device-train_loader-optimizer-criterion-file-epythongcngcn_ex_tmppy-line-69-in-train-loss-criterionoutput-datay-file-epythonjob_pythonnlp_envlibsite-packagestorchnnmodulesmodulepy-line-727-in-_call_impl-result-selfforwardinput-kwargs-file-epythonjob_pythonnlp_envlibsite-packagestorchnnmoduleslosspy-line-213-in-forward-return-fnll_lossinput-target-weightselfweight-ignore_indexselfignore_index-reductionselfreduction-file-epythonjob_pythonnlp_envlibsite-packagestorchnnfunctionalpy-line-2262-in-nll_loss-formatinputsize0-targetsize0-10" class="anchor" href="#traceback-most-recent-call-last-file-epythongcngcn_ex_tmppy-line-97-in-module-train_loss-trainmodel-device-train_loader-optimizer-criterion-file-epythongcngcn_ex_tmppy-line-69-in-train-loss-criterionoutput-datay-file-epythonjob_pythonnlp_envlibsite-packagestorchnnmodulesmodulepy-line-727-in-_call_impl-result-selfforwardinput-kwargs-file-epythonjob_pythonnlp_envlibsite-packagestorchnnmoduleslosspy-line-213-in-forward-return-fnll_lossinput-target-weightselfweight-ignore_indexselfignore_index-reductionselfreduction-file-epythonjob_pythonnlp_envlibsite-packagestorchnnfunctionalpy-line-2262-in-nll_loss-formatinputsize0-targetsize0-10"></a>Traceback (most recent call last):<br>
File “E:/python/GCN/GCN_EX_TMP.py”, line 97, in <br>
train_loss = train(model, device, train_loader, optimizer, criterion)<br>
File “E:/python/GCN/GCN_EX_TMP.py”, line 69, in train<br>
loss = criterion(output, data.y)<br>
File “E:\python\job_python\nlp_env\lib\site-packages\torch\nn\modules\module.py”, line 727, in _call_impl<br>
result = self.forward(*input, **kwargs)<br>
File “E:\python\job_python\nlp_env\lib\site-packages\torch\nn\modules\loss.py”, line 213, in forward<br>
return F.nll_loss(input, target, weight=self.weight, ignore_index=self.ignore_index, reduction=self.reduction)<br>
File “E:\python\job_python\nlp_env\lib\site-packages\torch\nn\functional.py”, line 2262, in nll_loss<br>
.format(input.size(0), target.size(0)))</h2>
<p>ValueError: Expected input batch_size (961) to match target batch_size (32).<br>
but, this code show me an Error that i cannot know how to fix  ㅠ…</p>
<p>would anyone help me? plz …</p>

username: ptrblck
cooked: <p>Based on the error message and the code showing a batch size of <code>32</code> is used it seems the model output was reshaped wrongfully as it shows a batch size of <code>961</code>.<br>
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 <code>32</code> and check which operation is creating the wrong shape.</p>
<p>PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier.</p>

username: dfdf_pakr
cooked: <p>very, thank you so much. i will try some checks</p>