# How to implement accumulated gradient？

**URL:** <https://discuss.pytorch.org/t/how-to-implement-accumulated-gradient/3822>\
**Category:** vision\
**Created:** [June 7, 2017, 6:33pm UTC](https://discuss.pytorch.org/t/how-to-implement-accumulated-gradient/3822 "2017-06-07T18:33:24Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![MORI](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@MORI](https://discuss.pytorch.org/u/MORI)\
**Post date:** [June 22, 2017, 4:10am UTC](https://discuss.pytorch.org/t/how-to-implement-accumulated-gradient/3822/5 "2017-06-22T04:10:21Z")

</div>

Hi smth,

So, eventually, there is no necessity to divide loss with iter\_size?  
I’m still bit confusing since apaszke mentioned about dividing here.

> [@PyTorch Gradients](https://discuss.pytorch.org/t/pytorch-gradients/884):
>
> Normally when we’re doing backprop we would do the following: loss.backward() # This calculates the gradients optimizer.step() # This updates the net However, what if I wish to accumulate the gradients? Meaning I want to run various loss.backward() multiple times first and accumulate the gradients before applying them in optimizer.step(). What do you recommend for storing the gradients? grads = loss.backward()? How do I feed the accumulated gradients to optimizer.step?

Thank you for your help.

---

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