RuntimeError: Mismatch in shape

REDHWAN_AL-GABRI: <p>I am trying to run <a href="https://github.com/natanielruiz/deep-head-pose/blob/master/code/train_hopenet.py" rel="noopener nofollow ugc">train_hopenet.py</a></p>
<p><code>python3 train_hopenet.py --dataset AFLW2000 --data_dir datasets/AFLW2000 --filename_list datasets/AFLW2000/files.txt  --output_string er</code></p>
<p>I got this error:</p>
<pre><code class="lang-auto">/home/redhwan/.local/lib/python3.8/site-packages/torch/optim/adam.py:90: UserWarning: optimizer contains a parameter group with duplicate parameters; in future, this will cause an error; see github.com/pytorch/pytorch/issues/40967 for more information
  super(Adam, self).__init__(params, defaults)
Ready to train network.
Traceback (most recent call last):
  File "train_hopenet.py", line 193, in &lt;module&gt;
    torch.autograd.backward(loss_seq, grad_seq)
  File "/home/redhwan/.local/lib/python3.8/site-packages/torch/autograd/__init__.py", line 166, in backward
    grad_tensors_ = _make_grads(tensors, grad_tensors_, is_grads_batched=False)
  File "/home/redhwan/.local/lib/python3.8/site-packages/torch/autograd/__init__.py", line 50, in _make_grads
    raise RuntimeError("Mismatch in shape: grad_output["
RuntimeError: Mismatch in shape: grad_output[0] has a shape of torch.Size([1]) and output[0] has a shape of torch.Size([]).
</code></pre>
<p>How can I solve the issue?</p>
<p><code>torch.__version__  = 1.12.0+cu102</code></p>
<p>Thanks a lot in advance!</p>

eqy: <p>You might want to check if changing this line:</p><aside class="onebox githubblob" data-onebox-src="https://github.com/natanielruiz/deep-head-pose/blob/f7bbb9981c2953c2eca67748d6492a64c8243946/code/train_hopenet.py#L187">
  <header class="source">

      <a href="https://github.com/natanielruiz/deep-head-pose/blob/f7bbb9981c2953c2eca67748d6492a64c8243946/code/train_hopenet.py#L187" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/natanielruiz/deep-head-pose/blob/f7bbb9981c2953c2eca67748d6492a64c8243946/code/train_hopenet.py#L187" target="_blank" rel="noopener nofollow ugc">natanielruiz/deep-head-pose/blob/f7bbb9981c2953c2eca67748d6492a64c8243946/code/train_hopenet.py#L187</a></h4>



    <pre class="onebox"><code class="lang-py">
      <ol class="start lines" start="177" style="counter-reset: li-counter 176 ;">
          <li>    loss_reg_yaw = reg_criterion(yaw_predicted, label_yaw_cont)</li>
          <li>    loss_reg_pitch = reg_criterion(pitch_predicted, label_pitch_cont)</li>
          <li>    loss_reg_roll = reg_criterion(roll_predicted, label_roll_cont)</li>
          <li>
          </li>
<li>    # Total loss</li>
          <li>    loss_yaw += alpha * loss_reg_yaw</li>
          <li>    loss_pitch += alpha * loss_reg_pitch</li>
          <li>    loss_roll += alpha * loss_reg_roll</li>
          <li>
          </li>
<li>    loss_seq = [loss_yaw, loss_pitch, loss_roll]</li>
          <li class="selected">    grad_seq = [torch.ones(1).cuda(gpu) for _ in range(len(loss_seq))]</li>
          <li>    optimizer.zero_grad()</li>
          <li>    torch.autograd.backward(loss_seq, grad_seq)</li>
          <li>    optimizer.step()</li>
          <li>
          </li>
<li>    if (i+1) % 100 == 0:</li>
          <li>        print ('Epoch [%d/%d], Iter [%d/%d] Losses: Yaw %.4f, Pitch %.4f, Roll %.4f'</li>
          <li>               %(epoch+1, num_epochs, i+1, len(pose_dataset)//batch_size, loss_yaw.data[0], loss_pitch.data[0], loss_roll.data[0]))</li>
          <li>
          </li>
<li># Save models at numbered epochs.</li>
          <li>if epoch % 1 == 0 and epoch &lt; num_epochs:</li>
      </ol>
    </code></pre>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>to <code>grad_seq = [torch.ones([1]).cuda(gpu) for _ in range(len(loss_seq))]</code> (note the added brackets) resolves the issue.</p>

REDHWAN_AL-GABRI: <aside class="quote no-group" data-username="eqy" data-post="2" data-topic="168627">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/eqy/48/46233_2.png" class="avatar"> eqy:</div>
<blockquote>
<p>grad_seq = [torch.ones([1]).cuda(gpu) for _ in range(len(loss_seq))]</p>
</blockquote>
</aside>
<p>First of all, thank you so much for your help.</p>
<p>Your suggestion didn’t solve the issue.</p>
<p>It is working well by:</p>
<p><code>grad_seq = [torch.tensor(1, dtype=torch.float) .cuda(gpu) for _ in range(len(loss_seq))]</code></p>