How to fix Mismatch in shape when using .backward() function

username: JmcginleyFAU
cooked: <p>This is my first time using Pytorch and I am trying to run the WGAN from <a href="https://github.com/jalola/improved-wgan-pytorch/blob/master/gan_train.py" rel="nofollow noopener">WGAN</a></p>
<p>When implementing my own dataset and running the provided code, I get an error on line 186 (link):</p>
<pre><code class="lang-auto">    156             gen_cost = aD(fake_data)
    157             gen_cost = gen_cost.mean()
--&gt; 158             gen_cost.backward(mone)
    159             gen_cost = -gen_cost
</code></pre>
<p>which produces:</p>
<pre><code class="lang-auto">/usr/local/lib/python3.6/dist-packages/torch/tensor.py in backward(self, gradient, retain_graph, create_graph)
    148                 products. Defaults to ``False``.
    149         """
--&gt; 150         torch.autograd.backward(self, gradient, retain_graph, create_graph)
    151 
    152     def register_hook(self, hook):

/usr/local/lib/python3.6/dist-packages/torch/autograd/__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
     91         grad_tensors = list(grad_tensors)
     92 
---&gt; 93     grad_tensors = _make_grads(tensors, grad_tensors)
     94     if retain_graph is None:
     95         retain_graph = create_graph

/usr/local/lib/python3.6/dist-packages/torch/autograd/__init__.py in _make_grads(outputs, grads)
     27                                    + str(grad.shape) + " and output["
     28                                    + str(outputs.index(out)) + "] has a shape of "
---&gt; 29                                    + str(out.shape) + ".")
     30             new_grads.append(grad)
     31         elif grad is None:

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>I am having a hard time understanding the relationship of gen_cost.backward(mone) and the grad_output and output variables.<br>
Would anyone be willing to explain this to me! Appreciate it!</p>

username: albanD
cooked: <p>Hi,</p>
<p>The error is not very clear but it happens because we recently introduced 0 sized Tensors.<br>
As you can see, the size mismatch is between a Tensor with 1 dimension of size 1 and a tensor with 0 dimensions that contains a single element.</p>
<p>Could you show how <code>mone</code> is created?<br>
If it is a fixed number, it should be created as <code>torch.tensor(42)</code>. If it is created using a reduction function (like sum, mean etc), the <code>keepdim</code> argument should be set to False.</p>

username: JmcginleyFAU
cooked: <p>Hello AlbanD,<br>
In the Github Repo the mone variable is created by</p>
<pre><code class="lang-auto">one = torch.FloatTensor([1])
mone = one * -1
aG = aG.to(device)
aD = aD.to(device)
one = one.to(device)
mone = mone.to(device)
</code></pre>

username: albanD
cooked: <p>You can change the <code>one</code> definition by:<br>
<code>one = torch.tensor(1, dtype=torch.float)</code></p>
<p>That should fix your problem <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>