Let me explain why the gradient changed. If you don’t clear the gradient, it will add the new gradient to the original. 0.6667 = 2/3 = 0.333 * 2. Try this:
import torch
from torch.autograd import Variable
w1 = Variable(torch.Tensor([1.0,2.0,3.0]),requires_grad=True)
w2 = Variable(torch.Tensor([1.0,2.0,3.0]),requires_grad=True)
d = torch.mean(w1)
d.backward()
print w1.grad.data
## clear the gradient manually
w1.grad = None
d = torch.mean(w1)
d.backward()
print w1.grad.data