MCPcopy Index your code
hub / github.com/tensorflow/tfjs-examples / discountAndNormalizeRewards

Function discountAndNormalizeRewards

cart-pole/index.js:358–372  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

356 * Array of tf.Tensor.
357 */
358function 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.

Callers 1

trainMethod · 0.85

Calls 1

discountRewardsFunction · 0.85

Tested by

no test coverage detected