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

**URL:** <https://discuss.pytorch.org/t/how-to-fix-mismatch-in-shape-when-using-backward-function/58865>\
**Category:** Uncategorized\
**Created:** [October 22, 2019, 4:43am UTC](https://discuss.pytorch.org/t/how-to-fix-mismatch-in-shape-when-using-backward-function/58865 "2019-10-22T04:43:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JmcginleyFAU](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@JmcginleyFAU](https://discuss.pytorch.org/u/JmcginleyFAU)\
**Post date:** [October 22, 2019, 4:43am UTC](https://discuss.pytorch.org/t/how-to-fix-mismatch-in-shape-when-using-backward-function/58865/1 "2019-10-22T04:43:23Z")

</div>

This is my first time using Pytorch and I am trying to run the WGAN from [WGAN](https://github.com/jalola/improved-wgan-pytorch/blob/master/gan_train.py)

When implementing my own dataset and running the provided code, I get an error on line 186 (link):

```auto
    156 gen_cost = aD(fake_data)
    157 gen_cost = gen_cost.mean()
--> 158 gen_cost.backward(mone)
    159 gen_cost = -gen_cost

```

which produces:

```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 """
--> 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 
---> 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 "
---> 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([]).

```

I am having a hard time understanding the relationship of gen\_cost.backward(mone) and the grad\_output and output variables.  
Would anyone be willing to explain this to me! Appreciate it!

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [October 22, 2019, 1:25pm UTC](https://discuss.pytorch.org/t/how-to-fix-mismatch-in-shape-when-using-backward-function/58865/2 "2019-10-22T13:25:08Z")

</div>

Hi,

The error is not very clear but it happens because we recently introduced 0 sized Tensors.  
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.

Could you show how `mone` is created?  
If it is a fixed number, it should be created as `torch.tensor(42)`. If it is created using a reduction function (like sum, mean etc), the `keepdim` argument should be set to False.

---

<div class="post-metadata">

**Author:** ![JmcginleyFAU](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@JmcginleyFAU](https://discuss.pytorch.org/u/JmcginleyFAU)\
**Post date:** [October 22, 2019, 3:01pm UTC](https://discuss.pytorch.org/t/how-to-fix-mismatch-in-shape-when-using-backward-function/58865/3 "2019-10-22T15:01:09Z")

</div>

Hello AlbanD,  
In the Github Repo the mone variable is created by

```auto
one = torch.FloatTensor([1])
mone = one * -1
aG = aG.to(device)
aD = aD.to(device)
one = one.to(device)
mone = mone.to(device)

```

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [October 22, 2019, 5:38pm UTC](https://discuss.pytorch.org/t/how-to-fix-mismatch-in-shape-when-using-backward-function/58865/4 "2019-10-22T17:38:08Z")

</div>

You can change the `one` definition by:  
`one = torch.tensor(1, dtype=torch.float)`

That should fix your problem 🙂
