* Discount and normalize reward values. * * This function performs two steps: * * 1. Discounts the reward values using `discountRate`. * 2. Normalize the reward values with the global reward mean and standard * deviation. * * @param {number[][]} rewardSequences Sequences of reward values.
(rewardSequences, discountRate)
| 356 | * Array of tf.Tensor. |
| 357 | */ |
| 358 | function discountAndNormalizeRewards(rewardSequences, discountRate) { |
| 359 | return tf.tidy(() => { |
| 360 | const discounted = []; |
| 361 | for (const sequence of rewardSequences) { |
| 362 | discounted.push(discountRewards(sequence, discountRate)) |
| 363 | } |
| 364 | // Compute the overall mean and stddev. |
| 365 | const concatenated = tf.concat(discounted); |
| 366 | const mean = tf.mean(concatenated); |
| 367 | const std = tf.sqrt(tf.mean(tf.square(concatenated.sub(mean)))); |
| 368 | // Normalize the reward sequences using the mean and std. |
| 369 | const normalized = discounted.map(rs => rs.sub(mean).div(std)); |
| 370 | return normalized; |
| 371 | }); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Scale the gradient values using normalized reward values and compute average. |
no test coverage detected