function binarize(y, threshold) {
  if (threshold == null) {
    threshold = 0.5;
  }
  tf.util.assert(
      threshold >= 0 && threshold <= 1,
      `Expected threshold to be >=0 and <=1, but got ${threshold}`);

  return tf.tidy(() => {
    const condition = y.greater(tf.scalar(threshold));
    return tf.where(condition, tf.onesLike(y), tf.zerosLike(y));
  });
}
Binarizes a tensor based on threshold of 0.5.

@param {tf.Tensor} y Tensor to be binarized.
@param {number} threshold (default: 0.5).
@returns {tf.Tensor} Binarized tensor.