I do need help right now...I am a pretty newbie here. Sorry

uni_cord: <p>Can anybody help with solving this? I think it is multiclass-multiouput case.</p>
<p>class CustomDataset(Dataset):<br>
def <strong>init</strong>(self, csv_data, img_root_path, transform=None):<br>
self.df = csv_data<br>
self.root_dir = img_root_path<br>
self.transform = transform</p>
<p>def <strong>len</strong>(self):<br>
return len(self.df)</p>
<p>def  <strong>getitem</strong>(self, index):<br>
img = PIL.Image.open(self.root_dir + self.df.iloc[index, 0]).convert(“RGB”)<br>
if self.transform is not None:<br>
img = self.transform(img)</p>
<pre><code>label = self.df.drop(columns=['fname', 'suara_paslon_1',	'suara_paslon_2',	'suara_paslon_3'])
label = torch.from_numpy(label.values).type(torch.LongTensor)

return img, label
</code></pre>
<p>bs = 32</p>
<p>train_transform = transforms.Compose([<br>
transforms.Resize((256, 256)),<br>
transforms.RandomHorizontalFlip(),<br>
transforms.RandomVerticalFlip(),<br>
transforms.RandomRotation(15),<br>
transforms.ColorJitter(brightness=0.3),<br>
transforms.ColorJitter(contrast=0.3),<br>
transforms.RandomAffine(0, shear=10),<br>
transforms.ToTensor()<br>
])</p>
<p>test_transform = transforms.Compose([<br>
transforms.Resize((256, 256)),<br>
transforms.ToTensor()<br>
])</p>
<p>train_set = CustomDataset(csv_data=df_train, img_root_path=‘/content/drive/MyDrive/Gammafest/data/Train/’, transform=train_transform)<br>
trainloader = DataLoader(train_set, batch_size=bs, num_workers=4)</p>
<p>test_set = CustomDataset(csv_data=df_test, img_root_path=‘/content/drive/MyDrive/Gammafest/data/Train/’, transform=test_transform)<br>
testloader = DataLoader(test_set, batch_size=bs)</p>
<p>label2cat = list(np.arange(0, 10))</p>
<p>class CNN(nn.Module):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>()<br>
self.conv = nn.Sequential(<br>
conv_block(3, 8),<br>
conv_block(8, 16),<br>
conv_block(16, 32),<br>
conv_block(32, 64),<br>
conv_block(64, 128),<br>
nn.Flatten()<br>
)</p>
<pre><code>    self.fc = nn.Sequential(
        linear_block(8192, 4096, dropout=0.1),
        linear_block(4096, 2048, dropout=0.1),
        linear_block(2048, 1024, dropout=0.1)
    )

    self.digit1_paslon_1 = linear_block(1024, 10, activation='lsoftmax')
    self.digit2_paslon_1 = linear_block(1024, 10, activation='lsoftmax')
    self.digit3_paslon_1 = linear_block(1024, 10, activation='lsoftmax')

    self.digit1_paslon_2 = linear_block(1024, 10, activation='lsoftmax')
    self.digit2_paslon_2 = linear_block(1024, 10, activation='lsoftmax')
    self.digit3_paslon_2 = linear_block(1024, 10, activation='lsoftmax')

    self.digit1_paslon_3 = linear_block(1024, 10, activation='lsoftmax')
    self.digit2_paslon_3 = linear_block(1024, 10, activation='lsoftmax')
    self.digit3_paslon_3 = linear_block(1024, 10, activation='lsoftmax')

def forward(self, x):
    x = self.conv(x)
    x = self.fc(x)

    pred_digit1_paslon_1 = self.digit1_paslon_1(x)
    pred_digit2_paslon_1 = self.digit2_paslon_1(x)
    pred_digit3_paslon_1 = self.digit3_paslon_1(x)

    pred_digit1_paslon_2 = self.digit1_paslon_2(x)
    pred_digit2_paslon_2 = self.digit2_paslon_2(x)
    pred_digit3_paslon_2 = self.digit3_paslon_2(x)

    pred_digit1_paslon_3 = self.digit1_paslon_3(x)
    pred_digit2_paslon_3 = self.digit2_paslon_3(x)
    pred_digit3_paslon_3 = self.digit3_paslon_3(x)

    return pred_digit1_paslon_1, pred_digit2_paslon_1, pred_digit3_paslon_1, pred_digit1_paslon_2, pred_digit2_paslon_2, pred_digit3_paslon_2, pred_digit1_paslon_3, pred_digit2_paslon_3, pred_digit3_paslon_3
