Creates a criterion that measures the Binary Cross Entropy between the target and the output:
Arguments
- weight
(Tensor, optional): a manual rescaling weight given to the loss of each batch element. If given, has to be a Tensor of size
nbatch
.- 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.
Details
The unreduced (i.e. with reduction
set to 'none'
) loss can be described as:
reduction
is not 'none'
(default 'mean'
), then
This is used for measuring the error of a reconstruction in for example
an auto-encoder. Note that the targets
Notice that if
However, an infinite term in the loss equation is not desirable for several reasons.
For one, if either
This would make BCELoss's backward method nonlinear with respect to
Shape
Input:
where means, any number of additional dimensionsTarget:
, same shape as the inputOutput: scalar. If
reduction
is'none'
, then , same shape as input.
Examples
if (torch_is_installed()) {
m <- nn_sigmoid()
loss <- nn_bce_loss()
input <- torch_randn(3, requires_grad = TRUE)
target <- torch_rand(3)
output <- loss(m(input), target)
output$backward()
}