# Detach, no\_grad and requires\_grad

**URL:** <https://discuss.pytorch.org/t/detach-no-grad-and-requires-grad/16915>\
**Category:** autograd\
**Created:** [April 25, 2018, 5:29am UTC](https://discuss.pytorch.org/t/detach-no-grad-and-requires-grad/16915 "2018-04-25T05:29:41Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![whoab](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/whoab/32/65571_2.png) [@whoab](https://discuss.pytorch.org/u/whoab)\
**Post date:** [March 19, 2019, 10:01am UTC](https://discuss.pytorch.org/t/detach-no-grad-and-requires-grad/16915/10 "2019-03-19T10:01:06Z")

</div>

@albanD Hi, does detach erase the existing weights?

So if we do:

policy\_loss = …  
self.policy\_optimizer.zero\_grad()  
policy\_loss.backward()  
self.policy\_optimizer.step(0  
policy\_loss.detach()

For some reason when I was doing this in a loop the weights of the policy.parameters() [connected to the policy optimizer] stopped changing. I don’t understand the mechanism for why.

---

_[View the full topic](https://discuss.pytorch.org/t/detach-no-grad-and-requires-grad/16915)._
