# How to do backward() for a net with multiple outputs?

**URL:** <https://discuss.pytorch.org/t/how-to-do-backward-for-a-net-with-multiple-outputs/87>\
**Category:** Uncategorized\
**Created:** [January 20, 2017, 5:45pm UTC](https://discuss.pytorch.org/t/how-to-do-backward-for-a-net-with-multiple-outputs/87 "2017-01-20T17:45:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pengsun](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/p/e47c2d/32.png) [@pengsun](https://discuss.pytorch.org/u/pengsun)\
**Post date:** [January 20, 2017, 5:45pm UTC](https://discuss.pytorch.org/t/how-to-do-backward-for-a-net-with-multiple-outputs/87/1 "2017-01-20T17:45:36Z")

</div>

e.g., in Torch 7 I have a net with the last module an nn.ConcatTable, then I make the gradOutputs a table of tensors and do the net:backward(inputs, gradOutputs)

How to do similar things with pytorch? I tried to backward() for each output, but it complained that backward should not be called multiple times?

---

<div class="post-metadata">

**Author:** ![colesbury](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/colesbury/32/26_2.png) [@colesbury](https://discuss.pytorch.org/u/colesbury)\
**Post date:** [January 20, 2017, 5:57pm UTC](https://discuss.pytorch.org/t/how-to-do-backward-for-a-net-with-multiple-outputs/87/2 "2017-01-20T17:57:32Z")

</div>

I think you want `torch.autograd.backward(variables, grads)`:

[http://pytorch.org/docs/autograd.html#torch.autograd.backward](http://pytorch.org/docs/autograd.html#torch.autograd.backward)

It takes a list of variables and a list of gradients (one for each variable).

---

<div class="post-metadata">

**Author:** ![pengsun](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/p/e47c2d/32.png) [@pengsun](https://discuss.pytorch.org/u/pengsun)\
**Post date:** [January 21, 2017, 6:00am UTC](https://discuss.pytorch.org/t/how-to-do-backward-for-a-net-with-multiple-outputs/87/3 "2017-01-21T06:00:18Z")

</div>

Yes it is what I want! Thanks!  
Hope this can be added to tutorial/examples as this kind of backward() is common for multi-task learning. I thought I had to write code like `variable.backward()` as the [deep learning example](https://github.com/pytorch/tutorials/blob/master/Deep%20Learning%20with%20PyTorch.ipynb).

---

<div class="post-metadata">

**Author:** ![pengsun](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/p/e47c2d/32.png) [@pengsun](https://discuss.pytorch.org/u/pengsun)\
**Post date:** [January 21, 2017, 8:59am UTC](https://discuss.pytorch.org/t/how-to-do-backward-for-a-net-with-multiple-outputs/87/4 "2017-01-21T08:59:52Z")

</div>

Hi, the doc you pointed to has a minor problem: the second argument, `grad_variables`, should be “sequence of tensors”, not “sequence of Variables”.  
See your code here:

> <https://github.com/pytorch/pytorch/blob/master/test/test_autograd.py#L144-L146>

---

<div class="post-metadata">

**Author:** ![apaszke](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/apaszke/32/21_2.png) [@apaszke](https://discuss.pytorch.org/u/apaszke)\
**Post date:** [January 21, 2017, 12:33pm UTC](https://discuss.pytorch.org/t/how-to-do-backward-for-a-net-with-multiple-outputs/87/5 "2017-01-21T12:33:00Z")

</div>

Thanks for the report, we’ll fix that!
