keras/src/metrics/f_score_metrics.py/FBetaScore/_weighted_sum
class FBetaScore:    def update_state(self, y_true, y_pred, sample_weight=None):
        y_true = ops.convert_to_tensor(y_true, dtype=self.dtype)
        y_pred = ops.convert_to_tensor(y_pred, dtype=self.dtype)
        if not self._built:
            self._build(y_true.shape, y_pred.shape)

        if self.threshold is None:
            threshold = ops.max(y_pred, axis=-1, keepdims=True)
            # make sure [0, 0, 0] doesn't become [1, 1, 1]
            # Use abs(x) > eps, instead of x != 0 to check for zero
            y_pred = ops.logical_and(
                y_pred >= threshold, ops.abs(y_pred) > 1e-9
            )
        else:
            y_pred = y_pred > self.threshold

        y_pred = ops.cast(y_pred, dtype=self.dtype)
        y_true = ops.cast(y_true, dtype=self.dtype)
        if sample_weight is not None:
            sample_weight = ops.convert_to_tensor(
                sample_weight, dtype=self.dtype
            )

        def _weighted_sum(val, sample_weight):
            if sample_weight is not None:
                val = ops.multiply(val, ops.expand_dims(sample_weight, 1))
            return ops.sum(val, axis=self.axis)

        self.true_positives.assign(
            self.true_positives + _weighted_sum(y_pred * y_true, sample_weight)
        )
        self.false_positives.assign(
            self.false_positives
            + _weighted_sum(y_pred * (1 - y_true), sample_weight)
        )
        self.false_negatives.assign(
            self.false_negatives
            + _weighted_sum((1 - y_pred) * y_true, sample_weight)
        )
        self.intermediate_weights.assign(
            self.intermediate_weights + _weighted_sum(y_true, sample_weight)
        )