# Check gradient flow in network

**URL:** <https://discuss.pytorch.org/t/check-gradient-flow-in-network/15063>\
**Category:** Uncategorized\
**Created:** [March 17, 2018, 7:42am UTC](https://discuss.pytorch.org/t/check-gradient-flow-in-network/15063 "2018-03-17T07:42:06Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![RoshanRane](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/roshanrane/32/6205_2.png) [@RoshanRane](https://discuss.pytorch.org/u/RoshanRane)\
**Post date:** [December 26, 2018, 8:48pm UTC](https://discuss.pytorch.org/t/check-gradient-flow-in-network/15063/10 "2018-12-26T20:48:33Z")

</div>

A much better implementation of the function ☀

```auto

def plot_grad_flow(named_parameters):
    '''Plots the gradients flowing through different layers in the net during training.
    Can be used for checking for possible gradient vanishing / exploding problems.
    
    Usage: Plug this function in Trainer class after loss.backwards() as 
    "plot_grad_flow(self.model.named_parameters())" to visualize the gradient flow'''
    ave_grads = []
    max_grads= []
    layers = []
    for n, p in named_parameters:
        if(p.requires_grad) and ("bias" not in n):
            layers.append(n)
            ave_grads.append(p.grad.abs().mean())
            max_grads.append(p.grad.abs().max())
    plt.bar(np.arange(len(max_grads)), max_grads, alpha=0.1, lw=1, color="c")
    plt.bar(np.arange(len(max_grads)), ave_grads, alpha=0.1, lw=1, color="b")
    plt.hlines(0, 0, len(ave_grads)+1, lw=2, color="k" )
    plt.xticks(range(0,len(ave_grads), 1), layers, rotation="vertical")
    plt.xlim(left=0, right=len(ave_grads))
    plt.ylim(bottom = -0.001, top=0.02) # zoom in on the lower gradient regions
    plt.xlabel("Layers")
    plt.ylabel("average gradient")
    plt.title("Gradient flow")
    plt.grid(True)
    plt.legend([Line2D([0], [0], color="c", lw=4),
                Line2D([0], [0], color="b", lw=4),
                Line2D([0], [0], color="k", lw=4)], ['max-gradient', 'mean-gradient', 'zero-gradient'])

```

---

_[View the full topic](https://discuss.pytorch.org/t/check-gradient-flow-in-network/15063)._
