# Runtime warning loss.backward resize

**URL:** <https://discuss.pytorch.org/t/runtime-warning-loss-backward-resize/144609>\
**Category:** C++\
**Created:** [February 20, 2022, 4:21pm UTC](https://discuss.pytorch.org/t/runtime-warning-loss-backward-resize/144609 "2022-02-20T16:21:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![frakkerman](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/frakkerman/32/46824_2.png) [@frakkerman](https://discuss.pytorch.org/u/frakkerman)\
**Post date:** [February 20, 2022, 4:21pm UTC](https://discuss.pytorch.org/t/runtime-warning-loss-backward-resize/144609/1 "2022-02-20T16:21:34Z")

</div>

I am using the C++ API.  
When using either l1\_loss or mse\_loss I get an annoying warning when calling loss.backward(). Small code snippet:

```auto
  opt.zero_grad();		
  auto forward = model->forward(minibatch.features);
  torch::Tensor loss = torch::mse_loss(forward, minibatch.labels);//torch::Tensor loss = torch::l1_loss(forward, minibatch.labels);
  loss.backward();
  opt.step();

```

The runtime warning I get is as follows:

```auto
[W ..\..\aten\src\ATen\native\Resize.cpp:23] Warning: An output with one or more elements was resized since it had shape [64, 1], which does not match the required output shape [64, 64].This behavior is deprecated, and in a future PyTorch release outputs will not be resized unless they have zero elements. You can explicitly reuse an out tensor t by resizing it, inplace, to zero elements with t.resize_(0). (function resize_output_check)

```

So, in this case my minibatch size is 64, the features and labels are all correctly dimensioned. I do not understand why it is expecting a [64,64] output shape.  
I searched for this online, found one topic ([DCGAN C++ warning after PyTorch update · Issue #819 · pytorch/examples · GitHub](https://github.com/pytorch/examples/issues/819)) that deals with this problem, but no good solution seems to be proposed.

Am I overlooking something in the mse/l1 loss functions? Is this a known issue? Is there a way to change the verbosity of warning printing in the C++ API? FYI: the code runs without errors and the neural net seems to fit properly. I use LibTorch version 1.10.2+cpu.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [February 21, 2022, 8:16pm UTC](https://discuss.pytorch.org/t/runtime-warning-loss-backward-resize/144609/2 "2022-02-21T20:16:00Z")

</div>

What shapes do the `forward` and `minibatch.labels` have?  
I’m wondering if you are broadcasting in `mse_loss` (which could yield wrong results, if that’s not intended).

---

<div class="post-metadata">

**Author:** ![frakkerman](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/frakkerman/32/46824_2.png) [@frakkerman](https://discuss.pytorch.org/u/frakkerman)\
**Post date:** [February 22, 2022, 8:16am UTC](https://discuss.pytorch.org/t/runtime-warning-loss-backward-resize/144609/3 "2022-02-22T08:16:37Z")

</div>

In the case of a minibatch size of 64 `forward` is a `CPUFloatType{64,1}`, so it contains a prediction for every sample in the minibatch. `minibatch.labels` is a `CPUFloatType{64}`.

So, you are completely right, I was broadcasting in `mse_loss`. I fixed this by making `minibatch.labels` a `CPUFloatType{64,1}`, so it has the same shape as `forward`.

Thanks for pointing me in the right direction!