</code></pre>
<p>config = set_config({<br>
‘batch_size’: bs<br>
})</p>
<p>model = CNN().to(device)</p>
<p>criterion = nn.NLLLoss()</p>
<p>optimizer = optim.AdamW(model.parameters(), lr=0.001)</p>
<p>callback = Callback(model, config, outdir=“model”)</p>
<p>from tqdm.auto import tqdm</p>
<p>def loop_fn(mode, dataset, dataloader, model, criterion, optimizer, device):<br>
if mode == “train”:<br>
model.train()<br>
elif mode == “test”:<br>
model.eval()<br>
cost = correct = 0<br>
for feature, target in tqdm(dataloader, desc=mode.title()):<br>
feature, target = feature.to(device), target.to(device)<br>
outputs = model(feature)</p>
<pre><code>    # Pisahkan output untuk setiap digit
    (pred_digit1_paslon_1, pred_digit2_paslon_1, pred_digit3_paslon_1,
     pred_digit1_paslon_2, pred_digit2_paslon_2, pred_digit3_paslon_2,
     pred_digit1_paslon_3, pred_digit2_paslon_3, pred_digit3_paslon_3) = outputs

    # Hitung kerugian untuk setiap prediksi digit
    loss1 = criterion(pred_digit1_paslon_1, target[:, 0])
    loss2 = criterion(pred_digit2_paslon_1, target[:, 1])
    loss3 = criterion(pred_digit3_paslon_1, target[:, 2])

    loss4 = criterion(pred_digit1_paslon_2, target[:, 3])
    loss5 = criterion(pred_digit2_paslon_2, target[:, 4])
    loss6 = criterion(pred_digit3_paslon_2, target[:, 5])

    loss7 = criterion(pred_digit1_paslon_3, target[:, 6])
    loss8 = criterion(pred_digit2_paslon_3, target[:, 7])
    loss9 = criterion(pred_digit3_paslon_3, target[:, 8])

    loss = loss1 + loss2 + loss3 + loss4 + loss5 + loss6 + loss7 + loss8 + loss9

    loss = criterion(output, target)

    if mode == "train":
        loss.backward()
        optimizer.step()
        optimizer.zero_grad()

    cost += loss.item() * feature.shape[0]
    correct += (output.argmax(1) == target).sum().item()
    # Mengasumsikan semua digit harus benar untuk dihitung sebagai prediksi yang benar
    correct += ((pred_digit1_paslon_1.argmax(1) == target[:, 0]) &amp;
                (pred_digit2_paslon_1.argmax(1) == target[:, 1]) &amp;
                (pred_digit3_paslon_1.argmax(1) == target[:, 2]) &amp;
                (pred_digit1_paslon_2.argmax(1) == target[:, 3]) &amp;
                (pred_digit2_paslon_2.argmax(1) == target[:, 4]) &amp;
                (pred_digit3_paslon_2.argmax(1) == target[:, 5]) &amp;
                (pred_digit1_paslon_3.argmax(1) == target[:, 6]) &amp;
                (pred_digit2_paslon_3.argmax(1) == target[:, 7]) &amp;
                (pred_digit3_paslon_3.argmax(1) == target[:, 8])).sum().item()

cost = cost / len(dataset)
acc = correct / len(dataset)
return cost, acc
</code></pre>
<p>while True:<br>
train_cost, train_score = loop_fn(“train”, train_set, trainloader, model, criterion, optimizer, device)<br>
with torch.no_grad():<br>
test_cost, test_score = loop_fn(“test”, test_set, testloader, model, criterion, optimizer, device)</p>
<pre><code># Logging
callback.log(train_cost, test_cost, train_score, test_score)

# Checkpoint
callback.save_checkpoint()

# Runtime Plotting
callback.cost_runtime_plotting()
callback.score_runtime_plotting()

# Early Stopping
if callback.early_stopping(model, monitor="test_score"):
    callback.plot_cost()
    callback.plot_score()
    break
</code></pre>
<p>this is the error:</p>
<hr>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in &lt;cell line: 1&gt;()<br>
1 while True:<br>
----&gt; 2     train_cost, train_score = loop_fn(“train”, train_set, trainloader, model, criterion, optimizer, device)<br>
3     with torch.no_grad():<br>
4         test_cost, test_score = loop_fn(“test”, test_set, testloader, model, criterion, optimizer, device)<br>
5</p>
<p>4 frames<br>
/usr/local/lib/python3.10/dist-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)<br>
2731     if size_average is not None or reduce is not None:<br>
2732         reduction = _Reduction.legacy_get_string(size_average, reduce)<br>
 → 2733     return torch._C._nn.nll_loss_nd(input, target, weight, _Reduction.get_enum(reduction), ignore_index)<br>
2734<br>
2735</p>
<p>RuntimeError: 0D or 1D target tensor expected, multi-target not supported</p>

uni_cord: <p>Excuse me, Sir <a class="mention" href="/u/ptrblck">@ptrblck</a><br>
Would you love to help me solving this problem?</p>

ptrblck: <p>This error is usually raised if the target shape is invalid as seen here:</p>
<pre data-code-wrap="python"><code class="lang-python">criterion = nn.NLLLoss()

batch_size = 10
nb_classes = 15

# works
output = torch.randn(batch_size, nb_classes, requires_grad=True)
target = torch.randint(0, nb_classes, (batch_size,))
loss = criterion(output, target)

# fails
output = torch.randn(batch_size, nb_classes, requires_grad=True)
target = torch.randint(0, nb_classes, (batch_size, 1))
loss = criterion(output, target)
# RuntimeError: 0D or 1D target tensor expected, multi-target not supported
</code></pre>
<p>Make sure the target contains the class indices and does not have a class dimension for <code>nn.NLLLoss</code> as seen in my example.</p>

uni_cord: <p>Hmmm…<br>
excuse me, sir<br>
but I still don’t understan what to do on my code</p>

ptrblck: <p>Check the shapes of your model output and the target.<br>
I’m not sure what:</p>
<aside class="quote no-group" data-username="uni_cord" data-post="1" data-topic="201467">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/u/f08c70/48.png" class="avatar"> uni_cord:</div>
<blockquote>
<p>I think it is multiclass-multiouput case.</p>
</blockquote>
</aside>
<p>means, but assume you are working on a multi-class classification, i.e. each sample is classified into one class only.</p>
<p>In this case, your model output should contain log probabilities in the shape <code>[batch_size, nb_classes]</code> and the target should contain class indices in the range <code>[0, nb_classes-1]</code> in the shape <code>[batch_size]</code> if you are using <code>nn.NLLLoss</code> (as seen in my code snippet).</p>