Hessian computation error - inplace operation

ameloa: <p>I’m trying to output the hessian of my loss function w.r.t. model parameters for a simple MNL choice model. I get the following error when executing loss.backward the final time: “one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [1, 1]], which is output 0 of TBackward, is at version 9401; expected version 9400 instead.”</p>
<p>model.parameters() contains 2 objects; an embedding of size (70,1) and a (1,1) float, both with requires_grad=True.</p>
<p>attached is a screen show of where the error occurs (in the fit() function at the last loss.backward() call before “with t.no_grad():”)<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/f/9f26c519985c4502687acf855da2e883915c5a7b.png" data-download-href="https://discuss.pytorch.org/uploads/default/9f26c519985c4502687acf855da2e883915c5a7b" title="Screen Shot 2021-08-03 at 11.43.26 AM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/f/9f26c519985c4502687acf855da2e883915c5a7b_2_690x459.png" alt="Screen Shot 2021-08-03 at 11.43.26 AM" data-base62-sha1="mHUYSIjVwrEksjqBmScfhjfhzK3" width="690" height="459" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/f/9f26c519985c4502687acf855da2e883915c5a7b_2_690x459.png, https://discuss.pytorch.org/uploads/default/original/3X/9/f/9f26c519985c4502687acf855da2e883915c5a7b.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/9/f/9f26c519985c4502687acf855da2e883915c5a7b.png 2x" data-dominant-color="F1F0EF"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screen Shot 2021-08-03 at 11.43.26 AM</span><span class="informations">812×541 96.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>The hessian computation works just fine as is for a simpler model with only one parameter (the (70,1) embedding). Any help is greatly appreciated!</p>

ameloa: <p>screen shot of the model class:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/2/42d78ad6a140e2ba60fa8c07bfaf8f1b610e0322.png" data-download-href="https://discuss.pytorch.org/uploads/default/42d78ad6a140e2ba60fa8c07bfaf8f1b610e0322" title="Screen Shot 2021-08-03 at 11.42.56 AM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/2/42d78ad6a140e2ba60fa8c07bfaf8f1b610e0322_2_531x500.png" alt="Screen Shot 2021-08-03 at 11.42.56 AM" data-base62-sha1="9xjjf5KyuptLSAGlkFKVL43Z8Jk" width="531" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/2/42d78ad6a140e2ba60fa8c07bfaf8f1b610e0322_2_531x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/4/2/42d78ad6a140e2ba60fa8c07bfaf8f1b610e0322_2_796x750.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/4/2/42d78ad6a140e2ba60fa8c07bfaf8f1b610e0322.png 2x" data-dominant-color="F5F4F4"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screen Shot 2021-08-03 at 11.42.56 AM</span><span class="informations">843×793 130 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>