ValueError: Expected input batch_size (491520) to match target batch_size (1)

username: Erenhub
cooked: <p>I try to do brain tumor segmentation for BRATS. Sadly i got this error, The problem seems to be here : loss = functional.nll_loss(y, y_hat)</p>
<p>when i try to implement another loss function such as nn.BCELoss, it doesnt recognize the nn and new fuction, the rest of the code:</p>
<p>def train(i_epoch = 0):<br>
model.train()</p>
<pre><code>iteration = len(train_loader) * i_epoch

for x, y_hat in train_loader:

    optimizer.zero_grad()

    y = model(x)
    y = torch.argmax(y)
    loss = functional.nll_loss(y, y_hat)  # crossentropy loss. If one class is too imbalanced,
                                          # we can use the weights argument!
    loss.backward()

    optimizer.step()

    iteration += 1

    if (iteration % 100 == 0):
        writer.add_scalar("loss", loss.data.item(), iteration)

        # img = 
        # writer.add_image("trainimg", y)

# validation:
loss_val = 0
model.eval()
for x, y_hat in val_loader:

    y = model(x)

    # if (loss_val == 0):
    #     writer.add_image("val image", toTensor(img), iteration)


    loss_val += functional.nll_loss(y, y_hat)  # crossentropy loss. If one class is too imbalanced,
                                          # we can use the weights argument!


writer.add_scalar("val", loss_val.data.item(), iteration)

print("Validation loss: {:10}".format(loss_val))</code></pre>

username: Erenhub
cooked: <p>it says : AttributeError: module ‘torch.nn.functional’ has no attribute ‘BCELoss’ although i already satisfy  :<br>
pip install torch==1.2.0 torchvision==0.4.1 in the colab…</p>

username: crowsonkb
cooked: <p><code>BCELoss</code> is in <code>torch.nn</code>: <a href="https://pytorch.org/docs/stable/nn.html#bceloss" rel="nofollow noopener">https://pytorch.org/docs/stable/nn.html#bceloss</a></p>
<p>If you want to use <code>torch.nn.functional</code>, it has <code>binary_cross_entropy()</code>: <a href="https://pytorch.org/docs/stable/nn.functional.html#binary-cross-entropy" rel="nofollow noopener">https://pytorch.org/docs/stable/nn.functional.html#binary-cross-entropy</a></p>

username: Erenhub
cooked: <p>now i get</p>
<p>Traceback (most recent call last):<br>
File “train_bu.py”, line 255, in <br>
train(i_epoch)<br>
File “train_bu.py”, line 213, in train<br>
loss = torch.nn.BCELoss(y, y_hat)  # crossentropy loss. If one class is too imbalanced,<br>
File “/usr/local/lib/python3.6/dist-packages/torch/nn/modules/loss.py”, line 495, in <strong>init</strong><br>
super(BCELoss, self).<strong>init</strong>(weight, size_average, reduce, reduction)<br>
File “/usr/local/lib/python3.6/dist-packages/torch/nn/modules/loss.py”, line 19, in <strong>init</strong><br>
super(_WeightedLoss, self).<strong>init</strong>(size_average, reduce, reduction)<br>
File “/usr/local/lib/python3.6/dist-packages/torch/nn/modules/loss.py”, line 12, in <strong>init</strong><br>
self.reduction = _Reduction.legacy_get_string(size_average, reduce)<br>
File “/usr/local/lib/python3.6/dist-packages/torch/nn/_reduction.py”, line 36, in legacy_get_string<br>
if size_average and reduce:<br>
RuntimeError: bool value of Tensor with more than one value is ambiguous</p>