function discountRewards(rewards, discountRate) {
  const discountedBuffer = tf.buffer([rewards.length]);
  let prev = 0;
  for (let i = rewards.length - 1; i >= 0; --i) {
    const current = discountRate * prev + rewards[i];
    discountedBuffer.set(current, i);
    prev = current;
  }
  return discountedBuffer.toTensor();
}
Discount the reward values.

@param {number[]} rewards The reward values to be discounted.
@param {number} discountRate Discount rate: a number between 0 and 1, e.g.,
  0.95.
@returns {tf.Tensor} The discounted reward values as a 1D tf.Tensor.