tools/autograd/templates/Functions.cpp
// Compute derivatives for targets.
// Assume targets are given as probabilities (i.e. without taking the logarithm).
Compute derivatives for targets.
Assume targets are given as probabilities .
Compute derivatives for targets . Assume targets are given as probabilities .
Tensor kl_div_target_backward(Tensor grad_output, Tensor self, Tensor target, int64_t reduction) {
  if (reduction == Reduction::None) {
    return grad_output.mul(target.log().add_(1).sub_(self)).masked_fill_(target == 0, 0.);
  }
  if (reduction == Reduction::ElementwiseMean) {
    return grad_output.mul(target.log().add_(1).sub_(self)).div_(target.numel()).masked_fill_(target == 0, 0.);
  }
  return grad_output.mul(target.log().add_(1).sub_(self)).masked_fill_(target == 0, 0.);
}
Tensor kl_div_target_backward ( Tensor grad_output , Tensor self , Tensor target , int64_t reduction ) { if ( reduction == Reduction :: None ) { return grad_output . mul ( target . log ( ) . add_ ( 1 ) . sub_ ( self ) ) . masked_fill_ ( target == 0 , 0. ) ; } if ( reduction == Reduction :: ElementwiseMean ) { return grad_output . mul ( target . log ( ) . add_ ( 1 ) . sub_ ( self ) ) . div_ ( target . numel ( ) ) . masked_fill_ ( target == 0 , 0. ) ; } return grad_output . mul ( target . log ( ) . add_ ( 1 ) . sub_ ( self ) ) . masked_fill_ ( target == 0 , 0. ) ; }
Compute derivatives for targets.
Compute derivatives for targets .