Hi Two!



 two_Two:

x = torch.randn((4,5),requires_grad = True)
z = x.mean()
z.backward()
#print(x.grad)

…
And the result of x.grad returns
tensor([[1.1500, 1.1500, 1.1500, 1.1500, 1.1500],
        [1.1500, 1.1500, 1.1500, 1.1500, 1.1500],
        [1.1500, 1.1500, 1.1500, 1.1500, 1.1500],
        [1.1500, 1.1500, 1.1500, 1.1500, 1.1500]])

…
I would like to know how do we compute and get the values both 1.1500 and 2.1500


The gradients you report are obviously wrong.  I cannot reproduce this
with a pytorch-version-0.3.0 test script:
import torch
torch.__version__

torch.manual_seed (2020)

w1 = torch.randn (4,5)
w2 = w1.clone()

x1 = torch.autograd.Variable (w1, requires_grad = True)
x1
z1 = x1.mean()
z1.backward()
x1.grad

x2 = torch.autograd.Variable (w2, requires_grad = True)
x2
z2 = x2.sum()
z2.backward()
x2.grad

Here is the output showing the correct gradients:
>>> import torch
>>> torch.__version__
'0.3.0b0+591e73e'
>>>
>>> torch.manual_seed (2020)
<torch._C.Generator object at 0x000001E2AF8B6630>
>>>
>>> w1 = torch.randn (4,5)
>>> w2 = w1.clone()
>>>
>>> x1 = torch.autograd.Variable (w1, requires_grad = True)
>>> x1
Variable containing:
 1.2372 -0.9604  1.5415 -0.4079  0.8806
 0.0529  0.0751  0.4777 -0.6759 -2.1489
-1.1463 -0.2720  1.0066 -0.0416 -1.2853
-0.4948 -1.2964 -1.2502 -0.7693  1.6856
[torch.FloatTensor of size 4x5]

>>> z1 = x1.mean()
>>> z1.backward()
>>> x1.grad
Variable containing:
1.00000e-02 *
  5.0000  5.0000  5.0000  5.0000  5.0000
  5.0000  5.0000  5.0000  5.0000  5.0000
  5.0000  5.0000  5.0000  5.0000  5.0000
  5.0000  5.0000  5.0000  5.0000  5.0000
[torch.FloatTensor of size 4x5]

>>>
>>> x2 = torch.autograd.Variable (w2, requires_grad = True)
>>> x2
Variable containing:
 1.2372 -0.9604  1.5415 -0.4079  0.8806
 0.0529  0.0751  0.4777 -0.6759 -2.1489
-1.1463 -0.2720  1.0066 -0.0416 -1.2853
-0.4948 -1.2964 -1.2502 -0.7693  1.6856
[torch.FloatTensor of size 4x5]

>>> z2 = x2.sum()
>>> z2.backward()
>>> x2.grad
Variable containing:
 1  1  1  1  1
 1  1  1  1  1
 1  1  1  1  1
 1  1  1  1  1
[torch.FloatTensor of size 4x5]

Could you post a complete, runnable script that reproduces your issue
and let us know what version of pytorch you are running?
Best.
K. Frank