binaryCrossEntropyGPU<T> function

GPUTensor<Scalar> binaryCrossEntropyGPU<T>(
  1. GPUTensor<T> prediction,
  2. GPUTensor<T> target,
  3. CommandBuffer tape, {
  4. GPUTensor<Scalar>? outTensor,
})

///////////////////////////////// Loss Functions (400-499) /// ///////////////////////////////// Appends commands for calculating the binary cross entropy over the elements of m of type T to tape.

Implementation

/// Appends commands for calculating the binary cross entropy over the elements of [m] of type [T] to [tape].
GPUTensor<Scalar> binaryCrossEntropyGPU<T>(
    GPUTensor<T> prediction, GPUTensor<T> target, CommandBuffer tape,
    {GPUTensor<Scalar>? outTensor}) {
  // Loss functions usually reduce the result to a single scalar
  GPUTensor<Scalar> out = outTensor ?? GPUTensor<Scalar>(0.0);

  tape.putInt(OP_BCE_LOSS_FORWARD);
  tape.putString(prediction.id);
  tape.putString(target.id);
  tape.putString(out.id);

  out.creator = GPUNode(
    [prediction, target],
    (CommandBuffer bTape) {
      bTape.putInt(OP_BCE_LOSS_BACKWARD);
      // CORRECT ORDER to match C++ bindings
      bTape.putString(out.gradId); // 1. Incoming grad (1.0)
      bTape.putString(prediction.id); // 2. Prediction
      bTape.putString(target.id); // 3. Target
      bTape.putString(prediction.gradId); // 4. Destination for the gradient
    },
    opName: 'binaryCrossEntropyGPU',
    cost: 1,
  );

  return out;
}