# Multiple output tutorial/examples

**URL:** <https://discuss.pytorch.org/t/multiple-output-tutorial-examples/3050>\
**Category:** Uncategorized\
**Created:** [May 16, 2017, 2:32am UTC](https://discuss.pytorch.org/t/multiple-output-tutorial-examples/3050 "2017-05-16T02:32:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ducminhkhoi](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@ducminhkhoi](https://discuss.pytorch.org/u/ducminhkhoi)\
**Post date:** [May 16, 2017, 2:32am UTC](https://discuss.pytorch.org/t/multiple-output-tutorial-examples/3050/1 "2017-05-16T02:32:26Z")

</div>

I am doing with a network with multiple outputs. I have read this topic: [How to do backward() for a net with multiple outputs?](https://discuss.pytorch.org/t/how-to-do-backward-for-a-net-with-multiple-outputs/87). But I still don’t understand how to do it. Do you guys have any clear tutorials/examples how to do it?

Thanks,

---

<div class="post-metadata">

**Author:** ![smth](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/smth/32/13_2.png) [@smth](https://discuss.pytorch.org/u/smth)\
**Post date:** [May 16, 2017, 6:06am UTC](https://discuss.pytorch.org/t/multiple-output-tutorial-examples/3050/2 "2017-05-16T06:06:44Z")

</div>

This comment has an example: [Multi Label Classification in pytorch](https://discuss.pytorch.org/t/multi-label-classification-in-pytorch/905/11?u=smth)

That thread has a lot of detail.

---

<div class="post-metadata">

**Author:** ![ducminhkhoi](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@ducminhkhoi](https://discuss.pytorch.org/u/ducminhkhoi)\
**Post date:** [May 16, 2017, 1:47pm UTC](https://discuss.pytorch.org/t/multiple-output-tutorial-examples/3050/3 "2017-05-16T13:47:52Z")

</div>

Thanks for your reply. But it is not exactly what I need. My network generates 3 outputs which have different loss functions (MSE, Cross Entropy,…). Or use the same loss function but completely different outputs (e.g. classification from list 1, list 2 and list 3)

Like this one (where Dense = Linear)

 ![](https://discuss.pytorch.org/uploads/default/original/1X/0e2c62df4cef75c73f46d0a3d72ebafb6fb4ecad.png)

---

<div class="post-metadata">

**Author:** ![smth](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/smth/32/13_2.png) [@smth](https://discuss.pytorch.org/u/smth)\
**Post date:** [May 19, 2017, 3:41pm UTC](https://discuss.pytorch.org/t/multiple-output-tutorial-examples/3050/4 "2017-05-19T15:41:27Z")

</div>

doing multiple output + losses should be straight-forward. In your `forward` function of the model, you have the variables corresponding to each output, and send them through three separate loss functions.

The outputs of the loss functions can be backpropagated all together using `torch.autograd.backward`

---

<div class="post-metadata">

**Author:** ![platero](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/platero/32/352_2.png) [@platero](https://discuss.pytorch.org/u/platero)\
**Post date:** [May 26, 2017, 3:00am UTC](https://discuss.pytorch.org/t/multiple-output-tutorial-examples/3050/5 "2017-05-26T03:00:05Z")

</div>

Thanks for your hint. I also find [this post](https://discuss.pytorch.org/t/how-to-do-backward-for-a-net-with-multiple-outputs/87/2). I try to compute a loss for each output and append them to a list as the following code,

```auto
loss_seq = []
        for o in output:
            cur_loss = criterion(o, target_var)
            loss_seq.append(cur_loss)

```

Then I print the losses, which seem quite correct.

Then I tried to do the backpropagation

```auto
 torch.autograd.backward(loss_seq)

```

However, an error occured:

```auto
 File "example/main.py", line 174, in train
    torch.autograd.backward(loss_seq)
TypeError: backward() takes at least 2 arguments (1 given)

```

Am I doing wrong? How can I do backpropagation with mutiple loss?

---

<div class="post-metadata">

**Author:** ![smth](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/smth/32/13_2.png) [@smth](https://discuss.pytorch.org/u/smth)\
**Post date:** [May 26, 2017, 3:04am UTC](https://discuss.pytorch.org/t/multiple-output-tutorial-examples/3050/6 "2017-05-26T03:04:27Z")

</div>

you have to give: `torch.autograd.backward(loss_seq, grad_seq)` where `grad_seq` is the gradients for each of the losses.

If you want you can give gradients to be all ones:  
`grad_seq = [loss_seq[0].new(1).fill_(1) for _ in range(len(loss_seq))]`

---

<div class="post-metadata">

**Author:** ![platero](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/platero/32/352_2.png) [@platero](https://discuss.pytorch.org/u/platero)\
**Post date:** [May 27, 2017, 12:45pm UTC](https://discuss.pytorch.org/t/multiple-output-tutorial-examples/3050/7 "2017-05-27T12:45:13Z")

</div>

Thank you very much. But why can we use all ones as gradients here?

---

<div class="post-metadata">

**Author:** ![smth](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/smth/32/13_2.png) [@smth](https://discuss.pytorch.org/u/smth)\
**Post date:** [May 28, 2017, 4:54am UTC](https://discuss.pytorch.org/t/multiple-output-tutorial-examples/3050/8 "2017-05-28T04:54:16Z")

</div>

if the loss is a scalar value, then all ones as gradients are what you will use if you call `loss.backward()`.

---

<div class="post-metadata">

**Author:** ![cerisara](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/cerisara/32/428_2.png) [@cerisara](https://discuss.pytorch.org/u/cerisara)\
**Post date:** [December 8, 2017, 1:46pm UTC](https://discuss.pytorch.org/t/multiple-output-tutorial-examples/3050/9 "2017-12-08T13:46:42Z")

</div>

Thanks: I understand the ones are the gradients dloss/dloss=1 that are put at the very end of the backpropagation graph.

But creating this grad\_seq this way fails with  
AttributeError: ‘Variable’ object has no attribute ‘new’

EDIT: OK, it looks like it automatically converts tensors to variables, so this seems to work:

```auto
grad_seq = [loss_seq[0].data.new(1).fill_(1) for _ in range(len(loss_seq))]

```

---

<div class="post-metadata">

**Author:** ![Timos\_Korres](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/timos_korres/32/13109_2.png) [@Timos\_Korres](https://discuss.pytorch.org/u/Timos_Korres)\
**Post date:** [June 17, 2019, 10:46pm UTC](https://discuss.pytorch.org/t/multiple-output-tutorial-examples/3050/11 "2019-06-17T22:46:12Z")

</div>

Can a model output multiple outputs (as in the above experiments) and only have a single loss function?
