# Back-propagate through torch.stft and torch.istft

**URL:** <https://discuss.pytorch.org/t/back-propagate-through-torch-stft-and-torch-istft/161694>\
**Category:** audio\
**Created:** [September 19, 2022, 9:17am UTC](https://discuss.pytorch.org/t/back-propagate-through-torch-stft-and-torch-istft/161694 "2022-09-19T09:17:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pepper8362](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/p/9e8a1a/32.png) [@pepper8362](https://discuss.pytorch.org/u/pepper8362)\
**Post date:** [September 19, 2022, 9:17am UTC](https://discuss.pytorch.org/t/back-propagate-through-torch-stft-and-torch-istft/161694/1 "2022-09-19T09:17:14Z")

</div>

Hi,

Do torch.stft and torch.istft support autograd? Is there a way to back-propagate through them?

Thanks

---

<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:** [September 19, 2022, 11:42pm UTC](https://discuss.pytorch.org/t/back-propagate-through-torch-stft-and-torch-istft/161694/2 "2022-09-19T23:42:43Z")

</div>

Yes, Autograd is able to backpropagate through these methods as seen in this small example:

```python
input = torch.randn(16, requires_grad=True)
out = torch.stft(input, 10) 
out.mean().backward()
print(input.grad)
# tensor([-3.5224e-02, 1.5741e-01, -6.3721e-02, 1.5741e-01, -2.8497e-02,
# 1.4815e-01, -1.8626e-09, 9.2593e-02, -1.8626e-09, 9.2593e-02,
# 2.8497e-02, 1.0185e-01, 3.5224e-02, 5.5556e-02, 6.3721e-02,
# 2.7778e-02])

```

---

<div class="post-metadata">

**Author:** ![pepper8362](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/p/9e8a1a/32.png) [@pepper8362](https://discuss.pytorch.org/u/pepper8362)\
**Post date:** [September 20, 2022, 3:49am UTC](https://discuss.pytorch.org/t/back-propagate-through-torch-stft-and-torch-istft/161694/3 "2022-09-20T03:49:52Z")

</div>

Hi,

Thanks for your reply. I tried your example but got

```auto
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/ubuntu/anaconda3/lib/python3.9/site-packages/torch/_tensor.py", line 307, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)
  File "/home/ubuntu/anaconda3/lib/python3.9/site-packages/torch/autograd/ __init__.py", line 154, in backward
    Variable._execution_engine.run_backward(
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

```

My pytorch version is 1.10.2+cu113. Does it mean I need to update to a newer version?

Thanks.

---

<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:** [September 20, 2022, 4:39am UTC](https://discuss.pytorch.org/t/back-propagate-through-torch-stft-and-torch-istft/161694/4 "2022-09-20T04:39:53Z")

</div>

No, I don’t think you need to update as my code snippet also works in `1.10.2`:

```python
>>> import torch
>>> torch. __version__'1.10.2+cu113'
>>> input = torch.randn(16, requires_grad=True)
>>> out = torch.stft(input, 10) 
>>> out.mean().backward()
>>> print(input.grad)
tensor([-3.5224e-02, 1.5741e-01, -6.3721e-02, 1.5741e-01, -2.8497e-02,
         1.4815e-01, -1.8626e-09, 9.2593e-02, -1.8626e-09, 9.2593e-02,
         2.8497e-02, 1.0185e-01, 3.5224e-02, 5.5556e-02, 6.3721e-02,
         2.7778e-02])

```

so I guess you might not have used my code snippet directly or you might have disabled the gradient calculation with a context manager or globally.

---

<div class="post-metadata">

**Author:** ![pepper8362](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/p/9e8a1a/32.png) [@pepper8362](https://discuss.pytorch.org/u/pepper8362)\
**Post date:** [September 20, 2022, 5:33am UTC](https://discuss.pytorch.org/t/back-propagate-through-torch-stft-and-torch-istft/161694/5 "2022-09-20T05:33:29Z")

</div>

> [@ptrblck](#):
>
> ```auto
> input = torch.randn(16, requires_grad=True)
> out = torch.stft(input, 10) 
> out.mean().backward()
> print(input.grad)
> 
> ```

Yes it works. I mistakenly disabled the gradient calculation. Thank you very much!
