Could you change your code example to include the forward pass? The one place where I have seen this error come up is when you do the forward pass once, and then do the backward pass more than once before the next forward pass.