binaryCrossEntropy function

Tensor<Scalar> binaryCrossEntropy(
  1. Tensor<Scalar> prediction,
  2. Tensor<Scalar> target
)

Implementation

Tensor<Scalar> binaryCrossEntropy(
  Tensor<Scalar> prediction,
  Tensor<Scalar> target,
) {
  Scalar outValue =
      -(target.value * log(prediction.value) +
          (1 - target.value) * log(1 - prediction.value));
  Tensor<Scalar> out = Tensor<Scalar>(outValue);
  out.creator = Node(
    [prediction, target],
    () {
      prediction.grad +=
          out.grad *
          ((prediction.value - target.value) /
              (prediction.value * (1 - prediction.value)));
    },
    opName: 'binaryCrossEntropy',
    cost: 1,
  );
  return out;
}