The output of numerical calculation of BCEWithLogitsLoss for PyTorch example
When I looked the example code of BCEWithLogitsLoss in PyTorch Docs. I am confusing about the output result of loss function and formula.

PyTorch Example:

&gt;&gt;&gt; loss = nn.BCEWithLogitsLoss()
&gt;&gt;&gt; input = torch.randn(3, requires_grad=True)
&gt;&gt;&gt; target = torch.empty(3).random_(2)
&gt;&gt;&gt; output = loss(input, target)
&gt;&gt;&gt; output.backward()

input : tensor([0.4764, -2.4063,  0.1563], requires_grad=True)
target: tensor([0., 1., 1.])
output: tensor(1.3567, grad_fn=&lt;BinaryCrossEntropyWithLogitsBackward&gt;)


But according to the formula by showing:



The output of loss function should have shape (3,) instead of single value ,since the shape of input and output both are (3,) . I was thinking that the output may was the sum of Ln or else, but still no idea. Could someone help me to explain that?

As @Dishin H Goyani reminded that the default reduction is 'mean'. I did a simple test.

&gt;&gt;&gt; target_n = target.numpy()
&gt;&gt;&gt; input_n  = input.detach().numpy()
&gt;&gt;&gt; def sigmoid(array):return 1/(1+np.exp(-array))
&gt;&gt;&gt; output_n = -1*(target_n*np.log(sigmoid(input_n))+(1-target_n)*np.log(1-sigmoid(input_n)))
output_n : array([0.95947516, 2.4926252 , 0.61806685], dtype=float32)
&gt;&gt;&gt; np.mean(output_n)
1.3567224


The result is matched.

As you seen, the default Wn is 1.

As reduction parameter default value is 'mean' in BCEWithLogitsLoss.

The output is mean - the sum of the output will be divided by the number of elements in the output.

Read Doc here for more detail:
Parameters
...
reduction (string, optional) – Specifies the reduction to apply to the output: 'none' | 'mean' | 'sum'.  


  'none': no reduction will be applied,
  'mean': the sum of the output will be divided by the number of elements in the output,
  'sum': the output will be summed.  


Note: size_average and reduce are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction. Default: 'mean'
...  
