RuntimeError: The size of tensor a (1966080) must match the size of tensor b (655360) at non-singleton dimension 0

username: lavender99
cooked: <p>i am having this error when i run</p>
<pre><code class="lang-auto">criterion.forward(test_lb.cuda(),test_op)
</code></pre>
<p>print(len(test_op))</p>
<p>print(len(test_lb))<br>
is 10 for both of them</p>

username: ptrblck
cooked: <p>Could you print the shapes of both tensors?</p>
<pre><code class="lang-python">print(test_lb.shape)
print(test_op.shape)
</code></pre>
<p>Also, which criterion are you using?</p>

username: lavender99
cooked: <p>The shapes are :<br>
Test_op (10,1,128,128)<br>
Test_lb (10,3,128,128)</p>

username: ptrblck
cooked: <p>Which criterion are you using?</p>

username: lavender99
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="4" data-topic="38871">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>Which criterion</p>
</blockquote>
</aside>
<pre><code class="lang-auto">class DiceCoeffLoss(nn.Module):
    def __init__(self,smooth=1):
        super(DiceCoeffLoss, self).__init__()
        self.smooth = smooth
    
    def forward(self,input, target):
        iflat = input.view(-1)
        tflat = target.view(-1)
        intersection = (iflat * tflat).sum()
        return 1 - ((2. * intersection + self.smooth) / (iflat.sum() + tflat.sum() + self.smooth))
criterion = DiceCoeffLoss()

</code></pre>

username: ptrblck
cooked: <p>It seems you are trying to calculate the dice loss for a multi-class prediction.<br>
If that’s the case, you shouldn’t just flatten the prediction and target, as this will yield the size mismatch error (your prediction contains more values than the target).</p>
<p>You could try to pass your target as a one-hot encoded tensor and use <a class="mention" href="/u/issamlaradji">@IssamLaradji</a>’s <a href="https://github.com/pytorch/pytorch/issues/1249#issuecomment-339904369" rel="nofollow noopener">approach</a>.</p>

username: lavender99
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="6" data-topic="38871">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>one-hot encoded tensor</p>
</blockquote>
</aside>
<p>sorry i do not understand how to use this approch for my code</p>

username: ptrblck
cooked: <p>Is my assumption correct that you are trying to predict three different classes?<br>
If so, you could convert your target tensor containing the class indices into a one-hot encoded target using:</p>
<pre><code class="lang-python">nb_classes = 3
target = torch.empty(1, 24, 24, dtype=torch.long).random_(nb_classes)
target = torch.zeros(1, nb_classes, 24, 24).scatter_(1, target.unsqueeze(1), 1)
</code></pre>

username: lavender99
cooked: <pre><code class="lang-auto">type or pascriterion = DiceCoeffLoss()
optimizer = torch.optim.Adam(model.parameters(),lr=1e-3,weight_decay=1e-5)
target = model(images)
nb_classes = 2
target = target.to('cuda')
model = model.to('cuda')
target = torch.empty(1, 256, 256, dtype=torch.long).random_(nb_classes)
target = torch.zeros(1, nb_classes, 256, 256).scatter_(1, target.unsqueeze(1), 1)
print (target.shape)
print(labels.shape)
criterion.forward(labels.cuda(),target)te code here
</code></pre>
<p>now this error appears<br>
RuntimeError: expected type torch.cuda.FloatTensor but got torch.FloatTensor<br>
also is there something wrong with the size like below ?<br>
torch.Size([1, 2, 256, 256])<br>
torch.Size([2, 1, 256, 256])</p>

username: ptrblck
cooked: <p>The <code>target</code> seems to be still on the CPU, while the model output (<code>labels</code>?) are on the GPU.<br>
Could you push the target also to the GPU and try the linked implementation?</p>

username: Shariq_Ali
cooked: <p>hello i’m facing the following error when calculating accuracy against MSELoss function. Following is my code and shapes.<br>
RuntimeError: The size of tensor a (2) must match the size of tensor b (3) at non-singleton dimension 1</p>
<pre><code>    input_var = Variable(data.float())
    target_var = Variable(target.float())

    optimizer.zero_grad()

    output = model(input_var)

    loss = criterion(output, target_var)
    _, preds = torch.max(output.data, 1)
    dice =  dice_coef(output,target)

    loss.backward()


    optimizer.step()

    train_loss += loss.item()*data.size(0)
    train_metric =  dice*data.size(0)

    total_train += target_var.nelement()
    correct_train += preds.eq(target_var.data).sum().item()
    train_acc = 100 * correct_train / total_train
    print("train Acc ",train_acc)
</code></pre>
<p>Shapes are<br>
target shape  torch.Size([2, 3, 512, 512])<br>
Output shape  torch.Size([2, 3, 512, 512])<br>
Preds shape  torch.Size([2, 512, 512])</p>

username: ptrblck
cooked: <p>It seems your targets are one-hot encoded.<br>
If that’s the case, use <code>torch.argmax(targets, 1)</code> to get the class indices.</p>

username: m_c_long
cooked: <p>hello im having error when i run<br>
_, pred = torch.argmax(y.data, dim=1)</p>
<p>ValueError: too many values to unpack (expected 2)<br>
i hope you can help me<br>
my code</p>
<pre><code>model_ft.eval()


count = 0
with torch.no_grad():
    for image, label in val_loader:
        image = image.cuda()
        label = label.cuda()
        
        y = model_ft(image)
        
        
        _, pred = torch.argmax(y.data, dim=1)
        count += torch.sum(preds == label.data)

    
val_acc = count.item() / len(val_data)
if best_acc &lt; val_acc:
  best_acc = val_acc
print("val accuracy: {:.4f}".format(val_acc))
</code></pre>
<p>print(“best accuracy: {:.4f}”.format(best_acc))</p>

username: ptrblck
cooked: <p><code>torch.argmax</code> returns only the indices for the max values.<br>
If you want to get the values and indices returned, you could use <code>torch.max()</code>.<br>
However, since you are deleting the first return value, I assume you only care about the indices, so</p>
<pre><code class="lang-python">pred = torch.argmax(y, dim=1)
</code></pre>
<p>should work.</p>
<p>PS: Don’t use the <code>.data</code> attribute, as it might yield unwanted side effects.</p>