# How to implement gradient penalty in PyTorch

**URL:** <https://discuss.pytorch.org/t/how-to-implement-gradient-penalty-in-pytorch/1656>\
**Category:** Uncategorized\
**Created:** [April 5, 2017, 7:24am UTC](https://discuss.pytorch.org/t/how-to-implement-gradient-penalty-in-pytorch/1656 "2017-04-05T07:24:58Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![chenyuntc](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/chenyuntc/32/299_2.png) [@chenyuntc](https://discuss.pytorch.org/u/chenyuntc)\
**Post date:** [May 8, 2017, 2:42am UTC](https://discuss.pytorch.org/t/how-to-implement-gradient-penalty-in-pytorch/1656/7 "2017-05-08T02:42:30Z")

</div>

You are right, most function are still old-style which don’t support grad of grad.  
There is a temporary fix: use difference rather than differential  
 ![](https://discuss.pytorch.org/uploads/default/original/1X/c0708eec6775d377c5695eec7d1863453b552720.png)  
x\_1,x\_2 are sampled from x\_hat  
idea from 郑华滨

---

_[View the full topic](https://discuss.pytorch.org/t/how-to-implement-gradient-penalty-in-pytorch/1656)._
