# Grad can be implicitly created only for scalar outputs

**URL:** <https://discuss.pytorch.org/t/grad-can-be-implicitly-created-only-for-scalar-outputs/116234>\
**Category:** Uncategorized\
**Created:** [March 28, 2021, 9:51am UTC](https://discuss.pytorch.org/t/grad-can-be-implicitly-created-only-for-scalar-outputs/116234 "2021-03-28T09:51:44Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [March 30, 2021, 5:15am UTC](https://discuss.pytorch.org/t/grad-can-be-implicitly-created-only-for-scalar-outputs/116234/2 "2021-03-30T05:15:10Z")

</div>

The error is raised, if you try to call `.backward()` on a non-scalar tensor (i.e. a tensor with more than a single element). If that’s the desired use case, you would have to provide the gradients manually e.g. via `.backward(gradient=torch.ones_like(loss))` or reduce the loss before (e.g. via `loss.mean().backward()`).  
@albanD explains it in [this post](https://discuss.pytorch.org/t/why-do-we-need-to-pass-the-gradient-parameter-to-the-backward-function-in-pytorch/116079/4) with more detail.

PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier 😉

---

_[View the full topic](https://discuss.pytorch.org/t/grad-can-be-implicitly-created-only-for-scalar-outputs/116234)._
