RuntimeError: Function _TorchNNFunctionBackward returned an invalid gradient at index 0 - expected device cuda:0 but got cpu

Ali_Camdal: <p>Hi, I’m currently working on quantum-classical neural networks using PyTorch and qiskit. I’ve created VGG16 NN with extension of quantum neural network by Torch Connector.<br>
When I try to train network on cuda I got an error:</p>
<p><em>RuntimeError: Function _TorchNNFunctionBackward returned an invalid gradient at index 0 - expected device cuda:0 but got cpu</em></p>
<p>I’ve push my model, input data and labels of them. What is the problem with it?<br>
Thank you in advance.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/e/8e745028ecd55ee73be899a52d2cbef631a0ad4b.png" data-download-href="https://discuss.pytorch.org/uploads/default/8e745028ecd55ee73be899a52d2cbef631a0ad4b" title="error"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/e/8e745028ecd55ee73be899a52d2cbef631a0ad4b_2_690x296.png" alt="error" data-base62-sha1="kkd020NuYzQnNT7qPIXsWIYM04j" width="690" height="296" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/e/8e745028ecd55ee73be899a52d2cbef631a0ad4b_2_690x296.png, https://discuss.pytorch.org/uploads/default/optimized/3X/8/e/8e745028ecd55ee73be899a52d2cbef631a0ad4b_2_1035x444.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/8/e/8e745028ecd55ee73be899a52d2cbef631a0ad4b.png 2x" data-dominant-color="F4F5F5"><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">error</span><span class="informations">1334×574 119 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>

ptrblck: <p>Could you post a minimal, executable code snippet to reproduce the issue, please?</p>

Ali_Camdal: <p>Thanks for response.<br>
This is the code that I used for training<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/a/7aea707b129549f3530c6db5d1d896315eeba0b8.png" data-download-href="https://discuss.pytorch.org/uploads/default/7aea707b129549f3530c6db5d1d896315eeba0b8" title="code"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/7/a/7aea707b129549f3530c6db5d1d896315eeba0b8_2_690x327.png" alt="code" data-base62-sha1="hxmxcfEKtp8rhZhzbtyWgCgyMfC" width="690" height="327" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/7/a/7aea707b129549f3530c6db5d1d896315eeba0b8_2_690x327.png, https://discuss.pytorch.org/uploads/default/optimized/3X/7/a/7aea707b129549f3530c6db5d1d896315eeba0b8_2_1035x490.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/7/a/7aea707b129549f3530c6db5d1d896315eeba0b8.png 2x" data-dominant-color="F3F3F3"><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">code</span><span class="informations">1199×569 64.5 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>

Ali_Camdal: <p>Also this is the model I’ve been using.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/2/92532f05be8381fa5823f808b010b1fb38cd19c7.png" data-download-href="https://discuss.pytorch.org/uploads/default/92532f05be8381fa5823f808b010b1fb38cd19c7" title="model"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/2/92532f05be8381fa5823f808b010b1fb38cd19c7_2_476x500.png" alt="model" data-base62-sha1="kSrVInMddsFgvpBT5vICn4Cc1z9" width="476" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/2/92532f05be8381fa5823f808b010b1fb38cd19c7_2_476x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/9/2/92532f05be8381fa5823f808b010b1fb38cd19c7_2_714x750.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/9/2/92532f05be8381fa5823f808b010b1fb38cd19c7_2_952x1000.png 2x" data-dominant-color="EDEDED"><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">model</span><span class="informations">1071×1123 208 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>