# Backward CNN with multiple output

**URL:** <https://discuss.pytorch.org/t/backward-cnn-with-multiple-output/3390>\
**Category:** autograd\
**Created:** [May 24, 2017, 3:21am UTC](https://discuss.pytorch.org/t/backward-cnn-with-multiple-output/3390 "2017-05-24T03:21:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeakey](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zeakey/32/4341_2.png) [@zeakey](https://discuss.pytorch.org/u/zeakey)\
**Post date:** [May 24, 2017, 3:21am UTC](https://discuss.pytorch.org/t/backward-cnn-with-multiple-output/3390/1 "2017-05-24T03:21:11Z")

</div>

I’m now reimplementing the “Holistically-nested Edge Detection”([arXiv](https://arxiv.org/abs/1504.06375)) with pytorch, which has multiple “side-outputs”.

So how can I backward CNN with multiple outputs ? This [thread](https://discuss.pytorch.org/t/how-to-do-backward-for-a-net-with-multiple-outputs/87) suggested to use `torch.autograd.backward([x1, x2], [grad_x1, grad_x2])`, but I run into error with this solution, following is my error message:

```auto
Traceback (most recent call last):
  File "main.py", line 68, in <module>
    train()
  File "main.py", line 63, in train
    torch.autograd.backward(outputs, grads)
  File "/usr/local/lib/python2.7/dist-packages/torch/autograd/ __init__.py", line 98, in backward
    variables, grad_variables, retain_graph)
RuntimeError: CUDNN_STATUS_MAPPING_ERROR

```

My code can be summarized as follow:

```auto
outputs = model(data)
loss, grads = some_numpy_tricks(outputs)
torch.autograd.backward(outputs, grads)
print "now loss=%f"%loss

```

In which `outputs` is a list containing multiple outputs of the model, and `grads` is the list containing gradients of loss w.r.t each model output.

So why did error occur and what’s the correct solution for my case?

---

<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, 3:33pm UTC](https://discuss.pytorch.org/t/backward-cnn-with-multiple-output/3390/2 "2017-05-28T15:33:31Z")

</div>

this seems like a cudnn bug.  
Can you give me a way to reproduce this on my side?

You can also try setting this flag (at the beginning of your script) and see if it goes away: `torch.backends.cudnn.benchmark=True`

---

<div class="post-metadata">

**Author:** ![zeakey](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zeakey/32/4341_2.png) [@zeakey](https://discuss.pytorch.org/u/zeakey)\
**Post date:** [June 21, 2017, 11:34am UTC](https://discuss.pytorch.org/t/backward-cnn-with-multiple-output/3390/3 "2017-06-21T11:34:39Z")

</div>

Hi Smth, sorry to bothering you again.

I met the new error:

```
RuntimeError: dependency not found for N5torch8autograd12ConvBackwardE

```

I have been working on this for long but cannot solve the problem.

I have committed the code to [Gitlab](https://gitlab.com/zeakey/pytorch-hed), could you please help me to solve this problem?

---

<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:** [June 21, 2017, 6:33pm UTC](https://discuss.pytorch.org/t/backward-cnn-with-multiple-output/3390/4 "2017-06-21T18:33:40Z")

</div>

which version of pytorch are you on? `print(torch. __version__ )`, how did you install pytorch (conda or pip)? and what OS are you on?

---

<div class="post-metadata">

**Author:** ![zeakey](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zeakey/32/4341_2.png) [@zeakey](https://discuss.pytorch.org/u/zeakey)\
**Post date:** [June 22, 2017, 2:21am UTC](https://discuss.pytorch.org/t/backward-cnn-with-multiple-output/3390/5 "2017-06-22T02:21:28Z")

</div>

Thanks for your reply, my environments are as follow:  
PyTorch:

```
In [2]: print torch. __version__
0.1.12_2

```

Installed via pip following the official instructions.  
OS:

```
uname -a
Linux server01 4.4.0-72-generic #93~14.04.1-Ubuntu SMP Fri Mar 31 15:05:15 UTC 2017 x86_64 x86_64 x86_64 
GNU/Linux

```

CUDA:

```
nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Sun_Sep__4_22:14:01_CDT_2016
Cuda compilation tools, release 8.0, V8.0.44

```

The error mentioned before occurs both on CPU and GPU mode, so it seems not to be a CUDA problem.

---

<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:** [September 21, 2017, 3:50am UTC](https://discuss.pytorch.org/t/backward-cnn-with-multiple-output/3390/6 "2017-09-21T03:50:18Z")

</div>

I’m really hoping that after you upgraded to v0.2.0, this issue has gone.

---

<div class="post-metadata">

**Author:** ![zeakey](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zeakey/32/4341_2.png) [@zeakey](https://discuss.pytorch.org/u/zeakey)\
**Post date:** [September 21, 2017, 5:38am UTC](https://discuss.pytorch.org/t/backward-cnn-with-multiple-output/3390/7 "2017-09-21T05:38:15Z")

</div>

OK, I will update to v0.2 and retry.

Thank you.

---

<div class="post-metadata">

**Author:** ![XuKai](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/x/4da419/32.png) [@XuKai](https://discuss.pytorch.org/u/XuKai)\
**Post date:** [September 28, 2017, 3:04am UTC](https://discuss.pytorch.org/t/backward-cnn-with-multiple-output/3390/8 "2017-09-28T03:04:04Z")

</div>

@zeakey Hi, have you finished the reimplementing? Will you public your code?

---

<div class="post-metadata">

**Author:** ![Matthew\_Wong](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/matthew_wong/32/12025_2.png) [@Matthew\_Wong](https://discuss.pytorch.org/u/Matthew_Wong)\
**Post date:** [September 7, 2019, 9:53pm UTC](https://discuss.pytorch.org/t/backward-cnn-with-multiple-output/3390/9 "2019-09-07T21:53:43Z")

</div>

Though don’t know why, it did work. Thanks so much.
