<p dir="auto">pytorch 1.0<br>
DOC err:torch.nn.CrossEntropyLoss<br>
if weight=NONE<br>
loss(x,class)=−x[class]+log(j∑​exp(x[j])) ,The result is correct.</p>
<p dir="auto">if weight!=NONE<br>
loss(x,class)=weight[class]*(−x[class]+log(j∑​exp(x[j]))) ,The result is wrong?</p>
<hr>
<h2 dir="auto">import torch<br>
import torch.nn.functional as F<br>
a=[[-5.,-6.],[3.,2.],[3.,2.]]<br>
c=[1,0,1]<br>
b=[3.,2.]<br>
input=torch.tensor(a,requires_grad=True)<br>
target = torch.tensor(c)<br>
weight = torch.tensor(b)<br>
output = F.cross_entropy(input,target,weight,reduce=True,size_average=True)<br>
z=output.sum()<br>
z.backward()<br>
print(output,input.grad)</h2>
<p dir="auto">tensor(0.8847, grad_fn=) tensor([[ 0.2089, -0.2089],<br>
[-0.1153,  0.1153],<br>
[ 0.2089, -0.2089]])</p>
<p dir="auto">Hi,</p>
<p dir="auto">there is something strange in the <code class="notranslate">backward</code> step (or maybe something I don't understand). If I define a Module that takes 3 inputs, the <code class="notranslate">grad_input</code> has to be of size 3, right ? But this is not the case here (from the backward_hook point of view):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F

def bh(m,go,gi):
    print(&quot;Grad Input&quot;)
    print(go)
    print(&quot;Grad Output&quot;)
    print(gi)

class M(nn.Module):
    def __init__(self):
        super(M,self).__init__()
        self.register_backward_hook(bh)


    def forward(self,x,y,z):
        return (x+y+z)

x=Variable(torch.randn(1,5),requires_grad=True)
y=Variable(torch.randn(1,5),requires_grad=True)
z=Variable(torch.randn(1,5),requires_grad=True)

criterion=nn.MSELoss()
mod=M()
out=mod(x,y,z)
loss=criterion(out,Variable(torch.randn(1,5)))
loss.backward()```

In that case, when I print grad_input throught the hook function, it is just composed of two elements... Could you tell me where am I wrong ? But `x.grad, y.grad and z.grad` seem correctly computed

cc @ezyang @gchanan @zou3519 @SsnL @albanD @gqchen"><pre lang="import" class="notranslate"><code class="notranslate">from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F

def bh(m,go,gi):
    print("Grad Input")
    print(go)
    print("Grad Output")
    print(gi)

class M(nn.Module):
    def __init__(self):
        super(M,self).__init__()
        self.register_backward_hook(bh)


    def forward(self,x,y,z):
        return (x+y+z)

x=Variable(torch.randn(1,5),requires_grad=True)
y=Variable(torch.randn(1,5),requires_grad=True)
z=Variable(torch.randn(1,5),requires_grad=True)

criterion=nn.MSELoss()
mod=M()
out=mod(x,y,z)
loss=criterion(out,Variable(torch.randn(1,5)))
loss.backward()```

In that case, when I print grad_input throught the hook function, it is just composed of two elements... Could you tell me where am I wrong ? But `x.grad, y.grad and z.grad` seem correctly computed

cc @ezyang @gchanan @zou3519 @SsnL @albanD @gqchen
</code></pre></div>